OpenAI 函数调用示例及功能入门教程

2023-12-05 0 525
目录
  • 引言
  • 范例
  • 初步体验
  • 用途
    • 将自然语言转换为 API 调用
    • 从文本中提取结构化数据

引言

我是AI小火箭的HB,我探索和写作人工智能和语言交叉点的所有事物,范围从LLM,聊天机器人,语音机器人,开发框架,以数据为中心的潜在空间等。

范例

OpenAI 函数调用示例及功能入门教程

初步体验

OpenAI新增了“函数调用”功能,这是什么呢?

我们先调用API来体验下。

下面是发送到模型的 JSON 文档。此调用的目的是生成一个 JSON 文件,该文件可用于发送到发送电子邮件的 API。您可以看到函数名称为 send_email,并定义了三个参数, to_address , subject 和 body ,即电子邮件正文。用户请求为:Send Cobus from humanfirst ai an email asking for the monthly report?

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\”
}
}
}
}
]
}\’

下面是返回的 JSON

{
\”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模型可以根据用户的指令自动选择不同的函数/服务。

现在再来看示例,就比较清晰了。

OpenAI 函数调用示例及功能入门教程

用途

根据官网文档,函数调用允许您更可靠地从模型中获取结构化数据。例如,您可以:

创建聊天机器人,通过调用外部 API 来回答问题(例如 ChatGPT 插件)

例如,定义像 send_email(to: string, body: string) 或 get_current_weather(location: string, unit: 'celsius' | 'fahrenheit') 这样的函数

将自然语言转换为 API 调用

例如,将“谁是我的顶级客户?”转换为 get_customers(min_revenue: int, created_before: string, limit: int) 并调用您的内部 API

从文本中提取结构化数据

例如,定义一个名为 extract_data(name: string, birthday: string) 或 sql_query(query: string) 的函数

函数调用的基本步骤顺序如下:

使用用户查询和函数参数中定义的一组函数调用模型。

模型可以选择调用函数;如果是这样,内容将是符合自定义架构的字符串化 JSON 对象(注意:模型可能会生成无效的 JSON 或幻觉参数)。

在代码中将字符串解析为 JSON,并使用提供的参数调用函数(如果存在)。

通过将函数响应追加为新消息来再次调用模型,并让模型将结果汇总回给用户。

AI小火箭已经支持函数调用和gpt-3.5-turbo-16k、gpt-3.5-turbo-0613、gpt-3.5-turbo-16k-0613,大家可以去体验下。

以上就是OpenAI 函数调用示例及功能入门教程的详细内容,更多关于OpenAI 函数调用的资料请关注悠久资源网其它相关文章!

您可能感兴趣的文章:

  • openaicreateChatCompletion函数使用实例
  • OPENAI API 微调 GPT-3 的 Ada 模型
  • 176万!GPT-4发布详解如何查看OpenAI的下载量
  • OpenAI发布GPT-4支持图像文本输入处理中文准确度大幅提升
  • OpenAI的Whisper模型进行语音识别使用详解
  • 人工智能深度学习OpenAI baselines的使用方法
  • OpenAI Function Calling特性作用详解

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

悠久资源 编程技巧 OpenAI 函数调用示例及功能入门教程 https://www.u-9.cn/biancheng/jiqiao/97402.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务