LangChain OpenTutorial
  • 🦜️🔗 The LangChain Open Tutorial for Everyone
  • 01-Basic
    • Getting Started on Windows
    • 02-Getting-Started-Mac
    • OpenAI API Key Generation and Testing Guide
    • LangSmith Tracking Setup
    • Using the OpenAI API (GPT-4o Multimodal)
    • Basic Example: Prompt+Model+OutputParser
    • LCEL Interface
    • Runnable
  • 02-Prompt
    • Prompt Template
    • Few-Shot Templates
    • LangChain Hub
    • Personal Prompts for LangChain
    • Prompt Caching
  • 03-OutputParser
    • PydanticOutputParser
    • PydanticOutputParser
    • CommaSeparatedListOutputParser
    • Structured Output Parser
    • JsonOutputParser
    • PandasDataFrameOutputParser
    • DatetimeOutputParser
    • EnumOutputParser
    • Output Fixing Parser
  • 04-Model
    • Using Various LLM Models
    • Chat Models
    • Caching
    • Caching VLLM
    • Model Serialization
    • Check Token Usage
    • Google Generative AI
    • Huggingface Endpoints
    • HuggingFace Local
    • HuggingFace Pipeline
    • ChatOllama
    • GPT4ALL
    • Video Q&A LLM (Gemini)
  • 05-Memory
    • ConversationBufferMemory
    • ConversationBufferWindowMemory
    • ConversationTokenBufferMemory
    • ConversationEntityMemory
    • ConversationKGMemory
    • ConversationSummaryMemory
    • VectorStoreRetrieverMemory
    • LCEL (Remembering Conversation History): Adding Memory
    • Memory Using SQLite
    • Conversation With History
  • 06-DocumentLoader
    • Document & Document Loader
    • PDF Loader
    • WebBaseLoader
    • CSV Loader
    • Excel File Loading in LangChain
    • Microsoft Word(doc, docx) With Langchain
    • Microsoft PowerPoint
    • TXT Loader
    • JSON
    • Arxiv Loader
    • UpstageDocumentParseLoader
    • LlamaParse
    • HWP (Hangeul) Loader
  • 07-TextSplitter
    • Character Text Splitter
    • 02. RecursiveCharacterTextSplitter
    • Text Splitting Methods in NLP
    • TokenTextSplitter
    • SemanticChunker
    • Split code with Langchain
    • MarkdownHeaderTextSplitter
    • HTMLHeaderTextSplitter
    • RecursiveJsonSplitter
  • 08-Embedding
    • OpenAI Embeddings
    • CacheBackedEmbeddings
    • HuggingFace Embeddings
    • Upstage
    • Ollama Embeddings With Langchain
    • LlamaCpp Embeddings With Langchain
    • GPT4ALL
    • Multimodal Embeddings With Langchain
  • 09-VectorStore
    • Vector Stores
    • Chroma
    • Faiss
    • Pinecone
    • Qdrant
    • Elasticsearch
    • MongoDB Atlas
    • PGVector
    • Neo4j
    • Weaviate
    • Faiss
    • {VectorStore Name}
  • 10-Retriever
    • VectorStore-backed Retriever
    • Contextual Compression Retriever
    • Ensemble Retriever
    • Long Context Reorder
    • Parent Document Retriever
    • MultiQueryRetriever
    • MultiVectorRetriever
    • Self-querying
    • TimeWeightedVectorStoreRetriever
    • TimeWeightedVectorStoreRetriever
    • Kiwi BM25 Retriever
    • Ensemble Retriever with Convex Combination (CC)
  • 11-Reranker
    • Cross Encoder Reranker
    • JinaReranker
    • FlashRank Reranker
  • 12-RAG
    • Understanding the basic structure of RAG
    • RAG Basic WebBaseLoader
    • Exploring RAG in LangChain
    • RAPTOR: Recursive Abstractive Processing for Tree-Organized Retrieval
    • Conversation-With-History
    • Translation
    • Multi Modal RAG
  • 13-LangChain-Expression-Language
    • RunnablePassthrough
    • Inspect Runnables
    • RunnableLambda
    • Routing
    • Runnable Parallel
    • Configure-Runtime-Chain-Components
    • Creating Runnable objects with chain decorator
    • RunnableWithMessageHistory
    • Generator
    • Binding
    • Fallbacks
    • RunnableRetry
    • WithListeners
    • How to stream runnables
  • 14-Chains
    • Summarization
    • SQL
    • Structured Output Chain
    • StructuredDataChat
  • 15-Agent
    • Tools
    • Bind Tools
    • Tool Calling Agent
    • Tool Calling Agent with More LLM Models
    • Iteration-human-in-the-loop
    • Agentic RAG
    • CSV/Excel Analysis Agent
    • Agent-with-Toolkits-File-Management
    • Make Report Using RAG, Web searching, Image generation Agent
    • TwoAgentDebateWithTools
    • React Agent
  • 16-Evaluations
    • Generate synthetic test dataset (with RAGAS)
    • Evaluation using RAGAS
    • HF-Upload
    • LangSmith-Dataset
    • LLM-as-Judge
    • Embedding-based Evaluator(embedding_distance)
    • LangSmith Custom LLM Evaluation
    • Heuristic Evaluation
    • Compare experiment evaluations
    • Summary Evaluators
    • Groundedness Evaluation
    • Pairwise Evaluation
    • LangSmith Repeat Evaluation
    • LangSmith Online Evaluation
    • LangFuse Online Evaluation
  • 17-LangGraph
    • 01-Core-Features
      • Understanding Common Python Syntax Used in LangGraph
      • Title
      • Building a Basic Chatbot with LangGraph
      • Building an Agent with LangGraph
      • Agent with Memory
      • LangGraph Streaming Outputs
      • Human-in-the-loop
      • LangGraph Manual State Update
      • Asking Humans for Help: Customizing State in LangGraph
      • DeleteMessages
      • DeleteMessages
      • LangGraph ToolNode
      • LangGraph ToolNode
      • Branch Creation for Parallel Node Execution
      • Conversation Summaries with LangGraph
      • Conversation Summaries with LangGraph
      • LangGrpah Subgraph
      • How to transform the input and output of a subgraph
      • LangGraph Streaming Mode
      • Errors
      • A Long-Term Memory Agent
    • 02-Structures
      • LangGraph-Building-Graphs
      • Naive RAG
      • Add Groundedness Check
      • Adding a Web Search Module
      • LangGraph-Add-Query-Rewrite
      • Agentic RAG
      • Adaptive RAG
      • Multi-Agent Structures (1)
      • Multi Agent Structures (2)
    • 03-Use-Cases
      • LangGraph Agent Simulation
      • Meta Prompt Generator based on User Requirements
      • CRAG: Corrective RAG
      • Plan-and-Execute
      • Multi Agent Collaboration Network
      • Multi Agent Collaboration Network
      • Multi-Agent Supervisor
      • 08-LangGraph-Hierarchical-Multi-Agent-Teams
      • 08-LangGraph-Hierarchical-Multi-Agent-Teams
      • SQL-Agent
      • 10-LangGraph-Research-Assistant
      • LangGraph Code Assistant
      • Deploy on LangGraph Cloud
      • Tree of Thoughts (ToT)
      • Ollama Deep Researcher (Deepseek-R1)
      • Functional API
      • Reflection in LangGraph
  • 19-Cookbook
    • 01-SQL
      • TextToSQL
      • SpeechToSQL
    • 02-RecommendationSystem
      • ResumeRecommendationReview
    • 03-GraphDB
      • Movie QA System with Graph Database
      • 05-TitanicQASystem
      • Real-Time GraphRAG QA
    • 04-GraphRAG
      • Academic Search System
      • Academic QA System with GraphRAG
    • 05-AIMemoryManagementSystem
      • ConversationMemoryManagementSystem
    • 06-Multimodal
      • Multimodal RAG
      • Shopping QnA
    • 07-Agent
      • 14-MoARAG
      • CoT Based Smart Web Search
      • 16-MultiAgentShoppingMallSystem
      • Agent-Based Dynamic Slot Filling
      • Code Debugging System
      • New Employee Onboarding Chatbot
      • 20-LangGraphStudio-MultiAgent
      • Multi-Agent Scheduler System
    • 08-Serving
      • FastAPI Serving
      • Sending Requests to Remote Graph Server
      • Building a Agent API with LangServe: Integrating Currency Exchange and Trip Planning
    • 08-SyntheticDataset
      • Synthetic Dataset Generation using RAG
    • 09-Monitoring
      • Langfuse Selfhosting
Powered by GitBook
On this page
  • Table of Contents
  • References
  • Environment Setup
  • Tools
  • Web Search
  • File Management
  • Retriever Tool
  • Create and Visualize the Agent
  • Execute Agent
  1. 15-Agent

React Agent

PreviousTwoAgentDebateWithToolsNext16-Evaluations

Last updated 28 days ago

  • Author:

  • Peer Review:

  • Proofread :

  • This is part of the

## Overview

In this tutorial, we explore the concept and implementation of a ReAct Agent.

ReAct Agent stands for Reasoning + Action, meaning that the LLM explicitly goes through a reasoning phase, utilizes tools (or actions), and then generates the final answer based on the obtained results.

Throughout this tutorial, we will implement a ReAct Agent by covering the following:

  • Tool Setup: Utilizing various tools such as web search, file management, document search based on VectorStore, etc.

  • Agent Creation: Practice how to use the ReAct Agent in LangChain.

  • Graph Execution: Execute the agent to observe the answers to queries.

Please follow the sections below to go through the entire process.

Table of Contents

References


Environment Setup

[Note]

  • langchain-opentutorial is a package that provides an easy-to-use environment setup, useful functions, and utilities for tutorials.

%%capture --no-stderr
%pip install langchain-opentutorial
from langchain_opentutorial import package

package.install(
    [
        "langsmith",
        "langchain",
        "langchain_core",
        "langchain_community",
        "langchain_openai",
        "langgraph",
        "faiss-cpu",
        "pymupdf",
    ],
    verbose=False,
    upgrade=False,
)
# Set environment variables
from langchain_opentutorial import set_env

set_env(
    {
        "OPENAI_API_KEY": "",
        "LANGCHAIN_API_KEY": "",
        "LANGCHAIN_TRACING_V2": "true",
        "LANGCHAIN_ENDPOINT": "https://api.smith.langchain.com",
        "LANGCHAIN_PROJECT": "11-React-Agent",
        "TAVILY_API_KEY": "",
    }
)

You can alternatively set OPENAI_API_KEY in a .env file and load it.

[Note] This is not necessary if you've already set OPENAI_API_KEY previously.

from dotenv import load_dotenv

load_dotenv(override=True)

How to Set Up Tavily Search

  • Get an API Key:

    To use Tavily Search, you need an API key.

Tools

A ReAct Agent uses various tools to solve problems. In this tutorial, we will set up and use the following tools.

Web Search

We use the TavilySearch tool for searching the latest information from the web. The code below creates a web search tool and tests it by retrieving some search results.

from langchain_community.tools.tavily_search import TavilySearchResults

# Create an instance of TavilySearchResults with k=5 for retrieving up to 5 search results
web_search = TavilySearchResults(k=5)
# Test the web search tool.
result = web_search.invoke(
    "Please find information related to the major AI-related from CES 2025"
)
result

File Management

Using FileManagementToolkit, you can create, delete, and modify files.

from langchain_community.agent_toolkits import FileManagementToolkit

# Set 'tmp' as the working directory.
working_directory = "tmp"
file_management_tools = FileManagementToolkit(
    root_dir=str(working_directory)
).get_tools()
file_management_tools

Retriever Tool

To search for information within documents such as PDFs, we create a Retriever. First, we load the PDF, split the text, then embed it into a VectorStore.

Tesla's Revenue Forecast Based on Business Model and Financial Statement Analysis

Please copy the downloaded file to the data folder for practice.

from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_community.vectorstores import FAISS
from langchain_openai import OpenAIEmbeddings
from langchain_community.document_loaders import PyMuPDFLoader

# Example PDF file path (adjust according to your environment)
pdf_file_path = "data/shsconf_icdeba2023_02022.pdf"

# Load the PDF using PyMuPDFLoader
loader = PyMuPDFLoader(pdf_file_path)

# Split text into smaller chunks
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
split_docs = loader.load_and_split(text_splitter)

# Create FAISS VectorStore
vector = FAISS.from_documents(split_docs, OpenAIEmbeddings())

# Create a retriever from the VectorStore
pdf_retriever = vector.as_retriever()
from langchain_core.tools.retriever import create_retriever_tool
from langchain_core.prompts import PromptTemplate

# Create a tool for PDF-based search
retriever_tool = create_retriever_tool(
    retriever=pdf_retriever,
    name="pdf_retriever",
    description="use this tool to search for information in Tesla PDF file",
    document_prompt=PromptTemplate.from_template(
        "<document><context>{page_content}</context><metadata><source>{source}</source><page>{page}</page></metadata></document>"
    ),
)

Combine these tools into a single list.

tools = [web_search, *file_management_tools, retriever_tool]
tools

Create and Visualize the Agent

We will now create a ReAct Agent and visualize the agent graph. In LangChain, a ReAct agent generates answers through step-by-step reasoning and tool usage.

The code below uses create_react_agent from langgraph to easily build a ReAct Agent and visualize its structure as a graph.

from langchain_openai import ChatOpenAI
from langgraph.checkpoint.memory import MemorySaver
from langgraph.prebuilt import create_react_agent

# Memory and model configuration
memory = MemorySaver()
model = ChatOpenAI(model_name="gpt-4o-mini")

# Create ReAct Agent
agent = create_react_agent(model, tools=tools, checkpointer=memory)

The code below visualizes the agent's graph structure.

from IPython.display import Image, display

display(
    Image(agent.get_graph().draw_mermaid_png(output_file_path="11-React-Agent.png"))
)
def print_stream(stream):
    for s in stream:
        message = s["messages"][-1]
        if isinstance(message, tuple):
            print(message)
        else:
            message.pretty_print()

Execute Agent

Let's execute the created ReAct Agent. We can track the step-by-step process of generating an answer to a query.

The example code below uses stream_graph to stream the agent's execution process. We place the user's query in the messages key, and the agent's reasoning will be displayed.

config = {"configurable": {"thread_id": "1"}}
inputs = {"messages": [("human", "Hello, my name is Teddy.")]}

print_stream(agent.stream(inputs, config=config, stream_mode="values"))
# Another example - maintaining chat flow
config = {"configurable": {"thread_id": "1"}}
inputs = {"messages": [("human", "What was my name again?")]}

print_stream(agent.stream(inputs, config=config, stream_mode="values"))
config = {"configurable": {"thread_id": "1"}}
inputs = {
    "messages": [("human", "Please summarize Tesla from shsconf_icdeba2023_02022.pdf.")]
}

print_stream(agent.stream(inputs, config=config, stream_mode="values"))
# Example of using web search + file management
config = {"configurable": {"thread_id": "1"}}
inputs = {
    "messages": [
        (
            "human",
            "Search for news about American writer John Smith's Pulitzer Prize and draft a brief report based on it.",
        )
    ]
}

print_stream(agent.stream(inputs, config=config, stream_mode="values"))
# A more concrete scenario example
instruction = """
Your task is to write a 'press release.'
----
Please process the following steps in order:
1. Search for news about American writer John Smith's Pulitzer Prize.
2. Based on the Pulitzer Prize news, write a press release/report.
3. Actively use markdown table format to summarize key points.
4. Save the output to a file named `agent_press_release.md`.
"""

config = {"configurable": {"thread_id": "1"}}
inputs = {"messages": [("human", instruction)]}
print_stream(agent.stream(inputs, config=config, stream_mode="values"))

Set up the environment. You may refer to for more details.

For more details, check out .

Author: Chenhao Fang Institution: Intelligent Accounting Management Institute, Guangdong University of Finance and Economics Link: File Name: shsconf_icdeba2023_02022.pdf

ReAct: Synergizing Reasoning and Acting in Language Models (Yao et al.)
LangChain Official Documentation
LangChain-OpenTutorial GitHub
Environment Setup
langchain-opentutorial
Generate your Tavily Search API key
Tesla's revenue forecast base on business model and financial statement analysis
ranian963
BokyungisaGod
LangChain Open Tutorial
Overview
Environment Setup
Tools
Web Search
File Management
Retriever Tool
Create and Visualize the Agent
Execute Agent