Skip to main content
Paper·arxiv.org
agentsreasoningtool-usellmai

ReAct: Reasoning and Acting in LLMs

Implement the ReAct framework to empower LLMs with reasoning and acting capabilities. This method combines internal thoughts with external actions and observations, creating more robust and goal-oriented AI agents.

beginner30 min5 steps
The play
  1. Grasp ReAct's Core Concept
    Understand ReAct as an iterative process where a Language Model (LLM) alternates between generating 'Thoughts' (reasoning steps) and 'Actions' (tool calls or external interactions), then processing 'Observations' from those actions. This loop allows for dynamic problem-solving.
  2. Define Tools for Your Agent
    Identify external functions or APIs your agent needs to interact with. For example, a search engine, a calculator, or a database query tool. Each tool should have a clear name and description of its input and output.
  3. Structure the ReAct Prompt
    Design a prompt that guides the LLM through the Thought-Action-Observation cycle. Clearly define the user's goal, available tools, and the expected format for the LLM's output (Thought, Action, Observation).
  4. Execute the ReAct Loop
    Send your structured prompt to an LLM. Parse the LLM's output to identify 'Thought', 'Action', and 'Action Input'. Execute the specified action using your defined tools, then feed the 'Observation' (tool's output) back into the LLM as part of the ongoing prompt context.
  5. Iterate and Refine
    Continue the Thought-Action-Observation cycle until the LLM generates a 'Final Answer'. Analyze the agent's performance and refine your prompt, tool descriptions, or available tools to improve accuracy and efficiency for future tasks.
Starter code
You are an AI assistant that can answer questions using tools.

Tools:
- search(query: str): Searches the web for information.

Use the following format:
Question: the input question you must answer
Thought: you should always think about what to do
Action: the action to take, should be one of [search]
Action Input: the input to the action
Observation: the result of the action
Thought: I now know the final answer
Final Answer: the final answer to the original input question

Begin!

Question: What is the current population of Tokyo?
Source
ReAct: Reasoning and Acting in LLMs — Action Pack