Skip to content

Commit 70be96e

Browse files
committed
Separate the instruction from the data in order to prevent prompt injection
1 parent 9e10cdb commit 70be96e

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

pontoon/machinery/openai_service.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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"Refine the {target_language} machine translation below to make it {characteristic}.\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)