Skip to content

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.gjs
index 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.gjs
index 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.rb
index 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::Base
end
def self.provider_params
- {
+ params = {
aws_bedrock: {
access_key_id: :secret,
role_arn: :text,
@@ -243,6 +243,15 @@ class LlmModel < ActiveRecord::Base
disable_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
+
+ params
end
def to_llm
diff --git a/plugins/discourse-ai/config/settings.yml b/plugins/discourse-ai/config/settings.yml
index 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: true
ai_strict_token_counting:
default: false
hidden: true
diff --git a/plugins/discourse-ai/discourse_automation/llm_report.rb b/plugins/discourse-ai/discourse_automation/llm_report.rb
index 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
+ end
field :suppress_notifications, component: :boolean
field :debug_mode, component: :boolean
diff --git a/plugins/discourse-ai/lib/completions/llm.rb b/plugins/discourse-ai/lib/completions/llm.rb
index ac3adc097ae..b844e3bc75d 100644
--- a/plugins/discourse-ai/lib/completions/llm.rb
+++ b/plugins/discourse-ai/lib/completions/llm.rb
@@ -178,8 +178,10 @@ module DiscourseAi
model_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 here
response_format =
diff --git a/plugins/discourse-ai/spec/lib/agents/bot_spec.rb b/plugins/discourse-ai/spec/lib/agents/bot_spec.rb
index 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 do
describe "#reply" do
it "sets top_p and temperature params" do
+ SiteSetting.ai_llm_temperature_top_p_enabled = true
DiscourseAi::Completions::Endpoints::Fake.delays = []
DiscourseAi::Completions::Endpoints::Fake.last_call = nil
diff --git a/plugins/discourse-ai/spec/lib/completions/endpoints/cohere_spec.rb b/plugins/discourse-ai/spec/lib/completions/endpoints/cohere_spec.rb
index 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 do
end
it "is able to perform non streaming completions" do
+ SiteSetting.ai_llm_temperature_top_p_enabled = true
body = {
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.rb
index 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 do
end
it "includes model params in the request" do
+ SiteSetting.ai_llm_temperature_top_p_enabled = true
response = <<~TEXT
data: {"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.rb
index 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 do
end
end
+ 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" do
it "fast-tracks problem check after threshold and resets on success" do
WebMock.stub_request(:post, model.url).to_return(