A platform for community discussion. Free, open, simple.
FEATURE: gate temperature/top_p behind setting (#38479)
Samb0504e4a9700
diff --git a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-agent-editor.gjs b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-agent-editor.gjsindex c55fdfcc951..9f0dfac06cc 100644--- a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-agent-editor.gjs+++ b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-agent-editor.gjs@@ -21,7 +21,7 @@ import { AUTO_GROUPS } from "discourse/lib/constants";import getURL from "discourse/lib/get-url";import Group from "discourse/models/group";import GroupChooser from "discourse/select-kit/components/group-chooser";-import { eq, gt, or } from "discourse/truth-helpers";+import { and, eq, gt, not, or } from "discourse/truth-helpers";import { i18n } from "discourse-i18n";import AiAgentResponseFormatEditor from "../components/modal/ai-agent-response-format-editor";import { toPlainObject } from "../lib/utilities";@@ -474,6 +474,7 @@ export default class AgentEditor extends Component {@name="vision_enabled"@title={{i18n "discourse_ai.ai_agent.vision_enabled"}}@tooltip={{i18n "discourse_ai.ai_agent.vision_enabled_help"}}+ @showTitle={{false}}@format="large"as |field|>@@ -498,7 +499,11 @@ export default class AgentEditor extends Component {</form.Field>{{/if}}- {{#unless data.system}}+ {{#if+ (and+ (not data.system) this.siteSettings.ai_llm_temperature_top_p_enabled+ )+ }}<form.Field@name="temperature"@title={{i18n "discourse_ai.ai_agent.temperature"}}@@ -520,7 +525,7 @@ export default class AgentEditor extends Component {><field.Input @type="number" step="any" lang="en" /></form.Field>- {{/unless}}+ {{/if}}<form.Section@title={{i18n "discourse_ai.ai_agent.examples.title"}}@@ -733,6 +738,7 @@ export default class AgentEditor extends Component {@name="show_thinking"@title={{i18n "discourse_ai.ai_agent.show_thinking"}}@tooltip={{i18n "discourse_ai.ai_agent.show_thinking_help"}}+ @showTitle={{false}}@format="large"as |field|>@@ -828,6 +834,7 @@ export default class AgentEditor extends Component {<form.Field@name="force_default_llm"@title={{i18n "discourse_ai.ai_agent.force_default_llm"}}+ @showTitle={{false}}@format="large"as |field|>@@ -870,6 +877,7 @@ export default class AgentEditor extends Component {@tooltip={{i18n"discourse_ai.ai_agent.allow_personal_messages_help"}}+ @showTitle={{false}}@format="large"as |field|>@@ -882,6 +890,7 @@ export default class AgentEditor extends Component {@tooltip={{i18n"discourse_ai.ai_agent.allow_topic_mentions_help"}}+ @showTitle={{false}}@format="large"as |field|>@@ -897,6 +906,7 @@ export default class AgentEditor extends Component {@tooltip={{i18n"discourse_ai.ai_agent.allow_chat_direct_messages_help"}}+ @showTitle={{false}}@format="large"as |field|>@@ -911,6 +921,7 @@ export default class AgentEditor extends Component {@tooltip={{i18n"discourse_ai.ai_agent.allow_chat_channel_mentions_help"}}+ @showTitle={{false}}@format="large"as |field|>diff --git a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-llm-editor-form.gjs b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-llm-editor-form.gjsindex 27a69afc3b0..9d89432f8e3 100644--- a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-llm-editor-form.gjs+++ b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-llm-editor-form.gjs@@ -387,6 +387,7 @@ export default class AiLlmEditorForm extends Component {@title={{i18n(concat "discourse_ai.llms.provider_fields." name)}}+ @showTitle={{not (eq params.type "checkbox")}}@format="large"as |field|>@@ -502,6 +503,7 @@ export default class AiLlmEditorForm extends Component {@name="vision_enabled"@title={{i18n "discourse_ai.llms.vision_enabled"}}@tooltip={{i18n "discourse_ai.llms.hints.vision_enabled"}}+ @showTitle={{false}}@format="large"as |field|>diff --git a/plugins/discourse-ai/app/models/llm_model.rb b/plugins/discourse-ai/app/models/llm_model.rbindex f9d844c6ecf..41c9f3d2fc4 100644--- a/plugins/discourse-ai/app/models/llm_model.rb+++ b/plugins/discourse-ai/app/models/llm_model.rb@@ -53,7 +53,7 @@ class LlmModel < ActiveRecord::Baseenddef self.provider_params- {+ params = {aws_bedrock: {access_key_id: :secret,role_arn: :text,@@ -243,6 +243,15 @@ class LlmModel < ActiveRecord::Basedisable_top_p: :checkbox,},}++ unless SiteSetting.ai_llm_temperature_top_p_enabled+ params.each_value do |provider_config|+ provider_config.delete(:disable_temperature)+ provider_config.delete(:disable_top_p)+ end+ end++ paramsenddef to_llmdiff --git a/plugins/discourse-ai/config/settings.yml b/plugins/discourse-ai/config/settings.ymlindex 798b3c6220c..28aa77b635c 100644--- a/plugins/discourse-ai/config/settings.yml+++ b/plugins/discourse-ai/config/settings.yml@@ -73,6 +73,10 @@ discourse_ai:ai_gemini_api_key:default: ""hidden: true+ ai_llm_temperature_top_p_enabled:+ default: false+ hidden: true+ client: trueai_strict_token_counting:default: falsehidden: truediff --git a/plugins/discourse-ai/discourse_automation/llm_report.rb b/plugins/discourse-ai/discourse_automation/llm_report.rbindex eba5bb92b0c..6384b1350df 100644--- a/plugins/discourse-ai/discourse_automation/llm_report.rb+++ b/plugins/discourse-ai/discourse_automation/llm_report.rb@@ -49,8 +49,10 @@ if defined?(DiscourseAutomation)field :allow_secure_categories, component: :boolean- field :top_p, component: :text- field :temperature, component: :text+ if SiteSetting.ai_llm_temperature_top_p_enabled+ field :top_p, component: :text+ field :temperature, component: :text+ endfield :suppress_notifications, component: :booleanfield :debug_mode, component: :booleandiff --git a/plugins/discourse-ai/lib/completions/llm.rb b/plugins/discourse-ai/lib/completions/llm.rbindex ac3adc097ae..b844e3bc75d 100644--- a/plugins/discourse-ai/lib/completions/llm.rb+++ b/plugins/discourse-ai/lib/completions/llm.rb@@ -178,8 +178,10 @@ module DiscourseAimodel_params = { max_tokens: max_tokens, stop_sequences: stop_sequences }- model_params[:temperature] = temperature if temperature- model_params[:top_p] = top_p if top_p+ if SiteSetting.ai_llm_temperature_top_p_enabled+ model_params[:temperature] = temperature if temperature+ model_params[:top_p] = top_p if top_p+ end# internals expect symbolized keys, so we normalize hereresponse_format =diff --git a/plugins/discourse-ai/spec/lib/agents/bot_spec.rb b/plugins/discourse-ai/spec/lib/agents/bot_spec.rbindex c682014f420..d8d1f13fc73 100644--- a/plugins/discourse-ai/spec/lib/agents/bot_spec.rb+++ b/plugins/discourse-ai/spec/lib/agents/bot_spec.rb@@ -31,6 +31,7 @@ RSpec.describe DiscourseAi::Agents::Bot dodescribe "#reply" doit "sets top_p and temperature params" do+ SiteSetting.ai_llm_temperature_top_p_enabled = trueDiscourseAi::Completions::Endpoints::Fake.delays = []DiscourseAi::Completions::Endpoints::Fake.last_call = nildiff --git a/plugins/discourse-ai/spec/lib/completions/endpoints/cohere_spec.rb b/plugins/discourse-ai/spec/lib/completions/endpoints/cohere_spec.rbindex 76f3e8fd20a..d4b38da300f 100644--- a/plugins/discourse-ai/spec/lib/completions/endpoints/cohere_spec.rb+++ b/plugins/discourse-ai/spec/lib/completions/endpoints/cohere_spec.rb@@ -241,6 +241,7 @@ RSpec.describe DiscourseAi::Completions::Endpoints::Cohere doendit "is able to perform non streaming completions" do+ SiteSetting.ai_llm_temperature_top_p_enabled = truebody = {response_id: "0a90275b-273d-4690-abce-8018edcec7d0",text: "You're welcome! How can I help you today?",diff --git a/plugins/discourse-ai/spec/lib/completions/endpoints/gemini_spec.rb b/plugins/discourse-ai/spec/lib/completions/endpoints/gemini_spec.rbindex 4720b7f5c8f..af448a8c888 100644--- a/plugins/discourse-ai/spec/lib/completions/endpoints/gemini_spec.rb+++ b/plugins/discourse-ai/spec/lib/completions/endpoints/gemini_spec.rb@@ -931,6 +931,7 @@ RSpec.describe DiscourseAi::Completions::Endpoints::Gemini doendit "includes model params in the request" do+ SiteSetting.ai_llm_temperature_top_p_enabled = trueresponse = <<~TEXTdata: {"candidates": [{"content": {"parts": [{"text": "Hello"}],"role": "model"}}],"usageMetadata": {"promptTokenCount": 399,"totalTokenCount": 399},"modelVersion": "gemini-1.5-pro-002"}diff --git a/plugins/discourse-ai/spec/lib/completions/llm_spec.rb b/plugins/discourse-ai/spec/lib/completions/llm_spec.rbindex ecf0ddea754..ea6865aaf66 100644--- a/plugins/discourse-ai/spec/lib/completions/llm_spec.rb+++ b/plugins/discourse-ai/spec/lib/completions/llm_spec.rb@@ -140,6 +140,35 @@ RSpec.describe DiscourseAi::Completions::Llm doendend+ context "with temperature and top_p" do+ fab!(:fake_model)++ before do+ DiscourseAi::Completions::Endpoints::Fake.delays = []+ DiscourseAi::Completions::Endpoints::Fake.last_call = nil+ end++ it "drops temperature and top_p when ai_llm_temperature_top_p_enabled is false" do+ SiteSetting.ai_llm_temperature_top_p_enabled = false+ fake_llm = described_class.proxy(fake_model)+ fake_llm.generate("hello", user:, temperature: 0.5, top_p: 0.9)++ last_call = DiscourseAi::Completions::Endpoints::Fake.last_call+ expect(last_call[:model_params]).not_to have_key(:temperature)+ expect(last_call[:model_params]).not_to have_key(:top_p)+ end++ it "passes temperature and top_p when ai_llm_temperature_top_p_enabled is true" do+ SiteSetting.ai_llm_temperature_top_p_enabled = true+ fake_llm = described_class.proxy(fake_model)+ fake_llm.generate("hello", user:, temperature: 0.5, top_p: 0.9)++ last_call = DiscourseAi::Completions::Endpoints::Fake.last_call+ expect(last_call[:model_params][:temperature]).to eq(0.5)+ expect(last_call[:model_params][:top_p]).to eq(0.9)+ end+ end+context "when tracking failures" doit "fast-tracks problem check after threshold and resets on success" doWebMock.stub_request(:post, model.url).to_return(