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

Commit 7d74780

Browse files
committed
DEV: Switch translations out of structured output as it returns only a single value
1 parent 1b16fc8 commit 7d74780

File tree

4 files changed

+59
-71
lines changed

4 files changed

+59
-71
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: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,62 +8,73 @@ 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.
2656
57+
Here are three examples of correct translations:
58+
59+
Input: #{examples[0][:input]}
60+
Output: #{examples[0][:output]}
61+
62+
Input: #{examples[1][:input]}
63+
Output: #{examples[1][:output]}
64+
65+
Input: #{examples[2][:input]}
66+
Output: #{examples[2][:output]}
67+
2768
The text to translate will be provided in JSON format with the following structure:
2869
{"content": "Text to translate", "target_locale": "Target language code"}
2970
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.
71+
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.
3472
PROMPT
3573
end
3674

37-
def response_format
38-
[{ "key" => "translation", "type" => "string" }]
39-
end
40-
4175
def temperature
4276
0.3
4377
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
6778
end
6879
end
6980
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)