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
Start learning this pattern below
Jump into concepts and practice - no test required
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.
Practice
Solution
Step 1: Understand the ReAct agent concept
The ReAct agent is designed to think (reason) and act (perform tasks) in steps.Step 2: Identify its main use
It helps solve problems that need multiple steps, like searching and calculating.Final Answer:
To combine reasoning and actions step-by-step to solve complex tasks -> Option DQuick Check:
ReAct agent = reasoning + actions [OK]
- Thinking it only acts without reasoning
- Confusing it with simple action-only agents
- Assuming it replaces language models
Solution
Step 1: Recall ReAct agent constructor parameters
The ReAct agent requires a language model (llm) and a list of tools (tools) as named arguments.Step 2: Check each option for correct syntax
agent = ReActAgent(llm=llm, tools=tools) correctly passes llm and tools by name. agent = ReActAgent(tools=llm, llm=tools) swaps them incorrectly. agent = ReActAgent() misses required arguments. agent = ReActAgent(llm) passes only llm without tools.Final Answer:
agent = ReActAgent(llm=llm, tools=tools) -> Option AQuick Check:
Correct parameters = llm and tools [OK]
- Swapping llm and tools arguments
- Omitting required parameters
- Passing parameters positionally without names
from langchain.agents import ReActAgent
from langchain.llms import OpenAI
llm = OpenAI(temperature=0)
tools = [search_tool, calculator_tool]
agent = ReActAgent(llm=llm, tools=tools)
response = agent.run('What is the capital of France and what is 5 plus 3?')Solution
Step 1: Understand ReAct agent multi-tool usage
The ReAct agent can use multiple tools and decides which to use based on the question.Step 2: Analyze the question and agent behavior
The question asks two things: capital of France (search) and 5 plus 3 (calculator). The agent will perform both actions step-by-step.Final Answer:
The agent will first search for the capital of France, then calculate 5 plus 3, returning both answers. -> Option AQuick Check:
Multi-tool agent answers multi-part questions [OK]
- Thinking agent uses only one tool per run
- Assuming order is reversed without reason
- Believing multiple tools cause errors
TypeError: ReActAgent.__init__() missing 1 required positional argument: 'llm'
Solution
Step 1: Interpret the error message
The error says the __init__ method is missing the required 'llm' argument.Step 2: Identify correct constructor usage
ReActAgent requires an llm parameter when created. Missing it causes this TypeError.Final Answer:
The ReActAgent was created without passing the required language model (llm) argument. -> Option CQuick Check:
Missing llm argument = TypeError [OK]
- Forgetting to pass llm argument
- Confusing tools argument with llm
- Misreading error as related to run method
Solution
Step 1: Identify the need for multiple tools
The question requires web search, math calculation, and database lookup, so multiple tools are needed.Step 2: Choose the correct agent setup
ReActAgent supports multiple tools passed as a list along with the llm to reason and act step-by-step.Step 3: Evaluate other options
Separate agents won't combine reasoning easily; calculator_tool alone can't do all tasks; no tools means no external actions.Final Answer:
Create a ReActAgent with llm and a tools list including search_tool, calculator_tool, and db_tool -> Option BQuick Check:
Multi-tool ReActAgent = best for multi-step tasks [OK]
- Trying to run separate agents instead of one combined
- Assuming one tool can do all tasks
- Not passing any tools to the agent
