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

Commit ad6a8cb

Browse files
authored
DEV: Switch translations out of structured output as it returns only a single value (#1503)
Since translations only require a single key back, there is little point in using structured output. This PR also includes some prompt updates dealing with quotes, details, and code. Related: #1502 This does mean reverting discourse/discourse-translator#257, but we can see how it goes.
1 parent 06743d1 commit ad6a8cb

File tree

6 files changed

+123
-135
lines changed

6 files changed

+123
-135
lines changed

lib/personas/locale_detector.rb

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,43 +34,22 @@ def system_prompt
3434
3535
5. Avoid using `und` and prefer `en` over `en-US` or `en-GB` unless the text specifically indicates a regional variant.
3636
37-
6. Format your response as a JSON object with a single key "locale" and the value as the language code.
37+
Two example scenarios:
38+
Input: "Can you tell me what '私の世界で一番好きな食べ物はちらし丼です' means?"
39+
Output: "en"
3840
39-
Your output should be in the following format:
40-
<output>
41-
{"locale": "xx"}
42-
</output>
43-
44-
Where "xx" is replaced by the appropriate language code.
41+
Input: [quote]\nNon smettere mai di credere nella bellezza dei tuoi sogni. Anche quando tutto sembra perduto, c'è sempre una luce che aspetta di essere trovata.\nOgni passo, anche il più piccolo, ti avvicina a ciò che desideri. La forza che cerchi è già dentro di te.\n[/quote]\n¿Cuál es el mensaje principal de esta cita?
42+
Output: "es"
4543
4644
Important: Base your analysis solely on the provided text. Do not use any external information or make assumptions about the text's origin or context beyond what is explicitly provided.
47-
PROMPT
48-
end
4945
50-
def response_format
51-
[{ "key" => "locale", "type" => "string" }]
46+
Your response must be a language code, and nothing else.
47+
PROMPT
5248
end
5349

5450
def temperature
5551
0
5652
end
57-
58-
def examples
59-
spanish = <<~MARKDOWN
60-
[quote]
61-
Non smettere mai di credere nella bellezza dei tuoi sogni. Anche quando tutto sembra perduto, c'è sempre una luce che aspetta di essere trovata.
62-
63-
Ogni passo, anche il più piccolo, ti avvicina a ciò che desideri. La forza che cerchi è già dentro di te.
64-
[/quote]
65-
66-
¿Cuál es el mensaje principal de esta cita?
67-
MARKDOWN
68-
69-
[
70-
["Can you tell me what '私の世界で一番好きな食べ物はちらし丼です' means?", { locale: "en" }.to_json],
71-
[spanish, { locale: "es" }.to_json],
72-
]
73-
end
7453
end
7554
end
7655
end

lib/personas/post_raw_translator.rb

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,62 +8,74 @@ def self.default_enabled
88
end
99

1010
def system_prompt
11+
examples = [
12+
{
13+
input: {
14+
content:
15+
"**Heathrow fechado**: Suspensão de voos deve continuar nos próximos dias, afirma gerente do aeroporto de Londres\n\n[details=Do site da BBC]\n\nA British Airways estimou que 85% de seus voos planejados seriam realizados no sábado, mas com atrasos em todos os voos. Às 7h GMT, a maioria das partidas havia ocorrido conforme o esperado, mas, das chegadas, nove dos primeiros 20 voos programados para aterrissar foram cancelados.\n\n[/details]",
16+
target_locale: "en",
17+
}.to_json,
18+
output:
19+
"**Heathrow Closed**: Flight Suspension Expected to Continue for the Coming Days, Says London Airport Manager\n\n[details=From the BBC website]\n\nBritish Airways estimated that 85% of its scheduled flights would operate on Saturday, but all flights were delayed. By 7:00 a.m. GMT, most departures had proceeded as expected, but of the arrivals, nine of the first 20 flights scheduled to land were canceled.\n\n[/details]",
20+
},
21+
{
22+
input: {
23+
content:
24+
"[quote] What does the new update include? [/quote]\n\nNew Update for Minecraft Adds Underwater Temples",
25+
target_locale: "es",
26+
}.to_json,
27+
output:
28+
"[quote]¿Qué incluye la nueva actualización?[/quote]\n\nNueva actualización para Minecraft añade templos submarinos",
29+
},
30+
{
31+
input: {
32+
content:
33+
"There has been an error in my update\n\n```ruby\napi_key = \"a quick brown fox\"\nfetch(\"https://api.example.com/data\", headers: { 'Authorization' => api_key })\n```\n\nPlease help me fix it.",
34+
target_locale: "ja",
35+
}.to_json,
36+
output:
37+
"アップデートでエラーが発生しました\n\n```ruby\napi_key = \"a quick brown fox\"\nfetch(\"https://api.example.com/data\", headers: { 'Authorization' => api_key })\n```\n\n修正にご協力ください。\"",
38+
},
39+
]
40+
1141
<<~PROMPT.strip
1242
You are a highly skilled translator tasked with translating content from one language to another. Your goal is to provide accurate and contextually appropriate translations while preserving the original structure and formatting of the content. Follow these instructions strictly:
1343
1444
1. Preserve Markdown elements, HTML elements, or newlines. Text must be translated without altering the original formatting.
1545
2. Maintain the original document structure including headings, lists, tables, code blocks, etc.
1646
3. Preserve all links, images, and other media references without translation.
17-
4. For technical terminology:
47+
4. For technical and brand terminology:
1848
- Provide the accepted target language term if it exists.
1949
- If no equivalent exists, transliterate the term and include the original term in parentheses.
2050
5. For ambiguous terms or phrases, choose the most contextually appropriate translation.
2151
6. Ensure the translation only contains the original language and the target language.
2252
2353
Follow these instructions on what NOT to do:
24-
7. Do not translate code snippets or programming language names, but ensure that any comments within the code are translated.
54+
7. Do not translate code snippets or programming language names, but ensure that any comments within the code are translated. Code can be represented in ``` or in single ` backticks or in <code> HTML tags.
2555
8. Do not add any content besides the translation.
56+
9. Do not add unnecessary newlines.
57+
58+
Here are three examples of correct translations:
59+
60+
Input: #{examples[0][:input]}
61+
Output: #{examples[0][:output]}
62+
63+
Input: #{examples[1][:input]}
64+
Output: #{examples[1][:output]}
65+
66+
Input: #{examples[2][:input]}
67+
Output: #{examples[2][:output]}
2668
2769
The text to translate will be provided in JSON format with the following structure:
2870
{"content": "Text to translate", "target_locale": "Target language code"}
2971
30-
Output your translation in the following JSON format:
31-
{"translation": "Your translated text here"}
32-
33-
You are being consumed via an API. Only return the translated text in the specified JSON format. Do not include any additional information or explanations.
72+
You are being consumed via an API that expects only the translated text. Only return the translated text in the correct language. Do not add questions or explanations.
3473
PROMPT
3574
end
3675

37-
def response_format
38-
[{ "key" => "translation", "type" => "string" }]
39-
end
40-
4176
def temperature
4277
0.3
4378
end
44-
45-
def examples
46-
[
47-
[
48-
{
49-
content:
50-
"**Heathrow fechado**: paralisação de voos deve continuar nos próximos dias, diz gestora do aeroporto de *Londres*",
51-
target_locale: "en",
52-
}.to_json,
53-
{
54-
translation:
55-
"**Heathrow closed**: flight disruption expected to continue in coming days, says *London* airport management",
56-
}.to_json,
57-
],
58-
[
59-
{
60-
content: "New Update for Minecraft Adds Underwater Temples",
61-
target_locale: "es",
62-
}.to_json,
63-
{ translation: "Nueva actualización para Minecraft añade templos submarinos" }.to_json,
64-
],
65-
]
66-
end
6779
end
6880
end
6981
end

lib/personas/short_text_translator.rb

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ def self.default_enabled
88
end
99

1010
def system_prompt
11+
examples = [
12+
{ input: { content: "Japan", target_locale: "es" }.to_json, output: "Japón" },
13+
{ input: { content: "Cats and Dogs", target_locale: "zh_CN" }.to_json, output: "猫和狗" },
14+
{
15+
input: { content: "Q&A", target_locale: "pt" }.to_json,
16+
output: "Perguntas e Respostas",
17+
},
18+
{ input: { content: "Minecraft", target_locale: "fr" }.to_json, output: "Minecraft" },
19+
]
20+
1121
<<~PROMPT.strip
1222
You are a translation service specializing in translating short pieces of text or a few words.
1323
These words may be things like a name, description, or title. Adhere to the following guidelines:
@@ -17,41 +27,30 @@ def system_prompt
1727
3. Translation maintains the original meaning
1828
4. Preserve any Markdown, HTML elements, links, parenthesis, or newlines
1929
30+
Here are four examples of correct translations:
31+
32+
Input: #{examples[0][:input]}
33+
Output: #{examples[0][:output]}
34+
35+
Input: #{examples[1][:input]}
36+
Output: #{examples[1][:output]}
37+
38+
Input: #{examples[2][:input]}
39+
Output: #{examples[2][:output]}
40+
41+
Input: #{examples[3][:input]}
42+
Output: #{examples[3][:output]}
43+
2044
The text to translate will be provided in JSON format with the following structure:
2145
{"content": "Text to translate", "target_locale": "Target language code"}
2246
23-
Provide your translation in the following JSON format:
24-
{"translation": "target_locale translation here"}
25-
26-
Translate the text now and provide your answer in the specified JSON format.
47+
You are being consumed via an API that expects only the translated text. Only return the translated text in the correct language. Do not add questions or explanations.
2748
PROMPT
2849
end
2950

30-
def response_format
31-
[{ "key" => "translation", "type" => "string" }]
32-
end
33-
3451
def temperature
3552
0.3
3653
end
37-
38-
def examples
39-
[
40-
[{ content: "Japan", target_locale: "es" }.to_json, { translation: "Japón" }.to_json],
41-
[
42-
{ content: "Cats and Dogs", target_locale: "zh_CN" }.to_json,
43-
{ translation: "猫和狗" }.to_json,
44-
],
45-
[
46-
{ content: "Q&A", target_locale: "pt" }.to_json,
47-
{ translation: "Perguntas e Respostas" }.to_json,
48-
],
49-
[
50-
{ content: "Minecraft", target_locale: "fr" }.to_json,
51-
{ translation: "Minecraft" }.to_json,
52-
],
53-
]
54-
end
5554
end
5655
end
5756
end

lib/personas/topic_title_translator.rb

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,32 @@ def self.default_enabled
88
end
99

1010
def system_prompt
11+
examples = [
12+
{
13+
input: {
14+
content: "New Update for Minecraft Adds Underwater Temples",
15+
target_locale: "es",
16+
}.to_json,
17+
output: "Nueva actualización para Minecraft añade templos submarinos",
18+
},
19+
{
20+
input: {
21+
content: "Toyota announces revolutionary battery technology",
22+
target_locale: "fr",
23+
}.to_json,
24+
output: "Toyota annonce une technologie de batteries révolutionnaire",
25+
},
26+
{
27+
input: {
28+
content:
29+
"Heathrow fechado: paralisação de voos deve continuar nos próximos dias, diz gestora do aeroporto de Londres",
30+
target_locale: "en",
31+
}.to_json,
32+
output:
33+
"Heathrow closed: flight disruption expected to continue in coming days, says London airport management",
34+
},
35+
]
36+
1137
<<~PROMPT.strip
1238
You are a translation service specializing in translating forum post titles from English to the asked target_locale. Your task is to provide accurate and contextually appropriate translations while adhering to the following guidelines:
1339
@@ -24,53 +50,27 @@ def system_prompt
2450
4. Adjust the translation if necessary to keep the length similar to the original title.
2551
5. Review your translation for accuracy and naturalness in the target_locale.
2652
53+
Here are three examples of correct translations:
54+
55+
Input: #{examples[0][:input]}
56+
Output: #{examples[0][:output]}
57+
58+
Input: #{examples[1][:input]}
59+
Output: #{examples[1][:output]}
60+
61+
Input: #{examples[2][:input]}
62+
Output: #{examples[2][:output]}
63+
2764
The text to translate will be provided in JSON format with the following structure:
2865
{"content": "Title to translate", "target_locale": "Target language code"}
2966
30-
Provide your translation in the following JSON format:
31-
{"translation": "Your target_locale translation here"}
32-
33-
Remember to keep proper nouns like "Minecraft" and "Toyota" in their original form. Translate the title now and provide your answer in the specified JSON format.
67+
Remember to keep proper nouns like "Minecraft" and "Toyota" in their original form. You are being consumed via an API that expects only the translated title. Only return the translated title in the correct language. Do not add questions or explanations.
3468
PROMPT
3569
end
3670

37-
def response_format
38-
[{ "key" => "translation", "type" => "string" }]
39-
end
40-
4171
def temperature
4272
0.3
4373
end
44-
45-
def examples
46-
[
47-
[
48-
{
49-
content: "New Update for Minecraft Adds Underwater Temples",
50-
target_locale: "es",
51-
}.to_json,
52-
{ translation: "Nueva actualización para Minecraft añade templos submarinos" }.to_json,
53-
],
54-
[
55-
{
56-
content: "Toyota announces revolutionary battery technology",
57-
target_locale: "fr",
58-
}.to_json,
59-
{ translation: "Toyota annonce une technologie de batteries révolutionnaire" }.to_json,
60-
],
61-
[
62-
{
63-
content:
64-
"Heathrow fechado: paralisação de voos deve continuar nos próximos dias, diz gestora do aeroporto de Londres",
65-
target_locale: "en",
66-
}.to_json,
67-
{
68-
translation:
69-
"Heathrow closed: flight disruption expected to continue in coming days, says London airport management",
70-
}.to_json,
71-
],
72-
]
73-
end
7474
end
7575
end
7676
end

lib/translation/base_translator.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,9 @@ def get_translation(text:, bot:, translation_user:)
4747
post: @post,
4848
)
4949

50-
structured_output = nil
51-
bot.reply(context) do |partial, _, type|
52-
structured_output = partial if type == :structured_output
53-
end
54-
55-
structured_output&.read_buffered_property(:translation)
50+
result = +""
51+
bot.reply(context) { |partial| result << partial }
52+
result
5653
end
5754

5855
def persona_setting

lib/translation/language_detector.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,12 @@ def detect
4242
post: @post,
4343
)
4444

45-
structured_output = nil
46-
bot.reply(context) do |partial, _, type|
47-
structured_output = partial if type == :structured_output
45+
result = +""
46+
bot.reply(context) do |partial|
47+
next if partial.strip.blank?
48+
result << partial
4849
end
49-
structured_output&.read_buffered_property(:locale) || []
50+
result
5051
end
5152
end
5253
end

0 commit comments

Comments
 (0)