本文介绍: 尽管大型语言模型(LLMs)在语言理解交互式决策任务展示了令人印象深刻的能力,但它们在推理例如思维提示)和行动(例如行动计划生成)方面的能力主要被研究为分开的主题。在本文中,我们探讨了LLMs使用,以交错方式生成推理追踪和特定任务的行动,从而在两者之间实现更大的协同作用推理追踪有助于模型诱导、跟踪更新行动计划以及处理异常,而行动使其能够外部来源(例如知识库环境)进行交互,以收集额外信息

LangChain系列文章

  1. LangChain 实现给动物取名字
  2. LangChain 2模块化prompt template并用streamlit生成网站 实现给动物取名字
  3. LangChain 3使用Agent访问Wikipedia和llm-math计算狗的平均年龄
  4. LangChain 4用向量数据库Faiss存储,读取YouTube的视频文本搜索Indexes for information retrieve
  5. LangChain 5易速鲜花内部问答系统
  6. LangChain 6根据图片生成推广文案HuggingFace中的image-caption模型
  7. LangChain 7 文本模型TextLangChain和聊天模型ChatLangChain
  8. LangChain 8 模型Model I/O:输入提示、调用模型、解析输出
  9. LangChain 9 模型Model I/O 聊天提示词ChatPromptTemplate, 少量样本提示词FewShotPrompt
  10. LangChain 10思维链Chain of Thought一步一步的思考 think step by step
  11. LangChain 11实现思维树Implementing the Tree of Thoughts in LangChain’s Chain
  12. LangChain 12调用模型HuggingFace中的Llama2和Google Flan t5
  13. LangChain 13输出解析Output Parsers 自动修复解析器
  14. LangChain 14 SequencialChain链接不同的组件
  15. LangChain 15根据问题自动路由Router Chain确定用户的意图
  16. LangChain 16 通过Memory记住历史对话的内容
  17. LangChain 17 LangSmith调试、测试、评估和监视基于任何LLM框架构建的链和智能代理
  18. LangChain 18 LangSmith监控评估Agent并创建对应的数据库
  19. LangChain 19 Agents Reason+Action自定义agent处理OpenAI的计算缺陷
    在这里插入图片描述

ReAct (Reason Action):在语言模型协同推理和行动
语言模型推理(例如思维提示)和行动(例如WebGPT,SayCan,ACT-1)方面变得更加出色,但这两个方向始终保持分离
ReAct问,如果这两种基本能力结合在一起会怎样
在这里插入图片描述

1. 摘要

尽管大型语言模型(LLMs)在语言理解交互式决策任务展示了令人印象深刻的能力,但它们在推理(例如思维提示)和行动(例如行动计划生成)方面的能力主要被研究为分开的主题。在本文中,我们探讨了LLMs使用,以交错方式生成推理追踪和特定任务的行动,从而在两者之间实现更大的协同作用推理追踪有助于模型诱导、跟踪更新行动计划以及处理异常,而行动使其能够与外部来源(例如知识库环境)进行交互,以收集额外信息我们我们方法ReAct用于各种语言和决策任务,并展示相对于最先进的基线的有效性,以及相对没有推理或行动组件方法改进人类解释性可信度。具体而言,在问题回答(HotpotQA)和事实验证(Fever)方面,ReAct通过简单的维基百科API交互,克服了思维链推理中流行的妄想和错误传播问题,并生成了更可解释人类任务解决轨迹,这些轨迹比没有推理追踪的基线更可解释。在两个交互式决策制定基准(ALFWorld和WebShop)上,ReAct分别以34%和10%的绝对成功率击败了模仿和强化学习方法,而只需一两个上下文示例即可进行提示

2. ReAct提示

ReAct提示由少量任务解决轨迹组成,其中包括人工撰写的文本推理追踪和行动,以及对行动的环境观察(请参阅论文附录中的示例!)
ReAct提示设计直观灵活,并在各种任务实现了最先进的少样本性能,从问题回答在线购物
在这里插入图片描述

3. HotpotQA示例

仅限原因的基线(即思维链)由于未与外部环境接轨以获取更新知识,因此存在错误信息(红色),并且必须依赖有限内部知识
仅限行动的基线由于缺乏推理,无法综合最终答案,尽管在这种情况下具有与ReAct相同的行动和观察。
相比之下,ReAct通过解释和事实的轨迹解决了任务
在这里插入图片描述

4. ALFWorld示例

对于决策任务,我们设计具有稀疏推理追踪人类轨迹,让LM决定何时思考与行动。
ReAct并不完美—以下是ALFWorld一个失败示例。但是,ReAct格式可以通过改变几个模型想法轻松进行人工检查行为校正,这是一种令人兴奋的新的人类对齐方法
在这里插入图片描述

4. ReAct微调初步结果

提示具有有限上下文窗口学习支持使用ReAct提示轨迹对HotpotQA的初步微调结果表明:(1)ReAct是各种模型尺寸最好的微调格式;(2)ReAct微调的较小模型优于提示的较大模型!
在这里插入图片描述

5. 代码实习

注册账号获取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/petsnamelangchain/tree/develop

参考

原文地址:https://blog.csdn.net/zgpeace/article/details/134796970

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_48634.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注