Agent ReAct框架介绍(ReAct Agent、ReAct = Reasoning + Acting、ReAct行动框架)问题——思考——工具调用——获得结果——思考——行动——最终结果

文章目录

  • [从零理解 ReAct:让 AI Agent 学会"思考 + 行动"](#从零理解 ReAct:让 AI Agent 学会“思考 + 行动”)
  • [一、什么是 ReAct?](#一、什么是 ReAct?)
  • [二、ReAct 的核心流程](#二、ReAct 的核心流程)
  • [三、ReAct 的 Prompt 结构](#三、ReAct 的 Prompt 结构)
  • [四、为什么 ReAct 很重要?](#四、为什么 ReAct 很重要?)
      • [1 可解释性强](#1 可解释性强)
      • [2 可以调用任意工具](#2 可以调用任意工具)
      • [3 能解决复杂任务](#3 能解决复杂任务)
  • [五、ReAct 的一个简单代码示例](#五、ReAct 的一个简单代码示例)
  • [六、ReAct 的局限性](#六、ReAct 的局限性)
      • [1 Token 消耗高](#1 Token 消耗高)
      • [2 推理可能错误](#2 推理可能错误)
      • [3 循环失控](#3 循环失控)
  • [七、ReAct 的演进](#七、ReAct 的演进)
  • 总结

从零理解 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:

python 复制代码
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。

相关推荐
rannn_1112 天前
OpenAI Function Calling 全解析:从函数定义到流式调用
人工智能·chatgpt·openai·ai agent
有一个好名字2 天前
CrewAI 高级04:输出格式、缓存与工作流编排
人工智能·ai agent
Mininglamp_27183 天前
会中 AI Skill 架构设计解析:3 种人设 × 7 种能力的技术实现
人工智能·语音识别·硬件·ai agent·skill
中间件XL3 天前
ai-agent框架spring ai/alibaba(四) RAG
rag·ai agent·智能体·spring ai
艺杯羹3 天前
Vibe Coding实战:从零构建网页3D交互角色
3d·ai·交互·ai编程·ai agent·vibe coding
有一个好名字4 天前
CrewAI 实战03:多 Agent 协作与层级管理
人工智能·ai agent
rannn_1114 天前
LLM Agent 最小实现:Tool Calling + Runtime Loop 的底层原理
python·ai·ai agent·大模型应用开发
Mininglamp_27184 天前
AI Agent 视觉驱动 vs RPA 规则驱动:两种自动化范式的技术差异
人工智能·自动化·rpa·ai agent·gui agent
不懒不懒5 天前
【基于 ReAct 框架的电商智能客服 AI Agent 设计与实现】
人工智能·大语言模型·通义千问·ai agent·ollama·react 框架·电商智能客服