A platform for community discussion. Free, open, simple.
FEATURE: Add a multiplier for max tokens, typically used for various llm settings like reasoning (#35859)
Natalie Taydb72d821cb27
diff --git a/plugins/discourse-ai/config/locales/server.en.yml b/plugins/discourse-ai/config/locales/server.en.ymlindex bdbfb0c245a..0b155b5c05a 100644--- a/plugins/discourse-ai/config/locales/server.en.yml+++ b/plugins/discourse-ai/config/locales/server.en.yml@@ -148,6 +148,7 @@ en:ai_translation_max_post_length: "The maximum number of characters for a post to be translated. Posts longer than this will not be translated."ai_translation_backfill_max_age_days: "The maximum age of a post and topic to be translated. Posts and topics older than this will not be translated. 0 disables backfilling, but will not disable translation of new posts."ai_translation_backfill_hourly_rate: "The number of posts and topics to translate per hour during backfill operations. Set to 0 to disable automatic backfilling of translations for existing content."+ ai_translation_max_tokens_multiplier: "Multiplier to determine the maximum number of tokens to use for translation. By default, we set the max tokens to approximately two to five times the number of characters in the post. This multiplier can be adjusted to allow for thinking or reasoning models which will use more tokens."reviewables:reasons:diff --git a/plugins/discourse-ai/config/settings.yml b/plugins/discourse-ai/config/settings.ymlindex 4b00738d7f4..8f0ec1bf16e 100644--- a/plugins/discourse-ai/config/settings.yml+++ b/plugins/discourse-ai/config/settings.yml@@ -513,6 +513,12 @@ discourse_ai:max: 20000client: falsearea: "ai-features/translation"+ ai_translation_max_tokens_multiplier:+ default: 1.0+ type: float+ min: 1.0+ max: 10.0+ area: "ai-features/translation"ai_translation_verbose_logs:default: falseclient: falsediff --git a/plugins/discourse-ai/lib/translation/base_translator.rb b/plugins/discourse-ai/lib/translation/base_translator.rbindex d51c8f9233f..986419dcead 100644--- a/plugins/discourse-ai/lib/translation/base_translator.rb+++ b/plugins/discourse-ai/lib/translation/base_translator.rb@@ -55,13 +55,21 @@ module DiscourseAienddef get_max_tokens(text)- if text.length < 100- 500- elsif text.length < 500- 1000- else- text.length * 2- end+ base_tokens =+ if text.length < 100+ 500+ elsif text.length < 500+ 1000+ else+ text.length * 2+ end++ (base_tokens * max_token_multiplier).to_i+ end++ def max_token_multiplier+ multiplier = SiteSetting.ai_translation_max_tokens_multiplier+ multiplier > 0 ? multiplier : 1.0enddef persona_settingdiff --git a/plugins/discourse-ai/spec/lib/translation/base_translator_spec.rb b/plugins/discourse-ai/spec/lib/translation/base_translator_spec.rbindex 56dada511f1..2e439b3d795 100644--- a/plugins/discourse-ai/spec/lib/translation/base_translator_spec.rb+++ b/plugins/discourse-ai/spec/lib/translation/base_translator_spec.rb@@ -63,11 +63,13 @@ describe DiscourseAi::Translation::BaseTranslator doexpect(mock_bot).to have_received(:reply).with(bot_context, llm_args: { max_tokens: 500 })end- it "sets max_tokens correctly based on text length" do+ it "sets max_tokens correctly based on text length and ai_translation_max_tokens_multiplier setting" do+ multiplier = 1.5+ SiteSetting.ai_translation_max_tokens_multiplier = multipliertest_cases = [- ["Short text", 500], # Short text (< 100 chars)- ["a" * 200, 1000], # Medium text (100-500 chars)- ["a" * 600, 1200], # Long text (> 500 chars, 600*2=1200)+ ["Short text", 500 * multiplier], # Short text (< 100 chars)+ ["a" * 200, 1000 * multiplier], # Medium text (100-500 chars)+ ["a" * 600, 1200 * multiplier], # Long text (> 500 chars, 600*2=1200)]test_cases.each do |text, expected_max_tokens|