LangChain系列文章
ReAct (Reason Action):在语言模型中协同推理和行动
语言模型在推理(例如思维链提示)和行动(例如WebGPT,SayCan,ACT-1)方面变得更加出色,但这两个方向始终保持分离。
ReAct问,如果这两种基本能力结合在一起会怎样?
尽管大型语言模型(LLMs)在语言理解和交互式决策任务中展示了令人印象深刻的能力,但它们在推理(例如思维链提示)和行动(例如行动计划生成)方面的能力主要被研究为分开的主题。在本文中,我们探讨了LLMs的使用,以交错方式生成推理追踪和特定任务的行动,从而在两者之间实现更大的协同作用:推理追踪有助于模型诱导、跟踪和更新行动计划以及处理异常,而行动使其能够与外部来源(例如知识库或环境)进行交互,以收集额外信息。我们将我们的方法ReAct应用于各种语言和决策任务,并展示其相对于最先进的基线的有效性,以及相对于没有推理或行动组件的方法的改进的人类可解释性和可信度。具体而言,在问题回答(HotpotQA)和事实验证(Fever)方面,ReAct通过与简单的维基百科API交互,克服了思维链推理中流行的妄想和错误传播问题,并生成了更可解释的人类式任务解决轨迹,这些轨迹比没有推理追踪的基线更可解释。在两个交互式决策制定基准(ALFWorld和WebShop)上,ReAct分别以34%和10%的绝对成功率击败了模仿和强化学习方法,而只需一两个上下文示例即可进行提示。
ReAct提示由少量任务解决轨迹组成,其中包括人工撰写的文本推理追踪和行动,以及对行动的环境观察(请参阅论文附录中的示例!)
ReAct提示设计直观灵活,并在各种任务中实现了最先进的少样本性能,从问题回答到在线购物!
仅限原因的基线(即思维链)由于未与外部环境接轨以获取和更新知识,因此存在错误信息(红色),并且必须依赖有限的内部知识。
仅限行动的基线由于缺乏推理,无法综合最终答案,尽管在这种情况下具有与ReAct相同的行动和观察。
相比之下,ReAct通过可解释和事实的轨迹解决了任务。
对于决策任务,我们设计了具有稀疏推理追踪的人类轨迹,让LM决定何时思考与行动。
ReAct并不完美—以下是ALFWorld的一个失败示例。但是,ReAct格式可以通过改变几个模型想法轻松进行人工检查和行为校正,这是一种令人兴奋的新的人类对齐方法!
提示具有有限的上下文窗口和学习支持。使用ReAct提示轨迹对HotpotQA的初步微调结果表明:(1)ReAct是各种模型尺寸中最好的微调格式;(2)ReAct微调的较小模型优于提示的较大模型!
注册账号和获取SERPAPI_API_KEY, https://serpapi.com/dashboard
Agents/chat_agents_google_search.py这段代码使用了 Langchain 库来构建一个基于 OpenAI 语言模型的智能问答代理(Agent),该代理整合了多个工具(如 SERP API 和数学处理工具)来处理复杂的查询。以下是对代码的详细解释和注释(代码参考黄佳老师的课程Demo,如需要知道代码细节请读原文)::
# 导入与 OpenAI 语言模型交互的模块。
from langchain.llms import OpenAI
# 导入用于创建和管理提示模板的模块。
from langchain.prompts import PromptTemplate
# 导入用于加载和初始化代理的工具和函数。
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.agents import AgentType
# 导入从 .env 文件加载环境变量的库。
from dotenv import load_dotenv
# 导入创建和管理 OpenAI 聊天模型实例的类。
from langchain.chat_models import ChatOpenAI
# 加载 .env 文件中的环境变量。
load_dotenv()
# 设置 SERP API 的环境变量(API 密钥)。
import os
os.environ["SERPAPI_API_KEY"] = 'xxxxx'
# 初始化 OpenAI 语言模型实例。
llm = OpenAI(temperature=0)
# 加载工具,包括 SERP API 和数学处理工具。
tools = load_tools(['serpapi', 'llm-math'], llm=llm)
# 初始化智能问答代理,设置为 ZERO_SHOT_REACT_DESCRIPTION 类型。
agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
# 使用代理运行查询,并打印响应。
response = agent.run("目前市场上草莓的平均价格是多少?如果我在此基础上加价15%卖出,应该如何定价?")
print(response)
输出结果如下
(develop)⚡ [1] % python Agents/chat_agents_google_search.py ~/Workspace/LLM/langchain-llm-app
> Entering new AgentExecutor chain...
I need to find the current market price of strawberries and then calculate the new price with a 15% markup.
Action: Search
Action Input: "current market price of strawberries"
Observation: ['Wholesale prices. In 2023, the approximate wholesale price range for US strawberries is between US$ 2.1 and US$ 4.2 per kilogram or between US$ 0.95 and ...', 'CHICAGO : STRAWBERRIES Market : STEADY. Location : CHICAGO Commodity : STRAWBERRIES Package: flats 4 2-lb containers with lids. Date, Low-High Price, Mostly Low ...', 'US Strawberries Price Received is at a current level of 115.00, down from 128.00 one year ago. This is a change of -10.16% from one year ago.', 'Average prices for traditional strawberries in the U.S. market were 6.70 USD/kg, a 34% rise year-on-year. The strawberry business in the United States has shown ...', 'Average Price: Strawberries, Dry Pint (Cost per 12 Ounces/340.2 Grams) in U.S. City Average (APU0000711415)', 'Discover price data of wholesale and farmgate prices of Fresh Strawberry. See the price changes and trends of countries or varieties you are interested in.', 'Wholesale Price of Strawberries. Date, Location, Origin, Package, Variety ... These reports show price data on commodities traded at the local wholesale market ...', 'Farm Price represents the prices farmers are paid by retailers and grocery ... Strawberries, 2019-05-19, $1.16, $2.23, $1.70, $1.99, $2.54, 82.33%. Romaine ...', 'Strawberry Price in the United States (FOB) - 2023. The average strawberry export price stood at $3,051 per ton in June 2023, dropping by -15.8% against the ...', 'Today Buy Fresh Strawberries, 1 lb Package at Walmart.com.']
Thought: I need to find the average price of strawberries in the US market.
Action: Search
Action Input: "average price of strawberries in US market"
Observation: ['In 2023, the approximate wholesale price range for US strawberries is between US$ 2.1 and US$ 4.2 per kilogram or between US$ 0.95 and US$ 1.9 per pound(lb).', 'Average Price: Strawberries, Dry Pint (Cost per 12 Ounces/340.2 Grams) in U.S. City Average (APU0000711415) ; Sep 2023: 2.628 ; Aug 2023: 2.609 ; Jul 2023: 2.421.', 'US Strawberries Price Received is at a current level of 115.00, down from 128.00 one year ago. This is a change of -10.16% from one year ago.', 'Average prices for traditional strawberries in the U.S. market were 6.70 USD/kg, a 34% rise year-on-year. The strawberry business in the United States has ...', 'The average strawberry export price stood at $3,051 per ton in June 2023, dropping by -15.8% against the previous month. Over the period under review, the ...', 'ATLANTA : STRAWBERRIES Offerings : VERY LIGHT. Location : ATLANTA Commodity : STRAWBERRIES Package: flats 8 1-lb containers with lids. Date, Low-High Price ...', 'Strawberries. A, B, C, D, E, F, G. 1, Strawberries—Average retail price per pound and per cup equivalent, 2016. 2, Form, Average retail price, Preparation, Size ...', 'In 2021, the average grower price for fresh strawberries was $143/hundredweight. (NASS, 2022). The U.S. strawberry industry is primarily located ...', 'Although during week 48 there was a decrease in the average price for strawberries in the US market of approximately 5%, from $8,93 per kilo ...', 'Strawberries, 2019-05-19, $1.16 ; Romaine Lettuce, 2019-05-19, $0.35 ; Red Leaf Lettuce, 2019-05-19, $0.32 ; Potatoes, 2019-05-19, $1.50 ...']
Thought: I need to find the average price of strawberries in the US market for 2023.
Action: Search
Action Input: "average price of strawberries in US market 2023"
Observation: {'type': 'organic_result', 'title': 'Strawberry Price in the United States (FOB) - 2023', 'source': 'IndexBox'}
Thought:Retrying langchain.llms.openai.completion_with_retry.<locals>._completion_with_retry in 4.0 seconds as it raised RateLimitError: Rate limit reached for text-davinci-003 in organization org-jkd8QtrppR9UcAr9C841gy2b on requests per min (RPM): Limit 3, Used 3, Requested 1. Please try again in 20s. Visit https://platform.openai.com/account/rate-limits to learn more. You can increase your rate limit by adding a payment method to your account at https://platform.openai.com/account/billing..
I now have the average price of strawberries in the US market for 2023.
Action: Calculator
Action Input: 115 * 1.15
代码
https://github.com/zgpeace/pets-name-langchain/tree/develop
参考