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.
ReAct agent implementation in 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.
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)
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)
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.
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)
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.
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.