将语音接入 Decisions

使用 GPT-Live 原生集成的 Decisions API,在用户仍在说话时以低延迟作出决策。

使用 GPT-Live 原生集成的 Decisions API,在用户仍在说话时以低延迟作出决策。

原生委派省去了一次经过应用的往返通信。使用自定义委派时,Live 会通知你的应用,再由应用调用 Decisions。使用原生委派时,Live 会直接调用 Decisions。

选择何时作出决策

  • 当 Live 识别出应用能够处理的请求时,让 Live 委派。在 session.delegation 中设置选项,并告诉 Live 何时委派。
  • 在用户说话或 Live 回复时,对对话作出响应。为输入或输出转录文本增量配置 session.triggers。

你可以在进行中的 Live 会话内更改问题。 发送 session.update,然后等待对应的 session.updated,再为该步骤添加决策项。在创建会话时设置委派类型。该类型在整个会话期间保持不变。

让 Live 委派

在一次请求中,Live 可以让 Decisions 从你定义的操作中作出选择,并决定应用是否应调用 Responses API。

假设你正在为汽车构建语音助手。驾驶员可以要求调高或调低温度、打开或关闭挡风玻璃除雾器,也可以获取路线和寻找用餐地点。选择 none 表示保持对应的车内控制设置不变。

调节车内环境

驾驶员

“我觉得有点冷。”

决策结果

  • 车内温度:调高
  • 除雾器:不变
  • 调用 Responses:否

你的应用

调高温度,读取新设置,并将完整的车内状态追加到 Decisions。等待确认,然后将结果发送给 Live。

Live 回复

“我把温度调高了一点。”

处理超出车内控制范围的请求

驾驶员

“我的挡风玻璃起雾了,请帮我除雾,导航到旧金山,再帮我找一些那里的好餐厅。”

决策结果

  • 车内温度:不变
  • 除雾器:开启
  • 调用 Responses:是

你的应用

  1. 打开除雾器:开启

  2. 将驾驶员的完整请求和车内控制结果发送给 Responses

使用 Responses 返回的工具调用开始导航到旧金山, 并查找那里的餐厅。将两项结果都返回给 Responses。

Live 告知

除雾器状态、导航是否已开始,以及搜索找到的餐厅。

配置委派

首先准备好正常工作的 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。

执行操作并报告结果

保存 session.delegation.created 中的 delegation.id。在具有该 delegation_id 的 decision.event 中,查找 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。打开除雾器,读取其状态,然后按上述方式追加更新后的车内状态快照。接着,从服务器调用 Responses,需具备 api.responses.write 权限。包含车内控制结果和驾驶员的完整请求,以便 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 返回最终答案。

只将 Responses 的简洁最终答案和已确认的车内状态发送给 Live。使用 session.commentary.append,并传入同一个 delegation_id。

对转录文本作出响应

如果触发时机应跟随对话进程,请使用转录文本触发器。数字人在听到“我得到那份工作了!”时,可以在用户仍在说话的时候微笑并点头。输出触发器则可以在 Live 说“太棒了!”时,让笑容更加灿烂,并让角色身体前倾。

聆听时 session.input_transcript.delta

你的用户

“我得到那份工作了!”

→

微笑 + 点头

高兴 · 中等强度

说话时 session.output_transcript.delta

Live

“太棒了!”

→

微笑 + 身体前倾

高兴 · 高强度

另一个聆听示例 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 正在说的话相匹配的选项。Live 会提供模型和触发器 ID。转录文本触发器独立于委派运行。

如果你过滤 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 可以合并待处理的增量,而且结果可能乱序到达。对每个方向应用最新的相关结果,并根据音频驱动嘴部动作。

找你所需

输入关键词,寻找文章、视频、工具、AI技巧或术语。

Esc 关闭搜索