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 应返回什么。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 与保存的 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.2-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 返回最终答复。
只向 Live 发送 Responses 给出的简明最终答复和已确认的车内状态。使用 session.commentary.append 并传入相同的 delegation_id。
根据转写文本做出反应
如果需要配合对话节奏做出反应,请使用转写触发器。例如,数字人听到“我被录用了!”时,可以在用户还没说完时就微笑点头。当 Live 回复“太好了!”时,输出触发器可以让角色笑得更灿烂,同时身体前倾。
session.input_transcript.delta“我被录用了!”
session.output_transcript.delta“太好了!”
session.input_transcript.delta“我没被录用。”
在 session.triggers 中添加如下触发器,让 Decisions 分别选择表情、强度和动作:
{
"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,并要求 Decisions 根据 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 可能会合并多段待处理的增量文本,结果也可能不按顺序到达。对于输入和输出两个方向,分别采用各自最新的相关结果,并根据音频驱动口型变化。