Meta Global AI Developer Hackathon. $1M in prizes.Get notified
Third-partyAgentsRAGllama-cookbook
Agentic workflows with Llama and LangGraphA set of notebooks to build agents with Llama and LangGraph.Read more

Introduction

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.

Building a RAG Agent with LangGraph and Llama 3

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.

Architecture

The RAG agent's architecture is illustrated in the following Mermaid diagram:

Agentic workflows with Llama and LangGraph diagram

Implementation

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.

python
​
from langchain_core.tools import tool
from langgraph.prebuilt import ToolNode
from langchain_community.tools.tavily_search import TavilySearchResults
​
@tool
def 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 workflow
from langgraph import StateGraph, START, END
class RAGState:
query: str
docs: list
answer: str
graph = StateGraph(RAGState)
# Add nodes for routing, grading, and generation
graph.add_node("router", Router())
graph.add_node("grade_docs", GradeDocs())
graph.add_node("generate_answer", GenerateAnswer())
# Define the workflow
graph.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 graph
rag_agent = graph.compile()

Structure of LangGraph RAG Agent

RAG mermaid diagram

Building a Local RAG Agent with LangGraph and Llama 3

The langgraph_rag_agent_local.ipynb notebook shows how to build an advanced RAG agent that runs locally and reliably.

Architecture

The local RAG agent's architecture is similar to the previous one, with the addition of local deployment:

Agentic workflows with Llama and LangGraph diagram

Implementation

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 workflow
from langgraph import StateGraph, START, END
class LocalRAGState:
query: str
docs: list
answer: str
local_graph = StateGraph(LocalRAGState)
# Add nodes for routing, grading, and generation
local_graph.add_node("router", Router())
local_graph.add_node("grade_docs", GradeDocs())
local_graph.add_node("generate_answer", GenerateAnswer())
# Define the workflow
local_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 graph
local_rag_agent = local_graph.compile()

Building a Tool-Calling Agent with LangGraph and Llama 3#

The langgraph_tool_calling_agent.ipynb notebook demonstrates how to build a tool-calling agent using LangGraph and Llama 3.

Architecture

The tool-calling agent's architecture is illustrated in the following Mermaid diagram:

Agentic workflows with Llama and LangGraph diagram

Implementation

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.

python
​
from langchain_core.tools import tool
from langgraph.prebuilt import ToolNode
@tool
def magic_function(input: int) -> int:
"""Applies a magic function to an input."""
return input + 2
# Define the tool-calling agent's workflow
from langgraph import StateGraph, START, END
class ToolCallingState:
query: str
tool: str
answer: str
tool_calling_graph = StateGraph(ToolCallingState)
# Add nodes for tool selection and execution
tool_calling_graph.add_node("tool_selector", ToolSelector())
tool_calling_graph.add_node("tool_execution", ToolExecution())
# Define the workflow
tool_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 graph
tool_calling_agent = tool_calling_graph.compile()

Related GitHub Repositories

  • Llama Cookbook - A comprehensive cookbook for Llama models, including recipes for agentic workflows and RAG agents.
  • LangGraph - A library for building agentic workflows with LangChain.
  • LangChain - A framework for building applications with large language models.
Meta blue bg
Stay up-to-date

Our latest updates delivered to your inbox

Subscribe to our newsletter to keep up with the latest AI updates, releases and more.

Meta
Products
Models
Documentation
Resources
Community
Terms & policies