AI・機械学習
中級
OpenAI Tool Use API(オープンエーアイツールユーズエーピーアイ)
OpenAIが提供するChat Completions APIのツール呼び出し機能。`tools`パラメータでJSON Schema定義のツールを渡し、モデルが適切なツールを選択・実行引数を生成する。
0 回閲覧
0 いいね
2026/6/6 更新
関連タグ
openai
tool-use
function-calling
gpt-4
api
OpenAI Tool Use APIの概要
OpenAIのTool Use APIは、Chat Completions APIにおいてLLMが外部ツールを呼び出せるようにする機能。2023年6月にfunctionsパラメータとして導入後、2024年にtools/tool_choice構文へ刷新。
基本的なAPI構造
{
"model": "gpt-4o",
"messages": [{...}],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "指定都市の現在の天気を取得する",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "都市名(例: Tokyo, Japan)"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"]
}
},
"required": ["location"]
}
}
}
],
"tool_choice": "auto"
}
tool_choiceオプション
| 値 | 説明 |
|---|---|
"auto" | モデルが呼び出し要否を判断(デフォルト) |
"none" | ツール呼び出しを禁止 |
"required" | 必ずツールを呼び出す |
{"type": "function", "function": {"name": "..."}} | 特定ツールを強制 |
レスポンスの処理
モデルがツール呼び出しを選択した場合、finish_reason: "tool_calls"で応答:
{
"choices": [{
"message": {
"role": "assistant",
"tool_calls": [{
"id": "call_abc123",
"type": "function",
"function": {
"name": "get_weather",
"arguments": "{\"location\": \"Tokyo\", \"unit\": \"celsius\"}"
}
}]
},
"finish_reason": "tool_calls"
}]
}
Parallel Tool Calls
parallel_tool_calls: true(デフォルト)で複数ツールを同時実行可能:
"tool_calls": [
{"id": "call_1", "function": {"name": "get_weather", "arguments": "{...}"}},
{"id": "call_2", "function": {"name": "get_stock", "arguments": "{...}"}}
]
Responses API(2025年)
新しいResponses APIでは組み込みツールも利用可能:
web_search_preview: リアルタイムWeb検索code_interpreter: Pythonコード実行file_search: アップロードファイル検索
実装のベストプラクティス
- descriptionは具体的かつ英語で記述(精度向上)
- requiredフィールドを最小限に(モデルの柔軟性確保)
- エラーレスポンスも
toolロールメッセージで返却 - ループ制御でmax_iterationsを設定(無限ループ防止)