背景
OpenAI のAPIドキュメント にもFunction Callingの簡単な説明がありましたが、簡易的すぎるので、もう少し詳細に説明した How_to_call_functions_with_chat_models を読んでいきます。
Function Callingとは
The purpose of this is to enable models to generate function arguments which adhere to the provided specifications.
関数の呼び出しの引数の生成を目的とした機能です
Basic Concept
まずsystem ロールに以下のプロンプトを定義しています。 これによって、GPT側で勝手に引数を推測することを禁止し、ユーザーに定義してもらうようにしています。
{"role": "system", "content": "Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous."}
functions = [
{
"name": "get_current_weather",
"description": "Get the current weather",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"format": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "The temperature unit to use. Infer this from the users location.",
},
},
"required": ["location", "format"],
},
},
{
"name": "get_n_day_weather_forecast",
"description": "Get an N-day weather forecast",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"format": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "The temperature unit to use. Infer this from the users location.",
},
"num_days": {
"type": "integer",
"description": "The number of days to forecast",
}
},
"required": ["location", "format", "num_days"]
},
},
]
このような関数を定義してchat_completionを呼び出します。
例えば
messages = []
messages.append({"role": "system", "content": "Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous."})
messages.append({"role": "user", "content": "What's the weather like today"})
chat_response = chat_completion_request(
messages, functions=functions
)
assistant_message = chat_response.json()["choices"][0]["message"]
messages.append(assistant_message)
assistant_message
と呼び出すと、 In which city and state would you like to know the current weather?
と返ってきます。つまり、関数呼び出しに必要な情報が集まるまで、chatGPTは情報を集めようと会話を続けようとしてくれます。