
Agentic workflows have revolutionized the way we build intelligent systems, enabling them to perform complex tasks autonomously. In this blog, we'll explore how to build agentic workflows using Llama and LangGraph, focusing on RAG agents and tool-calling agents. We'll dive into the implementation details of three key notebooks: langgraph_rag_agent.ipynb, langgraph_rag_agent_local.ipynb, and langgraph_tool_calling_agent.ipynb.
The langgraph_rag_agent.ipynb notebook demonstrates how to build a custom Llama 3 powered RAG agent using LangGraph. This agent combines ideas from three RAG papers: Corrective-RAG (CRAG), Self-RAG, and Adaptive RAG.
The RAG agent's architecture is illustrated in the following Mermaid diagram:
The implementation involves defining nodes for the agent's workflow, including routing, grading, and generation. We use LangGraph's ToolNode to integrate tools such as web search and vectorstore retrieval.
pythonfrom langchain_core.tools import toolfrom langgraph.prebuilt import ToolNodefrom langchain_community.tools.tavily_search import TavilySearchResults@tooldef web_search(input: str) -> str:"""Runs web search."""web_search_tool = TavilySearchResults()docs = web_search_tool.invoke({"query": input})return docs# Define the RAG agent's workflowfrom langgraph import StateGraph, START, ENDclass RAGState:query: strdocs: listanswer: strgraph = StateGraph(RAGState)# Add nodes for routing, grading, and generationgraph.add_node("router", Router())graph.add_node("grade_docs", GradeDocs())graph.add_node("generate_answer", GenerateAnswer())# Define the workflowgraph.add_edge(START, "router")graph.add_edge("router", "grade_docs")graph.add_edge("grade_docs", "generate_answer")graph.add_edge("generate_answer", END)# Compile the graphrag_agent = graph.compile()
The langgraph_rag_agent_local.ipynb notebook shows how to build an advanced RAG agent that runs locally and reliably.
The local RAG agent's architecture is similar to the previous one, with the addition of local deployment:
As above, the implementation involves defining nodes for the agent's workflow, including routing, grading, and generation--and we use LangGraph's ToolNode to integrate tools such as web search and vectorstore retrieval.
python# Define the local RAG agent's workflowfrom langgraph import StateGraph, START, ENDclass LocalRAGState:query: strdocs: listanswer: strlocal_graph = StateGraph(LocalRAGState)# Add nodes for routing, grading, and generationlocal_graph.add_node("router", Router())local_graph.add_node("grade_docs", GradeDocs())local_graph.add_node("generate_answer", GenerateAnswer())# Define the workflowlocal_graph.add_edge(START, "router")local_graph.add_edge("router", "grade_docs")local_graph.add_edge("grade_docs", "generate_answer")local_graph.add_edge("generate_answer", END)# Compile the graphlocal_rag_agent = local_graph.compile()
The langgraph_tool_calling_agent.ipynb notebook demonstrates how to build a tool-calling agent using LangGraph and Llama 3.
The tool-calling agent's architecture is illustrated in the following Mermaid diagram:
The implementation involves defining nodes for the agent's workflow, including tool selection and execution. Here we use LangGraph's ToolNode to integrate tools such as magic function and web search.
pythonfrom langchain_core.tools import toolfrom langgraph.prebuilt import ToolNode@tooldef magic_function(input: int) -> int:"""Applies a magic function to an input."""return input + 2# Define the tool-calling agent's workflowfrom langgraph import StateGraph, START, ENDclass ToolCallingState:query: strtool: stranswer: strtool_calling_graph = StateGraph(ToolCallingState)# Add nodes for tool selection and executiontool_calling_graph.add_node("tool_selector", ToolSelector())tool_calling_graph.add_node("tool_execution", ToolExecution())# Define the workflowtool_calling_graph.add_edge(START, "tool_selector")tool_calling_graph.add_edge("tool_selector", "tool_execution")tool_calling_graph.add_edge("tool_execution", END)# Compile the graphtool_calling_agent = tool_calling_graph.compile()
Subscribe to our newsletter to keep up with the latest AI updates, releases and more.