関数呼び出し
ファンクション・コールを使用して、レスポンスAPIワークフロー中にモデルがアプリケーションからデータまたはアクションをリクエストできるようにします。これは、カレンダ・データ、内部アプリケーションの状態、カスタム操作の結果など、プロンプト自体で使用できない情報または操作をモデルが必要とする場合に便利です。
このパターンでは、モデルは関数を直接実行しません。かわりに、関数名と引数が返され、アプリケーションによって関数が実行されてから、モデルを続行してユーザー対応の回答を生成できるように、アプリケーションが関数出力を返送します。
有効にする内容
関数呼出しは、アプリケーションが実行の制御を維持しつつ、モデルが外部情報が必要なタイミングを決定できるようにする必要がある場合に役立ちます。
ユース・ケースの例:
- カレンダ・イベントの検索
- アプリケーション・データの取得中
- 内部または外部APIの呼出し
- ビジネス・ロジックまたは計算の実行
関数内で、外部サービス、データベース、独自のライブラリAPIの1つ、CLI、またはローカルMCPサーバーを呼び出すことができます。
このアプローチでは、アプリケーション内で実行パスを保持しながら柔軟性を確保できます。
実行フロー
一般的な関数呼び出し相互作用は次のように機能します。
- クライアントは、1つ以上のファンクション・ツール定義を含むリクエストを送信します。
- モデルは、これらのツールのいずれかが必要かどうかを判断します。
- ツールが必要な場合、モデルは関数名と引数を返します。
- アプリケーションによってファンクションが実行され、結果が準備されます。
- アプリケーションは、その結果をフォローアップ・リクエストで返送します。
- モデルは、その結果を使用して応答を完了します。
状態処理オプション
これらのリクエスト間で状態を管理するには、次の2つの一般的な方法があります。
-
サービス管理状態
ほとんどのユース・ケースに推奨されます。フォローアップ・リクエストには
previous_response_idが含まれ、サービスは以前の交換を追跡します。 -
クライアント管理の状態
アプリケーションは、完全な相互作用履歴を保持し、蓄積されたコンテキストを各リクエストに送信します。
ツール定義を正確に保ちます。名前のクリア、適切に記述された説明、および明確に定義されたパラメータは、モデルが適切なツールを選択し、使用可能な引数を生成するのに役立ちます。
関数ツールの定義
関数ツールを定義するには、"type": "function"を使用してtoolsプロパティにエントリを追加します。
次の例では、指定した日付のカレンダ・イベントを取得するツールを定義します。
tools = [
{
"type": "function",
"name": "get_calendar_events",
"description": "Return calendar events scheduled for a specific date.",
"parameters": {
"type": "object",
"properties": {
"date": {
"type": "string",
"description": "Date to query, for example 2026-04-02"
}
},
"required": ["date"],
},
},
]このtools配列をclient.responses.create()リクエストに含めます。
例: サービス管理状態
このパターンでは、最初のリクエストによって、ツールが必要かどうかをモデルが決定できます。2番目のリクエストは、ツールの結果を返送し、以前のレスポンスを参照します。
import json
tools = [
{
"type": "function",
"name": "get_calendar_events",
"description": "Return calendar events scheduled for a specific date.",
"parameters": {
"type": "object",
"properties": {
"date": {
"type": "string",
"description": "Date to query, for example 2026-04-02"
}
},
"required": ["date"],
},
},
]
def get_calendar_events(date):
# Replace this with actual calendar logic or an API call
return [
{"time": "09:00", "title": "Team standup"},
{"time": "13:00", "title": "Design review"},
{"time": "16:00", "title": "Project check-in"},
]
# Initial request
response = client.responses.create(
model="openai.gpt-oss-120b",
tools=tools,
input="Show the calendar events for 2026-04-02.",
)
# Execute the requested function
tool_outputs = []
for item in response.output:
if item.type == "function_call" and item.name == "get_calendar_events":
args = json.loads(item.arguments)
events = get_calendar_events(**args)
tool_outputs.append({
"type": "function_call_output",
"call_id": item.call_id,
"output": json.dumps({"events": events}),
})
# Follow-up request
final = client.responses.create(
model="openai.gpt-oss-120b",
instructions="Summarize the schedule clearly for the user.",
tools=tools,
input=tool_outputs,
previous_response_id=response.id,
)
print(final.output_text)例: クライアント管理状態
このパターンでは、アプリケーションは完全な交換を保持し、フォローアップ・リクエストで再送信します。
import json
tools = [
{
"type": "function",
"name": "get_calendar_events",
"description": "Return calendar events scheduled for a specific date.",
"parameters": {
"type": "object",
"properties": {
"date": {
"type": "string",
"description": "Date to query, for example 2026-04-02"
}
},
"required": ["date"],
},
},
]
def get_calendar_events(date):
# Replace this with actual calendar logic or an API call
return [
{"time": "09:00", "title": "Team standup"},
{"time": "13:00", "title": "Design review"},
{"time": "16:00", "title": "Project check-in"},
]
conversation = [
{"role": "user", "content": "Show the calendar events for 2026-04-02."}
]
response = client.responses.create(
model="openai.gpt-oss-120b",
tools=tools,
input=conversation,
)
conversation += response.output
for item in response.output:
if item.type == "function_call" and item.name == "get_calendar_events":
args = json.loads(item.arguments)
events = get_calendar_events(**args)
conversation.append({
"type": "function_call_output",
"call_id": item.call_id,
"output": json.dumps({"events": events}),
})
final = client.responses.create(
model="openai.gpt-oss-120b",
instructions="Summarize the schedule clearly for the user.",
tools=tools,
input=conversation,
)
print(final.output_text)ファンクション・コールは、アプリケーションが実行、アクセス制御および統合ロジックを担当しつつ、モデルが必要とする情報を要求できる強力なオプションです。