从零理解 ReAct:让 AI Agent 学会“思考 + 行动”

在构建 AI Agent 的过程中,一个核心问题始终存在:

如何让大模型不仅会回答,还会主动使用工具解决问题?

例如:

  • 查询天气
  • 调用数据库
  • 搜索互联网
  • 执行代码

如果只依赖传统 Prompt,大模型往往无法稳定完成复杂任务。为了解决这个问题,研究者提出了 ReAct 框架

本文将带你系统理解 ReAct Agent 的核心思想、工作流程以及实际应用。


一、什么是 ReAct?

ReAct = Reasoning + Acting

ReAct 是由 Google ResearchPrinceton University 研究者提出的一种 Agent 推理框架。

对应论文:

ReAct: Synergizing Reasoning and Acting in Language Models

核心思想非常简单:

让大模型在解决问题时 交替进行“思考(Reasoning)”和“行动(Action)”

传统 LLM:

Question -> Answer

ReAct Agent:

Question
   ↓
Thought(思考)
   ↓
Action(调用工具)
   ↓
Observation(获得结果)
   ↓
Thought
   ↓
Action
   ↓
Final Answer

也就是说:

LLM 负责思考,工具负责执行。


二、ReAct 的核心流程

ReAct Agent 的基本循环如下:

Thought -> Action -> Observation

具体流程:

1️⃣ Thought(思考)

模型分析当前问题,并决定下一步。

示例:

Thought: 我需要先查找当前城市天气

2️⃣ Action(行动)

调用外部工具。

例如:

Action: search_weather("Los Angeles")

3️⃣ Observation(观察)

系统返回工具结果。

Observation: 18°C, cloudy

4️⃣ 继续推理

模型根据新的信息继续思考。

Thought: 天气较冷,建议穿外套

最终输出:

Final Answer: 今天洛杉矶气温18°C,建议穿外套。

三、ReAct 的 Prompt 结构

ReAct 的核心其实是 Prompt Engineering

典型 Prompt 结构:

You can use the following tools:

search: search information
calculator: do math

Use the format:

Question: the input question
Thought: think about what to do
Action: the action to take
Observation: the result of the action
...
Final Answer: the answer

示例:

Question: Who is the biggest celebrity of the US and how old is he?

Thought: I should search the biggest celebrity
Action: search("US biggest celebrity")
Observation: TangSanZang

Thought: I should search his age
Action: search("TangSanZang age")
Observation: 81

Final Answer: The biggest celebrity is TangSanZang, he is 25 years old.

四、为什么 ReAct 很重要?

ReAct 是现代 AI Agent 系统的基础设计模式

很多 Agent 框架都采用类似思想,例如:

  • LangChain
  • AutoGPT
  • LangGraph
  • OpenAI Assistants API

原因是:

1 可解释性强

ReAct 会输出推理过程:

Thought -> Action -> Observation

因此 每一步决策都可追踪


2 可以调用任意工具

例如:

  • 搜索 API
  • Python 执行
  • 数据库
  • 内部系统

Agent 可以变成:

LLM + 工具生态


3 能解决复杂任务

例如:

  • 多步骤搜索
  • 数据分析
  • 自动化任务

五、ReAct 的一个简单代码示例

使用 LangChain 可以快速实现 ReAct Agent:

from langchain.agents import initialize_agent
from langchain.agents import AgentType
from langchain.tools import Tool
from langchain.llms import OpenAI

llm = OpenAI()

tools = [
    Tool(
        name="Search",
        func=search,
        description="Search information from internet"
    )
]

agent = initialize_agent(
    tools,
    llm,
    agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION
)

agent.run("Who is the CEO of Tesla?")

执行过程:

Thought
Action
Observation
Thought
Final Answer

六、ReAct 的局限性

虽然 ReAct 很强,但也存在一些问题:

1 Token 消耗高

因为需要输出完整推理过程。


2 推理可能错误

如果 Thought 逻辑错了,后续步骤都会错。


3 循环失控

有些 Agent 会陷入:

Thought -> Action -> Observation -> Thought -> ...

因此很多框架会设置:

max_iterations

七、ReAct 的演进

在 ReAct 之后,又出现了很多 Agent 设计方法,例如:

  • Plan-and-Execute
  • Reflection
  • Tree of Thoughts
  • Self-Consistency

ReAct 仍然是最经典的 Agent 架构之一

很多生产系统仍然在使用它。


总结

ReAct 的核心思想其实非常优雅:

让 LLM 像人类一样:先思考,再行动。

流程就是:

Thought -> Action -> Observation -> Final Answer

通过这种模式,大模型不再只是聊天工具,而是可以成为:

真正能够解决问题的 AI Agent。

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐