白話理解 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 上追蹤我們