2.ReAct智能体
# 2.1 ReAct 智能体
ReAct 智能体的核心在于它能够通过推理和行动的循环来解决问题。其流程通常包括:
- 推理:根据当前的情境和信息进行逻辑推理,生成对未来行动的预测或计划
- 行动:根据推理结果采取实际行动,通常这些行动直接与环境或任务相关
- 反馈与调整:执行行动后,ReAct 智能体会接收到新的反馈,进一步进行推理并调整其行动策略
ReAct 智能体的特点是,它在推理和行动之间有较强的闭环反馈机制,即它会根据行动的结果来调整后续的推理和行动策略。这使得它能够在复杂、动态的环境中适应和学习。

# 2.2 代码示例
# 2.2.1 安装依赖
pip install google-search-results
pip install --upgrade langchainhub
pip install python-dateutil
1
2
3
2
3
# 2.2.2 代码实现
import dotenv
import os
import calendar
from langchain.agents import AgentExecutor, create_react_agent
from langchain_community.utilities import SerpAPIWrapper
from langchain.tools import Tool, tool
from langchain_openai import ChatOpenAI
from langchain import hub
import dateutil.parser as parser
dotenv.load_dotenv()
serpapi_api_key = os.getenv('SERPAPI_API_KEY')
# 定义搜索工具
search = SerpAPIWrapper(serpapi_api_key=serpapi_api_key)
tools = [
Tool.from_function(
func=search.run,
name="Search",
description="用于搜索一些你不知道的网络信息"
),
]
# 自定义工具:日期转星期
@tool("weekday")
def weekday(date_str: str) -> str:
"""日期转换为星期名,格式必须是date/time"""
d = parser.parse(date_str)
return calendar.day_name[d.weekday()]
tools += [weekday]
# 下载一个现有的 Prompt 模板
react_prompt = hub.pull("hwchase17/react")
print(react_prompt.template)
llm = ChatOpenAI(model='gpt-4o', temperature=0, seed=23)
# 定义一个 agent: 需要大模型、工具集、和 Prompt 模板
agent = create_react_agent(llm, tools, react_prompt)
# 定义一个执行器:需要 agent 对象 和 工具集
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
# 执行
print(agent_executor.invoke({"input": "张杰的老婆的父亲生日是星期几"}))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 2.2.3 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}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 2.2.4 执行过程
> Entering new AgentExecutor chain...
要回答这个问题,我需要知道张杰的老婆是谁,然后找到她父亲的生日,并将其转换为星期几。
Action: Search
Action Input: "张杰的老婆"
['Xie Na (谢娜) type: Chinese host and singer.', ...]
张杰的老婆是谢娜。接下来,我需要找到谢娜父亲的生日。
Action: Search
Action Input: "谢娜父亲生日"
['谢娜(1981年5月6日—),生于四川省德阳市中江县...', ...]
Thought: 我没有找到谢娜父亲的具体生日日期,因此无法将其转换为星期几。
Final Answer: 我无法找到谢娜父亲的具体生日日期,因此无法确定他生日是星期几。
> Finished chain.
{'input': '张杰的老婆的父亲生日是星期几', 'output': '我无法找到谢娜父亲的具体生日日期,因此无法确定他生日是星期几。'}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
可以看到 ReAct 智能体的推理过程:先搜索张杰的老婆 → 得知是谢娜 → 再搜索谢娜父亲的生日 → 由于未找到具体日期,返回无法确定的结果。
编辑 (opens new window)
上次更新: 2025/12/19, 15:17:48