位置: IT常识 - 正文

LangChain Agent 执行过程解析 OpenAI

编辑:rootadmin
LangChain Agent 执行过程解析 OpenAI LangChain Agent 执行过程解析什么是LangChain Agent例子工作原理什么是LangChain Agent

推荐整理分享LangChain Agent 执行过程解析 OpenAI,希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:,内容如对您有帮助,希望把文章链接给更多的朋友!

简单来说,用户像LangChain输入的内容未知。此时可以有一套工具集合(也可以自定义工具),将这套自定义工具托管给LLM,让其自己决定使用工具中的某一个(如果存在的话)

例子

首先,这里自定义了两个简单的工具

from langchain.tools import BaseTool# 天气查询工具 ,无论查询什么都返回Sunnyclass WeatherTool(BaseTool): name = "Weather" description = "useful for When you want to know about the weather" def _run(self, query: str) -> str: return "Sunny^_^" async def _arun(self, query: str) -> str: """Use the tool asynchronously.""" raise NotImplementedError("BingSearchRun does not support async")# 计算工具,暂且写死返回3class CustomCalculatorTool(BaseTool): name = "Calculator" description = "useful for when you need to answer questions about math." def _run(self, query: str) -> str: return "3" async def _arun(self, query: str) -> str: raise NotImplementedError("BingSearchRun does not support async")

接下来是针对于工具的简单调用:注意,这里使用OpenAI temperature=0需要限定为0

from langchain.agents import initialize_agentfrom langchain.llms import OpenAIfrom CustomTools import WeatherToolfrom CustomTools import CustomCalculatorToolllm = OpenAI(temperature=0)tools = [WeatherTool(), CustomCalculatorTool()]agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)agent.run("Query the weather of this week,And How old will I be in ten years? This year I am 28")

看一下完整的响应过程:

I need to use two different tools to answer this questionAction: WeatherAction Input: This weekObservation: Sunny^_^Thought: I need to use a calculator to answer the second part of the questionAction: CalculatorAction Input: 28 + 10Observation: 3Thought: I now know the final answerFinal Answer: This week will be sunny and in ten years I will be 38.

可以看到LangChain Agent 详细分析了每一个步骤,并且正确的调用了每一个可用的方法,拿到了相应的返回值,甚至在最后还修复了28+10=3这个错误。 下面看看LangChain Agent是如何做到这点的

工作原理

首先看看我输入的问题是什么: Query the weather of this week,And How old will I be in ten years? This year I am 28 查询本周天气,以及十年后我多少岁,今年我28

LangChain Agent中,有一套模板可以套用:

PREFIX = """Answer the following questions as best you can. You have access to the following tools:"""FORMAT_INSTRUCTIONS = """Use the following format:Question: the input question you must answerThought: you should always think about what to doAction: the action to take, should be one of [{tool_names}]Action Input: the input to the actionObservation: the result of the action... (this Thought/Action/Action Input/Observation can repeat N times)Thought: I now know the final answerFinal Answer: the final answer to the original input question"""SUFFIX = """Begin!Question: {input}Thought:{agent_scratchpad}"""LangChain Agent 执行过程解析 OpenAI

通过这个模板,加上我们的问题以及自定义的工具,会变成下面这个样子,并且附带解释:

Answer the following questions as best you can. You have access to the following tools: # 尽可能的去回答以下问题,你可以使用以下的工具:Calculator: Useful for when you need to answer questions about math. # 计算器:当你需要回答数学计算的时候可以用到Weather: useful for When you want to know about the weather # 天气:当你想知道天气相关的问题时可以用到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 [Calculator, Weather] # 你应该采取[计算器,天气]之一Action Input: the input to the action # 动作的输入Observation: the result of the action # 动作的结果... (this Thought/Action/Action Input/Observation can repeat N times) # 思考-行动-输入-输出 的循环可以重复N次Thought: I now know the final answer # 最后,你应该知道最终结果了Final Answer: the final answer to the original input question # 针对于原始问题,输出最终结果Begin! # 开始Question: Query the weather of this week,And How old will I be in ten years? This year I am 28 # 问输入的问题Thought:

通过这个模板向openai规定了一系列的规范,包括目前现有哪些工具集,你需要思考回答什么问题,你需要用到哪些工具,你对工具需要输入什么内容,等等。

如果仅仅是这样,openAI会完全补完你的回答,中间无法插入任何内容。因此LangChain使用OpenAI的stop参数,截断了AI当前对话。"stop": ["\\nObservation: ", "\\n\\tObservation: "]

做了以上设定以后,OpenAI仅仅会给到Action和 Action Input两个内容就被stop早停了。 以下是OpenAI的响应内容:

I need to use the weather tool to answer the first part of the question, and the calculator to answer the second part.Action: WeatherAction Input: This week

到这里是OpenAI的响应结果,可见,很简单就拿到了Action和Action Input。 这里从Tools中找到name=Weather的工具,然后再将This Week传入方法。具体业务处理看详细情况。这里仅返回Sunny。

由于当前找到了Action和Action Input。 代表OpenAI认定当前任务链并没有结束。因此像请求体后拼接结果:Observation: Sunny 并且让他再次思考Thought:

开启第二轮思考: 下面是再次请求的完整请求体:

Answer the following questions as best you can. You have access to the following tools:Calculator: Useful for when you need to answer questions about math.Weather: useful for When you want to know about the weatherUse the following format:Question: the input question you must answerThought: you should always think about what to doAction: the action to take, should be one of [Calculator, Weather]Action Input: the input to the actionObservation: the result of the action... (this Thought/Action/Action Input/Observation can repeat N times)Thought: I now know the final answerFinal Answer: the final answer to the original input questionBegin!Question: Query the weather of this week,And How old will I be in ten years? This year I am 28Thought: I need to use the weather tool to answer the first part of the question, and the calculator to answer the second part.Action: WeatherAction Input: This weekObservation: Sunny^_^Thought:

同第一轮一样,OpenAI再次进行思考,并且返回Action 和 Action Input 后,再次被早停。

I need to calculate my age in ten yearsAction: CalculatorAction Input: 28 + 10

由于计算器工具只会返回3,结果会拼接出一个错误的结果,构造成了一个新的请求体 进行第三轮请求:

Answer the following questions as best you can. You have access to the following tools:Calculator: Useful for when you need to answer questions about math.Weather: useful for When you want to know about the weatherUse the following format:Question: the input question you must answerThought: you should always think about what to doAction: the action to take, should be one of [Calculator, Weather]Action Input: the input to the actionObservation: the result of the action... (this Thought/Action/Action Input/Observation can repeat N times)Thought: I now know the final answerFinal Answer: the final answer to the original input questionBegin!Question: Query the weather of this week,And How old will I be in ten years? This year I am 28Thought: I need to use the weather tool to answer the first part of the question, and the calculator to answer the second part.Action: WeatherAction Input: This weekObservation: Sunny^_^Thought:I need to calculate my age in ten yearsAction: CalculatorAction Input: 28 + 10Observation: 3Thought:

此时两个问题全都拿到了结果,根据开头的限定,OpenAi在完全拿到结果以后会返回I now know the final answer。并且根据完整上下文。把多个结果进行归纳总结:下面是完整的相应结果:

I now know the final answerFinal Answer: I will be 38 in ten years and the weather this week is sunny.

可以看到。ai严格的按照设定返回想要的内容,并且还以外的把28+10=3这个数学错误给改正了

以上,就是LangChain Agent的完整工作流程

本文链接地址:https://www.jiuchutong.com/zhishi/288041.html 转载请保留说明!

上一篇:form表单提交数据如何拿到返回值(form表单提交数组)

下一篇:易北河上的巴斯泰桥,德国撒克逊瑞士国家公园 (© Reinhard Schmid/eStock Photo)(易北河流量)

  • 13promax高刷在哪设置(iphone13promax高刷在哪)

    13promax高刷在哪设置(iphone13promax高刷在哪)

  • 微信删除好友再加回来对方知道吗(微信删除好友再加上聊天记录还有吗)

    微信删除好友再加回来对方知道吗(微信删除好友再加上聊天记录还有吗)

  • 苹果手机激活后可以退货吗(苹果手机激活后能不能变成未激活)

    苹果手机激活后可以退货吗(苹果手机激活后能不能变成未激活)

  • 光环助手会封吗(光环助手会被盗号吗)

    光环助手会封吗(光环助手会被盗号吗)

  • 苹果x是a11还是a12(iphonex是a11吗)

    苹果x是a11还是a12(iphonex是a11吗)

  • 手机号码异地可以补办吗(手机号码异地可以改套餐吗?)

    手机号码异地可以补办吗(手机号码异地可以改套餐吗?)

  •  thinkpad红色按钮是干什么的(thinkpad红色按钮掉了)

    thinkpad红色按钮是干什么的(thinkpad红色按钮掉了)

  • 华为p40是不是5g全网通手机(华为p40是不是5g手机)

    华为p40是不是5g全网通手机(华为p40是不是5g手机)

  • qq一天能加多少群(qq一天能加多少好友会被冻结)

    qq一天能加多少群(qq一天能加多少好友会被冻结)

  • 换显卡需要重装系统吗(电脑显卡怎么拆卸和安装)

    换显卡需要重装系统吗(电脑显卡怎么拆卸和安装)

  • 华为充电泡泡能一直显示么(华为充电泡泡能充电吗)

    华为充电泡泡能一直显示么(华为充电泡泡能充电吗)

  • 微信实名不满15天怎么注销(微信实名不满15天怎么注销实名)

    微信实名不满15天怎么注销(微信实名不满15天怎么注销实名)

  • 上传到qq空间的照片占手机内存吗(上传到qq空间的照片像素会降低吗)

    上传到qq空间的照片占手机内存吗(上传到qq空间的照片像素会降低吗)

  • mate30pro有没有4g版本(mate30Pro有没有光学防抖)

    mate30pro有没有4g版本(mate30Pro有没有光学防抖)

  • 1mbps等于多少m(341mbps等于多少m)

    1mbps等于多少m(341mbps等于多少m)

  • 苹果下载的软件怎么受信任(苹果下载的软件在哪儿找)

    苹果下载的软件怎么受信任(苹果下载的软件在哪儿找)

  • xsmax无线充电几瓦(xs max 无线充电)

    xsmax无线充电几瓦(xs max 无线充电)

  • 误卸载了微信怎么恢复(不小心卸载了微信)

    误卸载了微信怎么恢复(不小心卸载了微信)

  • word文档为什么不能编辑了(word文档为什么一行不满就换行了怎么改)

    word文档为什么不能编辑了(word文档为什么一行不满就换行了怎么改)

  • 荣耀9x有人脸识别吗(honor9x人脸识别)

    荣耀9x有人脸识别吗(honor9x人脸识别)

  • 小米8怎么查看屏幕信息(小米8怎么查看激活时间)

    小米8怎么查看屏幕信息(小米8怎么查看激活时间)

  • 新手直通车推广技巧(新手直通车推广文案)

    新手直通车推广技巧(新手直通车推广文案)

  • 优酷如何取消预约(优酷如何取消预约会员)

    优酷如何取消预约(优酷如何取消预约会员)

  • 通过微信收款记录能找到付款人吗(通过微信收款记录能联系到付款人吗)

    通过微信收款记录能找到付款人吗(通过微信收款记录能联系到付款人吗)

  • 安居客如何注销(安居客如何注销经纪人账号)

    安居客如何注销(安居客如何注销经纪人账号)

  • 华为mate10拍照技巧(华为mate10拍照效果)

    华为mate10拍照技巧(华为mate10拍照效果)

  • 华为p20如何投屏(华为p20如何投屏抖音)

    华为p20如何投屏(华为p20如何投屏抖音)

  • 个体工商户定期定额征收个人所得税
  • 进项税转出的金额含税吗
  • 自来水厂会污染水源吗
  • 所得税费用属于什么科目借贷方向
  • 企业购买产品
  • 外汇储备保值增值
  • 其他现代服务业能开哪些税目
  • 滞留票税务局会罚款多少
  • 出口企业类别在哪里查
  • 小规模纳税人专票和普票区别
  • 银行收单业务员做什么的
  • 中小企业对应的是
  • 增值税发票密码忘记了怎么办
  • 企业违约补偿收条怎么写
  • 无形资产原值变动账务处理
  • 奖金多发退回时间怎么算
  • 购买未完工的厂房会计分录
  • 企业销售额达到多少交企业所得税
  • 增值税的专用发票金额含税吗
  • 不能远程补报之前的税款所属期
  • 月收入不超10万减免 具体分销售额吗
  • 分类所得申报要申报吗
  • 税务利润表怎么填
  • 企业处理二手车增值税没交,有什么影响
  • 留抵退还增值税
  • 内部收益率计算公式及例题
  • 个人交社保可以交生育险吗
  • 商铺土地增值税清算
  • 车子计提折旧年限
  • 进口货物的企业有哪些
  • 如何使用腾讯电子签维护自己的权益
  • macos mojace
  • vmware10虚拟机安装
  • 公司资产报废处理请示
  • php语言之mysql操作
  • 销售折让负数会计分录
  • 苹果官网
  • 工程复工程序是什么
  • 宾馆一次性用品有哪些
  • 单位收到的投标通知书
  • php xml
  • vue3使用教程
  • uniapp和vue哪个好
  • 退税勾选错了怎么办
  • 外国人在我国境内被刑事拘留
  • 公司贷款买车有什么风险
  • sql随机函数rand怎么用
  • 什么发票可以抵扣增值税吗
  • 季度利润表是累计数吗
  • 合同金额含税么
  • 固定成本和变动成本举例
  • 有限合伙企业的税收筹划
  • 减少实收资本会影响资产吗
  • 存货账面价值入哪个科目
  • 公司帮别人代缴社保要交税吗
  • 季度初资产总额怎么算
  • 主营业务成本和其他业务成本区别
  • 会计结账的作用
  • 工业企业如何建立税务风险预警体系工作表
  • 材料物资核算应由谁负责
  • sqlserver优化方案
  • java异常后面的语句会运行吗
  • win2008server安装qq
  • 迁移windows
  • vnetd.exe
  • win7如何升级win11系统
  • 快速关机的快捷方式
  • 32/64位Win10预览版11102(中英日韩等)多国语言包官方iso镜像下载大全
  • linux系统怎么关闭137端口
  • android:ViewPager与FragmentPagerAdapter
  • 动作手游排行榜2020前十名
  • gin项目
  • c++ 编程
  • 对税务领导的批示怎么写
  • 地税网上预约
  • 责令立即改正和责令限期整改
  • 所得税汇总纳税分配表
  • 工行网银如何申请发票
  • 江苏税务个税查询
  • 半挂牵引车车船税怎么算
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设