Skip to Content
開発ドキュメントガイドファンクションコーリング

ファンクションコーリング(Function Calling)

ファンクションコーリングにより、モデルがユーザーの要求に基づいて事前定義されたツール関数を自動的に選択・呼び出しし、データクエリ、API呼び出し、タスク実行などが可能になります。

基本概念

ファンクションコーリングの完全なフロー:

  1. ツールの定義 — リクエストで利用可能な関数とパラメータを記述
  2. モデルの判断 — モデルがツールの呼び出しが必要かどうかを判断
  3. 呼び出しの返却 — モデルが関数名とパラメータを返す
  4. 関数の実行 — 関数を実行して結果を取得
  5. 対話の継続 — 結果をモデルに送り返し、最終回答を生成

OpenAI プロトコル

function_calling.py
from openai import OpenAI import json client = OpenAI( base_url="https://api.ofox.ai/v1", api_key="<あなたの OFOXAI_API_KEY>" ) # 1. ツールを定義 tools = [{ "type": "function", "function": { "name": "get_weather", "description": "指定された都市のリアルタイム天気情報を取得", "parameters": { "type": "object", "properties": { "city": { "type": "string", "description": "都市名(例:東京、大阪)" }, "unit": { "type": "string", "enum": ["celsius", "fahrenheit"], "description": "温度の単位" } }, "required": ["city"] } } }] # 2. リクエストを送信 messages = [{"role": "user", "content": "東京の今日の天気はどうですか?"}] response = client.chat.completions.create( model="openai/gpt-4o", messages=messages, tools=tools, tool_choice="auto" ) message = response.choices[0].message # 3. ツール呼び出しを処理 if message.tool_calls: for tool_call in message.tool_calls: args = json.loads(tool_call.function.arguments) # 4. 関数を実行 result = get_weather(args["city"]) # あなた自身の実装 # 5. 結果をモデルに送り返す messages.append(message) messages.append({ "role": "tool", "tool_call_id": tool_call.id, "content": json.dumps(result) }) # 最終回答を取得 final = client.chat.completions.create( model="openai/gpt-4o", messages=messages, tools=tools ) print(final.choices[0].message.content)

Anthropic プロトコル

Anthropic は tools パラメータを使用しますが、形式がやや異なります:

anthropic_tools.py
import anthropic client = anthropic.Anthropic( base_url="https://api.ofox.ai/anthropic", api_key="<あなたの OFOXAI_API_KEY>" ) response = client.messages.create( model="anthropic/claude-sonnet-4.5", max_tokens=1024, tools=[{ "name": "get_weather", "description": "指定された都市のリアルタイム天気情報を取得", "input_schema": { "type": "object", "properties": { "city": {"type": "string", "description": "都市名"} }, "required": ["city"] } }], messages=[{"role": "user", "content": "東京の今日の天気はどうですか?"}] ) # tool_use コンテンツブロックを処理 for block in response.content: if block.type == "tool_use": print(f"ツール呼び出し: {block.name}, パラメータ: {block.input}")

並列ファンクションコーリング

モデルは1回のレスポンスで複数のツール呼び出しを返すことがあり、それらを並列に実行する必要があります:

# モデルが同時に複数のツール呼び出しをリクエストする場合 if message.tool_calls: # すべてのツール呼び出しを並列実行 import asyncio async def execute_tools(tool_calls): tasks = [] for tc in tool_calls: args = json.loads(tc.function.arguments) tasks.append(execute_function(tc.function.name, args)) return await asyncio.gather(*tasks)

tool_choice パラメータ

説明
"auto"モデルがツール呼び出しの要否を自動判断(デフォルト)
"none"ツール呼び出しを禁止
"required"ツール呼び出しを強制
{"type": "function", "function": {"name": "xxx"}}指定ツールの呼び出しを強制

対応モデル

以下のモデルが Function Calling をサポートしています:

  • OpenAI: gpt-4ogpt-4o-minio1o3-mini
  • Anthropic: claude-opus-4claude-sonnet-4claude-3-5-haiku
  • Google: gemini-3.1-pro-previewgemini-3-flash-previewgemini-3-pro-preview
  • 中国産モデル: deepseek-chatqwen-maxglm-4
Last updated on