chatgpt-4o-latest-20250326· OpenAI
LMArena Human Preference 140K
give me some lang-graph code with 3 agents, one to evaluate, one to route, one to search…
- User
give me some lang-graph code with 3 agents, one to evaluate, one to route, one to search against programming questions and math questions
- Assistant
Here’s a basic example of using LangGraph to build a multi-agent architecture with three agents in Python:
- Search Agent: Answers programming or math questions.
- Routing Agent: Determines the type of question (Math or Programming).
- Evaluation Agent: Reviews the result returned and checks if it’s suitable or needs retry.
We'll use LangChain + LangGraph and simulate agents using simple PromptChains for this demo.
🧠 LangGraph 3-Agent Example (Math + Programming Q&A)
📦 Install Dependencies
pip install langgraph langchain openai🔐 You must also set your OpenAI API Key as
OPENAI_API_KEY.
📁 Full Code Example
from langchain.chat_models import ChatOpenAI from langchain.chains import LLMChain from langchain.prompts import PromptTemplate from langgraph.graph import END, StateGraph from langgraph.graph.state import State # Define the state of the graph class QAState(State): question: str type: str = None answer: str = None eval_passed: bool = False # ➤ 1. Agent Definitions # Routing Agent: Determines if it's a math or programming question route_prompt = PromptTemplate.from_template(""" You are a smart assistant. Classify the following question as either "math" or "programming". Question: {question} Answer only with "math" or "programming". """) router_chain = LLMChain(llm=ChatOpenAI(), prompt=route_prompt) def router_agent(state: QAState): output = router_chain.run(state.dict()) state.type = output.strip().lower() return state # Math Search Agent math_prompt = PromptTemplate.from_template(""" You are a math expert. Provide a step-by-step solution to the following question: {question} """) math_chain = LLMChain(llm=ChatOpenAI(), prompt=math_prompt) # Programming Search Agent programming_prompt = PromptTemplate.from_template(""" You are a programming tutor. Solve the following question and explain the answer: {question} """) programming_chain = LLMChain(llm=ChatOpenAI(), prompt=programming_prompt) def search_agent(state: QAState): if state.type == "math": output = math_chain.run(state.dict()) elif state.type == "programming": output = programming_chain.run(state.dict()) else: output = "I don't know how to classify this question." state.answer = output return state # Evaluation Agent eval_prompt = PromptTemplate.from_template(""" You are an evaluator. Determine whether the following answer correctly solves the given question. Question: {question} Answer: {answer} Reply with "yes" or "no", and nothing else. """) eval_chain = LLMChain(llm=ChatOpenAI(), prompt=eval_prompt) def evaluation_agent(state: QAState): output = eval_chain.run(state.dict()).strip().lower() state.eval_passed = "yes" in output return state # ➤ 2. Build Graph with LangGraph # Create a graph builder = StateGraph(QAState) # Add nodes builder.add_node("router", router_agent) builder.add_node("search", search_agent) builder.add_node("evaluator", evaluation_agent) # Define edges builder.set_entry_point("router") builder.add_edge("router", "search") builder.add_edge("search", "evaluator") builder.add_conditional_edges("evaluator", condition=lambda state: state.eval_passed, path_map={ True: END, False: "search" # retry search if eval fails }) # Build and compile graph = builder.compile() # ➤ 3. Test It test_question = "Write a Python function to calculate the Fibonacci sequence." initial_state = QAState(question=test_question) final_state = graph.invoke(initial_state) # Print the final result print("Question:", final_state.question) print("Detected Type:", final_state.type) print("Answer:\n", final_state.answer) print("Evaluation Passed:", final_state.eval_passed)
🏁 Output (Example)
Question: Write a Python function to calculate the Fibonacci sequence. Detected Type: programming Answer: Here's a Python function... Evaluation Passed: True
✅ Features of This LangGraph
- Router: Uses LLM to classify.
- Search Agent: Branches into math or code.
- Evaluator: Quality control and retry logic.
- Retry Loop: Re-evaluates till good answer.
🧩 You Can Extend This By:
- Storing history in state.
- Using tools like Wolfram or Python REPL.
- Logging or caching results.
- Adding a fallback agent (e.g., web search).
If you want a version using
ToolNodeor customRunnableagents, I can provide a more advanced example too. Let me know!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.