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

本文主要是介绍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博客

这篇关于LLM大语言模型(十三):ChatGLM3-6B兼容Langchain的Function Call的一步一步的详细转换过程记录的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/933999

相关文章

IntelliJ IDEA 中配置 Spring MVC 环境的详细步骤及问题解决

《IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决》:本文主要介绍IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决,本文分步骤结合实例给大... 目录步骤 1:创建 Maven Web 项目步骤 2:添加 Spring MVC 依赖1、保存后执行2、将新的依赖

Java对象转换的实现方式汇总

《Java对象转换的实现方式汇总》:本文主要介绍Java对象转换的多种实现方式,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录Java对象转换的多种实现方式1. 手动映射(Manual Mapping)2. Builder模式3. 工具类辅助映

C语言中位操作的实际应用举例

《C语言中位操作的实际应用举例》:本文主要介绍C语言中位操作的实际应用,总结了位操作的使用场景,并指出了需要注意的问题,如可读性、平台依赖性和溢出风险,文中通过代码介绍的非常详细,需要的朋友可以参... 目录1. 嵌入式系统与硬件寄存器操作2. 网络协议解析3. 图像处理与颜色编码4. 高效处理布尔标志集合

Go语言开发实现查询IP信息的MCP服务器

《Go语言开发实现查询IP信息的MCP服务器》随着MCP的快速普及和广泛应用,MCP服务器也层出不穷,本文将详细介绍如何在Go语言中使用go-mcp库来开发一个查询IP信息的MCP... 目录前言mcp-ip-geo 服务器目录结构说明查询 IP 信息功能实现工具实现工具管理查询单个 IP 信息工具的实现服

如何为Yarn配置国内源的详细教程

《如何为Yarn配置国内源的详细教程》在使用Yarn进行项目开发时,由于网络原因,直接使用官方源可能会导致下载速度慢或连接失败,配置国内源可以显著提高包的下载速度和稳定性,本文将详细介绍如何为Yarn... 目录一、查询当前使用的镜像源二、设置国内源1. 设置为淘宝镜像源2. 设置为其他国内源三、还原为官方

最详细安装 PostgreSQL方法及常见问题解决

《最详细安装PostgreSQL方法及常见问题解决》:本文主要介绍最详细安装PostgreSQL方法及常见问题解决,介绍了在Windows系统上安装PostgreSQL及Linux系统上安装Po... 目录一、在 Windows 系统上安装 PostgreSQL1. 下载 PostgreSQL 安装包2.

python实现svg图片转换为png和gif

《python实现svg图片转换为png和gif》这篇文章主要为大家详细介绍了python如何实现将svg图片格式转换为png和gif,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录python实现svg图片转换为png和gifpython实现图片格式之间的相互转换延展:基于Py

C 语言中enum枚举的定义和使用小结

《C语言中enum枚举的定义和使用小结》在C语言里,enum(枚举)是一种用户自定义的数据类型,它能够让你创建一组具名的整数常量,下面我会从定义、使用、特性等方面详细介绍enum,感兴趣的朋友一起看... 目录1、引言2、基本定义3、定义枚举变量4、自定义枚举常量的值5、枚举与switch语句结合使用6、枚

MySql match against工具详细用法

《MySqlmatchagainst工具详细用法》在MySQL中,MATCH……AGAINST是全文索引(Full-Textindex)的查询语法,它允许你对文本进行高效的全文搜素,支持自然语言搜... 目录一、全文索引的基本概念二、创建全文索引三、自然语言搜索四、布尔搜索五、相关性排序六、全文索引的限制七

python中各种常见文件的读写操作与类型转换详细指南

《python中各种常见文件的读写操作与类型转换详细指南》这篇文章主要为大家详细介绍了python中各种常见文件(txt,xls,csv,sql,二进制文件)的读写操作与类型转换,感兴趣的小伙伴可以跟... 目录1.文件txt读写标准用法1.1写入文件1.2读取文件2. 二进制文件读取3. 大文件读取3.1