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

Commit f2d0eff

Browse files
committed
prompts
1 parent 314f654 commit f2d0eff

File tree

7 files changed

+39
-27
lines changed

7 files changed

+39
-27
lines changed

lib/personas/locale_detector.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,23 @@ def response_format
5252
def temperature
5353
0
5454
end
55+
56+
def examples
57+
spanish = <<~MARKDOWN
58+
[quote]
59+
Non smettere mai di credere nella bellezza dei tuoi sogni. Anche quando tutto sembra perduto, c'è sempre una luce che aspetta di essere trovata.
60+
61+
Ogni passo, anche il più piccolo, ti avvicina a ciò che desideri. La forza che cerchi è già dentro di te.
62+
[/quote]
63+
64+
¿Cuál es el mensaje principal de esta cita?
65+
MARKDOWN
66+
67+
[
68+
["Can you tell me what '私の世界で一番好きな食べ物はちらし丼です' means?", { locale: "en" }.to_json],
69+
[spanish, { locale: "es" }.to_json],
70+
]
71+
end
5572
end
5673
end
5774
end

lib/personas/post_raw_translator.rb

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ def self.default_enabled
99

1010
def system_prompt
1111
<<~PROMPT.strip
12-
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:
12+
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:
1313
14-
1. Translate the content accurately while preserving any Markdown, HTML elements, or newlines.
14+
1. Preserve Markdown elements, HTML elements, or newlines. Text must be translated without altering the original formatting.
1515
2. Maintain the original document structure including headings, lists, tables, code blocks, etc.
1616
3. Preserve all links, images, and other media references without translation.
17-
4. Handle code snippets appropriately:
18-
- Do not translate variable names, functions, or syntax within code blocks (```).
19-
- Translate comments within code blocks.
20-
5. For technical terminology:
17+
4. For technical terminology:
2118
- Provide the accepted target language term if it exists.
2219
- If no equivalent exists, transliterate the term and include the original term in parentheses.
23-
6. For ambiguous terms or phrases, choose the most contextually appropriate translation.
24-
7. Do not add any content besides the translation.
25-
8. Ensure the translation only contains the original language and the target language.
20+
5. For ambiguous terms or phrases, choose the most contextually appropriate translation.
21+
6. Ensure the translation only contains the original language and the target language.
22+
23+
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.
25+
8. Do not add any content besides the translation.
2626
2727
The text to translate will be provided in JSON format with the following structure:
2828
{"content": "Text to translate", "target_locale": "Target language code"}
@@ -62,17 +62,6 @@ def examples
6262
}.to_json,
6363
{ translation: "Nueva actualización para Minecraft añade templos submarinos" }.to_json,
6464
],
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-
],
7665
]
7766
end
7867
end

lib/translation/language_detector.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ module Translation
55
class LanguageDetector
66
DETECTION_CHAR_LIMIT = 1000
77

8-
def initialize(text)
8+
def initialize(text, topic: nil, post: nil)
99
@text = text
10+
@topic = topic
11+
@post = post
1012
end
1113

1214
def detect
@@ -36,6 +38,8 @@ def detect
3638
skip_tool_details: true,
3739
feature_name: "translation",
3840
messages: [{ type: :user, content: @text }],
41+
topic: topic,
42+
post: post,
3943
)
4044

4145
structured_output = nil

lib/translation/post_locale_detector.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def self.detect_locale(post)
77
return if post.blank?
88

99
text = PostDetectionText.get_text(post)
10-
detected_locale = LanguageDetector.new(text).detect
10+
detected_locale = LanguageDetector.new(text, post:).detect
1111
locale = LocaleNormalizer.normalize_to_i18n(detected_locale)
1212
post.update_column(:locale, locale)
1313
locale

lib/translation/topic_locale_detector.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class TopicLocaleDetector
66
def self.detect_locale(topic)
77
return if topic.blank?
88

9-
detected_locale = LanguageDetector.new(topic.title.dup).detect
9+
detected_locale = LanguageDetector.new(topic.title.dup, topic:).detect
1010
locale = LocaleNormalizer.normalize_to_i18n(detected_locale)
1111
topic.update_column(:locale, locale)
1212
locale

spec/lib/translation/post_locale_detector_spec.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ def language_detector_stub(opts)
88
mock = instance_double(DiscourseAi::Translation::LanguageDetector)
99
allow(DiscourseAi::Translation::LanguageDetector).to receive(:new).with(
1010
opts[:text],
11+
post: opts[:post],
1112
).and_return(mock)
1213
allow(mock).to receive(:detect).and_return(opts[:locale])
1314
end
@@ -17,7 +18,7 @@ def language_detector_stub(opts)
1718
end
1819

1920
it "updates the post locale with the detected locale" do
20-
language_detector_stub({ text: post.cooked, locale: "zh_CN" })
21+
language_detector_stub({ text: post.cooked, locale: "zh_CN", post: })
2122
expect { described_class.detect_locale(post) }.to change { post.reload.locale }.from(nil).to(
2223
"zh_CN",
2324
)
@@ -26,7 +27,7 @@ def language_detector_stub(opts)
2627
it "bypasses validations when updating locale" do
2728
post.update_column(:cooked, "A")
2829

29-
language_detector_stub({ text: post.cooked, locale: "zh_CN" })
30+
language_detector_stub({ text: post.cooked, locale: "zh_CN", post: })
3031

3132
described_class.detect_locale(post)
3233
expect(post.reload.locale).to eq("zh_CN")

spec/lib/translation/topic_locale_detector_spec.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def language_detector_stub(opts)
99
mock = instance_double(DiscourseAi::Translation::LanguageDetector)
1010
allow(DiscourseAi::Translation::LanguageDetector).to receive(:new).with(
1111
opts[:text],
12+
topic: opts[:topic],
1213
).and_return(mock)
1314
allow(mock).to receive(:detect).and_return(opts[:locale])
1415
end
@@ -18,7 +19,7 @@ def language_detector_stub(opts)
1819
end
1920

2021
it "updates the topic locale with the detected locale" do
21-
language_detector_stub({ text: "This is a cat topic", locale: "zh_CN" })
22+
language_detector_stub({ text: "This is a cat topic", locale: "zh_CN", topic: })
2223
expect { described_class.detect_locale(topic) }.to change { topic.reload.locale }.from(
2324
nil,
2425
).to("zh_CN")
@@ -29,7 +30,7 @@ def language_detector_stub(opts)
2930
SiteSetting.min_topic_title_length = 15
3031
SiteSetting.max_topic_title_length = 16
3132

32-
language_detector_stub({ text: "A", locale: "zh_CN" })
33+
language_detector_stub({ text: "A", locale: "zh_CN", topic: })
3334

3435
described_class.detect_locale(topic)
3536
expect(topic.reload.locale).to eq("zh_CN")

0 commit comments

Comments
 (0)