ReAct 是一种结合推理(Reasoning)和行动(Acting)的框架,用于指导大语言模型通过思考、行动和观察来完成复杂任务。
ReAct 运行流程如下图所示,收到问题后,首先进入 THOUGHT 阶段,LLM 思考如何解决,若 LLM 知道问题答案,会立即返回答案;若不知道,LLM 查看自己有哪些可以使用的工具,调用工具获取相关信息,若无合适工具使用,LLM 会立即返回。调用工具时进入 ACTION 阶段,获取与此问题相关信息。调用工具获取到信息,连同问题和历史信息(ReAct 进行了多轮时会有历史信息)一起发送给 LLM 即 OBSERVATION,LLM 会综合这些信息思考如何解决问题即再次进入 THOUGHT 阶段。
ReAct 流程
体验ReAct
我们使用 langgraph 体验下 ReAct。langgraph 已经预制 ReAct agent,我们拿来用就行。
from IPython.display import Image, display
from langchain_core.tools import tool
from langchain_core.prompts import ChatPromptTemplate
from langgraph.prebuilt import create_react_agent
# 这是自己封装的获取大模型的接口文件
import llm
# search 工具,可以从 internet 搜索相关信息
# 这里为了模拟测试,手动模拟搜索内容
@tool
def search(question: str) -> str:
"""
search the internet for information
Args:
question (str): the question to search
Returns:
the answer of the question
"""
user_input = input(f"the question is {question}\n the answer is: ")
return user_input
# LLM 提示词
prompt = ChatPromptTemplate.from_messages(
[
(
"system",
"You are an expert that can help me to solve the problem."
"You can split the problem into smaller problems and solve them step by step."
),
("placeholder", "{messages}"),
]
)
tools = [search]
# 使用 azure 大模型
my_llm = llm.get_llm_azure()
# 初始化 langgraph 自带的 ReAct agent
agent_executor = create_react_agent(my_llm, tools, state_modifier=prompt)
# 打印 langchain 流程图
display(Image(agent_executor.get_graph(xray=True).draw_mermaid_png()))
# 调用 ReAct agent 回答问题
events = agent_executor.stream({"messages": [("user", "成龙和李连杰谁的年龄大?")]}, stream_mode="values")
for event in events:
if "messages" in event:
print(event.get("messages")[-1].pretty_repr(html=True))
langchain ReAct 如何实现的?调用 display 函数打印它的 workflow 如下图所示。agent 实现了 THOUGHT 和 OBSERVATION阶段,tools 实现了 Action 阶段。
langchain graph
执行上面的程序,看下 ReAct 如何解决问题“成龙和李连杰谁的年龄大?”
================================ Human Message =================================
成龙和李连杰谁的年龄大?
================================== Ai Message ==================================
Tool Calls:
search (call_IZvY7hq8sDC3Enf88ILNGtYV)
Call ID: call_IZvY7hq8sDC3Enf88ILNGtYV
Args:
question: 成龙出生日期
the question is 成龙出生日期
the answer is: 1954.4.7
================================= Tool Message =================================
Name: search
1954.4.7
================================== Ai Message ==================================
Tool Calls:
search (call_1MEm2d1K9kjkLV6WV7267lQz)
Call ID: call_1MEm2d1K9kjkLV6WV7267lQz
Args:
question: 李连杰出生日期
the question is 李连杰出生日期
the answer is: 1963.4.26
================================= Tool Message =================================
Name: search
1963.4.26
================================== Ai Message ==================================
成龙出生于1954年4月7日,而李连杰出生于1963年4月26日。因此,成龙的年龄比李连杰大
具体来说,收到问题后,首先进入 THOUGHT阶段,LLM 思考如何解决,LLM 认为比较年龄大小,首先要知道成龙出生日期,LLM 知道自己可以使用工具 search 从互联网搜索信息,于是决定使用工具 search。调用工具 search 进入ACTION 阶段,获知成龙出生日期 1954.4.7。把问题、search工具调用、成龙出生日期一起发送给 LLM 即 OBSERVATION。LLM 综合这些信息思考还需要知道李连杰出生日期,于是通过相同的流程获取到李连杰出生日期1963.4.26,最终 LLM 认为已经获取了足够信息,直接返回了答案。
本文通过 langgraph 体验了下大模型 ReAct,可知其实现并不复杂,关键是理解其解决问题思路。