服务端事件

本文介绍 Qwen-TTS-Realtime API 的服务端事件。

相关文档:实时语音合成-通义千问

服务端事件

error

不论是遇到客户端错误还是服务端错误,服务端都会响应该事件。

参数

类型

说明

type

string

事件类型,该事件下固定为error

error

object

错误的详细信息。

error.type

string

错误类型。

error.code

string

错误码。

error.message

string

错误信息。

error.param

string

与错误相关的参数,如session.voice

{
  "event_id": "event_B2uoU7VOt1AAITsPRPH9n",
  "type": "error",
  "error": {
    "type": "invalid_request_error",
    "code": "invalid_value",
    "message": "Invalid value: 'qwen-tts'. Supported values are: 'Qwen2.5-tts'.",
    "param": "session.input_audio_transcription.model",
    "event_id": "event_123"
  }
}

session.created

客户端连接到服务端后,响应的第一个事件,该事件返回时会携带服务端对此次连接的默认配置信息。

参数

类型

说明

type

string

事件类型,该事件下固定为session.created

session

object

session配置。

session.id

string

session 的唯一标识。

session.object

string

session 的服务名。

session.mode

string

模型响应模式设置。

session.model

string

使用的模型。

session.voice

string

模型生成音频的音色。

session.response_format

string

模型输出音频的格式,当前只支持设置为"pcm"。

session.sample_rate

integer

模型输出音频的采样率,当前只支持设置为24000。

{
  "event_id": "event_xxx",
  "type": "session.created",
  "session": {
    "object": "realtime.session",
    "mode": "server_commit",
    "model": "qwen-tts-realtime",
    "voice": "Cherry",
    "response_format": "pcm",
    "sample_rate": 24000,
    "id": "sess_xxx"
  }
}

session.updated

接收到客户端的session.update请求并正确处理后返回。如果出现错误,则直接返回error事件。

参数

类型

说明

type

string

事件类型,该事件下固定为session.updated

session

object

session配置。

session.id

string

session 的唯一标识。

session.object

string

session 的服务名。

session.mode

string

模型输出模式设置。

session.model

string

使用的模型。

session.voice

string

模型生成音频的音色。

session.response_format

string

模型输出音频的格式,当前只支持设置为"pcm"。

session.sample_rate

integer

模型输出音频的采样率,当前只支持设置为24000。

{
  "event_id": "event_xxx",
  "type": "session.updated",
  "session": {
    "id": "sess_xxx",
    "object": "realtime.session",
    "model": "qwen-tts-realtime",
    "voice": "Cherry",
    "mode": "commit",
    "response_format": "pcm",
    "sample_rate": 24000
  }
}

input_text_buffer.committed

客户端发送input_text_buffer.commit事件后,服务端的响应事件。

参数

类型

说明

event_id

string

本次事件的标识。

type

string

事件类型,该事件下固定为input_text_buffer.committed

item_id

string

将创建的用户消息项的 ID。

{
  "event_id": "event_FC6MA88wS2oEeXkPvWsxX",
  "type": "input_text_buffer.committed",
  "item_id": ""
}

input_text_buffer.cleared

客户端发送input_audio_buffer.clear事件后,服务端的响应事件。

参数

类型

说明

event_id

string

本次事件的标识。

type

string

事件类型,该事件下固定为input_text_buffer.cleared

{
    "event_id": "event_1122",
    "type": "input_text_buffer.cleared"
}

response.created

服务端生成新的模型响应时,返回该事件。

参数

类型

说明

type

string

该事件下固定为response.created

event_id

string

本次事件的标识。

response

object

响应对象。

response.id

string

响应的唯一 ID。

response.object

string

对象类型,此事件下固定为realtime.response

response.status

string

响应的最终状态,取值范围 [completed, failed, in_progress, or incomplete]。

response.voice

string

模型生成音频的音色。

response.output

array

此事件下目前为空。

{
  "event_id": "event_IMnLqDvG6Ahhk7sWV2uOs",
  "type": "response.created",
  "response": {
    "id": "resp_USvBwHktHcz76r6GaIJUV",
    "object": "realtime.response",
    "conversation_id": "",
    "status": "in_progress",
    "voice": "Cherry",
    "output": []
  }
}

response.output_item.added

当新的item项需要输出时,服务端返回此事件。

参数

类型

说明

type

string

固定为response.output_item.added

response_id

string

可关联同一个response的所有输出。

output_index

integer

response中输出项的索引, 目前固定为0。

item

object

输出项信息。

item.id

string

输出项的唯一ID。

item.object

string

始终为 realtime.item 。

item.status

string

输出项的状态。

item.content

array

消息的内容。

{
  "event_id": "event_INDGnGNulaXCrStd9ZM5X",
  "type": "response.output_item.added",
  "response_id": "resp_USvBwHktHcz76r6GaIJUV",
  "output_index": 0,
  "item": {
    "id": "item_FIrYGaNVK3rbIZqeY4QjM",
    "object": "realtime.item",
    "type": "message",
    "status": "in_progress",
    "role": "assistant",
    "content": []
  }
}

response.content_part.added

当新的内容项需要输出时,服务端返回此事件。

参数

类型

说明

type

string

固定为response.content_part.added

response_id

string

响应的ID。

item_id

string

消息项ID。

output_index

integer

响应输出项的索引, 目前固定为0。

content_index

integer

响应输出项中内部部分的索引, 目前固定为0。

part

object

已完成的内容部分。

part.type

string

内容部分的类型。

part.text

string

内容部分的文本。

{
  "event_id": "event_DigZ95MWN36YYyyjcENoq",
  "type": "response.content_part.added",
  "response_id": "resp_USvBwHktHcz76r6GaIJUV",
  "item_id": "item_FIrYGaNVK3rbIZqeY4QjM",
  "output_index": 0,
  "content_index": 0,
  "part": {
    "type": "audio",
    "text": ""
  }
}

response.audio.delta

当模型增量生成新的audio数据时,系统会返回服务器 response.audio.delta 事件。

参数

类型

说明

type

string

固定为response.audio.delta

response_id

string

可以关联同一个response的所有输出。

item_id

string

消息项id,可以关联同一个消息item。

output_index

integer

response中输出项的索引, 目前固定为0。

content_index

integer

response中输出项中内部部分的索引, 目前固定为0。

delta

string

模型增量输出的audio数据,使用Base64编码。

{
  "event_id": "event_B1osWMZBtrEQbiIwW0qHQ",
  "type": "response.audio.delta",
  "response_id": "resp_B1osWTzBb8hO0WsELHgVP",
  "item_id": "item_B1osWH81fXDoyim1T5fsF",
  "output_index": 0,
  "content_index": 0,
  "delta": "base64 audio"
}

response.content_part.done

当新的内容项输出完成时,服务端返回此事件。

参数

类型

说明

type

string

固定为response.content_part.done

response_id

string

响应的ID。

item_id

string

消息项ID。

output_index

integer

响应输出项的索引, 目前固定为0。

content_index

integer

响应输出项中内部部分的索引, 目前固定为0。

part

object

已完成的内容部分。

part.type

string

内容部分的类型。

part.text

string

内容部分的文本。

{
  "event_id": "event_Vo2YUjlYQJ4colH8nVzkU",
  "type": "response.content_part.done",
  "response_id": "resp_USvBwHktHcz76r6GaIJUV",
  "item_id": "item_FIrYGaNVK3rbIZqeY4QjM",
  "output_index": 0,
  "content_index": 0,
  "part": {
    "type": "audio",
    "text": ""
  }
}

response.output_item.done

当新的item输出完成时,服务端返回此事件。

参数

类型

说明

type

string

固定为response.output_item.done

response_id

string

响应的ID。

output_index

integer

response中输出项的索引, 目前固定为0。

item

object

输出项信息。

item.id

string

输出项的唯一ID。

item.object

string

始终为 realtime.item 。

item.status

string

输出项的状态。

item.content

array

消息的内容。

{
  "event_id": "event_LO6SJRKIQ9NBayyYB8a1A",
  "type": "response.output_item.done",
  "response_id": "resp_USvBwHktHcz76r6GaIJUV",
  "output_index": 0,
  "item": {
    "id": "item_FIrYGaNVK3rbIZqeY4QjM",
    "object": "realtime.item",
    "type": "message",
    "status": "completed",
    "role": "assistant",
    "content": [
      {
        "type": "audio",
        "text": ""
      }
    ]
  }
}

response.audio.done

当模型生成audio数据完成时,系统会返回服务器 response.audio.done 事件。

参数

类型

说明

type

string

固定为response.audio.done

response_id

string

response_id,可以关联同一个resposne的所有输出。

item_id

string

消息项id,可以关联同一个消息item。

output_index

integer

response中输出项的索引, 目前固定为0。

content_index

integer

response中输出项中内部部分的索引, 目前固定为0。

{
  "event_id": "event_LZaOHPzXYMUXGBcVkBmKX",
  "type": "response.audio.done",
  "response_id": "resp_USvBwHktHcz76r6GaIJUV",
  "item_id": "item_FIrYGaNVK3rbIZqeY4QjM",
  "output_index": 0,
  "content_index": 0
}

response.done

当响应生成完成时,服务端会返回此事件。该事件中包含的 Response 对象将包含 Response 中的所有输出项,但不包括已返回的原始音频数据。

参数

类型

说明

type

string

固定为response.done

response

object

响应对象。

response.id

string

响应的唯一 ID。

response.object

string

对象类型,此事件下固定为realtime.response

response.output

array

响应的输出。

response.usage

object

本次响应对应的usage信息。

{
  "event_id": "event_xxx",
  "type": "response.done",
  "response": {
    "id": "resp_xxx",
    "object": "realtime.response",
    "conversation_id": "",
    "status": "completed",
    "modalities": [
      "text",
      "audio"
    ],
    "voice": "Cherry",
    "output": [
      {
        "id": "item_FIrYGaNVK3rbIZqeY4QjM",
        "object": "realtime.item",
        "type": "message",
        "status": "completed",
        "role": "assistant",
        "content": [
          {
            "type": "audio",
            "transcript": ""
          }
        ]
      }
    ],
    "usage": {
      "total_tokens": 67,
      "input_tokens": 3,
      "output_tokens": 64,
      "input_tokens_details": {
        "text_tokens": 3
      },
      "output_tokens_details": {
        "text_tokens": 0,
        "audio_tokens": 64
      }
    }
  }
}

session.finished

当所有响应生成完成时,服务端会返回此事件。

参数

类型

说明

type

string

固定为session.finished

event_id

object

本次事件的标识。

{
  "event_id": "event_2239",
  "type": "session.finished"
}