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

  • 滴滴出行订单记录怎么删除(滴滴出行订单记录删除怎么恢复)

    滴滴出行订单记录怎么删除(滴滴出行订单记录删除怎么恢复)

  • 华为荣耀9x可以分屏吗(华为荣耀9x可以录屏吗)

    华为荣耀9x可以分屏吗(华为荣耀9x可以录屏吗)

  • iphonex面容识别不了(iphonex面容识别后直接开锁)

    iphonex面容识别不了(iphonex面容识别后直接开锁)

  • 人脸识别解除微信红包异常(解除微信人脸识别登录)

    人脸识别解除微信红包异常(解除微信人脸识别登录)

  • 路由器ddns有什么用(无线路由器ddns有什么用)

    路由器ddns有什么用(无线路由器ddns有什么用)

  • 快手作品显示x什么意思(快手作品显示刷号类直播)

    快手作品显示x什么意思(快手作品显示刷号类直播)

  • 怎么把微信设置成深色模式(怎么把微信设置成英文)

    怎么把微信设置成深色模式(怎么把微信设置成英文)

  • 短信不在通知栏显示(短信不在通知栏怎么办)

    短信不在通知栏显示(短信不在通知栏怎么办)

  • 苹果7p是几g运行内存(苹果7p是多少g运行内存)

    苹果7p是几g运行内存(苹果7p是多少g运行内存)

  • opporeno2微信视频为什么没有美颜(opporeno2微信视频老掉)

    opporeno2微信视频为什么没有美颜(opporeno2微信视频老掉)

  • 下载群文件会被发现吗(下载群文件会被盗吗)

    下载群文件会被发现吗(下载群文件会被盗吗)

  • 为什么有些网站wifi进不去(为什么有些网站电信能打开移动打不开)

    为什么有些网站wifi进不去(为什么有些网站电信能打开移动打不开)

  • oppoa11是双卡双待手机吗(oppoa11双卡都可以4g吗)

    oppoa11是双卡双待手机吗(oppoa11双卡都可以4g吗)

  • 一加dc调光有什么用(一加的dc调光真的有用吗)

    一加dc调光有什么用(一加的dc调光真的有用吗)

  • 小米9pro怎么开启双击亮屏(小米9pro怎么开启双音喇叭)

    小米9pro怎么开启双击亮屏(小米9pro怎么开启双音喇叭)

  • 抖音如何找回登录账号(抖音忘记登录账号了怎么找回原来那个)

    抖音如何找回登录账号(抖音忘记登录账号了怎么找回原来那个)

  • 三星s9微信怎么分身(三星s9微信怎么返回桌面)

    三星s9微信怎么分身(三星s9微信怎么返回桌面)

  • iphonex怎么调控制中心(苹果x怎么控制亮度)

    iphonex怎么调控制中心(苹果x怎么控制亮度)

  • 充电仓怎么给耳机充电(充电仓怎么给无线耳机充电)

    充电仓怎么给耳机充电(充电仓怎么给无线耳机充电)

  • 华为手机的otc开关在哪(华为手机在哪里开otc)

    华为手机的otc开关在哪(华为手机在哪里开otc)

  • 抖音收藏好友能看到吗(抖音收藏的作品好友能看到吗)

    抖音收藏好友能看到吗(抖音收藏的作品好友能看到吗)

  • 小米9透明版和尊享版区别(小米9透明版和小米9pro)

    小米9透明版和尊享版区别(小米9透明版和小米9pro)

  • 算法的时间复杂度是(算法的时间复杂度取决于)

    算法的时间复杂度是(算法的时间复杂度取决于)

  • 拼多多到货会提醒吗(拼多多到货会提醒收货吗)

    拼多多到货会提醒吗(拼多多到货会提醒收货吗)

  • vivox27虚拟按键怎么隐藏(vivox20plus虚拟按键)

    vivox27虚拟按键怎么隐藏(vivox20plus虚拟按键)

  • 闲鱼怎么发帖子(闲鱼怎么发帖子卖)

    闲鱼怎么发帖子(闲鱼怎么发帖子卖)

  • 快手拍视频怎么瘦脸(快手拍视频怎么自动配字幕)

    快手拍视频怎么瘦脸(快手拍视频怎么自动配字幕)

  • 增值税发票如何抵扣税款
  • 小微企业应纳税所得额是指什么
  • 货运代理开票系统如何开票
  • 网上认证勾选平台登录不成功
  • 养殖企业如何做销售
  • 结算业务申请书和转账支票区别
  • 负债类科目有借方余额吗
  • 预计销售退回的钱怎么算
  • 购买商标权税率多少
  • 旅行社差额征收怎么做账
  • 建设工程中税费如何承担
  • 医院减免医药费后还可以报保险吗
  • 自己生产的产品用于职工福利
  • 发票认证费用
  • 会员卡退钱是退全额吗
  • 佣金支付方式有哪几种
  • 装修费属于劳务费吗
  • 加工费的增值税税率是多少
  • 应付利息在资产负债表中属于什么项目
  • 存货盘亏应该计什么科目
  • 成本算错了
  • 先开票还是先预约
  • 银行代扣社保费
  • 招投标交易服务平台有哪些
  • 监控 固定资产
  • vue使用方法
  • mac dock不见了
  • 公司购买空调计入什么费用
  • 因有减免税款不退怎么办
  • 购进商品怎么做会计分录
  • 固定资产成本中的相关税费
  • 计提增值税可以无付凭证吗
  • windows 11 正式版实际使用体验如何?
  • vue中使用gojs
  • php stristr函数
  • 资产处置损益计税基础是什么
  • 开发支出应属于什么科目
  • 作废的普通发票,顾客联拿不回来
  • wordpress添加css
  • 扣缴义务人需要办理税务登记吗
  • 珠宝行业会计核算内容
  • 数据读取流程
  • pytorch自定义网络层
  • modprobe operation not permitted
  • 用php编写一个简单的计算器程序
  • 合并会计报表的编制
  • wordpress jquery
  • 银行日记账更正
  • 增值税进项税额在借方还是贷方
  • 下月初可以认证上月的发票么
  • 缴纳的工会经费现金流量表怎么记
  • 交耕地占用税如何交
  • 税务评估价多久更新一次
  • 印花税签合同
  • 投资性房地产改造期间计提折旧吗
  • 新建厂房的费用怎么算
  • 计提未到期责任准备金的意义
  • 销售出库和产品入库有什么关系
  • 企业建账涉及哪些内容
  • sqlserver1053怎么解决
  • sqlserver增删改查执行语句
  • winxp密码忘了
  • freebsd ntfs
  • gnu linux编程指南
  • sethook.exe - sethook进程是什么文件 有何作用
  • win7系统安装教程不用u盘
  • Win8用Ribbon Disabler工具关闭Ribbon功能区界面
  • 深入剖析典型案例
  • node.js常用命令
  • media and
  • 使用scp获取远程linux服务器上的文件 linux远程拷贝文件
  • unity3d最新
  • jquery开发
  • Linux中的host命令应用实例详解
  • 安装node-sass报错
  • 简单的智能家居
  • javascript基础入门视频教程
  • python怎么自定义函数
  • 上海中考规定
  • 重庆市大学生田径锦标赛
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设