ファンクションコーリング
ファンクションコーリングにより、モデルはユーザーのニーズにあわせて、あらかじめていぎしたツールかんすうをじどうでえらび、よびだせます。データルックアップ、API コール、タスクじっこうなどにやくだちます。
きほんコンセプト#
ファンクションコーリングのぜんたいフロー:
- ツールをていぎ — リクエストでつかえるかんすうとパラメータをせつめいする
- モデルのはんだん — ツールコールがひつようかモデルがきめる
- コールをかえす — モデルがかんすうめいとパラメータをかえす
- かんすうをじっこう — あなたがかんすうをじっこうし、けっかをえる
- かいわをつづける — けっかをモデルへもどし、さいしゅうレスポンスをつくる
OpenAI プロトコル#
Python
コード
from openai import OpenAI
import json
client = OpenAI(
base_url="https://as.apinoma.com/v1",
api_key="<あなたの APINOMA_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)TypeScript
Anthropic プロトコル#
Anthropic は `tools` パラメータをつかい、フォーマットがすこしちがいます。
コード
import anthropic
client = anthropic.Anthropic(
base_url="https://as.apinoma.com/anthropic",
api_key="<あなたの APINOMA_API_KEY>"
)
response = client.messages.create(
model="anthropic/claude-sonnet-4.6",
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 content block を処理
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-4o`、`gpt-4o-mini`、`o1`、`o3-mini`
- Anthropic: `claude-opus-4`、`claude-sonnet-4`、`claude-3-5-haiku`
- Google: `gemini-pro-compatible-pro-preview`、`gemini-pro-compatible-flash-lite-preview`、`gemini-3-pro-preview`
- ちゅうごくモデル: `deepseek-chat`, `qwen-max`, `glm-4`
さいしゅうこうしん: 2026 ねん 6 がつ 23 にち