LLM function call

简介

在大模型领域,尤其是在自然语言处理(NLP)和人工智能(AI)应用中,function call 是一种非常重要的技术手段。它主要用于让模型在生成或处理文本的过程中调用外部功能或执行特定任务,有以下应用场景:

  • 数据查询和处理

​ AI模型可以调用数据库查询函数,以获取所需的数据。例如,在客户服务应用中,模型可以调用函数从客户数据库中提取客户信息

  • 执行计算或分析

​ 在需要复杂计算或数据分析的场景中,模型可以调用外部的计算函数。例如,金融分析系统中,模型可以调用函数进行风险评估或收益预测。

  • 第三方API集成

​ 模型可以通过function call集成第三方API服务。例如,调用天气API获取当前天气信息,或者调用地图服务API获取地理位置信息。

  • 自动化工作流程

​ 模型可以调用各种自动化脚本或函数以完成特定任务,例如发送邮件、生成报告或触发警报。

实现流程

使用大模型的function call能力需要进行以下步骤:

  1. 函数定义:事先定义好可以调用的函数及其接口。这些函数通常定义在代码库中,并以API形式暴露给AI模型。
  2. 函数调用机制:模型生成特定的调用指令,这些指令被解析并触发相应的函数执行。指令通常包含函数名及所需的参数。
  3. 响应处理:函数执行完成后,将结果返回给模型,模型根据返回的结果进行后续的生成或决策。例如,获取到的数据库查询结果可以直接用于生成下一步的文本输出。

示例

以下是一个简单的示例,展示如何在AI对话系统中使用function call:

用户请求: “请告诉我今天的天气。”

模型响应:

  1. 解析用户意图,识别需要调用天气API。
  2. 生成function call指令,如get_weather(location="current location")
  3. 调用天气API获取天气信息。
  4. 将API返回的天气信息整合到回复中生成最终输出。

最终输出: “今天的天气是晴天,气温大约25摄氏度。”

OpenAI Tool call

目前OpenAI提供的接口建议使用的是tool call,接口文档:https://platform.openai.com/docs/api-reference/chat/create,使用示例:https://cookbook.openai.com/examples/how_to_call_functions_with_chat_models

  1. 用户询问问题

如果提供了多个functions,本轮想指定某个function,可以使用tool_choice参数,如:tool_choice: {"type": "function", "function": {"name": "gaode_weather"}},默认情况下传auto让模型根据用户的输入去决定是否要调用工具,如果本轮不需要模型调用工具则传none

curl http://localhost:80/v1/chat/completions \-H "Content-Type: application/json" \-H "Authorization: Bearer $OPENAI_API_KEY" \-d '{"model": "gpt-4","messages": [{"role": "system", "content": "Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous."},{"role": "user", "content": "今天深圳天气怎么样?"}],"tools": [{"type": "function","function": {"name": "gaode_weather","description": "A tool when you want to ask about the weather or weather-related question.","parameters": {"type": "object","properties": {"city": {"type": "string","description": "If you don't know you can extract the city name from the question or you can reply:Please tell me your city. You have to extract the Chinese city name from the question."}},"required": ["city"]}}}],"tool_choice": "auto",}'
  1. 提取模型返回的内容

有的比较新的模型,可以一次性返回多个tool调用。不支持的旧模型则是按多轮调用,比如先调用日期工具,再调用天气工具

返回内容:

{"id": "chatcmpl-abc123","object": "chat.completion","created": 1699896916,"model": "gpt-3.5-turbo-0125","choices": [{"index": 0,"message": {"role": "assistant","content": null,"tool_calls": [{"id": "call_Dn2RJJSxzDm49vlVTehseJ0k","type": "function","function": {"name": "gaode_weather","arguments": "{\n\"city\": \"深圳\"\n}"}}]},"logprobs": null,"finish_reason": "tool_calls"}],"usage": {"prompt_tokens": 82,"completion_tokens": 17,"total_tokens": 99}
}
  1. 调用高德天气API,获取返回结果,将工具的调用结果加入到请求的message参数中,并调用模型获取用户问题的结果。
curl http://localhost:80/v1/chat/completions \-H "Content-Type: application/json" \-H "Authorization: Bearer $OPENAI_API_KEY" \-d '{"model": "gpt-4","messages": [{"role": "system", "content": "Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous."},{"role": "user", "content": "今天深圳天气怎么样?"},{"role": "assistant","tool_calls": [{"id": "call_Dn2RJJSxzDm49vlVTehseJ0k","type": "function","function": {"name": "gaode_weather","arguments": "{\n\"city\": \"深圳\"\n}"}}]},{"role": "tool","tool_call_id": "call_Dn2RJJSxzDm49vlVTehseJ0k","name": "gaode_weather","content": "[{\"date\": \"2024-06-18\", \"week\": \"2\", \"dayweather\": \"阵雨\", \"daytemp_float\": \"32.0\", \"daywind\": \"北\", \"nightweather\": \"多云\", \"nighttemp_float\": \"27.0\"}, {\"date\": \"2024-06-19\", \"week\": \"3\", \"dayweather\": \"阵雨\", \"daytemp_float\": \"32.0\", \"daywind\": \"北\", \"nightweather\": \"多云\", \"nighttemp_float\": \"27.0\"}, {\"date\": \"2024-06-20\", \"week\": \"4\", \"dayweather\": \"阵雨\", \"daytemp_float\": \"32.0\", \"daywind\": \"南\", \"nightweather\": \"多云\", \"nighttemp_float\": \"28.0\"}, {\"date\": \"2024-06-21\", \"week\": \"5\", \"dayweather\": \"多云\", \"daytemp_float\": \"33.0\", \"daywind\": \"南\", \"nightweather\": \"多云\", \"nighttemp_float\": \"28.0\"}]"}]}'
  1. 模型根据工具的调用结果回答用户问题
{"id": "chatcmpl-123","object": "chat.completion","created": 1677652288,"model": "gpt-3.5-turbo-0125","system_fingerprint": "fp_44709d6fcb","choices": [{"index": 0,"message": {"role": "assistant","content": "深圳未来几天的天气情况如下:\n\n2024年6月18日,星期六,天气为阵雨,白天温度为32.0摄氏度,北风,晚上多云,温度为27.0摄氏度;\n\n2024年6月19日,星期日,天气为阵雨,白天温度为32.0摄氏度,北风,晚上多云,温度为27.0摄氏度;\n\n2024年6月20日,星期一,天气为阵雨,白天温度为32.0摄氏度,南风,晚上多云,温度为28.0摄氏度;\n\n2024年6月21日,星期二,天气为多云,白天温度为33.0摄氏度,南风,晚上多云,温度为28.0摄氏度。\n\n请注意天气变化,合理安排行程。"},"logprobs": null,"finish_reason": "stop"}],"usage": {"prompt_tokens": 9,"completion_tokens": 12,"total_tokens": 21}
}

OpenAI Function call

目前function call已经被openAI标记成deprecated,建议用新的Tool call方式,但是目前很多项目用的还是function call的方案,未及时升级,细节参数可以参考文档:https://platform.openai.com/docs/api-reference/chat/create

  1. 用户询问问题

如果提供了多个functions,本轮想指定某个function,可以指定function_call参数,如:function_call: {"name": "gaode_weather"},则模型只负责解析该function需要的参数信息。

curl http://localhost:80/v1/chat/completions \-H "Content-Type: application/json" \-H "Authorization: Bearer $OPENAI_API_KEY" \-d '{"model": "gpt-4","messages": [{"role": "system", "content": "Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous."},{"role": "user", "content": "今天深圳天气怎么样?"}],"functions": [{"name": "gaode_weather","description": "A tool when you want to ask about the weather or weather-related question.","parameters": {"type": "object","properties": {"city": {"type": "string","description": "If you don't know you can extract the city name from the question or you can reply:Please tell me your city. You have to extract the Chinese city name from the question."}},"required": ["city"]}}]}'
  1. 提取模型返回的内容,调用工具获取结果

返回内容

{"id": "chatcmpl-123","object": "chat.completion","created": 1677652288,"model": "gpt-3.5-turbo-0125","system_fingerprint": "fp_44709d6fcb","choices": [{"index": 0,"message": {"role": "assistant","function_call": {"name": "gaode_weather","arguments": "{\n  \"city\": \"深圳\"\n}"}},"logprobs": null,"finish_reason": "function_call"}],"usage": {"prompt_tokens": 9,"completion_tokens": 12,"total_tokens": 21}
}
  1. 调用高德天气API,获取返回结果,将工具的调用结果加入到请求的message参数中,并调用模型获取用户问题的结果。
curl http://localhost:80/v1/chat/completions \-H "Content-Type: application/json" \-H "Authorization: Bearer $OPENAI_API_KEY" \-d '{"model": "gpt-4","messages": [{"role": "system", "content": "Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous."},{"role": "user", "content": "今天深圳天气怎么样?"},{"role": "assistant","function_call": {"name": "gaode_weather","arguments": "{\n  \"city\": \"深圳\"\n}"}},{"role": "function","name": "gaode_weather","content": "[{\"date\": \"2024-06-18\", \"week\": \"2\", \"dayweather\": \"阵雨\", \"daytemp_float\": \"32.0\", \"daywind\": \"北\", \"nightweather\": \"多云\", \"nighttemp_float\": \"27.0\"}, {\"date\": \"2024-06-19\", \"week\": \"3\", \"dayweather\": \"阵雨\", \"daytemp_float\": \"32.0\", \"daywind\": \"北\", \"nightweather\": \"多云\", \"nighttemp_float\": \"27.0\"}, {\"date\": \"2024-06-20\", \"week\": \"4\", \"dayweather\": \"阵雨\", \"daytemp_float\": \"32.0\", \"daywind\": \"南\", \"nightweather\": \"多云\", \"nighttemp_float\": \"28.0\"}, {\"date\": \"2024-06-21\", \"week\": \"5\", \"dayweather\": \"多云\", \"daytemp_float\": \"33.0\", \"daywind\": \"南\", \"nightweather\": \"多云\", \"nighttemp_float\": \"28.0\"}]"}]}'
  1. 模型根据工具的调用结果回答用户问题
{"id": "chatcmpl-123","object": "chat.completion","created": 1677652288,"model": "gpt-3.5-turbo-0125","system_fingerprint": "fp_44709d6fcb","choices": [{"index": 0,"message": {"role": "assistant","content": "深圳未来几天的天气情况如下:\n\n2024年6月18日,星期六,天气为阵雨,白天温度为32.0摄氏度,北风,晚上多云,温度为27.0摄氏度;\n\n2024年6月19日,星期日,天气为阵雨,白天温度为32.0摄氏度,北风,晚上多云,温度为27.0摄氏度;\n\n2024年6月20日,星期一,天气为阵雨,白天温度为32.0摄氏度,南风,晚上多云,温度为28.0摄氏度;\n\n2024年6月21日,星期二,天气为多云,白天温度为33.0摄氏度,南风,晚上多云,温度为28.0摄氏度。\n\n请注意天气变化,合理安排行程。"},"logprobs": null,"finish_reason": "stop"}],"usage": {"prompt_tokens": 9,"completion_tokens": 12,"total_tokens": 21}
}

ReAct Prompting

更老的模型,并不具备工具调用的能力,为了实现工具调用,通常是通过ReAct(Reasoning and Acting)Prompting来实现的,它旨在通过将推理(Reasoning)和行动(Acting)相结合来增强大语言模型的能力。这种方法特别适用于需要复杂决策和多步骤处理的任务。

ReAct Prompting通过以下步骤对用户问题生成输出:

  1. 推理(Reasoning): 模型首先进行推理,理解用户的请求,并确定需要执行的操作。推理过程可以包括问题分解、步骤规划等。
  2. 行动(Acting):根据推理结果,调用外部工具或API,执行具体操作,如检索信息、执行计算、访问数据库等。
  3. 整合结果:模型将外部操作的结果整合到最终输出中,生成用户所需的响应。

通用步骤模板如下,每个模型可能适配的提示词不同,需要结合模型做相应的调整:

用户请求:请告诉我今天的天气。
模型推理:用户想知道今天的天气,需要调用天气API。
模型行动:调用get_weather(location="current location")。
整合结果:根据API返回的结果生成最终输出。

以下是基于Qwen模型使用ReAct Prompting的示例,细节可以参考:https://github.com/QwenLM/Qwen/blob/main/examples/react_prompt.md

  1. 将工具,用户问题格式化成prompt,注意需要将Observation:Observation:\n加入stop token,免得模型自问自答。
Answer the following questions as best you can. You have access to the following tools:gaode_weather: Call this tool to interact with the gaode_weather API. What is the gaode_weather API useful for? A tool when you want to ask about the weather or weather-related question. Parameters: [{"name": "city", "description": "If you don't know you can extract the city name from the question or you can reply:Please tell me your city. You have to extract the Chinese city name from the question.", "required": true, "schema": {"type": "string"}}] Format the arguments as a JSON object.Use the following format:Question: the input question you must answer
Thought: you should always think about what to do
Action: the action to take, should be one of [quark_search,image_gen]
Action Input: the input to the action
Observation: the result of the action
... (this Thought/Action/Action Input/Observation can be repeated zero or more times)
Thought: I now know the final answer
Final Answer: the final answer to the original input questionBegin!Question: 深圳天气

模型返回:

Thought: 我应该用gaode_weather来查询深圳天气
Action: gaode_weather
Action Input: {"city": "深圳"}
  1. 调用工具获取结果,并让模型根据工具的结果继续作答

假设工具的返回内容为

[{"date": "2024-06-18", "week": "2", "dayweather": "阵雨", "daytemp_float": "32.0", "daywind": "北", "nightweather": "多云", "nighttemp_float": "27.0"}, {"date": "2024-06-19", "week": "3", "dayweather": "阵雨", "daytemp_float": "32.0", "daywind": "北", "nightweather": "多云", "nighttemp_float": "27.0"}, {"date": "2024-06-20", "week": "4", "dayweather": "阵雨", "daytemp_float": "32.0", "daywind": "南", "nightweather": "多云", "nighttemp_float": "28.0"}, {"date": "2024-06-21", "week": "5", "dayweather": "多云", "daytemp_float": "33.0", "daywind": "南", "nightweather": "多云", "nighttemp_float": "28.0"}]

接下来,将首次请求模型时的prompt和调用工具的结果拼装成如下prompt:

Answer the following questions as best you can. You have access to the following tools:gaode_weather: Call this tool to interact with the gaode_weather API. What is the gaode_weather API useful for? A tool when you want to ask about the weather or weather-related question. Parameters: [{"name": "city", "description": "If you don't know you can extract the city name from the question or you can reply:Please tell me your city. You have to extract the Chinese city name from the question.", "required": true, "schema": {"type": "string"}}] Format the arguments as a JSON object.Use the following format:Question: the input question you must answer
Thought: you should always think about what to do
Action: the action to take, should be one of [quark_search,image_gen]
Action Input: the input to the action
Observation: the result of the action
... (this Thought/Action/Action Input/Observation can be repeated zero or more times)
Thought: I now know the final answer
Final Answer: the final answer to the original input questionBegin!Question: 深圳天气
Thought: 我应该用gaode_weather来查询深圳天气
Action: gaode_weather
Action Input: {"city": "深圳"}
Observation: [{"date": "2024-06-18", "week": "2", "dayweather": "阵雨", "daytemp_float": "32.0", "daywind": "北", "nightweather": "多云", "nighttemp_float": "27.0"}, {"date": "2024-06-19", "week": "3", "dayweather": "阵雨", "daytemp_float": "32.0", "daywind": "北", "nightweather": "多云", "nighttemp_float": "27.0"}, {"date": "2024-06-20", "week": "4", "dayweather": "阵雨", "daytemp_float": "32.0", "daywind": "南", "nightweather": "多云", "nighttemp_float": "28.0"}, {"date": "2024-06-21", "week": "5", "dayweather": "多云", "daytemp_float": "33.0", "daywind": "南", "nightweather": "多云", "nighttemp_float": "28.0"}]

用新的prompt去调用模型,模型将返回以下结果:

Thought: 我已经成功使用gaode_weather查询到了深圳天气。
Final Answer: 深圳未来几天的天气情况如下:\n\n2024年6月18日,星期六,天气为阵雨,白天温度为32.0摄氏度,北风,晚上多云,温度为27.0摄氏度;\n\n2024年6月19日,星期日,天气为阵雨,白天温度为32.0摄氏度,北风,晚上多云,温度为27.0摄氏度;\n\n2024年6月20日,星期一,天气为阵雨,白天温度为32.0摄氏度,南风,晚上多云,温度为28.0摄氏度;\n\n2024年6月21日,星期二,天气为多云,白天温度为33.0摄氏度,南风,晚上多云,温度为28.0摄氏度。\n\n请注意天气变化,合理安排行程。

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

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

相关文章

【Android】Android Studio 使用Kotlin写代码时代码提示残缺问题解决

问题描述 Android Studio升级之后,从Android Studio 4.2升级到Android Studio Arctic Fox版本,因为项目比较老,使用的Gradle 版本是3.1.3,这个版本的Android Studio最低支持Gradle 3.1版本,应该算是比较合适的版本。 …

不翻墙安装yolov8环境下的RT-DETR并实现PCB表面缺陷检测

目录 一、新建conda环境二、安装yolov8环境1.克隆安装包2.安装依赖包3.测试模型 任务2:基于RT-DETR实现PKU-PCB表面缺陷检测数据准备 数据增强测试 总结 一、新建conda环境 创建并激活conda环境: 在conda创建一个名为yolov8的新环境,并在其中…

C++网络编程实践:使用C++11基于epoll技术实现一个超大并发TCP服务器

理解epoll epoll 是一种 I/O 复用技术,它允许一个线程有效地管理多个文件描述符(在本例中为套接字),而无需为每个连接创建单独的线程。这是通过事件驱动的方式来实现的,epoll 能够通知你哪些套接字已经准备好进行读写…

国际网络专线的开通流程

1. 选择服务商:首先,您需要选择一个可靠的服务商来提供国际网络专线服务。确保服务商具有良好的声誉和专业知识,以便为您提供高质量的网络连接和支持。 2. 评估需求:在与服务商沟通之前,您需要明确自己的网络需求。这…

dp经典问题:LCS问题

dp:LCS问题 最长公共子序列(Longest Common Subsequence, LCS)问题 是寻找两个字符串中最长的子序列,使得这个子序列在两个字符串中出现的相对顺序保持一致,但不要求连续。 力扣原题链接 1.定义 给定两个字符串 S1…

Python快速搭建网站

使用 Python 快速搭建网站通常可以选择以下几种流行的框架: Flask:轻量级框架,适合快速原型开发。Django:功能强大的框架,适合构建复杂的大型应用。 下面将分别介绍如何使用 Flask 和 Django 快速搭建一个简单的网站…

猫狗识别—视频识别

猫狗识别—视频识别 1. 导入所需的库:2. 创建Tkinter主窗口并设置标题:3. 设置窗口的宽度和高度:4. 创建一个Canvas,它将用于显示视频帧:5. 初始化一个视频流变量cap,用于存储OpenCV的视频捕获对象&#xf…

C语言 给定半径和高,计算圆,球,圆柱各参数

设圆半径r1.5&#xff0c;圆柱高h3&#xff0c;求圆周长&#xff0c;圆面积&#xff0c;圆球表面积&#xff0c;圆球体积&#xff0c;圆柱体积。用scanf输入数据&#xff0c;输出计算结果&#xff0c;输出时要求有文字说明&#xff0c;取小数点后2位数字。 #include <stdio.…

【速速收藏】适用于Linux系统的五个优秀PDF编辑器

PDF (Portable Document Format) 是便携文档格式的缩写&#xff0c;这是一种用于电子共享文档的标准格式&#xff0c;广泛应用于各种文档类型的存储和分发。然而&#xff0c;有时我们可能需要对PDF文档进行更改和编辑。本文将介绍五款在Linux平台上广受欢迎的PDF编辑器。 ​​…

陀螺仪LSM6DSV16X与AI集成(8)----MotionFX库解析空间坐标

陀螺仪LSM6DSV16X与AI集成.8--MotionFX库解析空间坐标 概述视频教学样品申请源码下载开启CRC串口设置开启X-CUBE-MEMS1设置加速度和角速度量程速率选择设置FIFO速率设置FIFO时间戳批处理速率配置过滤链初始化定义MotionFX文件卡尔曼滤波算法主程序执行流程lsm6dsv16x_motion_fx…

Linux基础 - 常用命令

目录 零. 简介 一 . 常见 Ubuntu 命令 二. apt-get 下载 三. 网络命令 四. 常用命令的总结 零. 简介 在 Ubuntu 中&#xff0c;命令是用于与操作系统进行交互和执行各种操作的指令。通过在终端中输入命令&#xff0c;可以完成文件管理、系统配置、软件安装、进程管理等各种…

【分布式事务】Seata AT实战

目录 Seata 介绍 Seata 术语 Seata AT 模式 介绍 实战&#xff08;nacos注册中心&#xff0c;db存储&#xff09; 部署 Seata 实现 RM 实现 TM 可能遇到的问题 1. Seata 部署成功&#xff0c;服务启动成功&#xff0c;全局事务不生效 2. 服务启动报错 can not get …

[java]集合类stream的相关操作

1.对list中的map进行分组 下面例子中&#xff0c;根据高度height属性进行分组 List<Map<String, Float>>originalList new ArrayList<>();originalList.add(new HashMap<String,Float>() {{put("lng", 180.0f);put("lat",90f);…

C++使用Poco库封装一个FTP客户端类

0x00 Poco库中 Poco::Net::FTPClientSession Poco库中FTP客户端类是 Poco::Net::FTPClientSession , 该类的接口比较简单。 上传文件接口&#xff1a; beginUpload() , endUpload() 下载文件接口&#xff1a; beginDownload() , endDownload() 0x01 FTPCli类说明 FTPCli类…

CSS规则——font-face

font-face 什么是font-face&#xff1f; 想要让网页文字千变万化&#xff0c;仅靠font-family还不够&#xff0c;还要借助font-face&#xff08;是一个 CSS 规则&#xff0c;它允许你在网页上使用自定义字体&#xff0c;而不仅仅是用户系统中预装的字体。这意味着你可以通过提…

jemeter基本使用

后端关验签&#xff0c;设置请求头编码和token 配置编码和token

Linux安装minio及mc客户端(包含ARM处理器架构)

&#x1f353; 简介&#xff1a;java系列技术分享(&#x1f449;持续更新中…&#x1f525;) &#x1f353; 初衷:一起学习、一起进步、坚持不懈 &#x1f353; 如果文章内容有误与您的想法不一致,欢迎大家在评论区指正&#x1f64f; &#x1f353; 希望这篇文章对你有所帮助,欢…

练习实践:ubuntu18.04安装、配置Nginx+PHP环境,两种配置方式,多站点

参考来源&#xff1a; https://help.aliyun.com/document_detail/464753.html https://www.cnblogs.com/laosan007/p/12803287.html https://blog.csdn.net/qq_55364077/article/details/132207083 【安装同版本7.2的php】 需要知道对应php和nginx的安装版本 需要安装php-fpm…

<sa8650>QCX Usecase 使用详解—如何在 QCX 框架中添加新的自定义Usecase/Pipeline

<sa8650>QCX Usecase 使用详解—如何在 QCX 框架中添加新的自定义Usecase/Pipeline 一、前言二、为 Usecase/Pipeline 创建新文件夹三、Create Usecase XML四、为 camxAutoo_Test 管道创建拓扑五、添加Usecase/Pipeline编译六、 使用 Qcarcam_Test 应用程序运行Usecase一、前…

List of installed software and plug-ins

* Indicates lower priority real machine — install: *diskgenius *diskinfo *diskmark *IDM *zoomit *wiseprogramUnistaller *winxray *spaceSniffer vscode notepad winrar everything huorong firefox vmware inside — general: typora qbittorrent IDM zoomit x-mind…