模板网站搭建,本溪做网站,网站文案的重要性,网站图片 原则目录 大语言模型(LLM) 的function calling实验#xff1a;OpenAI之function calling序列图#xff1a;function calling如何工作详情: 对话内容参考代码 后续: 使用LangChain实现function calling参考 大语言模型(LLM) 的function calling
大语言模型(LLM)可以使用自然语言与… 目录 大语言模型(LLM) 的function calling实验OpenAI之function calling序列图function calling如何工作详情: 对话内容参考代码 后续: 使用LangChain实现function calling参考 大语言模型(LLM) 的function calling
大语言模型(LLM)可以使用自然语言与人类对话。但在使用它完成某项复杂工作时很多时候必须依赖其他外部工具这包括但不限于
训练的知识库和提示词以外的知识。包括某些垂直细分领域以及非公开的数据。计算任务。相信我即使它给出的结果看起来很像样你也不能相信它在计算方面的能力它无法保证100%的准确性。实时数据。需要外部工具提供。
能识别需要使用的外部工具能根据其结果数据完成对话的功能叫做function calling。
实验OpenAI之function calling
OpenAI的GPT作为LLM的代表作我们将给它提出如下问题
问题一共有3个人每个人有15个苹果10个鸭梨一共有多少苹果 注 这个简单的逻辑和算数题只作为实验用途实际应用中可以扩展到复杂的计算。 我们将给GPT提供两个function/tool。一个是乘法一个是加法。 注 其中加法用来迷惑GPT。 我们期待的结果GPT能判断使用乘法及其参数并使用乘法function calling给出的结果数据最终返回正确答案 三个人一共有45个苹果。
序列图function calling如何工作
我们的代码和GPT将怎样完成这个过程呢这里将整个过程描绘在下面的时序图中 #mermaid-svg-nNhMXTrqXGcAY2M6 {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-nNhMXTrqXGcAY2M6 .error-icon{fill:#552222;}#mermaid-svg-nNhMXTrqXGcAY2M6 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-nNhMXTrqXGcAY2M6 .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-nNhMXTrqXGcAY2M6 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-nNhMXTrqXGcAY2M6 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-nNhMXTrqXGcAY2M6 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-nNhMXTrqXGcAY2M6 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-nNhMXTrqXGcAY2M6 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-nNhMXTrqXGcAY2M6 .marker.cross{stroke:#333333;}#mermaid-svg-nNhMXTrqXGcAY2M6 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-nNhMXTrqXGcAY2M6 .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-nNhMXTrqXGcAY2M6 text.actortspan{fill:black;stroke:none;}#mermaid-svg-nNhMXTrqXGcAY2M6 .actor-line{stroke:grey;}#mermaid-svg-nNhMXTrqXGcAY2M6 .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-nNhMXTrqXGcAY2M6 .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-nNhMXTrqXGcAY2M6 #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-nNhMXTrqXGcAY2M6 .sequenceNumber{fill:white;}#mermaid-svg-nNhMXTrqXGcAY2M6 #sequencenumber{fill:#333;}#mermaid-svg-nNhMXTrqXGcAY2M6 #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-nNhMXTrqXGcAY2M6 .messageText{fill:#333;stroke:#333;}#mermaid-svg-nNhMXTrqXGcAY2M6 .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-nNhMXTrqXGcAY2M6 .labelText,#mermaid-svg-nNhMXTrqXGcAY2M6 .labelTexttspan{fill:black;stroke:none;}#mermaid-svg-nNhMXTrqXGcAY2M6 .loopText,#mermaid-svg-nNhMXTrqXGcAY2M6 .loopTexttspan{fill:black;stroke:none;}#mermaid-svg-nNhMXTrqXGcAY2M6 .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-nNhMXTrqXGcAY2M6 .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-nNhMXTrqXGcAY2M6 .noteText,#mermaid-svg-nNhMXTrqXGcAY2M6 .noteTexttspan{fill:black;stroke:none;}#mermaid-svg-nNhMXTrqXGcAY2M6 .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-nNhMXTrqXGcAY2M6 .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-nNhMXTrqXGcAY2M6 .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-nNhMXTrqXGcAY2M6 .actorPopupMenu{position:absolute;}#mermaid-svg-nNhMXTrqXGcAY2M6 .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-nNhMXTrqXGcAY2M6 .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-nNhMXTrqXGcAY2M6 .actor-man circle,#mermaid-svg-nNhMXTrqXGcAY2M6 line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-nNhMXTrqXGcAY2M6 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 代码 大模型LLM 1. 调用对话接口告诉LLM提示词可使用的functions/tools定义 2. 需要调用的functions/tools及调用参数 3. 将步骤2中LLM的回复加入对话 4. 循环执行function calling并将结果加入对话 5. 调用对话接口需要以上所有对话信息 6. 最终回答 alt [分支需要function calling] [分支不需要function calling] 代码 大模型LLM 详情: 对话内容
以下内容是真实的对话历史程序和GPT配合按照我们的预想完成了整个过程并最终给出了正确答案。 注以下用到的UserMessage, AIMessage, FunctionMessage都是LangChain中的概念它比较贴切的抽象了不同role的对话项。 步骤1中的对话项UserMessage 向GPT输入对话提示词。
#提示词
{role: user,content: 一共有3个人每个人有15个苹果10个鸭梨一共有多少苹果
}另外在调用GPT接口时定义了2个function type tools乘法multiply和加法add。内容参见下一部分的代码部分。 步骤2中的对话项AIMessage GPT返回需要调用的functions/tools及其调用参数。
#这里GPT没有给出最终答案它识别出了需要调用乘法multiply参数一first_int为3个人参数二second_int为15个苹果/每人。
{content: null,role: assistant,function_call: null,tool_calls: [{id: call_ZMbo4SiA2iaZUSLJMyX8ZzkP,function: {arguments: {\first_int\:3,\second_int\:15},name: multiply},type: function}]
}步骤4中的对话项FunctionMessage function calling的调用结果数据。 tool_call_id对应步骤2中的tool_calls元素中的id。content为程序调用function/tool后的结果数据。
#将function calling的结果为3*1545设定role为tool将其加入对话中。
{tool_call_id: call_ZMbo4SiA2iaZUSLJMyX8ZzkP,role: tool,name: multiply,content: 45
}步骤6中的对话项AIMessage 程序将以上所有对话项发送给GPTGPT用自然语言返回最终结论。
#最终结果为三个人一共有45个苹果。
{content: 三个人一共有45个苹果。,role: assistant,function_call: null,tool_calls: null
}参考代码
function calling的实现代码如下
import json# 初始化环境和OpenAI
from openai import OpenAI
from dotenv import load_dotenv, find_dotenv
_ load_dotenv(find_dotenv())
client OpenAI()#调用GPT大模型
def get_completion(messages, tools, modelgpt-3.5-turbo):response client.chat.completions.create(modelmodel,messagesmessages,# tool_choice支持设置 auto由模型决定是否调用tool 或者 none 不调用tool作为value。 有tools定义时默认由模型决定。# 也可以强制要求必须调用指定的函数如下所示# tool_choice {type: function, function: {name: multiply}} , toolstools)return response.choices[0].message#定义function/tool 1: multiply
def multiply(first_int: int, second_int: int) - int:两个整数相乘return first_int * second_int#定义function/tool 2: add
def add(first_add: int, second_add: int) - int:两个整数相加return first_add second_add#以列表形式将function calling的格式告诉大模型
tools[{type: function,function: {name: multiply,description: 两个整数相乘,parameters: {type: object,properties: {first_int: {type: integer,description: 第一个乘数,},second_int: {type: integer,description: 第二个乘数,}},required: [first_int, second_int],}}},{type: function,function: {name: add,description: 两个整数相加,parameters: {type: object,properties: {first_add: {type: integer,description: 第一个加数,},second_add: {type: integer,description: 第二个加数,}},required: [first_add, second_add],}}
}]# 调用大模型
prompt 一共有3个人每个人有15个苹果10个鸭梨一共有多少苹果
messages [{role: user, content: prompt}
]
response get_completion(messages, tools)# 把大模型的回复加入到对话中
messages.append(response) # 处理大模型需要function calling的情况
while (response.tool_calls is not None):# 循环进行function calling将结果加入到对话中for tool_call in response.tool_calls:selected_tool {add: add, multiply: multiply}[tool_call.function.name]args json.loads(tool_call.function.arguments)tool_output selected_tool(**args)messages.append({tool_call_id: tool_call.id, # 用于标识函数调用的 IDrole: tool,name: tool_call.function.name,content: str(tool_output) # 数值result 必须转成字符串})# 调用大模型并把大模型的回复加入到对话中response get_completion(messages, tools)messages.append(response) print(最终回复)
print(response.content) 后续: 使用LangChain实现function calling
后续将更新如何使用LangChain实现function callingLangChain对比原生调用能提供哪些便利以及其中可能出现的坑。
参考
OpenAI / function calling LangChain / Tool/function calling