Agent的ReAct交互模式
论文地址:https://arxiv.org/abs/2210.03629对应的 langchain prompt hub地址:https://smith.langchain.com/hub/langchain-ai/react-agent-template?organizationId=cb8a99d8-3b1e-433f-9b84-14c4ab9300cf
在 AI模型的应用开发中,Agent可以通过工具调用与外部进行交互。而通过 ReAct 的方式,可以 让Agent的 中枢——“AI大模型”发挥出更强的能力。在相关的论文中给出了具体的对比指标。
ReAct详解
ReAct 包含了 Reason 与 Act 两个部分,可以理解为就是思维链 + 外部工具调用。
提示词
# 系统提示词
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}
示例:
我们将以下变量注入提示词,为大模型提供一个获取股票价格的工具:
instructions = "你是一个股票助手,可以回答股票相关的问题"
query = "青岛啤酒和贵州茅台的收盘价哪个贵?"
prompt = REACT_PROMPT.format(instructions=instructions,tools=tools,tool_names="get_closing_price",input=query)
messages = [{"role": "user", "content": prompt}]
对话过程将如下:
大模型的回复:
```
Thought: Do I need to use a tool? Yes
Action: get_closing_price
Action Input: {"name": "青岛啤酒"}
```
人类的回复:Observation: 67.92
大模型的回复:
```
Thought: Do I need to use a tool? Yes
Action: get_closing_price
Action Input: {"name": "贵州茅台"}
```
人类的回复:Observation: 1488.21
大模型的回复:
```
Thought: Do I need to use a tool? No
Final Answer: 贵州茅台的收盘价为1488.21元,青岛啤酒的收盘价为67.92元,因此贵州茅台的收盘价比青岛啤酒
贵。
```
最终答案: 贵州茅台的收盘价为1488.21元,青岛啤酒的收盘价为67.92元,因此贵州茅台的收盘价比青岛啤酒贵。
重要字段详解
在大模型了解到问题后,未获取到最终答案时的回复字段如下:
Thought:对问题和需调用工具的思考
Action: 调用的工具名称
Action Input: 调用的工具参数
工具调用结果的字段
Observation: 工具的调用结果
大模型准备给出最终答案时的回复字段
Thought: 对问题和需调用工具的思考(这种情况下思考结果为不需要调用工具)
Final Answer: 最终的答案
ReAct与Function tool的思考
在 agent设计中,在使用工具的时候,可以通过Function tool 或者 ReAct来进行实现。通过Function tool 可以更充分的发挥大模型调用工具的能力。而目前因为大模型能力的提高,在通过提示词实现 ReAct 完成工具调用的时候,基本也不会出现问题。在实际使用中,也可以由Function tool 调用来完成ReAct 中的工具调用功能。
工具调用时参数的问题:在使用ReAct 时,可能会出现很长的思考过程,导致最终响应时间特别长,可以考虑优化或者结合大模型的思考深度去处理。
通过部分数据的微调可以提高大模型在ReAct 场景下的表现效果。
License:
CC BY 4.0