关于chatgpt:OpenAI-函数调用-功能入门

2次阅读

共计 2467 个字符,预计需要花费 7 分钟才能阅读完成。

作者:AI 小火箭的 HB

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

范例

初步体验
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 模型能够依据用户的指令主动抉择不同的函数 / 服务。
当初再来看示例,就比拟清晰了。

用处
依据官网文档,函数调用容许您更牢靠地从模型中获取结构化数据。例如,您能够:

创立聊天机器人,通过调用内部 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 小火箭
AI 小火箭曾经反对函数调用和 gpt-3.5-turbo-16k、gpt-3.5-turbo-0613、gpt-3.5-turbo-16k-0613,大家能够去体验下。

正文完
 0