function_call APIのパラメータタイプについて、もっと調べてみた

OpenAIのAI、function_callのパラメータについて、もうちょっと調べてみました。
試してみた結果、type以外の細かいことは、descriptionに書けということです。

JSONスキーマっての見たら、もうちょっと細かい指定ができるんではないか?と思って、 https://json-schema.org/

まずは、minLengthとmaxLengthを試してみた。 [https://json-schema.org/understanding-json-schema/reference/string.html#id5]

import openai
import json

def submit():
    messages = [{"role": "user", "content": "test the sample_function"}]
    functions = [
        {
            "name": "sample_function",
            "description": "test for parameters",
            "parameters": {
                "type": "object",
                "properties": {
                    "arg1": {
                        "type": "string",
                        "minLength": 1,
                        "maxLength": 2,
                        "description": "string data",
                    }
                },
                "required": ["arg1"],
            },
        }
    ]
    try:
        response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo-0613",
            messages=messages,
            functions=functions,
            function_call="auto",
        )
        print( response.choices[0] )
    except Exception as ex:
        return ex.args

submit()

実行結果

$ python3 test.py 
{
  "finish_reason": "function_call",
  "index": 0,
  "message": {
    "content": null,
    "function_call": {
      "arguments": "{\n  \"arg1\": \"Hello World\"\n}",
      "name": "sample_function"
    },
    "role": "assistant"
  }
}

minLengthとmaxLengthはあっさり無視されるらしい。

次は、[https://json-schema.org/understanding-json-schema/reference/string.html#id6](Regular Expressions)
これが使えたら、結構うれしいんじゃないか?

                "properties": {
                    "arg1": {
                        "type": "string",
                        "pattern": "^(\\([0-9]{3}\\))?[0-9]{3}-[0-9]{4}$",
                        "description": "string data",
                    }
                },

実行結果

$ python3 test.py 
{
  "finish_reason": "function_call",
  "index": 0,
  "message": {
    "content": null,
    "function_call": {
      "arguments": "{\n  \"arg1\": \"Hello World\"\n}",
      "name": "sample_function"
    },
    "role": "assistant"
  }
}

やっぱり無視されました。ですよね〜〜〜〜。

もしかして type に numberを指定しても無視するんじゃないか?   と思って

import openai
import json

def submit():
    messages = [{"role": "user", "content": "generate test data for sample_function. and test it."}]
    functions = [
        {
            "name": "sample_function",
            "description": "test for parameters",
            "parameters": {
                "type": "object",
                "properties": {
                    "arg1": {
                        "type": "number",
                        "description": "",
                    }
                },
                "required": ["arg1"],
            },
        }
    ]
    try:
        response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo-0613",
            messages=messages,
            functions=functions,
            function_call="auto",
        )
        print( response.choices[0] )
    except Exception as ex:
        return ex.args

submit()

実行結果

{
  "finish_reason": "stop",
  "index": 0,
  "message": {
    "content": "To generate test data for `sample_function`, I need to know the expected output or the desired behavior of the function. Please provide more information about the expected behavior, any constraints on the input, and the desired output format.",
    "role": "assistant"
  }
}

うーむ。functoinの仕様を明確にしろってか??? なら、これでどうだ?

            "name": "sample_function",
            "description": "return tha input data, just sample",
            "parameters": {
                "type": "object",
                "properties": {
                    "arg1": {
                        "type": "number",
                        "description": "",
                    }
                },
                "required": ["arg1"],
            },

実行結果

{
  "finish_reason": "function_call",
  "index": 0,
  "message": {
    "content": null,
    "function_call": {
      "arguments": "{\n  \"arg1\": 5\n}",
      "name": "sample_function"
    },
    "role": "assistant"
  }
}

おお! 数字ってのは守ってくれるらしい。
じゃあ、範囲指定はどうか?

                    "arg1": {
                        "type": "number",
                        "minimum": 100,
                        "maximum": 200,
                        "description": "",
                    }

実行結果

{
  "finish_reason": "function_call",
  "index": 0,
  "message": {
    "content": null,
    "function_call": {
      "arguments": "{\n  \"arg1\": 10\n}",
      "name": "sample_function"
    },
    "role": "assistant"
  }
}

うむ、やっぱり範囲指定は無視するらしい。
つまり以下のようにdescriptionに書けば・・・・

                    "arg1": {
                        "type": "number",
                        "description": "range 100 to 200",
                    }

実行結果

{
  "finish_reason": "function_call",
  "index": 0,
  "message": {
    "content": null,
    "function_call": {
      "arguments": "{\n  \"arg1\": 150\n}",
      "name": "sample_function"
    },
    "role": "assistant"
  }
}

ちゃんと守ってくれるらしい。

Built with Hugo
テーマ StackJimmy によって設計されています。