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
LangGraph Code with 3 Agents for Programming and Math Questions
Here's a LangGraph implementation with three agents:
- Evaluator Agent: Assesses the quality of responses
- Router Agent: Decides which specialist agent to use
- Search Agent: Handles both programming and math questions
from typing import TypedDict, Annotated, List from langgraph.graph import StateGraph, END from langgraph.prebuilt import ToolNode from langchain_core.messages import BaseMessage, HumanMessage from langchain_core.tools import tool from langchain_core.prompts import ChatPromptTemplate from langchain_core.runnables import RunnableLambda from langchain_community.tools import DuckDuckGoSearchRun from langchain_community.tools import WikipediaQueryRun from langchain_community.utilities import WikipediaAPIWrapper from langchain_core.messages import SystemMessage import operator from langchain_core.runnables import RunnablePassthrough # Define the state class AgentState(TypedDict): messages: Annotated[List[BaseMessage], operator.add] sender: str # To track which agent is sending the message # Search Agent Tools search_tool = DuckDuckGoSearchRun() wikipedia_tool = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper()) @tool def programming_search(query: str) -> str: """Useful for searching programming-related information.""" return search_tool.run(f"programming {query}") @tool def math_search(query: str) -> str: """Useful for searching math-related information.""" return search_tool.run(f"mathematics {query}") @tool def wikipedia_search(query: str) -> str: """Useful for general knowledge searches.""" return wikipedia_tool.run(query) search_tools = [programming_search, math_search, wikipedia_search] # Create the search agent search_agent_prompt = ChatPromptTemplate.from_messages([ ("system", """You are a specialized search agent that handles both programming and math questions. Use the appropriate tools to find accurate information. For programming questions, focus on: - Syntax and language features - Algorithms and data structures - Best practices and design patterns For math questions, focus on: - Theorems and proofs - Problem-solving techniques - Mathematical concepts and formulas Always provide clear, concise answers with proper citations when possible."""), ("placeholder", "{messages}"), ]) # Router Agent router_prompt = ChatPromptTemplate.from_messages([ ("system", """You are a router agent that decides which specialist agent should handle the question. Analyze the question and determine if it's: 1. A programming question (code, algorithms, software development) 2. A math question (equations, proofs, mathematical concepts) 3. A general question that needs evaluation Respond with only one of these options: 'programming', 'math', or 'evaluation'."""), ("placeholder", "{messages}"), ]) # Evaluator Agent evaluator_prompt = ChatPromptTemplate.from_messages([ ("system", """You are an evaluator agent that assesses the quality of responses. Evaluate the response based on: - Accuracy of information - Completeness of the answer - Clarity of explanation - Relevance to the question Provide a score from 1-10 and brief feedback on how to improve the response."""), ("placeholder", "{messages}"), ]) # Create the graph workflow = StateGraph(AgentState) # Define the nodes search_agent = search_agent_prompt | RunnablePassthrough.assign( tools=search_tools ) | ToolNode(search_tools) router_agent = router_prompt | RunnablePassthrough.assign( tools=[] ) | RunnableLambda(lambda x: {"response": x["messages"][-1].content}) evaluator_agent = evaluator_prompt | RunnablePassthrough.assign( tools=[] ) | RunnableLambda(lambda x: {"evaluation": x["messages"][-1].content}) # Add nodes to the graph workflow.add_node("search_agent", search_agent) workflow.add_node("router_agent", router_agent) workflow.add_node("evaluator_agent", evaluator_agent) # Define the edges workflow.add_edge("router_agent", "search_agent") workflow.add_edge("search_agent", "evaluator_agent") workflow.add_edge("evaluator_agent", END) # Define the entry point workflow.set_entry_point("router_agent") # Compile the graph app = workflow.compile() # Example usage def run_question(question: str): # Initialize the state state = {"messages": [HumanMessage(content=question)], "sender": "user"} # Run the graph result = app.invoke(state) # Print the results print("Original Question:", question) print("\nSearch Agent Response:", result["messages"][-2].content) print("\nEvaluation:", result["messages"][-1].content) # Example questions programming_question = "How do I implement a binary search tree in Python?" math_question = "What is the proof of the Pythagorean theorem?" print("=== Programming Question ===") run_question(programming_question) print("\n=== Math Question ===") run_question(math_question)Key Components:
-
Search Agent:
- Handles both programming and math questions
- Uses DuckDuckGo and Wikipedia for searches
- Has specialized tools for each domain
-
Router Agent:
- Determines whether a question is about programming, math, or needs evaluation
- Routes the question to the appropriate agent
-
Evaluator Agent:
- Assesses the quality of responses
- Provides feedback and scoring
How It Works:
- The user asks a question
- The router agent determines the question type
- The search agent finds relevant information
- The evaluator agent assesses the response quality
- The final evaluation is returned to the user
You can extend this by adding more specialized agents or tools as needed. The graph structure makes it easy to modify the workflow.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.