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

Commit df925f8

Browse files
authored
DEV: Move examples out of prompt (#1438)
* DEV: Move examples out of prompt
1 parent 32dc45b commit df925f8

File tree

4 files changed

+88
-40
lines changed

4 files changed

+88
-40
lines changed

lib/personas/post_raw_translator.rb

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ def system_prompt
1111
<<~PROMPT.strip
1212
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 carefully:
1313
14-
Translation Instructions:
1514
1. Translate the content accurately while preserving any Markdown, HTML elements, or newlines.
1615
2. Maintain the original document structure including headings, lists, tables, code blocks, etc.
1716
3. Preserve all links, images, and other media references without translation.
@@ -31,18 +30,7 @@ def system_prompt
3130
Output your translation in the following JSON format:
3231
{"translation": "Your translated text here"}
3332
34-
Here are three examples of correct translations:
35-
36-
Original: {"content":"New Update for Minecraft Adds Underwater Temples", "target_locale":"Spanish"}
37-
Correct translation: {"translation": "Nueva actualización para Minecraft añade templos submarinos"}
38-
39-
Original: {"content": "# Machine Learning 101\n\nMachine Learning (ML) is a subset of Artificial Intelligence (AI) that focuses on the development of algorithms and statistical models that enable computer systems to improve their performance on a specific task through experience.\n\n## Key Concepts\n\n1. **Supervised Learning**: The algorithm learns from labeled training data.\n2. **Unsupervised Learning**: The algorithm finds patterns in unlabeled data.\n3. **Reinforcement Learning**: The algorithm learns through interaction with an environment.\n\n```python\n# Simple example of a machine learning model\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.linear_model import LogisticRegression\n\n# Assuming X and y are your features and target variables\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n\nmodel = LogisticRegression()\nmodel.fit(X_train, y_train)\n\n# Evaluate the model\naccuracy = model.score(X_test, y_test)\nprint(f'Model accuracy: {accuracy}')\n```\n\nFor more information, visit [Machine Learning on Wikipedia](https://en.wikipedia.org/wiki/Machine_learning).", "target_locale":"French"}
40-
Correct translation: {"translation": "# Machine Learning 101\n\nLe Machine Learning (ML) est un sous-ensemble de l'Intelligence Artificielle (IA) qui se concentre sur le développement d'algorithmes et de modèles statistiques permettant aux systèmes informatiques d'améliorer leurs performances sur une tâche spécifique grâce à l'expérience.\n\n## Concepts clés\n\n1. **Apprentissage supervisé** : L'algorithme apprend à partir de données d'entraînement étiquetées.\n2. **Apprentissage non supervisé** : L'algorithme trouve des motifs dans des données non étiquetées.\n3. **Apprentissage par renforcement** : L'algorithme apprend à travers l'interaction avec un environnement.\n\n```python\n# Exemple simple d'un modèle de machine learning\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.linear_model import LogisticRegression\n\n# En supposant que X et y sont vos variables de caractéristiques et cibles\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n\nmodel = LogisticRegression()\nmodel.fit(X_train, y_train)\n\n# Évaluer le modèle\naccuracy = model.score(X_test, y_test)\nprint(f'Model accuracy: {accuracy}')\n```\n\nPour plus d'informations, visitez [Machine Learning sur Wikipedia](https://en.wikipedia.org/wiki/Machine_learning)."}
41-
42-
Original: {"content": "**Heathrow fechado**: paralisação de voos deve continuar nos próximos dias, diz gestora do aeroporto de *Londres*", "target_locale": "English"}
43-
Correct translation: {"translation": "**Heathrow closed**: flight disruption expected to continue in coming days, says *London* airport management"}
44-
45-
Remember, 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 in your response.
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.
4634
PROMPT
4735
end
4836

@@ -53,6 +41,40 @@ def response_format
5341
def temperature
5442
0.3
5543
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+
{
67+
content:
68+
"# Machine Learning 101\n\nMachine Learning (ML) is a subset of Artificial Intelligence (AI) that focuses on the development of algorithms and statistical models that enable computer systems to improve their performance on a specific task through experience.\n\n## Key Concepts\n\n1. **Supervised Learning**: The algorithm learns from labeled training data.\n2. **Unsupervised Learning**: The algorithm finds patterns in unlabeled data.\n3. **Reinforcement Learning**: The algorithm learns through interaction with an environment.\n\n```python\n# Simple example of a machine learning model\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.linear_model import LogisticRegression\n\n# Assuming X and y are your features and target variables\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n\nmodel = LogisticRegression()\nmodel.fit(X_train, y_train)\n\n# Evaluate the model\naccuracy = model.score(X_test, y_test)\nprint(f'Model accuracy: {accuracy}')\n```\n\nFor more information, visit [Machine Learning on Wikipedia](https://en.wikipedia.org/wiki/Machine_learning).",
69+
target_locale: "fr",
70+
}.to_json,
71+
{
72+
translation:
73+
"# Machine Learning 101\n\nLe Machine Learning (ML) est un sous-ensemble de l'Intelligence Artificielle (IA) qui se concentre sur le développement d'algorithmes et de modèles statistiques permettant aux systèmes informatiques d'améliorer leurs performances sur une tâche spécifique grâce à l'expérience.\n\n## Concepts clés\n\n1. **Apprentissage supervisé** : L'algorithme apprend à partir de données d'entraînement étiquetées.\n2. **Apprentissage non supervisé** : L'algorithme trouve des motifs dans des données non étiquetées.\n3. **Apprentissage par renforcement** : L'algorithme apprend à travers l'interaction avec un environnement.\n\n```python\n# Exemple simple d'un modèle de machine learning\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.linear_model import LogisticRegression\n\n# En supposant que X et y sont vos variables de caractéristiques et cibles\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n\nmodel = LogisticRegression()\nmodel.fit(X_train, y_train)\n\n# Évaluer le modèle\naccuracy = model.score(X_test, y_test)\nprint(f'Model accuracy: {accuracy}')\n```\n\nPour plus d'informations, visitez [Machine Learning sur Wikipedia](https://en.wikipedia.org/wiki/Machine_learning).",
74+
}.to_json,
75+
],
76+
]
77+
end
5678
end
5779
end
58-
end
80+
end

lib/personas/short_text_translator.rb

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def system_prompt
1212
You are a translation service specializing in translating short pieces of text or a few words.
1313
These words may be things like a name, description, or title. Adhere to the following guidelines:
1414
15-
1. Keep proper nouns and technical terms in their original language
15+
1. Keep proper nouns (like 'Minecraft' or 'Toyota') and technical terms (like 'JSON') in their original language
1616
2. Keep the translated content close to the original length
1717
3. Translation maintains the original meaning
1818
4. Preserve any Markdown, HTML elements, links, parenthesis, or newlines
@@ -23,18 +23,7 @@ def system_prompt
2323
Provide your translation in the following JSON format:
2424
{"translation": "target_locale translation here"}
2525
26-
Here are three examples of correct translation
27-
28-
Original: {"content":"Japan", "target_locale":"es"}
29-
Correct translation: {"translation": "Japón"}
30-
31-
Original: {"content":"Cats and Dogs", "target_locale":"zh_CN"}
32-
Correct translation: {"translation": "猫和狗"}
33-
34-
Original: {"content": "Q&A", "target_locale": "pt"}
35-
Correct translation: {"translation": "Perguntas e Respostas"}
36-
37-
Remember to keep proper nouns like "Minecraft" and "Toyota" in their original form. Translate the text now and provide your answer in the specified JSON format.
26+
Translate the text now and provide your answer in the specified JSON format.
3827
PROMPT
3928
end
4029

@@ -45,6 +34,24 @@ def response_format
4534
def temperature
4635
0.3
4736
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
4855
end
4956
end
5057
end

lib/personas/topic_title_translator.rb

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,6 @@ def system_prompt
3030
Provide your translation in the following JSON format:
3131
{"translation": "Your target_locale translation here"}
3232
33-
Here are three examples of correct translation
34-
35-
Original: {"content":"New Update for Minecraft Adds Underwater Temples", "target_locale":"es"}
36-
Correct translation: {"translation": "Nueva actualización para Minecraft añade templos submarinos"}
37-
38-
Original: {"content":"Toyota announces revolutionary battery technology", "target_locale":"fr"}
39-
Correct translation: {"translation": "Toyota annonce une technologie de batteries révolutionnaire"}
40-
41-
Original: {"content": "Heathrow fechado: paralisação de voos deve continuar nos próximos dias, diz gestora do aeroporto de Londres", "target_locale": "en"}
42-
Correct translation: {"translation": "Heathrow closed: flight disruption expected to continue in coming days, says London airport management"}
43-
4433
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.
4534
PROMPT
4635
end
@@ -52,6 +41,36 @@ def response_format
5241
def temperature
5342
0.3
5443
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
5574
end
5675
end
57-
end
76+
end

spec/lib/translation/base_translator_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
DiscourseAi::Translation::PostRawTranslator.new(text:, target_locale:, post:)
2929
allow(DiscourseAi::Completions::Prompt).to receive(:new).with(
3030
persona.system_prompt,
31-
messages: [{ type: :user, content: post_translator.formatted_content }],
31+
messages: array_including({ type: :user, content: post_translator.formatted_content }),
3232
post_id: post.id,
3333
topic_id: post.topic_id,
3434
).and_call_original

0 commit comments

Comments
 (0)