位置: 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)(易北河流量)

  • 又论博客优化应该注意的几个地方 (博客优化方案)

    又论博客优化应该注意的几个地方 (博客优化方案)

  • 传统企业竞价必知道的4大误区(竞价行业发展潜力)

    传统企业竞价必知道的4大误区(竞价行业发展潜力)

  • 错误习惯成自然(错误决定改变人的一生)

    错误习惯成自然(错误决定改变人的一生)

  • ipad2021支持多少w快充(ipad2020支持多少w)

    ipad2021支持多少w快充(ipad2020支持多少w)

  • 荣耀30pro支持屏幕指纹的吗(荣耀30pro屏幕是lcd还是oled)

    荣耀30pro支持屏幕指纹的吗(荣耀30pro屏幕是lcd还是oled)

  • 苹果手机警报在哪设置(iphone手机警报声)

    苹果手机警报在哪设置(iphone手机警报声)

  • 微信语音10秒自动断开(微信语音10秒自动断开怎么回事)

    微信语音10秒自动断开(微信语音10秒自动断开怎么回事)

  • vivos1怎么设置视频通话美颜(vivox60怎么设置视频美颜)

    vivos1怎么设置视频通话美颜(vivox60怎么设置视频美颜)

  • i12耳机怎么双耳(i12如何双耳连接)

    i12耳机怎么双耳(i12如何双耳连接)

  • 抖音作品上传成功了为什么别人看不到(抖音作品上传成功后一分钟就删除了,好友还能看见吗)

    抖音作品上传成功了为什么别人看不到(抖音作品上传成功后一分钟就删除了,好友还能看见吗)

  • 微信打包怎么弄(如何微信打包)

    微信打包怎么弄(如何微信打包)

  • 华为p40pro微信视频有美颜功能吗(华为p40pro微信视频怎么开美颜功能设置)

    华为p40pro微信视频有美颜功能吗(华为p40pro微信视频怎么开美颜功能设置)

  • 在word中用户可以用什么的方式保护文档不受破坏(在Word中用户可以绘制斜线表头)

    在word中用户可以用什么的方式保护文档不受破坏(在Word中用户可以绘制斜线表头)

  • r15右上角有个耳机(oppor15手机右上角有一个耳机)

    r15右上角有个耳机(oppor15手机右上角有一个耳机)

  • 相机电池可以一直放在里面吗(相机电池可以一直用吗)

    相机电池可以一直放在里面吗(相机电池可以一直用吗)

  • 小米手环怎么测睡眠质量的(小米手环测睡眠需要开启什么)

    小米手环怎么测睡眠质量的(小米手环测睡眠需要开启什么)

  • ios13怎么设置暗黑模式(苹果ios13暗黑模式怎么设置)

    ios13怎么设置暗黑模式(苹果ios13暗黑模式怎么设置)

  • 荣耀20支持面容解锁吗(荣耀支持面容支付的手机)

    荣耀20支持面容解锁吗(荣耀支持面容支付的手机)

  • 苹果手机的就寝闹钟怎么删除(苹果手机的就寝模式在哪里)

    苹果手机的就寝闹钟怎么删除(苹果手机的就寝模式在哪里)

  • 拼多多聚焦展位是什么意思(拼多多聚焦展位怎么玩)

    拼多多聚焦展位是什么意思(拼多多聚焦展位怎么玩)

  • 抖音聊天记录能查到吗(抖音聊天记录能在两个设备上同步吗)

    抖音聊天记录能查到吗(抖音聊天记录能在两个设备上同步吗)

  • 华为手机屏幕旋转关闭教程(华为手机屏幕旋转设置)

    华为手机屏幕旋转关闭教程(华为手机屏幕旋转设置)

  • qtaet2s.exe - qtaet2s是什么进程 有什么用

    qtaet2s.exe - qtaet2s是什么进程 有什么用

  • Linux dpkg-query 命令用法详解(Debian Linux中软件包的查询工具)

    Linux dpkg-query 命令用法详解(Debian Linux中软件包的查询工具)

  • 金税盘开票软件密码忘记怎么办
  • 城建税计税依据扣除增值税期末留抵
  • 小规模纳税人做账要做增值税吗
  • 财务费用中的汇兑收益增加的原因
  • 小规模纳税人免征增值税政策
  • 2019年残保金申报时间
  • 其他收益是否需缴税
  • 钉钉报销费用明细怎么写
  • 跨年发票企业所得税
  • 以前年度损益调整账务处理分录
  • 营改增对建筑行业税负的影响
  • 开具发票时提示离线发票累计金额超限?教你如何处理
  • 其他应付款需要做预算会计吗
  • 工业企业成本结转金额怎么确定
  • 公司贷款评估费的做账
  • 定期结汇会计分录?
  • win10运行红色警戒2卡顿
  • android 设置按钮颜色
  • 什么是应付工资金额
  • 小规模纳税人增值税专用发票税率
  • bios设置教程视频
  • phpwind教程
  • 编译安装php7
  • 苹果电脑进入安全模式按什么键
  • fpp是什么文件
  • vue实现导出
  • 寿命最短的苹果手机
  • PHP:mcrypt_enc_is_block_mode()的用法_Mcrypt函数
  • 内格罗斯岛
  • 财务部门产生的费用入什么?
  • 赞助支出计入应纳税所得额吗
  • framework core
  • php 模拟post
  • php7.3安装
  • vuejs props
  • vue循环数组渲染列表
  • chat p
  • egi脑电数据处理
  • 如何一次性删除微信账单记录
  • 小企业会计准则和一般企业会计准则的区别
  • pythonif嵌套语句
  • 在sqlserver2008中
  • 银行托管账户的规定有哪些
  • 成品油红字发票开具后库存数量如何冲回?
  • mysql日期和时间类型
  • 体检费用需要缴哪些费用
  • 税法中特许权费包括哪些
  • 一般纳税人简易计税方法适用范围
  • 主办会计的工作内容和职责
  • 处置无形资产净收益计入什么科目
  • 消费税的账务处理方法
  • 混合销售举例说明
  • 公司投资者如何避免风险
  • 税务局退回水利基金账务怎么处理
  • 转销无法收回的应收账款会计分录
  • 货款和发票金额不一致
  • 电子税务局自然人扣缴客户端
  • 企业当年实现的净利润即为企业当年可供分配的利润
  • 应缴纳房产税的房产
  • 资产负债表中应交税费为负数是什么意思
  • 结账时能否用红笔写名字
  • sql server分组查询
  • freebsd安装mysql
  • pptp和l2tp哪个比较安全
  • centos 安装选择
  • linux命令telnet
  • rasman.exe - rasman是什么进程 有什么作用
  • win8系统咋样
  • WIN7如何关闭自动关机
  • 创建ubuntu
  • javascript中的判断语句
  • 有哪些小工具
  • ubuntu列出用户
  • 后台运行bat定时器程序示例分享
  • ubuntu libtorch
  • unity点击播放声音
  • python tkinter tabview
  • javascript怎么学
  • 分类所得税和综合所得税的优缺点
  • 转让土地的土地增值税
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设