コンテンツにスキップ

ツール呼び出し

ツール呼び出し (function calling) は、提供した JSON ツール定義に基づいてモデルが構造化された関数呼び出しを生成する機能です。ai& は OpenAI Chat Completions と Anthropic Messages の両方の API でツール呼び出しに対応しています。

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",
)

OpenAI 形式のリクエストでは parallel_tool_calls: true (デフォルト) を指定して、1 ターンで複数のツール呼び出しを生成できます。

  • "auto" — モデルがツールを使うかどうかを判断 (tools を指定した場合のデフォルト)。
  • "none" — このリクエストではツール利用を無効化。
  • "required" — 必ずツールを呼び出させる。
  • { "type": "function", "function": { "name": "..." } } — 特定のツールに限定。