OpenAI新增了“函数调用”功能,这是什么呢?
我们先调用API来体验下。
curl--location'https://api.openai.com/v1/chat/completions' \
--header'Content-Type: application/json' \
--header'Authorization: Bearer sk-xxxx' \
--data'{
"model": "gpt-3.5-turbo-0613",
"messages": [
{"role": "user", "content": "Send Cobus from humanfirst ai an email asking for the monthly report?"}
],
"functions": [
{
"name": "send_email",
"description": "Please send an email.",
"parameters": {
"type": "object",
"properties": {
"to_address": {
"type": "string",
"description": "To address for email"
},
"subject": {
"type": "string",
"description": "subject of the email"
},
"body": {
"type": "string",
"description": "Body of the email"
}
}
}
}
]
}'
{
"id": "chatcmpl-7TQuwzJpQAY470saQM2RPfxwF6DDE",
"object": "chat.completion",
"created": 1687249338,
"model": "gpt-3.5-turbo-0613",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": null,
"function_call": {
"name": "send_email",
"arguments": "{\n \"to_address\": \"cobus@humanfirst.ai\",\n \"subject\": \"Request for Monthly Report\",\n \"body\": \"Hi Cobus,\\n\\nI hope you're doing well. Could you please share the monthly report with me? It would be great to have it before the end of the week.\\n\\nThanks,\\n[Your Name]\"\n}"
}
},
"finish_reason": "function_call"
}
],
"usage": {
"prompt_tokens": 86,
"completion_tokens": 82,
"total_tokens": 168
}
}
GPT模型会返回需要调用的函数名 send_email和对应的参数(放在arguments字段)。
{
"to_address": "cobus@humanfirst.ai",
"subject": "Request for Monthly Report",
"body": "Hi Cobus,\n\nI hope you're doing well. Could you please share the monthly report with me? It would be great to have it before the end of the week.\n\nThanks,\n[Your Name]"
}
这就非常有用,第三方的应用可以提供多个函数/服务(类似插件),GPT模型可以根据用户的指令自动选择不同的函数/服务。
现在再来看示例,就比较清晰了。
根据官网文档,函数调用允许您更可靠地从模型中获取结构化数据。例如,您可以:
函数调用的基本步骤顺序如下:
AI小火箭已经支持函数调用和gpt-3.5-turbo-16k、gpt-3.5-turbo-0613、gpt-3.5-turbo-16k-0613,大家可以去体验下。