Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 1f267a8

Browse files
committed
FEATURE: Block seeded models for being a persona default
1 parent c49b455 commit 1f267a8

File tree

4 files changed

+46
-7
lines changed

4 files changed

+46
-7
lines changed

app/controllers/discourse_ai/admin/ai_personas_controller.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ def index
2727
}
2828
end
2929
llms =
30-
DiscourseAi::Configuration::LlmEnumerator.values.map do |hash|
31-
{ id: hash[:value], name: hash[:name] }
32-
end
30+
DiscourseAi::Configuration::LlmEnumerator
31+
.values(allowed_seeded_llms: SiteSetting.ai_bot_allowed_seeded_models)
32+
.map { |hash| { id: hash[:value], name: hash[:name] } }
3333
render json: { ai_personas: ai_personas, meta: { tools: tools, llms: llms } }
3434
end
3535

app/models/ai_persona.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class AiPersona < ActiveRecord::Base
1212
validates :system_prompt, presence: true, length: { maximum: 10_000_000 }
1313
validate :system_persona_unchangeable, on: :update, if: :system
1414
validate :chat_preconditions
15+
validate :allowed_seeded_model, if: :default_llm
1516
validates :max_context_posts, numericality: { greater_than: 0 }, allow_nil: true
1617
# leaves some room for growth but sets a maximum to avoid memory issues
1718
# we may want to revisit this in the future
@@ -275,6 +276,17 @@ def ensure_not_system
275276
throw :abort
276277
end
277278
end
279+
280+
def allowed_seeded_model
281+
return if default_llm.blank?
282+
283+
llm = LlmModel.find_by(id: default_llm.split(":").last.to_i)
284+
return if !llm.seeded?
285+
286+
return if SiteSetting.ai_bot_allowed_seeded_models.include?(llm.id.to_s)
287+
288+
errors.add(:default_llm, I18n.t("discourse_ai.llm.configuration.invalid_seeded_model"))
289+
end
278290
end
279291

280292
# == Schema Information

config/settings.yml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ discourse_ai:
2626
default: 60
2727
hidden: true
2828

29-
3029
ai_openai_dall_e_3_url: "https://api.openai.com/v1/images/generations"
31-
ai_openai_embeddings_url:
30+
ai_openai_embeddings_url:
3231
hidden: true
3332
default: "https://api.openai.com/v1/embeddings"
3433
ai_openai_organization:
@@ -57,7 +56,7 @@ discourse_ai:
5756
ai_hugging_face_tei_endpoint_srv:
5857
default: ""
5958
hidden: true
60-
ai_hugging_face_tei_api_key:
59+
ai_hugging_face_tei_api_key:
6160
default: ""
6261
hidden: true
6362
ai_hugging_face_tei_reranker_endpoint:
@@ -203,7 +202,7 @@ discourse_ai:
203202
client: true
204203
hidden: true
205204

206-
ai_embeddings_discourse_service_api_endpoint:
205+
ai_embeddings_discourse_service_api_endpoint:
207206
default: ""
208207
hidden: true
209208
ai_embeddings_discourse_service_api_endpoint_srv:
@@ -307,6 +306,11 @@ discourse_ai:
307306
ai_bot_github_access_token:
308307
default: ""
309308
secret: true
309+
ai_bot_allowed_seeded_models:
310+
default: ""
311+
hidden: true
312+
type: list
313+
list_type: compact
310314
ai_automation_max_triage_per_minute:
311315
default: 60
312316
hidden: true

spec/models/ai_persona_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,29 @@
172172
)
173173
end
174174

175+
it "validates allowed seeded model" do
176+
persona =
177+
AiPersona.new(
178+
name: "test",
179+
description: "test",
180+
system_prompt: "test",
181+
tools: [],
182+
allowed_group_ids: [],
183+
default_llm: "seeded_model:-1",
184+
)
185+
186+
llm_model = Fabricate(:llm_model, id: -1)
187+
SiteSetting.ai_bot_allowed_seeded_models = ""
188+
189+
expect(persona.valid?).to eq(false)
190+
expect(persona.errors[:default_llm]).to include(
191+
I18n.t("discourse_ai.llm.configuration.invalid_seeded_model"),
192+
)
193+
194+
SiteSetting.ai_bot_allowed_seeded_models = "-1"
195+
expect(persona.valid?).to eq(true)
196+
end
197+
175198
it "does not leak caches between sites" do
176199
AiPersona.create!(
177200
name: "pun_bot",

0 commit comments

Comments
 (0)