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。

相关推荐
Super Scraper4 天前
如何批量抓取 TikTok 数据而不被封锁?完整指南
爬虫·ai·自动化·抖音·tiktok·ai agent
DogDaoDao4 天前
【GitHub】CL4R1T4S:AI 系统提示词的透明革命
人工智能·python·ai·大模型·github·ai agent·cl4r1t4s
Mininglamp_27184 天前
Vibe Coding 之后是 Vibe Operating?
后端·开源·多智能体·ai agent·mano-p
带娃的IT创业者5 天前
GitHub 热门: coleam00/Archon —— 当 AI Agent 学会自我进化
人工智能·github·开源项目·ai agent·智能体·自我进化
DogDaoDao5 天前
【GitHub】 Headroom 深度解析:AI Agent 上下文压缩层的完整技术拆解
人工智能·深度学习·程序员·github·ai agent·智能体·agent skill
暗黑小白6 天前
第二篇:不碰模型,意图识别快 9 倍 —— P0→P1→P2 流水线设计
人工智能·架构·ai agent
跟风舞烟学编程6 天前
Hermes Agent 从入门到企业实战-01:Hermes-Agent核心架构
人工智能·ai agent·hermes agent·自进化 agent
暗黑小白6 天前
第六篇:本地模型选型 —— 4 个模型 × 2 种设备 × 2 项任务的全量对比
架构·ai agent
暗黑小白6 天前
第五篇:Reranker 与 BM25 —— 在精排提升与降级可靠性之间划一条线
架构·ai agent
暗黑小白6 天前
第十篇:纠纷协调与可观测性 —— 多Agent协作的全链路追踪
架构·ai agent