0
0
LangChainframework~5 mins

ReAct agent implementation in LangChain

Choose your learning style9 modes available
Introduction

The ReAct agent helps a program think and act step-by-step by combining reasoning and actions. It makes the program smarter by letting it decide what to do next based on what it learns.

When you want a chatbot to answer complex questions by searching and reasoning.
When building a helper that needs to gather information before giving a final answer.
When you want to combine multiple tools or APIs in a smart, stepwise way.
When you want your program to explain its thinking while solving a problem.
Syntax
LangChain
from langchain.agents import initialize_agent, AgentType
from langchain.llms import OpenAI
from langchain.tools import Tool

# Define your tools
tools = [Tool(name="Search", func=search_function, description="Useful for searching the web")]

# Initialize your language model
llm = OpenAI(temperature=0)

# Create the ReAct agent
agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)

The initialize_agent function sets up the agent with tools and a language model.

The AgentType.ZERO_SHOT_REACT_DESCRIPTION tells LangChain to use the ReAct pattern for reasoning and acting.

Examples
This example shows how to set up a ReAct agent with one simple search tool.
LangChain
from langchain.agents import initialize_agent, AgentType
from langchain.llms import OpenAI
from langchain.tools import Tool

# Simple tool example
def search_function(query: str) -> str:
    return f"Searching for {query}..."

tools = [Tool(name="Search", func=search_function, description="Search the web")]
llm = OpenAI(temperature=0)
agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
This example adds a calculator tool so the agent can do math and search.
LangChain
from langchain.agents import initialize_agent, AgentType
from langchain.llms import OpenAI
from langchain.tools import Tool

# Multiple tools example

def search_function(query: str) -> str:
    return f"Searching for {query}..."

def calculator(input: str) -> str:
    return str(eval(input))

tools = [
    Tool(name="Search", func=search_function, description="Search the web"),
    Tool(name="Calculator", func=calculator, description="Do math calculations")
]

llm = OpenAI(temperature=0)
agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
Sample Program

This program creates a ReAct agent with two tools: a search and a calculator. It asks the agent to do a math calculation and then search for a topic. The agent thinks step-by-step and uses the right tool for each part.

LangChain
from langchain.agents import initialize_agent, AgentType
from langchain.llms import OpenAI
from langchain.tools import Tool

# Define a simple search tool

def search_function(query: str) -> str:
    return f"Result for '{query}'"

# Define a calculator tool

def calculator(input: str) -> str:
    try:
        return str(eval(input))
    except Exception:
        return "Error in calculation"

# Create tools list
tools = [
    Tool(name="Search", func=search_function, description="Search the web"),
    Tool(name="Calculator", func=calculator, description="Perform math calculations")
]

# Initialize language model
llm = OpenAI(temperature=0)

# Initialize ReAct agent
agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=False)

# Run the agent with a question
question = "What is 12 * 12 and then search for 'Python programming'?"
response = agent.run(question)
print(response)
OutputSuccess
Important Notes

Make sure your tools have clear descriptions so the agent knows when to use them.

Set verbose=True during development to see the agent's thought process in the console.

Use a low temperature (like 0) in the language model for more predictable answers.

Summary

The ReAct agent combines thinking and acting to solve problems step-by-step.

You set it up by giving it tools and a language model in LangChain.

It is useful for tasks that need reasoning and multiple actions, like searching and calculating.