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

相关文章

Python设置Cookie永不超时的详细指南

《Python设置Cookie永不超时的详细指南》Cookie是一种存储在用户浏览器中的小型数据片段,用于记录用户的登录状态、偏好设置等信息,下面小编就来和大家详细讲讲Python如何设置Cookie... 目录一、Cookie的作用与重要性二、Cookie过期的原因三、实现Cookie永不超时的方法(一)

Java进程异常故障定位及排查过程

《Java进程异常故障定位及排查过程》:本文主要介绍Java进程异常故障定位及排查过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、故障发现与初步判断1. 监控系统告警2. 日志初步分析二、核心排查工具与步骤1. 进程状态检查2. CPU 飙升问题3. 内存

关于集合与数组转换实现方法

《关于集合与数组转换实现方法》:本文主要介绍关于集合与数组转换实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、Arrays.asList()1.1、方法作用1.2、内部实现1.3、修改元素的影响1.4、注意事项2、list.toArray()2.1、方

SpringBoot整合liteflow的详细过程

《SpringBoot整合liteflow的详细过程》:本文主要介绍SpringBoot整合liteflow的详细过程,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋...  liteflow 是什么? 能做什么?总之一句话:能帮你规范写代码逻辑 ,编排并解耦业务逻辑,代码

Go语言中nil判断的注意事项(最新推荐)

《Go语言中nil判断的注意事项(最新推荐)》本文给大家介绍Go语言中nil判断的注意事项,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1.接口变量的特殊行为2.nil的合法类型3.nil值的实用行为4.自定义类型与nil5.反射判断nil6.函数返回的

Java中调用数据库存储过程的示例代码

《Java中调用数据库存储过程的示例代码》本文介绍Java通过JDBC调用数据库存储过程的方法,涵盖参数类型、执行步骤及数据库差异,需注意异常处理与资源管理,以优化性能并实现复杂业务逻辑,感兴趣的朋友... 目录一、存储过程概述二、Java调用存储过程的基本javascript步骤三、Java调用存储过程示

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁

MySQL中的InnoDB单表访问过程

《MySQL中的InnoDB单表访问过程》:本文主要介绍MySQL中的InnoDB单表访问过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、环境3、访问类型【1】const【2】ref【3】ref_or_null【4】range【5】index【6】

在Spring Boot中集成RabbitMQ的实战记录

《在SpringBoot中集成RabbitMQ的实战记录》本文介绍SpringBoot集成RabbitMQ的步骤,涵盖配置连接、消息发送与接收,并对比两种定义Exchange与队列的方式:手动声明(... 目录前言准备工作1. 安装 RabbitMQ2. 消息发送者(Producer)配置1. 创建 Spr

浏览器插件cursor实现自动注册、续杯的详细过程

《浏览器插件cursor实现自动注册、续杯的详细过程》Cursor简易注册助手脚本通过自动化邮箱填写和验证码获取流程,大大简化了Cursor的注册过程,它不仅提高了注册效率,还通过友好的用户界面和详细... 目录前言功能概述使用方法安装脚本使用流程邮箱输入页面验证码页面实战演示技术实现核心功能实现1. 随机