5
4

More than 1 year has passed since last update.

Function calling でリスト(配列)のパラメータを定義する

Posted at

概要

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": ["玉ねぎ", "人参", "じゃがいも"]
}
5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4