Chapter 12 · ReAct: Reasoning and Acting in Concert
Chapter goals: Understand the core idea of the ReAct framework — the alternating loop of reasoning and action — master the Thought-Action-Observation structure, and learn to implement a ReAct Agent using LangChain.
12.1 What Is ReAct
ReAct (Reasoning + Acting) is a framework proposed by Yao et al. (2022). Its core innovation is letting the language model simultaneously perform reasoning and action, iterating to solve complex problems through a "think-act-observe" loop.
Traditional methods either only reason (CoT) or only act (Act), while ReAct combines the advantages of both:
- Reasoning: the model can explain why it chose a certain action
- Acting: the model can obtain additional information through tools
ReAct loop:
Thought → Action → Observation → Thought → Action → ... → Final Answer12.2 ReAct's Structured Format
ReAct builds its reasoning trace using three key elements:
1. Thought (Reasoning)
The model's internal reasoning, explaining what information is needed now and why a certain action is chosen.
Thought: I need to find out the population of Paris to compare it with London.2. Action
The instruction to call a tool, formatted as Action: <tool_name>\nAction Input: <input>
Action: Search
Action Input: "population of Paris"3. Observation
The return result of the tool execution
Observation: 2,161,000 (as of 2019)Complete Example
Question: What is the elevation range for the area that the eastern sector of the Colorado orogeny extends into?
Thought 1: I need to search Colorado orogeny, find the area that the eastern sector extends into, then find the elevation range.
Action 1: Search[Colorado orogeny]
Observation 1: The Colorado orogeny was an episode of mountain building in Colorado and surrounding areas.
Thought 2: It doesn't mention the eastern sector. I need to look up eastern sector.
Action 2: Lookup[eastern sector]
Observation 2: The eastern sector extends into the High Plains and is called the Central Plains orogeny.
Thought 3: I need to search High Plains and find its elevation range.
Action 3: Search[High Plains elevation]
Observation 3: The High Plains rise from around 1,800 to 7,000 ft.
Thought 4: I now have the elevation range.
Action 4: Finish[1,800 to 7,000 ft]12.3 Implementing ReAct with LangChain
LangChain provides a built-in ReAct agent implementation:
from langchain.agents import load_tools, initialize_agent
from langchain.llms import OpenAI
import os
# Configure LLM and tools
llm = OpenAI(model_name="gpt-3.5-turbo-instruct", temperature=0)
tools = load_tools(["google-serper", "llm-math"], llm=llm)
# Initialize ReAct Agent
agent = initialize_agent(
tools,
llm,
agent="zero-shot-react-description",
verbose=True
)
# Execute a complex query
result = agent.run(
"Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?"
)Output example:
> Entering new AgentExecutor chain...
I need to find out who Olivia Wilde's boyfriend is and then calculate his age raised to the 0.23 power.
Action: Search
Action Input: "Olivia Wilde boyfriend"
Observation: Olivia Wilde started dating Harry Styles...
Thought: I need to find out Harry Styles' age.
Action: Search
Action Input: "Harry Styles age"
Observation: 29 years
Thought: I need to calculate 29 raised to the 0.23 power.
Action: Calculator
Action Input: 29^0.23
Observation: 2.169
Thought: I now know the final answer.
Final Answer: Harry Styles, Olivia Wilde's boyfriend, is 29 years old and his age raised to the 0.23 power is 2.169.
> Finished chain.12.4 ReAct vs Other Methods Comparison
| Feature | ReAct | CoT | Act Only |
|---|---|---|---|
| Reasoning ability | ✅ Built-in | ✅ Built-in | ❌ None |
| Tool usage | ✅ Dynamic selection | ❌ None | ✅ Fixed order |
| Interpretability | ✅ High (shows thinking) | ✅ High | ❌ Low |
| Flexibility | ✅ High | ⚠️ Medium | ⚠️ Low |
| Implementation complexity | ⚠️ Medium | ✅ Low | ✅ Low |
12.5 ReAct Best Practices
1. Tool Design Principles
- Clear description: each tool provides a detailed description
- Single responsibility: each tool does one thing
- Error handling: tools should return clear error messages
2. Prompt Template Optimization
react_prompt = """Answer the following questions as best you can.
You have access to the following tools:
{tools}
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 [{tool_names}]
Action Input: the input to the action
Observation: the result of the action
... (this Thought/Action/Action Input/Observation can repeat N times)
Thought: I now know the final answer
Final Answer: the final answer to the original input question
Begin!
Question: {input}
Thought: {agent_scratchpad}"""3. Performance Optimization
- Limit iterations: prevent infinite loops (max_iterations=10)
- Cache observations: reuse Observations for identical queries
- Parallel tool calls: support parallel search
Chapter Summary
- ReAct core: Thought-Action-Observation loop, reasoning and action in concert
- Implementation: LangChain provides the
zero-shot-react-descriptiontemplate - Advantage: more flexible and interpretable than pure CoT or pure Act
- Practice points: design good tool descriptions, limit iteration counts, consider caching
🧪 随堂测验
点击你认为正确的选项。答错时会展示正确答案与原因解析。
1. What are the two core components of the ReAct framework?
2. What is the correct order of the ReAct loop?
3. What is the key configuration parameter for a ReAct Agent in LangChain?
4. What is ReAct's main advantage over pure CoT?
🛠️ Hands-on Practice
- Use LangChain to implement a ReAct Agent that can search Wikipedia and answer questions involving multiple人物 relationships.
- Compare ReAct, CoT, and Act-only approaches on the same complex problem in terms of回答 quality and time.
- Add parallel tool call support to the ReAct Agent and compare serial vs. parallel efficiency.