Skip to content

Commit baa12d2

Browse files
authored
OpenAIService Prompt improvements (mozilla#4018)
Separate the instruction from the data in order to prevent prompt injection
1 parent 7ef49a4 commit baa12d2

1 file changed

Lines changed: 15 additions & 10 deletions

File tree

pontoon/machinery/openai_service.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ def get_translation(
2222
f"The target language '{target_language_name}' is not supported."
2323
)
2424

25+
intro_text = f"Refine the {target_language} machine translation below to make it {characteristic}."
26+
2527
common_rules = textwrap.dedent(
26-
"""1) ENDING PUNCTUATION — SEMANTICS, NOT LITERAL CHAR:
28+
"""Follow these rules IN ORDER OF PRIORITY:
29+
1) ENDING PUNCTUATION — SEMANTICS, NOT LITERAL CHAR:
2730
- Detect the English ending: none, ".", "?", "!", "…".
2831
- The translation MUST express the same ending SEMANTIC:
2932
• if English ends with "?" → translation ends with a question.
@@ -36,18 +39,16 @@ def get_translation(
3639
)
3740

3841
informal = textwrap.dedent(
39-
f"""You will be provided with text in English, along with its machine-generated translation in {target_language}.
40-
Revise the {target_language} translation to use simpler language,
41-
Follow these rules IN ORDER OF PRIORITY:
42+
f"""{intro_text}
43+
Revise the {target_language} translation to use simpler language.
4244
{common_rules}
4345
3) Clarity and Simplicity: keep wording straightforward and consistent.
4446
Output only the revised translation."""
4547
)
4648

4749
formal = textwrap.dedent(
48-
f"""You will be provided with text in English, along with its machine-generated translation in {target_language}.
50+
f"""{intro_text}
4951
Revise the {target_language} translation to use a higher level of formality.
50-
Follow these rules IN ORDER OF PRIORITY:
5152
{common_rules}
5253
3) Consistency: maintain a consistent level of formality throughout; do not mix formal and informal modes.
5354
4) Preserve all HTML tags and their order. Do not add, remove, or reorder tags.
@@ -56,9 +57,8 @@ def get_translation(
5657
)
5758

5859
rephrased = textwrap.dedent(
59-
f"""You will be provided with text in English, along with its machine-generated translation in {target_language}.
60+
f"""{intro_text}
6061
Provide an alternative translation that preserves the original meaning while varying the wording.
61-
Follow these rules IN ORDER OF PRIORITY:
6262
{common_rules}
6363
3) Cultural and Idiomatic Fit: adapt idioms and culturally marked expressions appropriately for {target_language}; you may restructure sentences but must not introduce new information or omit essential meaning.
6464
4) Clarity and Naturalness: ensure the result reads naturally and is easy to understand.
@@ -75,8 +75,13 @@ def get_translation(
7575
if system_message is None:
7676
raise ValueError(f"Unrecognized characteristic: '{characteristic}'")
7777

78-
# Construct the user prompt with the language name
79-
user_prompt = f"Refine the following {target_language} machine translation to make it {characteristic}: '{translated_text}' based on the original English text: '{english_text}'."
78+
# Separate the instruction from the data.
79+
# It makes it hard for injected text to masquerade as instructions.
80+
user_prompt = (
81+
f"{intro_text}\n\n"
82+
f"ENGLISH SOURCE:\n{english_text}\n\n"
83+
f"MACHINE TRANSLATION TO REFINE:\n{translated_text}"
84+
)
8085

8186
# Call the OpenAI API with the constructed prompt
8287
response = self.client.chat.completions.create(

0 commit comments

Comments
 (0)