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

server/2024/10/18 8:23:10/

# 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.ppmy.cn/server/14480.html

相关文章

应用在防蓝光显示器中的LED防蓝光灯珠

相比抗蓝光眼镜、防蓝光覆膜、软体降低蓝光强度这些“软”净蓝手段,通过对LED的发光磷粉进行LED背光进行技术革新,可实现硬件“净蓝”。其能够将90%以上的有害蓝光转换为450nm以上的长波低能光线,从硬件的角度解决了蓝光危害眼睛的问题&#…

Axios

文章目录 AxiosAxios特性安装使用方法Axios 实例拦截器取消请求响应结构错误处理在Vue中封装Axios Axios Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中。 文档:https://axios.nodejs.cn/ Axios特性 从浏览器中创建 XMLHttpReques…

【C语言】联合体详解

目录 1.联合体的声明 2.联合体的特点 3.相同成员的结构体和联合体对比 4.联合体大小的计算 1.联合体的声明 像结构体一样,联合体也是由一个或者多个成员构成,这些成员可以不同的类型。但是编译器只为最大的成员分配足够的内存空间。 联合体的特点是所…

区分stable diffusion中的通道数与张量维度

区分stable diffusion中的通道数与张量形状 1.通道数:1.1 channel 31.2 channel 4 2.张量形状2.1 3D 张量2.2 4D 张量2.2.1 通常2.2.2 stable diffusion 3.应用3.1 问题3.2 举例3.3 张量可以理解为多维可变数组 前言:通道数与张量形状都在数值3和4之间…

设计模式-状态模式在Java中的使用示例-信用卡业务系统

场景 在软件系统中,有些对象也像水一样具有多种状态,这些状态在某些情况下能够相互转换,而且对象在不同的状态下也将具有不同的行为。 为了更好地对这些具有多种状态的对象进行设计,我们可以使用一种被称之为状态模式的设计模式…

【Python-编程模式】

Python-编程模式 ■ 单例模式■ 工厂模式■■ ■ 单例模式 新建文件 str_tools.py 如下代码。 class StrTools:passstr_tool StrTools()在其他文件使用时导入该变量。 from str_tools_py import str_tool s1 str_tool s2 str_tool print(id(s1)) print(id(s2))■ 工厂模式…

20240330-1-词嵌入模型w2v+tf-idf

Word2Vector 1.什么是词嵌入模型? 把词映射为实数域向量的技术也叫词嵌⼊ 2.介绍一下Word2Vec 谷歌2013年提出的Word2Vec是目前最常用的词嵌入模型之一。Word2Vec实际是一种浅层的神经网络模型,它有两种网络结构,分别是连续词袋&#xff…

Hive 数据倾斜

1.什么是数据倾斜 数据倾斜:数据分布不均匀,造成数据大量的集中到一点,造成数据热点。主要表现为任务进度长时间维持在 99%或者 100%的附近,查看任务监控页面,发现只有少量 reduce 子任务未完成,因为其处理…