概要
Function calling が OpenAI や Azure で利用できる。
サンプルで Function のパラメータを定義していたがリスト(配列)の扱い方が分からなかったので調べた。
List, Array
結論
JSON Schema を利用しているらしい。
以下を参考にして設定可能
https://json-schema.org/understanding-json-schema/reference/array.html
サンプルスクリプト
properties で type を array
にしている
import openai
functions = [
{
"name": "search_recipe",
"description": "指定された材料でレシピを検索する",
"parameters": {
"type": "object",
"properties": {
"ingredients": {"type": "array", "items": {"type": "string"}},
},
"required": ["ingredients"],
},
}
]
response = openai.ChatCompletion.create(
deployment_id="gpt-35-turbo",
messages=[{"role": "user", "content": "次の食材でレシピを考えて\n玉ねぎ\n人参\nじゃがいも"}],
functions=functions,
function_call="auto",
)
print(response["choices"][0]["message"]["function_call"]["name"])
print(response["choices"][0]["message"]["function_call"]["arguments"])
実行結果。パラメータをリストで受け取れている。
search_recipe
{
"ingredients": ["玉ねぎ", "人参", "じゃがいも"]
}