透過 Decisions API 與 GPT-Live 的原生整合,你可以在使用者還在說話時就以低延遲做出決策。
原生委派可省去一次與應用程式之間的往返通訊。使用自訂委派時,Live 會先通知應用程式,再由應用程式呼叫 Decisions。使用原生委派時,Live 會直接呼叫 Decisions。
選擇決策時機
- 讓 Live 委派請求:當 Live 辨識出應用程式能處理的請求時,就將請求委派出去。在
session.delegation中設定選項,並指示 Live 何時委派。 - 根據對話做出反應:在使用者說話或 Live 回覆時做出反應。設定
session.triggers,讓輸入或輸出轉錄文字的增量更新觸發決策。
你可以在 Live 工作階段進行中變更問題。 送出 session.update,等收到對應的 session.updated 後,再為該步驟新增決策項目。建立工作階段時就要設定委派類型,之後整個工作階段都會使用同一類型。
讓 Live 委派請求
Live 只需發出一次請求,就能讓 Decisions 從你定義的動作中做選擇,並判斷應用程式是否應呼叫 Responses API。
假設你正在開發車用語音助理。駕駛可以要求調高或調低車內溫度、開啟或關閉擋風玻璃除霧功能,也可以要求導航並尋找用餐地點。若選擇 none,對應的車內設定就會維持原狀。
調整車內設定
「我覺得有點冷。」
- 車內溫度調高
- 除霧功能不變更
- 呼叫 Responses否
「我把溫度調高了一點。」
處理車內控制以外的請求
- 車內溫度不變更
- 除霧功能開啟
- 呼叫 Responses是
依照 Responses 傳回的工具呼叫,開始導航至舊金山,並搜尋當地餐廳。將兩項結果都傳回 Responses。
除霧功能的狀態、導航是否已啟動,以及搜尋到的餐廳。
設定委派
先確認你有可正常使用的 Live 連線和 api.decisions.write 權限。使用平常連線到 Live 時所用的標頭即可,無須加上 Decisions 專用的 alpha 標頭。
在 WebRTC 工作階段請求的 session 中加入以下物件,與現有的 transport 放在同一層。工作階段指示告訴 Live 何時委派,選擇題則告訴 Decisions 要傳回什麼。一次原生決策請求就能回答這三個問題,每題獨立判斷。
{
"model": "gpt-live-1",
"instructions": "Delegate the driver’s whole request, including cabin controls, navigation, and local search. Wait for the app to report what happened, then tell the driver in natural language.",
"delegation": {
"type": "decisions",
"decisions": {
"model": "gpt-6-luna",
"questions": [
{
"type": "choice",
"name": "cabin_temperature",
"instructions": "Choose temperature_up if the driver feels cold or wants more heat, temperature_down if they feel hot or want to cool down, or none if the request is unrelated.",
"choices": [
{ "value": "temperature_up" },
{ "value": "temperature_down" },
{ "value": "none" }
]
},
{
"type": "choice",
"name": "windshield_defogger",
"instructions": "Choose on if the driver reports a foggy windshield or asks to defog it, off if they say the glass is clear now or ask to turn the defogger off, or none if the request is unrelated.",
"choices": [
{ "value": "on" },
{ "value": "off" },
{ "value": "none" }
]
},
{
"type": "choice",
"name": "needs_response",
"instructions": "Choose yes if any part of the request goes beyond these cabin controls, such as getting directions or finding restaurants. Otherwise choose no.",
"choices": [{ "value": "yes" }, { "value": "no" }]
}
]
}
}
}
Live 會自動將對話的轉錄文字傳給 Decisions。
執行動作並回報結果
請儲存 delegation.id,其值可從 session.delegation.created 事件取得。收到 decision.event 時,確認其 delegation_id 與先前儲存的識別碼相符,且 event.type 為 decision.completed,再依問題名稱從 event.decision.answers 讀取各題選定的選項。若答案為 none,就不執行對應的動作。如果你有篩選 WebRTC 事件,請同時允許 session.delegation.created 和 decision.event。
針對「我覺得有點冷」這句話,Decisions 會分別選擇 temperature_up、none 和 no。調高溫度後,讀取實際設定。假設儀表板此時顯示空調處於自動模式,目標溫度為 22 °C,除霧功能已關閉。使用 decision.item.create 將已確認的車內狀態傳給 Decisions。
{
"type": "decision.item.create",
"event_id": "evt_cabin_after_temperature_up_1",
"item": {
"type": "message",
"role": "user",
"content": [
{
"type": "input_text",
"text": "Use the latest cabin state snapshot as the current state. Cabin temperature is set to 22 °C in automatic mode. Windshield defogger is off."
}
]
}
}
只有在儀表板顯示的狀態變更時,才附加一份精簡但涵蓋完整車內狀態的快照。以最新快照為目前狀態。保留先前的輸入及其順序,即可保留提示詞快取所需的前綴。
等收到 decision.item.created 後,再使用 session.commentary.append 將結果傳給 Live,並帶入這次請求的委派 ID。
{
"type": "session.commentary.append",
"delegation_id": "del_cabin_1",
"content": "I raised the cabin temperature and confirmed the new setting on the dashboard."
}
針對前往舊金山的請求,Decisions 會分別選擇 none、on 和 yes。開啟除霧功能,讀取其實際狀態,再依上述方式附加更新後的車內狀態快照。接著,使用 api.responses.write 權限從伺服器呼叫 Responses。請一併傳送車內控制結果與駕駛的完整請求,讓 Responses 能判斷「那裡」指的是舊金山。
curl https://api.openai.com/v1/responses \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-6.1-sol",
"instructions": "The car has handled the cabin controls. Use the tools for the rest of the request, then briefly tell the driver what happened based on the results.",
"input": "Driver: My windshield is foggy, so please defog it, navigate to San Francisco, and find me some good restaurants there.\nCabin results: Windshield defogger confirmed on. Temperature unchanged.",
"tools": [
{
"type": "function",
"name": "start_navigation",
"description": "Start navigation to a destination and report whether the route started.",
"parameters": {
"type": "object",
"properties": { "destination": { "type": "string" } },
"required": ["destination"],
"additionalProperties": false
},
"strict": true
},
{
"type": "function",
"name": "find_restaurants",
"description": "Find well-rated restaurants in the named city or area.",
"parameters": {
"type": "object",
"properties": { "location": { "type": "string" } },
"required": ["location"],
"additionalProperties": false
},
"strict": true
}
]
}'這是 Responses 函式呼叫流程中的第一個請求。針對每個 function_call,使用提供的引數執行指定的函式,再傳回 function_call_output,其中應包含實際結果或錯誤,以及對應的 call_id。重複此流程,直到 Responses 傳回最終答案。
只將 Responses 的精簡最終答案與已確認的車內狀態傳給 Live。使用 session.commentary.append,並帶入同一個 delegation_id。
根據轉錄文字做出反應
如果反應時機需要配合對話進度,可以使用轉錄文字觸發條件。例如,虛擬角色聽到「我錄取了!」時,可以在使用者還沒說完時就微笑點頭。當 Live 說「太好了!」時,輸出端的觸發條件可以讓角色笑得更燦爛,身體也往前傾。
session.input_transcript.delta「我錄取了!」
session.output_transcript.delta「太好了!」
session.input_transcript.delta「我沒被錄取。」
在 session.triggers 中加入如下的觸發條件,讓模型分別選擇表情、強度與動作:
{
"type": "decisions",
"event_type": "session.input_transcript.delta",
"decisions": {
"questions": [
{
"type": "choice",
"name": "expression",
"instructions": "Choose your expression as you listen to the person's latest words.",
"choices": [
{ "value": "neutral" },
{ "value": "happy" },
{ "value": "sad" },
{ "value": "concerned" }
]
},
{
"type": "choice",
"name": "intensity",
"instructions": "Choose how strongly to show that expression.",
"choices": [
{ "value": "low" },
{ "value": "medium" },
{ "value": "high" }
]
},
{
"type": "choice",
"name": "gesture",
"instructions": "Choose a gesture that fits what you just heard.",
"choices": [
{ "value": "none" },
{ "value": "nod" },
{ "value": "shake" },
{ "value": "lean" }
]
}
]
}
}
若要讓角色在回覆時也有動畫,請新增第二個觸發條件,將 event_type 設為 session.output_transcript.delta,並要求模型根據 Live 正在說的話選擇合適的反應。模型與觸發條件 ID 由 Live 提供。轉錄文字觸發條件可獨立於委派運作。
如果你有篩選 WebRTC 事件,請允許 decision.event,並依照各觸發條件的設定,允許對應的轉錄事件:session.input_transcript.delta、session.output_transcript.delta,或兩者都允許。只有在來源轉錄事件也獲准傳送時,Live 才會傳送該事件觸發的結果。
每個觸發結果都會指出是哪一次轉錄文字的增量更新觸發了決策。以下是「我錄取了!」範例中 decision.event 的相關欄位:
{
"type": "decision.event",
"delegation_id": null,
"trigger_id": "trg_listening_1",
"source_event_type": "session.input_transcript.delta",
"source_event_id": "evt_input_42",
"event": {
"type": "decision.completed",
"decision": {
"answers": [
{ "type": "choice", "name": "expression", "choice": "happy" },
{ "type": "choice", "name": "intensity", "choice": "medium" },
{ "type": "choice", "name": "gesture", "choice": "nod" }
]
}
}
}
使用 source_event_id 找出對應的轉錄文字增量更新。Live 可能會合併尚待處理的增量更新,結果也可能不按順序送達。請分別針對輸入與輸出套用最新的相關結果,並根據音訊控制嘴部動作。