FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

On translation retry, pass current result and validation error · fastapi/fastapi@b7d2eb5 · GitHub

Repository navigation

Commit b7d2eb5

Browse files
committed
On translation retry, pass current result and validation error
1 parent 1e72cfe commit b7d2eb5

2 files changed

Lines changed: 65 additions & 33 deletions

File tree

‎scripts/general-llm-prompt.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ The original content is written in Markdown, write the translation in Markdown a
66

77
The original content will be surrounded by triple percentage signs (%%%). Do not include the triple percentage signs in the translation.
88

9+
[placeholder_for_additional_instructions]
10+
911
### Technical terms in English
1012

1113
For technical terms in English that don't have a common translation term, use the original term in English.

‎scripts/translate.py‎

Lines changed: 63 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -57,39 +57,19 @@ def generate_en_path(*, lang: str, path: Path) -> Path:
5757
return out_path
5858

5959

60-
@app.command()
61-
def translate_page(
62-
*,
63-
language: Annotated[str, typer.Option(envvar="LANGUAGE")],
64-
en_path: Annotated[Path, typer.Option(envvar="EN_PATH")],
65-
) -> None:
66-
assert language != "en", (
67-
"`en` is the source language, choose another language as translation target"
60+
def get_prompt(
61+
lang_prompt_content: str,
62+
old_translation: str | None,
63+
language: str,
64+
language_name: str,
65+
original_content: str,
66+
additional_instructions: str,
67+
) -> str:
68+
general_prompt_with_additional_instructions = general_prompt.replace(
69+
"[placeholder_for_additional_instructions]", additional_instructions
6870
)
69-
langs = get_langs()
70-
language_name = langs[language]
71-
lang_path = Path(f"docs/{language}")
72-
lang_path.mkdir(exist_ok=True)
73-
lang_prompt_path = lang_path / "llm-prompt.md"
74-
assert lang_prompt_path.exists(), f"Prompt file not found: {lang_prompt_path}"
75-
lang_prompt_content = lang_prompt_path.read_text(encoding="utf-8")
76-
77-
en_docs_path = Path("docs/en/docs")
78-
assert str(en_path).startswith(str(en_docs_path)), (
79-
f"Path must be inside {en_docs_path}"
80-
)
81-
out_path = generate_lang_path(lang=language, path=en_path)
82-
out_path.parent.mkdir(parents=True, exist_ok=True)
83-
original_content = en_path.read_text(encoding="utf-8")
84-
old_translation: str | None = None
85-
if out_path.exists():
86-
print(f"Found existing translation: {out_path}")
87-
old_translation = out_path.read_text(encoding="utf-8")
88-
print(f"Translating {en_path} to {language} ({language_name})")
89-
agent = Agent("openai:gpt-5")
90-
9171
prompt_segments = [
92-
general_prompt,
72+
general_prompt_with_additional_instructions,
9373
lang_prompt_content,
9474
]
9575
if old_translation:
@@ -119,12 +99,57 @@ def translate_page(
11999
f"%%%\n{original_content}%%%",
120100
]
121101
)
122-
prompt = "\n\n".join(prompt_segments)
102+
return "\n\n".join(prompt_segments)
103+
104+
105+
@app.command()
106+
def translate_page(
107+
*,
108+
language: Annotated[str, typer.Option(envvar="LANGUAGE")],
109+
en_path: Annotated[Path, typer.Option(envvar="EN_PATH")],
110+
) -> None:
111+
assert language != "en", (
112+
"`en` is the source language, choose another language as translation target"
113+
)
114+
langs = get_langs()
115+
language_name = langs[language]
116+
lang_path = Path(f"docs/{language}")
117+
lang_path.mkdir(exist_ok=True)
118+
lang_prompt_path = lang_path / "llm-prompt.md"
119+
assert lang_prompt_path.exists(), f"Prompt file not found: {lang_prompt_path}"
120+
lang_prompt_content = lang_prompt_path.read_text(encoding="utf-8")
121+
122+
en_docs_path = Path("docs/en/docs")
123+
assert str(en_path).startswith(str(en_docs_path)), (
124+
f"Path must be inside {en_docs_path}"
125+
)
126+
out_path = generate_lang_path(lang=language, path=en_path)
127+
out_path.parent.mkdir(parents=True, exist_ok=True)
128+
original_content = en_path.read_text(encoding="utf-8")
129+
old_translation: str | None = None
130+
if out_path.exists():
131+
print(f"Found existing translation: {out_path}")
132+
old_translation = out_path.read_text(encoding="utf-8")
133+
print(f"Translating {en_path} to {language} ({language_name})")
134+
agent = Agent("openai:gpt-5")
123135

124136
MAX_ATTEMPTS = 3
137+
additional_instructions = ""
125138
for attempt_no in range(1, MAX_ATTEMPTS + 1):
126139
print(f"Running agent for {out_path} (attempt {attempt_no}/{MAX_ATTEMPTS})")
127-
result = agent.run_sync(prompt)
140+
prompt = get_prompt(
141+
lang_prompt_content=lang_prompt_content,
142+
old_translation=old_translation,
143+
language=language,
144+
language_name=language_name,
145+
original_content=original_content,
146+
additional_instructions=additional_instructions,
147+
)
148+
result = agent.run_sync(
149+
prompt.replace(
150+
"[placeholder_for_additional_instructions]", additional_instructions
151+
)
152+
)
128153
out_content = f"{result.output.strip()}\n"
129154
try:
130155
check_translation(
@@ -139,6 +164,11 @@ def translate_page(
139164
print(
140165
f"Translation check failed on attempt {attempt_no}/{MAX_ATTEMPTS}: {e}"
141166
)
167+
additional_instructions = (
168+
f"Current translation fails validation checks ({str(e)}). "
169+
"Please, pay special attention to it."
170+
)
171+
old_translation = out_content
142172
continue # Retry if not reached max attempts
143173
else: # Max retry attempts reached
144174
print(f"Translation failed for {out_path} after {MAX_ATTEMPTS} attempts")

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL