函式呼叫(function calling) 可说是这次ChatGPT API 更新的杀手级更新。所谓函式呼叫,就是让你把外部函式的形状写入ChatGPT API,这样ChatGPT API 就能输出对的格式来呼叫你的函式。让我们透过官方发布的例子来了解:
假如你现在问ChatGPT 「波士顿目前天气如何」,ChatGPT 会吐给你乱回答的内容,因为它的训练资料只到2021 年。假如要有最新的资料,会需要呼叫外部的API,例如有个天气API,呼叫后可以拿到最新的天气资讯。
假设今天这个天气API 的输入格式要两个参数,一个是地点,需要是字串;第二个是单位,需要是摄氏或华氏。过去的作法是,你要在指令(prompt) 里面跟ChatGPT 说,请输出一个JSON 字串,里面要包含地点与单位,这两个栏位。但这时就有个问题,即使你有下这个指令,ChatGPT 的输出仍可能会不如你预期。最常见的状况是,它会回「好的没问题,以下是JSON 字串的输出」这个开头的回复
💣 然后这时候就炸掉了,因为当你把「好的没问题,以下是JSON 字串的输出」输入到天气API,天气API 会没办法处理。这是过去要做ChatGPT 应用程式会遇到的一个痛点。要能有效整合,就需要有对的格式。格式错误将可能让程式运行出问题,弄不好整组炸掉就悲剧了。
函式呼叫这功能就是让ChatGPT API 能用对的格式跟外部工具互动。今天改成用函式呼叫的功能,ChatGPT API 会依据你定义好的格式,给出对的输出。以这边为例,它不会再用「好的没问题,以下是JSON 字串的输出」开头。而是会直接输出一个JSON 字串。这时你可以拿去打天气API。
import requests
import os
import json
url = "https://api.openai.com/v1/chat/completions"
auth = ('', os.getenv('OPENAI_API_KEY'))
headers = {
"Content-Type": "application/json",
}
data = {
"model": "gpt-3.5-turbo-0613",
"messages": [
{"role": "user", "content": "What is the weather like in Boston?"}
],
"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"]
}
}
]
}
response = requests.post(url, auth=auth, headers=headers, data=json.dumps(data))
print(response.text)
这时会拿到
{
"id": "chatcmpl-123",
...
"choices": [{
"index": 0,
"message": {
"role": "assistant",
"content": null,
"function_call": {
"name": "get_current_weather",
"arguments": "{ \"location\": \"Boston, MA\"}"
}
},
"finish_reason": "function_call"
}]
}
打完天气API,你拿到波士顿:22 度、单位:摄氏,接着再喂回去给ChatGPT API,这时ChatGPT API 就能精准回覆「波士顿现在的天气是摄氏22 度」
把上面的结果拿去呼叫天气API,会拿到
import requests
import os
import json
url = "https://api.openai.com/v1/chat/completions"
auth = ('', os.getenv('OPENAI_API_KEY'))
headers = {
"Content-Type": "application/json",
}
data = {
"model": "gpt-3.5-turbo-0613",
"messages": [
{"role": "user", "content": "What is the weather like in Boston?"},
{"role": "assistant", "content": None, "function_call": {"name": "get_current_weather", "arguments": "{ \"location\": \"Boston, MA\"}"}},
{"role": "function", "name": "get_current_weather", "content": "{\"temperature\": \"22\", \"unit\": \"celsius\", \"description\": \"Sunny\"}"}
],
"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"]
}
}
]
}
response = requests.post(url, auth=auth, headers=headers, data=json.dumps(data))
print(response.text)
最后拿到的就是正确的资料,同时又是自然语言
{
"id": "chatcmpl-123",
...
"choices": [{
"index": 0,
"message": {
"role": "assistant",
"content": "The weather in Boston is currently sunny with a temperature of 22 degrees Celsius.",
},
"finish_reason": "stop"
}]
}
在了解完函式呼叫功能后,大概可以更理解OpenAI 的未来策略,如同执行长Sam Altman 先前提到的,不会是再去做ChatGPT 这种应用;而是会以平台的角度出发。函式呼叫能让外部工具更好与ChatGPT API 整合,这会让ChatGPT 这个平台变得更有价值。