How to Recreate a Translation Agent with LangGraph and LLMs
This guide demonstrates building a steerable LLM‑based translation workflow using LangGraph, covering the initial translation, model‑generated reflection suggestions, and final improvement steps with full Python code examples and a complete execution result.
Translation Agent Demonstration
Invoke the translation workflow with English source text, Chinese target language, and a prompt that illustrates the LLM‑based translation engine’s steerability.
# execute task
result = app.invoke({
"source_lang": "English",
"target_lang": "中文",
"source_text": """By using an LLM as the heart of the translation engine, this system is highly steerable. For example, by changing the prompts, it is easier using this workflow than a traditional machine translation (MT) system to:
Modify the output's style, such as formal/informal.
Specify how to handle idioms and special terms like names, technical terms, and acronyms. For example, including a glossary in the prompt lets you make sure particular terms (such as open source, H100 or GPU) are translated consistently.
Specify specific regional use of the language, or specific dialects, to serve a target audience. For example, Spanish spoken in Latin America is different from Spanish spoken in Spain; French spoken in Canada is different from how it is spoken in France."""
})Workflow Result
The workflow returns the initial translation, model‑generated improvement suggestions, and the final refined translation.
LangGraph Overview
LangGraph is a Python library for building stateful, multi‑agent applications that work with large language models. It extends the LangChain expression language, supports cyclic workflows, and draws inspiration from Pregel, Apache Beam, and NetworkX.
Key Features
Loop support : Enables workflows with cycles, essential for many agent architectures.
State management : Stores and retrieves information across steps.
Multi‑agent : Allows several agents to interact.
Scalability : Suitable for production‑grade workloads.
High‑level and low‑level APIs : Provides ready‑made agent types and custom workflow composition.
Translation Agent Project
Open‑source repository: https://github.com/andrewyng/translation-agent
Core Functions
one_chunk_initial_translation : Calls the LLM to translate the whole source text.
one_chunk_reflect_on_translation : Asks the LLM to critique the initial translation and return concrete suggestions.
one_chunk_improve_translation : Uses the suggestions to produce a refined translation.
DeepSeek API Wrapper
Utility code configures the DeepSeek endpoint, creates an OpenAI client, and defines get_completion to send prompts and receive model responses.
Recreating the Agent with LangGraph
A State TypedDict describes the workflow state (source/target language, text, optional country, intermediate results). A StateGraph instance registers three nodes— initial_translation, reflect_on_translation, and improve_translation —and connects them sequentially.
Node Implementations
initial_translation builds a system message and prompt, calls get_completion, prints the result, and stores translation_1.
reflect_on_translation constructs a reflection prompt (optionally adding regional style rules), calls get_completion, prints suggestions, and stores reflection.
improve_translation creates an editing prompt that incorporates the expert suggestions, calls get_completion, prints the final translation, and stores translation_2.
Workflow Assembly
workflow.add_node("initial_translation", initial_translation)
workflow.add_node("reflect_on_translation", reflect_on_translation)
workflow.add_node("improve_translation", improve_translation)
workflow.set_entry_point("initial_translation")
workflow.add_edge("initial_translation", "reflect_on_translation")
workflow.add_edge("reflect_on_translation", "improve_translation")
workflow.add_edge("improve_translation", END)After compilation, invoke the workflow with a dictionary containing source_lang, target_lang, and source_text. The result includes the original translation, the model’s improvement suggestions, and the final refined translation.
DeepSeek API Configuration
import json, openai, os
from langgraph.graph import StateGraph, START, END
deep_seek_api_key = 'sk-xxxx'
deep_seek_url = 'https://api.deepseek.com/v1'
deep_seek_default_model = 'deepseek-reasoner'
client = openai.OpenAI(api_key=deep_seek_api_key, base_url=deep_seek_url)
default_model = deep_seek_default_model
def get_completion(prompt: str, system_message: str = "You are a helpful assistant.", model: str = default_model, temperature: float = 0.3, json_mode: bool = False) -> str:
"""Send a request to DeepSeek API and return the generated text."""
response = client.chat.completions.create(
model=model,
temperature=temperature,
top_p=1,
messages=[
{"role": "system", "content": system_message},
{"role": "user", "content": prompt},
],
)
return response.choices[0].message.contentState Definition
from typing import TypedDict, Optional
class State(TypedDict):
source_lang: str
target_lang: str
source_text: str
country: Optional[str] = None
translation_1: Optional[str] = None
reflection: Optional[str] = None
translation_2: Optional[str] = None
workflow = StateGraph(State)Initial Translation Node
def initial_translation(state):
source_lang = state.get("source_lang")
target_lang = state.get("target_lang")
source_text = state.get("source_text")
system_message = f"You are an expert linguist, specializing in translation from {source_lang} to {target_lang}."
prompt = f"""This is an {source_lang} to {target_lang} translation, please provide the {target_lang} translation for this text.
Do not provide any explanations or text apart from the translation.
{source_lang}: {source_text}
{target_lang}:"""
translation = get_completion(prompt, system_message=system_message)
print("[Initial translation]:", translation)
return {"translation_1": translation}Reflection Node
def reflect_on_translation(state):
source_lang = state.get("source_lang")
target_lang = state.get("target_lang")
source_text = state.get("source_text")
translation_1 = state.get("translation_1")
country = state.get("country") or ""
system_message = f"You are an expert linguist specializing in translation from {source_lang} to {target_lang}. Your goal is to improve the translation."
additional_rule = f"The final style and tone should match the colloquial {target_lang} spoken in {country}." if country else ""
prompt = f"""Your task is to read the source text and its translation, then give constructive criticism and suggestions to improve the translation.
{additional_rule}
< SOURCE_TEXT >
{source_text}
< /SOURCE_TEXT >
< TRANSLATION >
{translation_1}
< /TRANSLATION >
When writing suggestions, consider:
(i) accuracy, (ii) fluency, (iii) style, (iv) terminology.
Write a list of specific, helpful suggestions. Output only the suggestions."""
reflection = get_completion(prompt, system_message=system_message)
print("[Reflection suggestions]:", reflection)
return {"reflection": reflection}Improvement Node
def improve_translation(state):
source_lang = state.get("source_lang")
target_lang = state.get("target_lang")
source_text = state.get("source_text")
translation_1 = state.get("translation_1")
reflection = state.get("reflection")
system_message = f"You are an expert linguist, specializing in translation editing from {source_lang} to {target_lang}."
prompt = f"""Edit the translation using the expert suggestions.
< SOURCE_TEXT >
{source_text}
< /SOURCE_TEXT >
< TRANSLATION >
{translation_1}
< /TRANSLATION >
< EXPERT_SUGGESTIONS >
{reflection}
< /EXPERT_SUGGESTIONS >
Ensure (i) accuracy, (ii) fluency, (iii) style, (iv) terminology, (v) other errors are addressed.
Output only the new translation."""
translation_2 = get_completion(prompt, system_message=system_message)
print("[Final translation]:", translation_2)
return {"translation_2": translation_2}Compile and Run
# Compile the workflow
app = workflow.compile()
# Execute the workflow
result = app.invoke({
"source_lang": "English",
"target_lang": "中文",
"source_text": "By using an LLM as the heart of the translation engine, this system is highly steerable. For example, by changing the prompts, it is easier using this workflow than a traditional machine translation (MT) system to:
Modify the output's style, such as formal/informal.
Specify how to handle idioms and special terms like names, technical terms, and acronyms. For example, including a glossary in the prompt lets you make sure particular terms (such as open source, H100 or GPU) are translated consistently.
Specify specific regional use of the language, or specific dialects, to serve a target audience. For example, Spanish spoken in Latin America is different from Spanish spoken in Spain; French spoken in Canada is different from how it is spoken in France."
})
print(result)Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Sohu Tech Products
A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
