耗时3天整理,一篇文章学会OpenAI大杀器-Function call

耗时3天整理,一篇文章学会OpenAI大杀器-Function call  第1张


1、让ChatGPT帮我买张火车票


想象下,你创建一个人工智能助手,你可以这样对他说:“帮我买北京的火车票,提前一天提醒我”!如果是ChatGPT的话,它必然会无情地拒绝你。


耗时3天整理,一篇文章学会OpenAI大杀器-Function call  第2张


ChatGPT是世界上最强大的模型,不过,它虽然知道你想让它帮你买票,但它却不懂如何买票,它能力的上限就摆在那儿了。好在OpenAI在GPT模型引入了一个强大的功能--函数调用(function call)。


相信有些小伙伴应该已经用过ChatGPT的plugins功能,Plugins的功能有不少是基于function call进行的


今天,我们一起来创建一个获取最新新闻的GPT,给大家展示如何使用function call,用于深入理解函数调用的概念以及它给我们带来的可能性。


2、函数调用(function call)是啥?


函数调用(function call)是OpenAIGPT-4-0613和GPT-3.5 Turbo-0613模型支持的功能,这两个模型经过训练,可以根据用户的提示检测需不需要调用用户提供的函数,并且用一个很规范的结构返回,而不是直接返回常规的文本。


函数调用(function)允许ChatGPT和其他的系统进行信息的交互,让ChatGPT回答他们原本无法回答的问题。例如,我们需要查询实时的天气,这些数据是ChatGPT没有的,所以ChatGPT需要从别的平台获取最新的天气情况。换句话就是说,函数调用,就是提供了一种方式,教AI模型怎么样和外面的系统进行交互。


3、函数调用(function-call)目的是什么?


函数调用,可以用来增强GPT模型的功能,让GPT能做到更多事情。其实还有两种方式可以增强GPT模型的能力。


  • 微调: 提供标注数据进一步训练模型,不过微调需要很多时间和精力准备训练数据。嵌入:构建机器人知识库,通过构建上下文和知识库的内容进行关联,从而让GPT获得更丰富的回答。

函数调用是第三种扩展GPT功能的方式,这种方式和其他两种不一样,它可以让我们和外部的系统进行交互!


4、怎么样使用函数调用?


在有函数调用(funciton-call)时候,我们调用GPT构建AI应用的模式非常简单。主要有几个步骤


1、用户(client)发请求给我们的服务(chat server)


2、我们的服务(chat server)给GPT提示词


3、重复执行



耗时3天整理,一篇文章学会OpenAI大杀器-Function call  第3张


有了函数调用(function-call),调用的方式就比之前的复杂一些了,具体的步骤为


1、发送用户的提示词以及可以调用的函数


2、GPT模型根据用户的提示词,判断是用普通文本还是函数调用的格式响应


3、如果是函数调用格式,那么Chat Server就会执行这个函数,并且将结果返回给GPT


4、然后模型使用提供的数据,用连贯的文本响应。


耗时3天整理,一篇文章学会OpenAI大杀器-Function call  第4张


5、用API完成函数调用


在调用GPT接口时,我们一般使用的是Completions接口,这个接口发送的是POST请求,发送的数据格式如下图所示


{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "What's weather like today?"
}
]
}


GPT返回的可能是下面这些内容


{
"id": "chatcmpl-FWVo3hYwjrpAptzepU46JamvvgBzb",
"object": "chat.completion",
"created": 1687983115,
"model": "gpt-3.5-turbo-0613",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "I'm sorry, but I don't have access to real-time information, including current weather conditions or forecasts. To find out the weather for your location today, I recommend using a weather website, app, or a voice-activated assistant like Siri, Google Assistant, or Alexa. Simply ask one of these services for the weather in your area, and they should be able to provide you with up-to-date information."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 15,
"completion_tokens": 44,
"total_tokens": 59
}
}


在这里,为了记录上下文,我们需要在每个请求上,将整个消息历史记录返回给 API。例如,如果我们想继续讨论之前的问题,那么相应的 JSON 应该将是:


{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "How many planets does the solar system have?"
},
{
"role": "assistant",
"content": "I'm sorry, but I don't have access to real-time information, including current weather conditions or forecasts. To find out the weather for your location today, I recommend using a weather website, app, or a voice-activated assistant like Siri, Google Assistant, or Alexa. Simply ask one of these services for the weather in your area, and they should be able to provide you with up-to-date information."
{
"role": "user",
"content": "how do I access these apps?"
}
]
}


在上面的例子中,我们明确知道了,ChatGPT查不了实时信息,接下来,我们会加上function call,让ChatGPT可以查询实时信息。


{
"model": "gpt-3.5-turbo-0613",
"messages": [
{
"role": "user",
"content": "How is the weather in NYC?"
}
],
"functions": [
{
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
},
"unit": {
"type": "string",
"enum": [
"celsius",
"fahrenheit"
]
}
},
"required": [
"location"
]
}
}
]
}


当GPT模型决定调用我们提供的函数,那么我们就会收到下面类似的返回


{
"id": "chatcmpl-7WWG94C1DCFlAk5xmUwrZ9OOhFnOq",
"object": "chat.completion",
"created": 1687984857,
"model": "gpt-3.5-turbo-0613",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": null,
"function_call": {
"name": "get_current_weather",
"arguments": "{\n \"location\": \"New York, NY\"\n}"
}
},
"finish_reason": "function_call"
}
],
"usage": {
"prompt_tokens": 81,
"completion_tokens": 19,
"total_tokens": 100
}
}


get_current_weather将会使用返回的参数调用。OpenAI不执行该函数,我们的服务会执行这个函数,并且获取结果后解析返回给OpenAI。


一旦我们检索到天气数据,我们就会使用一种名为 的新的角色将其发送回模型function。例如:


{
"model":"gpt-3.5-turbo-0613",
"messages":[
{
"role":"user",
"content":"How is the weather in NYC?"
},
{
"role":"assistant",
"content":null,
"function_call":{
"name":"get_current_weather",
"arguments":"{\n \"location\": \"New York, NY\"\n}"
}
},
{
"role":"function",
"name":"get_current_weather",
"content":"Temperature: 57F, Condition: Raining"
}
],
"functions":[
{
"name":"get_current_weather",
"description":"Get the current weather in a given location",
"parameters":{
"type":"object",
"properties":{
"location":{
"type":"string",
"description":"The city and state, e.g. San Francisco, CA"
},
"unit":{
"type":"string",
"enum":[
"celsius",
"fahrenheit"
]
}
},
"required":[
"location"
]
}
}
]
}


这里注意下,我们将整个消息历史记录传递给了 API,包括原始提示词、模型的函数调用以及代码中执行天气函数的结果,这种方式可以让模型能够理解调用函数的上下文。


最后,模型可能会回复一个格式正确的答案,回答我们最初的问题:


{
"id": "chatcmpl-7WWQUccvLUfjhbIcuvFrj2MDJVEiN",
"object": "chat.completion",
"created": 1687985498,
"model": "gpt-3.5-turbo-0613",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "The weather in New York City is currently raining with a temperature of 57 degrees Fahrenheit."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 119,
"completion_tokens": 19,
"total_tokens": 138
}
}


以上,就是Function Call在调用过程中交互数据的格式,接下来,我们使用实际的例子,使用python开发function call的简单应用。


5.1 新闻机器人


为了实现这个功能,我们需要OpenAI 的key,key的获取可以在https://platform.openai.com/account/api-k

eys

获取key之后,我们需要安装一些python依赖包


pip install openai tiktoken


我们需要导入一些依赖库


import openai
import tiktoken
import json
import os
import requests


接下来,将定义几个常量:


  • 指定 GPT 模型。我们将使用gpt-3.5-turbo-16k我们预设的提示词。用于对字符串和消息中的标记进行计数的编码;需要确保我们不超过语言模型的限制。调用的函数的最大数量openai.api_key 从openai平台获取的keyzsxq_cookie 知识星球的cookie

llm_model = "gpt-3.5-turbo-16k"
llm_max_tokens = 15500
llm_system_prompt = "You are an assistant that provides news and headlines to user requests. Always try to get the lastest breaking stories using the available function calls."
encoding_model_messages = "gpt-3.5-turbo-0613"
encoding_model_strings = "cl100k_base"
function_call_limit = 3
openai.api_key = "sk-xxx" # 需要设置
zsxq_cookie="zsxq_access_token=xxx" # 需要设置
news_key = "" # 新闻key


所有 GPT模型都有token限制。如果超过此限制,API 将抛出错误而不是响应我们的请求。因此,我们需要一个函数来计算token的数量。


{
"id": "chatcmpl-FWVo3hYwjrpAptzepU46JamvvgBzb",
"object": "chat.completion",
"created": 1687983115,
"model": "gpt-3.5-turbo-0613",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "I'm sorry, but I don't have access to real-time information, including current weather conditions or forecasts. To find out the weather for your location today, I recommend using a weather website, app, or a voice-activated assistant like Siri, Google Assistant, or Alexa. Simply ask one of these services for the weather in your area, and they should be able to provide you with up-to-date information."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 15,
"completion_tokens": 44,
"total_tokens": 59
}
}
0


现在我们需要有一个函数来获取新闻,我们可以在https://newsapi.org/获取查询新闻的KEY


{
"id": "chatcmpl-FWVo3hYwjrpAptzepU46JamvvgBzb",
"object": "chat.completion",
"created": 1687983115,
"model": "gpt-3.5-turbo-0613",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "I'm sorry, but I don't have access to real-time information, including current weather conditions or forecasts. To find out the weather for your location today, I recommend using a weather website, app, or a voice-activated assistant like Siri, Google Assistant, or Alexa. Simply ask one of these services for the weather in your area, and they should be able to provide you with up-to-date information."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 15,
"completion_tokens": 44,
"total_tokens": 59
}
}
1


为了让GPT模型知道我们存在get_top_headlines函数可以调用,我们需要用JSON结构描述我们的函数


{
"id": "chatcmpl-FWVo3hYwjrpAptzepU46JamvvgBzb",
"object": "chat.completion",
"created": 1687983115,
"model": "gpt-3.5-turbo-0613",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "I'm sorry, but I don't have access to real-time information, including current weather conditions or forecasts. To find out the weather for your location today, I recommend using a weather website, app, or a voice-activated assistant like Siri, Google Assistant, or Alexa. Simply ask one of these services for the weather in your area, and they should be able to provide you with up-to-date information."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 15,
"completion_tokens": 44,
"total_tokens": 59
}
}
2


接下来,我们将定义complete函数,执行和GPT大模型交互的任务,主要步骤为:


  1. 在消息末尾添加系统提示。这个用于添加消息的上下文如果token总数超过模型的限制,则删除旧消息。将请求发送到 GPT API。从列表末尾删除系统消息


{
"id": "chatcmpl-FWVo3hYwjrpAptzepU46JamvvgBzb",
"object": "chat.completion",
"created": 1687983115,
"model": "gpt-3.5-turbo-0613",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "I'm sorry, but I don't have access to real-time information, including current weather conditions or forecasts. To find out the weather for your location today, I recommend using a weather website, app, or a voice-activated assistant like Siri, Google Assistant, or Alexa. Simply ask one of these services for the weather in your area, and they should be able to provide you with up-to-date information."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 15,
"completion_tokens": 44,
"total_tokens": 59
}
}
3


最后,我们添加一个Run函数,循环接受我们的请求发送给GPT API


{
"id": "chatcmpl-FWVo3hYwjrpAptzepU46JamvvgBzb",
"object": "chat.completion",
"created": 1687983115,
"model": "gpt-3.5-turbo-0613",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "I'm sorry, but I don't have access to real-time information, including current weather conditions or forecasts. To find out the weather for your location today, I recommend using a weather website, app, or a voice-activated assistant like Siri, Google Assistant, or Alexa. Simply ask one of these services for the weather in your area, and they should be able to provide you with up-to-date information."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 15,
"completion_tokens": 44,
"total_tokens": 59
}
}
4


最后,我们可以运行我们的程序进行测试了,要注意,运行这个python程序的电脑一定要在国外,另外一定要设置相应的key。国内的电脑无法调用GPT模型的API


{
"id": "chatcmpl-FWVo3hYwjrpAptzepU46JamvvgBzb",
"object": "chat.completion",
"created": 1687983115,
"model": "gpt-3.5-turbo-0613",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "I'm sorry, but I don't have access to real-time information, including current weather conditions or forecasts. To find out the weather for your location today, I recommend using a weather website, app, or a voice-activated assistant like Siri, Google Assistant, or Alexa. Simply ask one of these services for the weather in your area, and they should be able to provide you with up-to-date information."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 15,
"completion_tokens": 44,
"total_tokens": 59
}
}
5

耗时3天整理,一篇文章学会OpenAI大杀器-Function call  第5张


5.2 破局精华贴机器人


在前面,我们已经添加了查询新闻的功能了,在这里,我们再加个简单的功能。让GPT模型帮我们查找知识星球的精华贴。


在前面我们应该都已经知道了,增加函数,我们需要添加一个函数描述,在这里我们只需要在原来的代码基础上添加即可


{
"id": "chatcmpl-FWVo3hYwjrpAptzepU46JamvvgBzb",
"object": "chat.completion",
"created": 1687983115,
"model": "gpt-3.5-turbo-0613",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "I'm sorry, but I don't have access to real-time information, including current weather conditions or forecasts. To find out the weather for your location today, I recommend using a weather website, app, or a voice-activated assistant like Siri, Google Assistant, or Alexa. Simply ask one of these services for the weather in your area, and they should be able to provide you with up-to-date information."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 15,
"completion_tokens": 44,
"total_tokens": 59
}
}
6


然后,我们再实现我们的get_zsxq_article接口,获取星球的精华文章


{
"id": "chatcmpl-FWVo3hYwjrpAptzepU46JamvvgBzb",
"object": "chat.completion",
"created": 1687983115,
"model": "gpt-3.5-turbo-0613",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "I'm sorry, but I don't have access to real-time information, including current weather conditions or forecasts. To find out the weather for your location today, I recommend using a weather website, app, or a voice-activated assistant like Siri, Google Assistant, or Alexa. Simply ask one of these services for the weather in your area, and they should be able to provide you with up-to-date information."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 15,
"completion_tokens": 44,
"total_tokens": 59
}
}
7


在这里,我们还有个关键点,要设置get_zsxq_article的cookie,这个步骤也很简单,我们下载一个谷歌浏览器,然后打开知识星球的网页版本,按F12


耗时3天整理,一篇文章学会OpenAI大杀器-Function call  第6张


在弹出右侧的框后,我们在刷新下知识星球的网页,然后按照红框中的指示,找到cookie即可。


耗时3天整理,一篇文章学会OpenAI大杀器-Function call  第7张


在获取cookie,填充到我们的代码后,我们可以运行,看看效果


耗时3天整理,一篇文章学会OpenAI大杀器-Function call  第8张


到这里,我们的实战演示就结束了。


那么,学会了function call,我们还有哪些东西可以做呢?


比如说,做一个简历润色机器人?或者做一个文章归纳小助手?


发挥你们的想象力吧!


文章来自于知乎 “小潘”,作者 “小潘”


耗时3天整理,一篇文章学会OpenAI大杀器-Function call  第9张