白话理解 ChatGPT API 的函式呼叫功能 (function calling)

2023年6月14日

🚀 ChatGPT 求職工作術
想透過 ChatGPT 提升求職與工作效率嗎?《ChatGPT 高效求職與工作術》限時 46 折優惠中👉 前往上課

函式呼叫 (function calling) 可说是这次 ChatGPT API 更新的杀手级更新。所谓函式呼叫,就是让你把外部函式的形状写入 ChatGPT API,这样 ChatGPT API 就能输出对的格式来呼叫你的函式。让我们透过官方发布的例子来了解:

1️⃣ 假如你现在问 ChatGPT 「波士顿目前天气如何」,ChatGPT 会吐给你乱回答的内容,因为它的训练资料只到 2021 年。假如要有最新的资料,会需要呼叫外部的 API,例如有个天气 API,呼叫后可以拿到最新的天气资讯。

2️⃣ 假设今天这个天气 API 的输入格式要两个参数,一个是地点,需要是字串;第二个是单位,需要是摄氏或华氏。过去的作法是,你要在指令 (prompt) 里面跟 ChatGPT 说,请输出一个 JSON 字串,里面要包含地点与单位,这两个栏位。但这时就有个问题,即使你有下这个指令,ChatGPT 的输出仍可能会不如你预期。最常见的状况是,它会回「好的没问题,以下是 JSON 字串的输出」这个开头的回覆

💣 然后这时候就炸掉了,因为当你把 「好的没问题,以下是 JSON 字串的输出」输入到天气 API,天气 API 会没办法处理。这是过去要做 ChatGPT 应用程式会遇到的一个痛点。要能有效整合,就需要有对的格式。格式错误将可能让程式运行出问题,弄不好整组炸掉就悲剧了。

3️⃣ 函式呼叫这功能就是让 ChatGPT API 能用对的格式跟外部工具互动。今天改成用函式呼叫的功能,ChatGPT API 会依据你定义好的格式,给出对的输出。以这边为例,它不会再用「好的没问题,以下是 JSON 字串的输出」开头。而是会直接输出一个 JSON 字串。这时你可以拿去打天气 API。

举例来说,带有函式呼叫的,会带上 functions

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"]
      }
    }
  ]
}'

这时会拿到

{
  "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"
  }]
}

4️⃣ 打完天气 API,你拿到波士顿:22 度、单位:摄氏,接着再喂回去给 ChatGPT API,这时 ChatGPT API 就能精准回覆「波士顿现在的天气是摄氏 22 度」

把上面的结果拿去呼叫天气 API,会拿到

{ "temperature": 22, "unit": "celsius", "description": "Sunny" }

接着再拿去打 ChatGPT API

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?"},
    {"role": "assistant", "content": null, "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"]
      }
    }
  ]
}'

最后拿到的就是正确的资料,同时又是自然语言

{
  "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 这个平台变得更有价值。因为身为企业或开发者,假如今天 Anthropic Claude 的成果追上 ChatGPT 了,企业与开发者会选能有更好整合性的。因此,我们对于 OpenAI 的作为平台的发展仍是相当乐观。

🧵 如果你想收到最即時的內容更新,可以在 FacebookInstagram 上追蹤我們