最終更新:2025-07-31 (木) 09:15:22 (405d)  

Function Calling
Top / Function Calling

2023/06/13

関数を定義して渡すとOpenAI/APIが関数を呼び出す必要があるタイミング (ユーザーの入力に応じて) を検出し、関数シグネチャに準拠した JSON で応答

https://openai.com/index/function-calling-and-other-api-updates/

https://platform.openai.com/docs/guides/function-calling

  • from openai import OpenAI
    
    client = OpenAI()
    
    tools = [{
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get current temperature for a given location.",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "City and country e.g. Bogotá, Colombia"
                    }
                },
                "required": [
                    "location"
                ],
                "additionalProperties": False
            },
            "strict": True
        }
    }]
    
    completion = client.chat.completions.create(
        model="gpt-4o",
        messages=[{"role": "user", "content": "What is the weather like in Paris today?"}],
        tools=tools
    )
    
    print(completion.choices[0].message.tool_calls)

Anthropic API