Skip to content

A platform for community discussion. Free, open, simple.

DEV: Use structured output for translations (#39079)

Natalie Tay1cb3af3a1e99
diff --git a/plugins/discourse-ai/lib/agents/post_raw_translator.rb b/plugins/discourse-ai/lib/agents/post_raw_translator.rb
index 896357866d4..9f72f87c102 100644
--- a/plugins/discourse-ai/lib/agents/post_raw_translator.rb
+++ b/plugins/discourse-ai/lib/agents/post_raw_translator.rb
@@ -86,8 +86,8 @@ module DiscourseAi
PROMPT
end
- def temperature
- 0.3
+ def response_format
+ [{ "key" => "output", "type" => "string" }]
end
end
end
diff --git a/plugins/discourse-ai/lib/agents/short_text_translator.rb b/plugins/discourse-ai/lib/agents/short_text_translator.rb
index bdce8abe12f..3370b993375 100644
--- a/plugins/discourse-ai/lib/agents/short_text_translator.rb
+++ b/plugins/discourse-ai/lib/agents/short_text_translator.rb
@@ -48,8 +48,8 @@ module DiscourseAi
PROMPT
end
- def temperature
- 0.3
+ def response_format
+ [{ "key" => "output", "type" => "string" }]
end
end
end
diff --git a/plugins/discourse-ai/lib/agents/topic_title_translator.rb b/plugins/discourse-ai/lib/agents/topic_title_translator.rb
index 247570e9fb1..1e2d4648206 100644
--- a/plugins/discourse-ai/lib/agents/topic_title_translator.rb
+++ b/plugins/discourse-ai/lib/agents/topic_title_translator.rb
@@ -61,8 +61,8 @@ module DiscourseAi
PROMPT
end
- def temperature
- 0.3
+ def response_format
+ [{ "key" => "output", "type" => "string" }]
end
end
end
diff --git a/plugins/discourse-ai/lib/translation/base_translator.rb b/plugins/discourse-ai/lib/translation/base_translator.rb
index fbf8496b89c..901c929a7a4 100644
--- a/plugins/discourse-ai/lib/translation/base_translator.rb
+++ b/plugins/discourse-ai/lib/translation/base_translator.rb
@@ -50,9 +50,16 @@ module DiscourseAi
)
llm_args = { max_tokens: model.max_output_tokens }
+ structured_output = nil
result = +""
- bot.reply(context, llm_args:) { |partial| result << partial }
- result
+ bot.reply(context, llm_args:) do |partial, _, type|
+ if type == :structured_output
+ structured_output = partial
+ else
+ result << partial
+ end
+ end
+ structured_output&.read_buffered_property(:output) || result
end
def agent_setting
diff --git a/plugins/discourse-ai/spec/lib/translation/base_translator_spec.rb b/plugins/discourse-ai/spec/lib/translation/base_translator_spec.rb
index f1927f2e80f..21ce0e3289f 100644
--- a/plugins/discourse-ai/spec/lib/translation/base_translator_spec.rb
+++ b/plugins/discourse-ai/spec/lib/translation/base_translator_spec.rb
@@ -44,7 +44,7 @@ describe DiscourseAi::Translation::BaseTranslator do
mock_bot = instance_double(DiscourseAi::Agents::Bot)
allow(DiscourseAi::Agents::Bot).to receive(:as).and_return(mock_bot)
- allow(mock_bot).to receive(:reply).and_yield(llm_response)
+ allow(mock_bot).to receive(:reply).and_yield(llm_response, nil, nil)
post_translator.translate