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

Commit 6059b6e

Browse files
authored
FIX: Customizable max_output_tokens for AI triage. (#1510)
We enforced a hard limit of 700 tokens in this script, which is not enough when using thinking models, which can quickly use all of them. A temporary solution could be bumping the limit, but there is no guarantee we won't hit it again, and it's hard to find one value that fits all scenarios. Another alternative could be removing it and relying on the LLM config's `max_output_token`, but if you want different rules and want to assign different limits, you are forced to duplicate the config each time. Considering all this, we are adding a dedicated field for this in the triage script, giving you an easy way to tweak it to your needs. If empty, no limit is applied.
1 parent 5d80a34 commit 6059b6e

File tree

4 files changed

+12
-1
lines changed

4 files changed

+12
-1
lines changed

config/locales/client.en.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ en:
180180
temperature:
181181
label: "Temperature"
182182
description: "Temperature to use for the LLM. Increase to increase randomness (leave empty to use model default)"
183+
max_output_tokens:
184+
label: "Max output tokens"
185+
description: "When specified, sets an upper bound to the maximum number of tokens the model can generate. Respects LLM's max output tokens limit"
183186

184187
discourse_ai:
185188
title: "AI"

discourse_automation/llm_triage.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
field :max_post_tokens, component: :text
2525
field :stop_sequences, component: :text_list, required: false
2626
field :temperature, component: :text
27+
field :max_output_tokens, component: :text
2728

2829
# Actions
2930
field :category, component: :category
@@ -85,6 +86,9 @@
8586
temperature = temperature.to_f
8687
end
8788

89+
max_output_tokens = fields.dig("max_output_tokens", "value").to_i
90+
max_output_tokens = nil if max_output_tokens <= 0
91+
8892
max_post_tokens = nil if max_post_tokens <= 0
8993

9094
stop_sequences = fields.dig("stop_sequences", "value")
@@ -122,6 +126,7 @@
122126
stop_sequences: stop_sequences,
123127
automation: self.automation,
124128
temperature: temperature,
129+
max_output_tokens: max_output_tokens,
125130
action: context["action"],
126131
)
127132
rescue => e

lib/automation/llm_triage.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def self.handle(
2121
temperature: nil,
2222
whisper: nil,
2323
reply_persona_id: nil,
24+
max_output_tokens: nil,
2425
action: nil
2526
)
2627
if category_id.blank? && tags.blank? && canned_reply.blank? && hide_topic.blank? &&
@@ -59,8 +60,8 @@ def self.handle(
5960
result =
6061
llm.generate(
6162
prompt,
63+
max_tokens: max_output_tokens,
6264
temperature: temperature,
63-
max_tokens: 700, # ~500 words
6465
user: Discourse.system_user,
6566
stop_sequences: stop_sequences,
6667
feature_name: "llm_triage",

spec/lib/discourse_automation/llm_triage_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ def add_automation_field(name, value, type: "text")
9595
reply_user.update!(admin: true)
9696
add_automation_field("include_personal_messages", true, type: :boolean)
9797
add_automation_field("temperature", "0.2")
98+
add_automation_field("max_output_tokens", "700")
9899
post = Fabricate(:post, topic: personal_message)
99100

100101
prompt_options = nil
@@ -107,6 +108,7 @@ def add_automation_field(name, value, type: "text")
107108
end
108109

109110
expect(prompt_options[:temperature]).to eq(0.2)
111+
expect(prompt_options[:max_tokens]).to eq(700)
110112

111113
last_post = post.topic.reload.posts.order(:post_number).last
112114
expect(last_post.raw).to eq(canned_reply_text)

0 commit comments

Comments
 (0)