🙆‍♀️

【GPT-5】- Free-Form Function Callingとは?

に公開

執筆日

2025/8/9

Free-Form Function Callingとは?

関数呼び出し時にJSON形式でラップせず、テキストそのまま(生のペイロード)を送れる機能らしい。
どういう意味?ワカラン..

検証してみる

サンプルコード

main.py
from openai import AzureOpenAI

endpoint = "Azure AI Foundryのエンドポイント"
subscription_key = "Azure AI Foundryのキー"
api_version = "2025-03-01-preview"

client = AzureOpenAI(
    api_version=api_version,
    azure_endpoint=endpoint,
    api_key=subscription_key,
)

response = client.responses.create(
    model="gpt-5-mini",
    input="Please use the code_exec tool to calculate the area of a circle with radius equal to the number of 'r's in strawberry",
    text={"format": {"type": "text"}},
    tools=[
        {
            "type": "custom",
            "name": "code_exec",
            "description": "Executes arbitrary python code",
        }
    ]
)
print(response.output)

出力結果

[ResponseReasoningItem(id='rs_68969ceb120881a0b2057e9aada1b17a011ab93e95d285b6', summary=[], type='reasoning', encrypted_content=None, status=None), ResponseOutputMessage(id='ctc_68969cf152e081a08e72859a92874700011ab93e95d285b6', content=None, role=None, status='completed', type='custom_tool_call', call_id='call_zHyGawc5R9gdxUUtxSTCf7y6', input='r = "strawberry".count(\'r\')\nimport math\narea = math.pi * r**2\n# Return radius and area\nr, area\n', name='code_exec')]

ResponseOutputMessageのinputがプロンプトの返答になっているな。

input='r = "strawberry".count('r')\nimport math\narea = math.pi * r**2\n# Return radius and area\nr, area\n', name='code_exec')

C#にしてみる

main.py
from openai import AzureOpenAI

endpoint = "Azure AI Foundryのエンドポイント"
subscription_key = "Azure AI Foundryのキー"
api_version = "2025-03-01-preview"

client = AzureOpenAI(
    api_version=api_version,
    azure_endpoint=endpoint,
    api_key=subscription_key,
)

response = client.responses.create(
    model="gpt-5-mini",
    input="Please use the code_exec tool to calculate the area of a circle with radius equal to the number of 'r's in strawberry",
    text={"format": {"type": "text"}},
    tools=[
        {
            "type": "custom",
            "name": "code_exec",
            "description": "Executes arbitrary C# code",#ここを修正
        }
    ]
)
client = AzureOpenAI(
    api_version=api_version,
    azure_endpoint=endpoint,
    api_key=subscription_key,
)

response = client.responses.create(
    model="gpt-5-mini",
    input="Please use the code_exec tool to calculate the area of a circle with radius equal to the number of 'r's in strawberry",
    text={"format": {"type": "text"}},
    tools=[
        {
            "type": "custom",
            "name": "code_exec",
            "description": "Executes arbitrary C# code",
        }
    ]
)
for item in response.output:
    if hasattr(item, 'type') and item.type == 'custom_tool_call':
        print("===Code ===")
        print(item.input)
        print("======================")

出力結果

=== Code ===
using System;
using System.Linq;

string word = "strawberry";
int rCount = word.Count(c => c == 'r');
double radius = rCount;
double area = Math.PI * radius * radius;
Console.WriteLine(rCount);
Console.WriteLine(area);
======================

C#になっているなー。

SQLにしてみる

main.py
from openai import AzureOpenAI

endpoint = "Azure AI Foundryのエンドポイント"
subscription_key = "Azure AI Foundryのキー"
api_version = "2025-03-01-preview"

client = AzureOpenAI(
    api_version=api_version,
    azure_endpoint=endpoint,
    api_key=subscription_key,
)

response = client.responses.create(
    model="gpt-5-mini",
    input="Please use the code_exec tool to query the SQL Server database and return all rows from the Customers table where Country is 'Japan'.",
    text={"format": {"type": "text"}},
    tools=[
    {
        "type": "custom",
        "name": "sql_exec",
        "description": "Executes SQL Server queries to inspect table structure and data",
    }
    ]
)
for item in response.output:
    if hasattr(item, 'type') and item.type == 'custom_tool_call':
        print("=== Code ===")
        print(item.input)
        print("======================")

出力結果

=== Code ===
SELECT * FROM Customers WHERE Country = 'Japan';

======================

要は、

コードやコマンドをそのまま出力できるのかー。後段の処理で生のテキストの方がいい場合には便利だなー。

ヘッドウォータース

Discussion