LLM大语言模型(十三):ChatGLM3-6B兼容Langchain的Function Call的一步一步的详细转换过程记录

# LangChain:原始prompt

System: Respond to the human as helpfully and accurately as possible. You have access to the following tools:

Calculator: Useful for when you need to calculate math problems, args: {\'calculation\': {\'description\': \'calculation to perform\', \'title\': \'Calculation\', \'type\': \'string\'}}

Use a json blob to specify a tool by providing an action key (tool name) and an action_input key (tool input).

Valid "action" values: "Final Answer" or Calculator

Provide only ONE action per $JSON_BLOB, as shown:

```
{
    "action": $TOOL_NAME,
    "action_input": $INPUT
}
```
Follow this format:

Question: input question to answer
Thought: consider previous and subsequent steps
Action:
```
$JSON_BLOB
```
Observation: action result
... (repeat Thought/Action/Observation N times)
Thought: I know what to respond
Action:
```
{
    "action": "Final Answer",
    "action_input": "Final response to human"
}

Begin! Reminder to ALWAYS respond with a valid json blob of a single action. Use tools if necessary. Respond directly if appropriate. Format is Action:```$JSON_BLOB```then Observation
Human: 34 * 34

(reminder to respond in a JSON blob no matter what)


# ChatGLM:找到原始prompt中关于tool的说明 

Calculator: Useful for when you need to calculate math problems, args: {'calculation': {'description': 'calculation to perform', 'title': 'Calculation', 'type': 'string'}}

# ChatGLM:找到原始prompt中用户输入

Human: 34 * 34\n\n\n(reminder to respond in a JSON blob no matter what)

# ChatGLM:将原始prompt转换为ChatGLM的会话格式,并记录到self.history,同时找到用户输入作为接下来的query=34 * 34

[{'role': 'system', 'content': 'Answer the following questions as best as you can. You have access to the following tools:', 'tools': [{'name': 'Calculator', 'description': 'Useful for when you need to calculate math problems', 'parameters': {'calculation': {'description': 'calculation to perform', 'type': 'string'}}}]}, {'role': 'user', 'content': '34 * 34\n\n\n (reminder to respond in a JSON blob no matter what)'}
]

# ChatGLM:依据self.history和query进行生成,生成结果赋值给self.history,新的self.history内容如下

[{'role': 'system', 'content': 'Answer the following questions as best as you can. You have access to the following tools:', 'tools': [{'name': 'Calculator', 'description': 'Useful for when you need to calculate math problems', 'parameters': {'calculation': {'description': 'calculation to perform', 'type': 'string'}}}]}, {'role': 'user', 'content': '34 * 34\n\n\n (reminder to respond in a JSON blob no matter what)'}, {'role': 'user', 'content': '34 * 34'}, {'role': 'assistant', 'metadata': 'Calculator', 'content': " ```python\ntool_call(calculation='34*34')\n```"}]

==新增了两条信息==

{'role': 'user', 'content': '34 * 34'}, 
{'role': 'assistant', 'metadata': 'Calculator', 'content': " ```python\ntool_call(calculation='34*34')\n```"}

# ChatGLM:解析LLM最新回答中的tool,并作为_call()函数的返回


response = '\nAction: \n```\n{"action": "Calculator", "action_input": {"calculation": "34*34"}}\n```'

# ChatGLM:更新_call()的入参History,增加一个pair=(prompt,response),传递给LangChain


==此时prompt就是原始prompt==
==response就是ChatGLM生成的接下来要用到的Tool,也就是原始prompt里希望LLM返回的结果==

# LangChain:执行Tool的调用,得到Tool的返回值,继续调用LLM


==这时候LLM还没有返回Final answer,所以要继续执行LLM==

# ChatGLM:此时的prompt是在原始prompt基础上再增加了上一步Tool的调用信息


'System: Respond to the human as helpfully and accurately as possible. You have access to the following tools:\n\nCalculator: Useful for when you need to calculate math problems, args: {\'calculation\': {\'description\': \'calculation to perform\', \'title\': \'Calculation\', \'type\': \'string\'}}\n\nUse a json blob to specify a tool by providing an action key (tool name) and an action_input key (tool input).\n\nValid "action" values: "Final Answer" or Calculator\n\nProvide only ONE action per $JSON_BLOB, as shown:\n\n```\n{\n  "action": $TOOL_NAME,\n  "action_input": $INPUT\n}\n```\n\nFollow this format:\n\nQuestion: input question to answer\nThought: consider previous and subsequent steps\nAction:\n```\n$JSON_BLOB\n```\nObservation: action result\n... (repeat Thought/Action/Observation N times)\nThought: I know what to respond\nAction:\n```\n{\n  "action": "Final Answer",\n  "action_input": "Final response to human"\n}\n\nBegin! Reminder to ALWAYS respond with a valid json blob of a single action. Use tools if necessary. Respond directly if appropriate. Format is Action:```$JSON_BLOB```then Observation\nHuman: 34 * 34\n\n\n

Action: \n```\n{"action": "Calculator", "action_input": {"calculation": "34*34"}}\n```\nObservation: 1156\nThought: \n 
==这一段是新增的,增加了上一步Action的Tool的执行结果==

(reminder to respond in a JSON blob no matter what)'

# ChatGLM解析新prompt中的observation


得到1156
向self.history新增一条信息:
{'role': 'observation', 'content': '1156'}

# ChatGLM:再次执行chat,进行生成


入参:此时query是空,history是所有的历史
返回结果,新增如下两条信息:
{'role': 'user', 'content': ''}
{'role': 'assistant', 'metadata': '', 'content': '{\n    " calculation": "34*34",\n    " result": 1156\n}'}

# ChatGLM:解析tool,发现self.history里最后一条消息的metadata是空,说明没有tool需要调用了,可以拼接Final answer,_call()返回值如下


response = '\nAction: \n```\n{"action": "Final Answer", "action_input": "{\\n    \\" calculation\\": \\"34*34\\",\\n    \\" result\\": 1156\\n}"}\n```'

# ChatGLM:_call()向入参的History里增加了一个新的pair


0=新的prompt
1=response

# LangChain:收到了Final Answer,调用结束,最后输出


{'input': '34 * 34', 'output': '{\n    " calculation": "34*34",\n    " result": 1156\n}'}

 参考

  1. LLM大语言模型(十二):关于ChatGLM3-6B不兼容Langchain 的Function Call-CSDN博客
  2.  LLM大语言模型(十一):基于自定义的ChatGLM3-6B构建LangChain的chain-CSDN博客
  3. LLM大语言模型(十):LangChain自定义Agent使用自定义的LLM-CSDN博客
  4. LLM大语言模型(九):LangChain封装自定义的LLM-CSDN博客
  5. LLM大语言模型(八):ChatGLM3-6B使用的tokenizer模型BAAI/bge-large-zh-v1.5-CSDN博客
  6. LLM大语言模型(七):部署ChatGLM3-6B并提供HTTP server能力
  7. LLM大语言模型(四):在ChatGLM3-6B中使用langchain_chatglm3-6b langchain-CSDN博客

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/4375.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

打地鼠游戏(python期中)

考点: 随机数库:random 时间函数库:time 注意与日期函数库(datetime)区分 代码实现 import random import timedef display_holes(hole_index):holes [* if i ! hole_index else o for i in range(1, 11)]prin…

自动化爬虫工具:you-get安装与使用

Windows下的安装命令: pip install you-get linux下的安装命令: pip3 install you-get 下载完成后,我们可以看到如下的警告,意思就是这个工具并未被添加到环境变量中,如果我们想在命令行中直接调用,需要…

IOS 纯代码自定义UIView案例

#import <UIKit/UIKit.h>NS_ASSUME_NONNULL_BEGINinterface OrderAfterPeriodSelectNumView : UIView //左边标题 property (nonatomic,strong) UILabel *titleLab; //数量 property (nonatomic,strong) UILabel *numLab;end #import "OrderAfterPeriodSelectNumVie…

vim的IDE进阶之路

一 ctags 1 安装 安装ctags比较简单&#xff0c;我用的是vim-plug&#xff0c;网络上随便一搜应该就有很多教程&#xff0c;而且没有什么坑 2 使用 vim之函数跳转功能_nvim函数跳转-CSDN博客https://blog.csdn.net/ballack_linux/article/details/71036072不过针对cuda程序…

2024年电子商务与大数据经济国际会议 (EBDE 2024)

2024年电子商务与大数据经济国际会议 (EBDE 2024) 2024 International Conference on E-commerce and Big Data Economy 【会议简介】 2024年电子商务与大数据经济国际会议即将在厦门召开。本次会议旨在汇聚全球电子商务与大数据经济领域的专家学者&#xff0c;共同探讨电子商务…

nacos-redis-springboot

新项目 准备工作 nacos 版本 2.0.3 redis 最终版本说明 springcloud-alibaba&#xff1a;2.2.7RELEASE springcloud&#xff1a;Hoxton.SR12 springboot&#xff1a;2.3.12.RELEASE Nacos&#xff1a;2.0.3 步骤 启动nacos和redis 准备nacos配置文件 server: port…

使用frp实现内网穿透教程

文章目录 简介frp 是什么&#xff1f;为什么选择 frp&#xff1f; 概念工作原理代理类型 内网穿透教程服务端安装和配置本地Windows&#xff08;客户端&#xff09;安装和配置本地Linux虚拟机&#xff08;客户端&#xff09;安装和配置使用 systemd 管理服务端注意事项 简介 f…

埃氏筛选-判断素数

核心思路如下&#xff1a; 初始化&#xff1a;创建一个布尔数组 isshushu&#xff0c;其长度等于要检查的数 n。这个数组用于标记每个数是否为质数&#xff0c;初始时所有数都假设为质数&#xff08;即数组元素均为 false&#xff09;。 筛选&#xff1a;从最小的质数2开始&a…

GPT学术优化推荐(gpt_academic )

GPT学术优化 (GPT Academic):支持一键润色、一键中英互译、一键代码解释、chat分析报告生成、PDF论文全文翻译功能、互联网信息聚合GPT等等 ChatGPT/GLM提供图形交互界面&#xff0c;特别优化论文阅读/润色/写作体验&#xff0c;模块化设计&#xff0c;支持自定义快捷按钮&…

在线培训考试系统在线考试功能注意事项

在线培训考试系统在线考试功能注意事项 考试前务必注意是否开启防切屏、摄像头监考等防作弊措施&#xff0c;系统一旦检测到触发了疑似作弊行为会立刻自动交卷&#xff0c;考试终止&#xff1b; 答题者准备好后&#xff0c;可点击“开始答题”按钮进入考试&#xff0c;注意考…

【Vue】如何创建一个Vue-cli程序

一、准备工作 1、下载Node.js 官网地址 https://nodejs.org/en 2、查看版本 cmd下通过node-v,查看版本号&#xff1b; cmd下通过npm-v,查看是否打印版本号。 3、安装淘宝加速器 npm install cnpm -g 4、安装Vue-cli cnpm install vue-cli -g 二、创建Vue程序 1、创建一个V…

Aurora-64B/10B、XDMA与DDR结合设计高速数据流通路设计/Aurora光纤设计/XDMA读取DDR设计/基于FPGA的高速数据传输设计

因最近想通过FPGA把数据从光纤传到PC&#xff0c;借此机会和大家一起学习Aurora、XDMA结合DDR 制作不易&#xff0c;记得三连哦&#xff0c;给我动力&#xff0c;持续更新&#xff01;&#xff01;&#xff01; 完整工程文件下载&#xff1a;XDMA读写DDR工程 提取码&…

网络通信协议,UDP和TCP,初步了解

UDP&#xff08;User Datagram Protocol&#xff09;和TCP&#xff08;Transmission Control Protocol&#xff09;是两种常见的网络通信协议&#xff0c;用于在计算机网络中进行数据传输。 1. TCP&#xff1a;Transmission Control Protocol&#xff08;传输控制协议&#xf…

【经典算法】LeetCode31. 下一个排列(Java/C/Python3/GO实现含注释说明,中等)

题目描述 整数数组的一个 排列 就是将其所有成员以序列或线性顺序排列。例如&#xff0c;arr [1,2,3] &#xff0c;以下这些都可以视作 arr 的排列&#xff1a;[1,2,3]、[1,3,2]、[3,1,2]、[2,3,1] 。 整数数组的 下一个排列 是指其整数的下一个字典序更大的排列。更正式地&…

微信小程序的常用API②

一、动画API &#xff08;1&#xff09;作用&#xff1a;用于在微信小程序中完成动画效果的制作 &#xff08;2&#xff09;使用&#xff1a;创建实例 wx.createAnimation() &#xff08;3&#xff09;常用属性&#xff1a; duration 【number型】 动画持续时间&…

探索Flutter 3.0:跨平台开发的新越界

Flutter 3.0 是谷歌推出的最新版本&#xff0c;它是一个开源的UI开发框架&#xff0c;可以用来创建高质量的原生接口在iOS和Android上。自从首次发布以来&#xff0c;Flutter 已经快速发展成为最受欢迎的跨平台移动开发框架之一。Flutter 3.0 带来了许多重要的更新和改进&#…

《C++学习笔记---入门篇2》---传值引用与传引用返回详解

先看这个程序&#xff0c;随着Count栈帧的销毁&#xff0c;会创建一个临时变量将n的值带回&#xff0c;可以实现我们的目的。 再看这个情况的时候&#xff0c;对于n来说他存放的位置在静态区&#xff0c;他不会随着函数栈帧的销毁而销毁&#xff0c;返回的时候依旧靠着临时变量…

Jmeter05:配置环境变量

1 Jmeter 环境 1.1 什么是环境变量&#xff1f;path什么用&#xff1f; 系统设置之一&#xff0c;通过设置PATH&#xff0c;可以让程序在DOS命令行直接启动 1.2 path怎么用 如果想让一个程序可以在DOS直接启动&#xff0c;需要将该程序目录配置进PATH 1.3 PATH和我们的关系…

golang调用阿里云发短信

之前用golang封装的一个发送阿里云短信的工具包&#xff0c;代码如下 client.go package smsimport ("context""github.com/go-playground/validator/v10""github.com/pkg/errors" )type Client interface {// Send 发送短信Send(ctx context.…

Python脚本抢票【笔记】

Python脚本抢票【笔记】 前言版权推荐Python脚本抢票【Python】microsoft edge驱动器下载以及使用最后 前言 2024-4-17 18:19:15 以下内容源自《【笔记】》 仅供学习交流使用 版权 禁止其他平台发布时删除以下此话 本文首次发布于CSDN平台 作者是CSDN日星月云 博客主页是ht…