ReAct 模式:让大模型先思考再行动的 Agent Prompt 设计
如果你要让大模型按照 ReAct 模式 工作,Prompt 的核心目标是:
告诉模型:先思考,再决定调用什么工具,拿到结果后继续思考,直到得到答案。
最基础的 ReAct Prompt
你是一个智能助手。
回答问题时,请遵循以下格式:
Thought: 分析当前问题,思考下一步应该做什么
Action: 需要调用的工具名称
Action Input: 工具参数
Observation: 工具返回结果
...
当获得足够信息后:
Final Answer: 给出最终答案
例如:
Question:
北京今天天气怎么样?
模型输出:
Thought:
我需要查询实时天气。
Action:
WeatherTool
Action Input:
北京
Observation:
晴天,28℃
Final Answer:
北京今天晴天,气温28℃。
生产环境常用 Prompt
实际开发不会只写这么简单。
通常会增加规则:
你是一个能够使用工具的 AI Agent。
你拥有以下工具:
1. Search
用于搜索互联网信息
2. Calculator
用于数学计算
请遵循以下规则:
1. 优先通过推理解决问题
2. 当信息不足时调用工具
3. 不要编造 Observation
4. Observation 必须来自工具返回
5. 每次只能调用一个工具
6. 获得足够信息后立即输出 Final Answer
格式:
Thought:
...
Action:
...
Action Input:
...
Observation:
...
Final Answer:
...
一个完整示例
假设有两个工具:
Search
Calculator
用户:
苹果公司市值是多少人民币?
模型:
Thought:
我需要先获取苹果公司的最新市值。
Action:
Search
Action Input:
Apple market cap
系统执行工具:
Observation:
Apple market cap = 3.2 trillion USD
模型继续:
Thought:
需要把美元转换成人民币。
Action:
Search
Action Input:
USD CNY exchange rate
返回:
Observation:
1 USD = 7.2 CNY
继续:
Thought:
现在可以计算。
Action:
Calculator
Action Input:
3.2 * 7.2
返回:
Observation:
23.04
最终:
Final Answer:
苹果公司市值约为23.04万亿元人民币。
LangChain 官方 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
Thought: you should always think about what to do
Action: the action to take
Action Input: the input to the action
Observation: the result of the action
... (this Thought/Action/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:
这几乎是所有 ReAct Agent 的祖先模板。
中更推荐的 Prompt
如果你是做 AI Agent 开发,推荐使用这种结构:
# Role
你是一个智能 Agent。
# Goal
通过推理和工具调用解决用户问题。
# Tools
{tools}
# Constraints
- 不允许编造工具结果
- Observation 必须来自工具返回
- 每轮只能调用一个工具
- 信息足够时立即结束
# Workflow
Thought → Action → Observation
重复执行直到问题解决
# Output Format
Thought:
...
Action:
...
Action Input:
...
Observation:
...
Final Answer:
...
这种写法更符合现在 Agent 框架(如 LangChain、LangGraph、Spring AI、AutoGen)的设计习惯。
现代 Agent 中的改进
现在很多系统已经不直接让模型输出:
Action:
Search
Action Input:
...
而是让模型输出结构化 JSON(Function Calling)。
例如:
{
"tool": "search",
"arguments": {
"query": "Apple market cap"
}
}
本质上仍然是 ReAct:
Thought
↓
Tool Call(JSON)
↓
Observation
↓
Thought
↓
Tool Call(JSON)
↓
Final Answer
只是把文本格式的 Action 换成了更稳定的结构化调用。
可以把 ReAct Prompt 记成一个万能公式:
Role
+
Tools
+
Rules
+
Thought → Action → Observation Loop
+
Final Answer
时如果被问到"如何设计一个 ReAct Prompt",按照这个结构回答,基本就覆盖了核心设计思想。