ツール呼び出し
ツール呼び出し (function calling) は、提供した JSON ツール定義に基づいてモデルが構造化された関数呼び出しを生成する機能です。ai& は OpenAI Chat Completions と Anthropic Messages の両方の API でツール呼び出しに対応しています。
ツールの定義
Section titled “ツールの定義”tools = [{ "type": "function", "function": { "name": "get_weather", "description": "Get current weather for a city.", "parameters": { "type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"], }, },}]
response = client.chat.completions.create( model="...", messages=[{"role": "user", "content": "Weather in Tokyo?"}], tools=tools, tool_choice="auto",)tools = [{ "name": "get_weather", "description": "Get current weather for a city.", "input_schema": { "type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"], },}]
response = client.messages.create( model="...", max_tokens=1024, tools=tools, messages=[{"role": "user", "content": "Weather in Tokyo?"}],)並列ツール呼び出し
Section titled “並列ツール呼び出し”OpenAI 形式のリクエストでは parallel_tool_calls: true (デフォルト) を指定して、1 ターンで複数のツール呼び出しを生成できます。
tool_choice の制御
Section titled “tool_choice の制御”"auto"— モデルがツールを使うかどうかを判断 (toolsを指定した場合のデフォルト)。"none"— このリクエストではツール利用を無効化。"required"— 必ずツールを呼び出させる。{ "type": "function", "function": { "name": "..." } }— 特定のツールに限定。