Skip to content

第 12 章 · ReAct:推理与行动协同

本章目标:理解 ReAct 框架的核心思想——推理与行动的交替循环,掌握 Thought-Action-Observation 结构,并学会使用 LangChain 实现 ReAct Agent。

12.1 什么是 ReAct

ReAct(Reasoning + Acting)是由 Yao et al. (2022) 提出的框架,核心创新是让语言模型同时完成推理和行动两个任务,并通过"思考-行动-观察"循环迭代求解复杂问题。

传统方法要么只推理(CoT),要么只行动(Act),而 ReAct 结合了两者的优势:

  • 推理:模型可以解释为什么选择某个动作
  • 行动:模型可以通过工具获取额外信息
ReAct 循环:
Thought → Action → Observation → Thought → Action → ... → Final Answer

12.2 ReAct 的结构化格式

ReAct 使用三个关键元素构建推理轨迹:

1. Thought(思考)

模型的内部推理,解释当前需要什么信息、为什么选择某个动作。

Thought: I need to find out the population of Paris to compare it with London.

2. Action(行动)

调用工具的指令,格式为 Action: <工具名>\nAction Input: <输入>

Action: Search
Action Input: "population of Paris"

3. Observation(观察)

工具执行的返回结果

Observation: 2,161,000 (as of 2019)

完整示例

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 使用 LangChain 实现 ReAct

LangChain 提供了内置的 ReAct agent 实现:

python
from langchain.agents import load_tools, initialize_agent
from langchain.llms import OpenAI
import os

# 配置 LLM 和工具
llm = OpenAI(model_name="gpt-3.5-turbo-instruct", temperature=0)
tools = load_tools(["google-serper", "llm-math"], llm=llm)

# 初始化 ReAct Agent
agent = initialize_agent(
    tools,
    llm,
    agent="zero-shot-react-description",
    verbose=True
)

# 执行复杂查询
result = agent.run(
    "Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?"
)

输出示例:

> 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 其他方法对比

特性ReActCoTAct Only
推理能力✅ 内置✅ 内置❌ 无
工具使用✅ 动态选择❌ 无✅ 固定顺序
可解释性✅ 高(展示思考)✅ 高❌ 低
灵活性✅ 高⚠️ 中⚠️ 低
实现复杂度⚠️ 中✅ 低✅ 低

12.5 ReAct 的最佳实践

1. 工具设计原则

  • 清晰描述:每个工具提供详细的 description
  • 单一职责:每个工具只做一件事
  • 错误处理:工具应返回明确的错误信息

2. Prompt 模板优化

python
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. 性能优化

  • 限制循环次数:防止无限循环(max_iterations=10)
  • 缓存观察结果:相同查询复用 Observation
  • 并行工具调用:支持并行 search

本章小结

  • ReAct 核心:Thought-Action-Observation 循环,推理与行动协同
  • 实现方式:LangChain 提供 zero-shot-react-description 模板
  • 优势:比纯 CoT 或纯 Act 更灵活、可解释
  • 实践要点:设计好的工具描述、限制迭代次数、考虑缓存

🧪 随堂测验

点击你认为正确的选项。答错时会展示正确答案与原因解析。

1. ReAct 框架的两个核心组成部分是什么?

2. ReAct 循环的正确顺序是?

3. LangChain 中 ReAct Agent 的关键配置参数是?

4. ReAct 相比纯 CoT 的主要优势是?

🛠️ 动手实践

  1. 使用 LangChain 实现一个 ReAct Agent,让它能够搜索维基百科并回答涉及多人物关系的问题。
  2. 对比 ReAct、CoT、Act-only 三种方法在同一复杂问题上的回答质量和耗时。
  3. 为 ReAct Agent 添加并行工具调用支持,比较串行与并行的效率差异。

下一章:大模型与提示工程概述