On translation retry, pass current result and validation error

This commit is contained in:
Yurii Motov
2026-03-20 11:29:45 +01:00
parent 7c5797c6db
commit c7ca14486e
2 changed files with 66 additions and 34 deletions

View File

@@ -6,6 +6,8 @@ The original content is written in Markdown, write the translation in Markdown a
The original content will be surrounded by triple percentage signs (%%%). Do not include the triple percentage signs in the translation.
[placeholder_for_additional_instructions]
### Technical terms in English
For technical terms in English that don't have a common translation term, use the original term in English.

View File

@@ -57,6 +57,51 @@ def generate_en_path(*, lang: str, path: Path) -> Path:
return out_path
def get_prompt(
lang_prompt_content: str,
old_translation: str | None,
language: str,
language_name: str,
original_content: str,
additional_instructions: str,
) -> str:
general_prompt_with_additional_instructions = general_prompt.replace(
"[placeholder_for_additional_instructions]", additional_instructions
)
prompt_segments = [
general_prompt_with_additional_instructions,
lang_prompt_content,
]
if old_translation:
prompt_segments.extend(
[
"There is an existing previous translation for the original English content, that may be outdated.",
"Update the translation only where necessary:",
"- If the original English content has added parts, also add these parts to the translation.",
"- If the original English content has removed parts, also remove them from the translation, unless you were instructed earlier to not do that in specific cases.",
"- If parts of the original English content have changed, also change those parts in the translation.",
"- If the previous translation violates current instructions, update it.",
"- Otherwise, preserve the original translation LINE-BY-LINE, AS-IS.",
"Do not:",
"- rephrase or rewrite correct lines just to improve the style.",
"- add or remove line breaks, unless the original English content changed.",
"- change formatting or whitespace unless absolutely required.",
"Only change what must be changed. The goal is to minimize diffs for easier human review.",
"UNLESS you were instructed earlier to behave different, there MUST NOT be whole sentences or partial sentences in the updated translation, which are not in the original English content, and there MUST NOT be whole sentences or partial sentences in the original English content, which are not in the updated translation. Remember: the updated translation shall be IN SYNC with the original English content.",
"Previous translation:",
f"%%%\n{old_translation}%%%",
]
)
prompt_segments.extend(
[
f"Translate to {language} ({language_name}).",
"Original content:",
f"%%%\n{original_content}%%%",
]
)
return "\n\n".join(prompt_segments)
@app.command()
def translate_page(
*,
@@ -88,43 +133,23 @@ def translate_page(
print(f"Translating {en_path} to {language} ({language_name})")
agent = Agent("openai:gpt-5")
prompt_segments = [
general_prompt,
lang_prompt_content,
]
if old_translation:
prompt_segments.extend(
[
"There is an existing previous translation for the original English content, that may be outdated.",
"Update the translation only where necessary:",
"- If the original English content has added parts, also add these parts to the translation.",
"- If the original English content has removed parts, also remove them from the translation, unless you were instructed earlier to not do that in specific cases.",
"- If parts of the original English content have changed, also change those parts in the translation.",
"- If the previous translation violates current instructions, update it.",
"- Otherwise, preserve the original translation LINE-BY-LINE, AS-IS.",
"Do not:",
"- rephrase or rewrite correct lines just to improve the style.",
"- add or remove line breaks, unless the original English content changed.",
"- change formatting or whitespace unless absolutely required.",
"Only change what must be changed. The goal is to minimize diffs for easier human review.",
"UNLESS you were instructed earlier to behave different, there MUST NOT be whole sentences or partial sentences in the updated translation, which are not in the original English content, and there MUST NOT be whole sentences or partial sentences in the original English content, which are not in the updated translation. Remember: the updated translation shall be IN SYNC with the original English content.",
"Previous translation:",
f"%%%\n{old_translation}%%%",
]
)
prompt_segments.extend(
[
f"Translate to {language} ({language_name}).",
"Original content:",
f"%%%\n{original_content}%%%",
]
)
prompt = "\n\n".join(prompt_segments)
MAX_ATTEMPTS = 3
additional_instructions = ""
for attempt_no in range(1, MAX_ATTEMPTS + 1):
print(f"Running agent for {out_path} (attempt {attempt_no}/{MAX_ATTEMPTS})")
result = agent.run_sync(prompt)
prompt = get_prompt(
lang_prompt_content=lang_prompt_content,
old_translation=old_translation,
language=language,
language_name=language_name,
original_content=original_content,
additional_instructions=additional_instructions,
)
result = agent.run_sync(
prompt.replace(
"[placeholder_for_additional_instructions]", additional_instructions
)
)
out_content = f"{result.output.strip()}\n"
try:
check_translation(
@@ -139,6 +164,11 @@ def translate_page(
print(
f"Translation check failed on attempt {attempt_no}/{MAX_ATTEMPTS}: {e}"
)
additional_instructions = (
f"Current translation fails validation checks ({str(e)}). "
"Please, pay special attention to it."
)
old_translation = out_content
continue # Retry if not reached max attempts
else: # Max retry attempts reached
print(f"Translation failed for {out_path} after {MAX_ATTEMPTS} attempts")