Tool calling
モデルに関数を呼び出させます。OpenAI SDK と Anthropic SDK の両方で動作します。
ツール呼び出し(関数呼び出し)を使うと、モデルは直接回答する代わりに、あなたが宣言した関数を 呼び出すための構造化された引数を返します。外部 API 照会、データベース検索、計算など、モデル自身が できない処理をコードが代わりに実行し、その結果をモデルに返せます。PleumRouter は OpenAI 形式の tools を受け取り、リクエストが Anthropic(Claude)モデルにルーティングされる際 には、ルーターが自動的に Anthropic のツール形式へ変換します — リクエストの形を変える必要はありません。
リクエスト#
| パラメータ | 型 | 必須 | 説明 |
|---|---|---|---|
| tools | array | 任意 | モデルが呼び出せる関数のリスト。OpenAI 形式 [{"type": "function", "function": {"name", "description", "parameters"}}] を使い、parameters は JSON Schema です。 |
| tool_choice | string | object | 任意 | "auto"(モデルが判断) · "required"(必ずツールを呼ぶ) · "none"(ツールを使わない)、または {"type": "function", "function": {"name": "..."}} で特定の関数を強制。 |
| parallel_tool_calls | boolean | 任意 | 1 回のレスポンスで複数のツールを同時に呼び出せるかどうか。OpenAI 互換プロバイダーでのみ適用され、Anthropic 向けには変換されません。 |
リクエストボディに tools と(任意で)tool_choice を 含めて POST /v1/chat/completions を呼び出します。認証は plm_ API キーを Authorization: Bearer または x-api-key ヘッダーで渡します。
curl https://apirouter.pleum.ai/v1/chat/completions \
-H "Authorization: Bearer $PLEUM_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o",
"messages": [
{"role": "user", "content": "What is the weather in Seoul?"}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather for a city.",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "City name, e.g. Seoul"},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
},
"required": ["city"]
}
}
}
],
"tool_choice": "auto"
}'レスポンス#
モデルがツールを呼び出すと判断すると、choices[0].message.content は null になり、message.tool_calls 配列に呼び出す関数と 引数が入ります。arguments は JSON 文字列なので、パースしてから使ってください。 このとき finish_reason は "tool_calls" です。すべての チャットレスポンスには PleumRouter 拡張の cost(ウォン建て費用・為替レート・ マークアップ)と request_id も含まれます。
{
"id": "chatcmpl-gpt-4o-612ms",
"object": "chat.completion",
"model": "gpt-4o",
"provider": "openai",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": null,
"tool_calls": [
{
"id": "call_abc123",
"type": "function",
"function": {
"name": "get_weather",
"arguments": "{\"city\": \"Seoul\", \"unit\": \"celsius\"}"
}
}
]
},
"finish_reason": "tool_calls"
}
],
"usage": {
"prompt_tokens": 78,
"completion_tokens": 21,
"total_tokens": 99
},
"cost": {
"usd": 0.000396,
"krw": 1,
"fx_rate": 1525.0,
"markup_rate": 0.0
},
"request_id": "req_01J9X2Qm7..."
}マルチターンループ#
tool_calls を受け取ったら、コード側で関数を実行し、同じ messages 配列にモデルの assistant メッセージ(tool_calls 含む)と実行結果を role: "tool" メッセージとして追記して再度呼び出します。tool メッセージには tool_call_id、name、結果を入れた content を含めます。モデルが最終回答を出すまでこのループを繰り返します。
{
"model": "gpt-4o",
"messages": [
{"role": "user", "content": "What is the weather in Seoul?"},
{
"role": "assistant",
"content": null,
"tool_calls": [
{
"id": "call_abc123",
"type": "function",
"function": {
"name": "get_weather",
"arguments": "{\"city\": \"Seoul\", \"unit\": \"celsius\"}"
}
}
]
},
{
"role": "tool",
"tool_call_id": "call_abc123",
"name": "get_weather",
"content": "{\"temp\": 21, \"unit\": \"celsius\", \"sky\": \"clear\"}"
}
],
"tools": [
{"type": "function", "function": {"name": "get_weather", "description": "Get the current weather for a city.", "parameters": {"type": "object", "properties": {"city": {"type": "string"}, "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}}, "required": ["city"]}}}
]
}parallel_tool_calls は OpenAI 互換プロバイダー専用です。Anthropic(Claude) モデルはルーターがツール形式を自動変換して処理するため、リクエストを変える必要はありませんが、この フラグは Anthropic には転送されません。