記事

functionCalling

#tech
https://zenn.dev/kazuwombat/articles/1f39f003298028

この記事を大体は理解できる

自分なりの言葉で理解したことを書いておく

OpenAIに何かしらのリクエストを送って、帰ってきた値によって、外部のAPIやシステムをCallするときにはめっちゃ便利な代物
帰ってくるときに、function_callがあれば、外部のAPIを呼び出すことができるという判定ができる

要は、OpenAIがこの返答には、外部のシステムやAPI出すことが必要かどうかを判定してくれるというのが本質かな

{
  // ...
  choices: [
    {
      index: 0,
      message: {
        role: "assistant",
        content: null,
        // 関数呼び出し
        function_call: {
          // 関数名
          name: "get_tommorow_weather",
          // 引数
          arguments: '{\n  "location": "東京",\n  "date": "2023/06/14"\n}'
        }
      },
    // 関数の呼び出しを意味する
      finish_reason: "function_call"
    }
  ]
}

-----
// 関数の呼び出しが必要か
if (response.choices[0].finish_reason === "function_call") {
  const functionCall = firstResponse.choices[0].message.function_call;

  // 引数をparse
  const { location, date } = JSON.parse(functionCall.arguments);
  // 引数を使って天気APIを実行
  const weather = await getWeather(location, date);
}

© 2026 Shotaro Kinjo