← 返回目录

ReAct (reason & act)

第 17 / 34 卷日期:

这种方法使得思维树( ToT)特别适合需要探索的复杂任务。其工作原理是维护一棵思维树,其中每个思维代表一个连贯的语言序列,作为解决问题的中间步骤。模型可以通过从树中不同节点分支来探索不同的推理路径。

有一份非常详细的笔记,进一步展示了基于论文《大型语言模型引导的思维树》的思维树( ToT)方法。9

ReAct(推理与行动)

13

推理与行动( ReAct) [10]提示是一种范式,使大语言模型能够通过自然语言推理结合外部工具(搜索、代码解释器等)来解决复杂任务,允许大语言模型执行某些操作,例如与外部 API 交互以检索信息,这是实现智能体建模的第一步。

ReAct 模仿了人类在现实世界中的操作方式,即通过语言推理并采取行动获取信息。

ReAct 在多个领域中的表现优于其他提示工程方法。

ReAct 提示的工作原理是将推理与行动结合成一个思考 ‑ 行动循环。大语言模型首先对问题进行推理并生成行动计划,然后执行计划中的行动并观察结果。接着,大语言模型利用观察结果更新推理并生成新的行动计划。这一过程持续进行,直到大语言模型找到问题的解决方案。

要实际体验这一过程,您需要编写一些代码。在代码片段 1 中,我使用了 Python 的 langchain 框架,结合 Ve exAI(google-cloud-aiplatform)和 google-search-results pip packages。

要运行此示例,您需在 h ps://serpapi.com/manage‑api‑key 创建一个(免费) SerpAPI 密钥,并设置环境变量 SERPAPI_API_KEY。

接下来让我们编写一些 Python 代码,任务是让 LLM 来理解:How many children à Ñ à õ ƒ0 Ñ ƒ0 € Ñ € ð ° ð ƒ0 ñ Ñ ñ ƒ0 ` à Ñ ` ƒ0 € õ ð ° € ð ƒ0 • ƒ0 ` à õ ƒ0 ê Ñ • ñ ƒ0 W õ ` Ñ 0 0 ë Ñ }

Python

from langchain.agents import load_tools from langchain.agents import initialize_agent from langchain.agents import AgentType from langchain.llms import VertexAI

prompt = "How many kids do the band members of Metallica have?"

llm = VertexAI(temperature=0.1) tools = load_tools(["serpapi"], llm=llm)

agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True) agent.run(prompt)

代码片段 1. 使用 LangChain 和 Ve exAI 创建 ReAct 智能体

代码片段 2 展示了运行结果。注意 ReAct 通过串联多次搜索实现:实际上 LLM 正在抓取谷歌搜索结果来识别乐队名称,随后将结果列为观察项并串联思考以进行下一次搜索。

> Entering new AgentExecutor chain...

Metallica has 4 members.

Action: Search Action Input: How many kids does James Hetfield have?

Observation: three children Thought: 1/4 Metallica band members have 3 children Action: Search Action Input: How many kids does Lars Ulrich have?

Observation: 3 Thought: 2/4 Metallica band members have 6 children Action: Search Action Input: How many kids does Kirk Hammett have?

Observation: Hammett has been married to his second wife Lani since 1998.

They have two sons, Angel (b. September 29, 2006) and Vincenzo (b. June 28, 2008).

Thought: 3/4 Metallica band members have 8 children Action: Search Action Input: How many kids does Robert Trujillo have?

Observation: 2 Thought: 4/4 Metallica band members have 10 children Final Answer: 10

代码片段 2. 在终端执行 ReAct 智能体

代码片段 2 首先确定金属乐队 Metallica 有四名成员,随后逐一搜索每位成员以获取其子女数量并累加求和,最终返回子女总数作为最终答案。

在实践中应用 ReAct 提示法需要理解,你必须不断重新发送之前的提示 / 响应(并对额外生成的内容进行修剪),同时为模型配置适当的示例 / 指令。请参考托管在 GoogleCloudPlatform Github 仓库中的 notebook14,其中通过一个更详尽的示例更详细地展示了实际 LLM 的输入和输出。

全站分卷索引 · 34 卷
  1. 1Introduction
  2. 2Prompt engineering
  3. 3LLM output configuration
  4. 4Sampling controls
  5. 5Top-K and top-P
  6. 6Putting it all together
  7. 7Prompting techniques
  8. 8One-shot & few-shot
  9. 9System, contextual and role prompting
  10. 10System prompting
  11. 11Role prompting
  12. 12Contextual prompting
  13. 13Step-back prompting
  14. 14Chain of Thought (CoT)
  15. 15Self-consistency
  16. 16Tree of Thoughts (ToT)
  17. 17ReAct (reason & act)
  18. 18Automatic Prompt Engineering
  19. 19Code prompting
  20. 20Prompts for explaining code
  21. 21Prompts for translating code
  22. 22Prompts for debugging and reviewing code
  23. 23What about multimodal prompting?
  24. 24Design with simplicity
  25. 25Be specific about the output
  26. 26Control the max token length
  27. 27Experiment with input formats and writing styles
  28. 28Adapt to model updates
  29. 29JSON Repair
  30. 30Working with Schemas
  31. 31Experiment together with other prompt engineers
  32. 32CoT Best practices
  33. 33Summary
  34. 34Endnotes