Function calling 介绍
现在,开发者可以向 gpt-4-0613
和 gpt-3.5-turbo-0613
描述函数,并让模型智能地选择输出一个包含调用这些函数所需参数的 JSON 对象。 这是一种更可靠地将 GPT 的能力与外部工具和 API 连接起来的新方式。

创建通过调用外部工具(例如 ChatGPT 插件)回答问题的聊天机器人。
将查询如“给 Anaya 发电子邮件,看看她是否想在下周五喝咖啡”转换为函数调用,如send_email(to: string, body: string)
,或者“波士顿的天气如何?”转换为 get_current_weather(location: string, unit: 'celsius' | 'fahrenheit')
。
将自然语言转换为 API 调用或数据库查询。
将“本月我的前十位客户是谁?”转换为内部 API 调用,例如 get_customers_by_revenue(start_date: string, end_date: string, limit: int)
,或者“上个月 Acme 公司下了多少订单?”转换为使用 sql_query(query: string)
的 SQL 查询。
从文本中提取结构化数据。
定义一个名为 extract_people_data(people: [{name: string, birthday: string, location: string}])
的函数,以提取在维基百科文章中提到的所有人。
这些用例是通过我们 /v1/chat/completions
端点中的新 API 参数 functions
和 function_call
实现的,允许开发者通过 JSON Schema 向模型描述函数,并选择性地要求它调用特定的函数。在我们的开发者文档中开始,并且如果您发现函数调用可以改进的情况,请添加到 evals。
Function calling 案例
获取天气 (CURL方式)
通过CURL调用 只有一个参数传递
curl https://api.openai.com/v1/chat/completions -u :$OPENAI_API_KEY -H 'Content-Type: application/json' -d '{
"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"]
}
}
]
}'
返回结果为:
get_current_weather("Boston")
判断用户之间的关系(Python Requests)
Python HTTP调用 有两个参数传递
import requests
import json
url = "请求地址"
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer API Key'
}
data = {
"model": "gpt-3.5-turbo-0613",
"messages": [
{"role": "user", "content": "李华和小王是不是认识?"},
],
"functions": [
{
"name": "get_connection",
"description": "判断用户1和用户2 是否为朋友关系",
"parameters": {
"type": "object",
"properties": {
"user_id1": {
"type": "string",
"description": "用户ID 1"
},
"user_id2": {
"type": "string",
"description": "用户ID 2"
},
},
"required": ["user_id1", "user_id2"]
}
}
]
}
data = requests.post(url, headers=headers, json=data).json()
data
返回结果为:
get_connection("李华", "小王")
获取linux执行命令 (API)
import openai
import json
openai.api_base = '访问地址'
openai.api_key = 'key'
example_user_input = "in linux, to install cuda, cudnn and pytorch"
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo-0613",
messages=[{"role": "user", "content": example_user_input}],
functions=[
{
"name": "get_commands",
"description": "Get a list of bash commands on an Ubuntu machine",
"parameters": {
"type": "object",
"properties": {
"commands": {
"type": "array",
"items": {
"type": "string",
"description": "A terminal command string"
},
"description": "List of terminal command strings to be executed"
}
},
"required": ["commands"]
}
}
],
function_call={"name": "get_commands"},
)
reply_content = completion.choices[0].message
reply_content
返回结果为:
get_commands(
["sudo apt-get update", "sudo apt-get install nvidia-cuda-toolkit", "sudo apt-get install libcudnn8", "pip install torch torchvision"]
)
注意事项
Function calling暂时只能搭配
gpt-4-0613
和gpt-3.5-turbo-0613
两个模型进行使用,并且GPT4的效果会更好。当通过英文进提问时, Function calling的效果更好。因为 Function calling本身也是通过大模型进行预测的。
通过API访问时候, Function calling效果更好,也更加容易生成函数调用结果,而不是纯对话内容。
在对话时可以传入多个待选的函数,GPT会选择其中一个,并生成其对应的参数。
GPT可以为一个函数生成对应的多个传参,但无法同时生成多个函数调用逻辑。
Function calling比较适合结合函数 & 参数定义(数值、浮点数、布尔型、日期)生成调用,并不擅长为list参数生成具体的取值。
参考链接
https://openai.com/blog/function-calling-and-other-api-updates
https://github.com/Sentdex/ChatGPT-API-Basics/blob/main/function_calling.ipynb






