服务端事件

本文介绍Qwen-Omni 实时 API的服务端事件。

相关文档:实时多模态

服务端事件

error

不论是遇到客户端错误还是服务端错误,服务端会返回错误信息。通常来说,大多数错误都是可恢复的,不会影响会话继续进行。

参数

类型

说明

type

string

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

error

object

错误的详细信息。

error.type

string

错误类型。

error.code

string

错误码。

error.message

string

错误信息。

error.param

string

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

{
  "event_id": "event_RoUu4T8yExPMI37GKwaOC",
  "type": "error",
  "error": {
    "type": "invalid_request_error",
    "code": "invalid_value",
    "message": "Invalid modalities: ['audio']. Supported combinations are: ['text'] and ['audio', 'text'].",
    "param": "session.modalities"
  }
}

session.created

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

参数

类型

说明

type

string

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

session

object

session配置。

session.modalities

array

模型输出模态设置,支持设置["text"]或["text","audio"],不支持单独设置为["audio"]。

session.voice

string

模型生成音频的音色,支持:"Chelsie", "Serena", "Ethan", "Cherry"。音色效果请参见:支持的音频音色

session.input_audio_format

string

用户输入音频的格式,当前只支持设置为"pcm16"。

session.output_audio_format

string

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

session.input_audio_transcription

string

对用户输入的音频启用语音转写时相关配置。

session.input_audio_transcription.model

string

语音转写对应模型,当前固定为"gummy-realtime-v1"。

session.turn_detection

object

语音活动检测(VAD)的配置。

设置为 None 表示不启用 VAD,由用户手动发送音频。

session.turn_detection.type

string

服务端VAD类型,目前固定为"server_vad"。

session.turn_detection.threshold

float

VAD检测阈值。建议在嘈杂的环境中增加, 在安静的环境中降低。

  • 取值越接近-1,噪音被判定为语音的概率越大。

  • 取值越接近1,噪音被判定为语音的概率越小。

默认为 0.2, 参数范围:[-1.0, 1.0]。

session.turn_detection.silence_duration_ms

integer

检测语音停止的静音持续时间,超过此值后会触发模型响应。默认值为800,参数范围[200, 6000]。

{
    "event_id": "event_XlIT7ohkMGIsuSz154lbA",
    "type": "session.created",
    "session": {
        "object": "realtime.session",
        "model": "qwen-omni-turbo-realtime",
        "modalities": [
            "text",
            "audio"
        ],
        "instructions": "...model instructions here...",
        "voice": "Chelsie",
        "input_audio_format": "pcm16",
        "output_audio_format": "pcm16",
        "input_audio_transcription": {
            "model": "gummy-realtime-v1"
        },
        "turn_detection": {
            "type": "server_vad",
            "threshold": 0.5,
            "prefix_padding_ms": 300,
            "silence_duration_ms": 800,
            "create_response": true,
            "interrupt_response": true
        },
        "tools": [],
        "tool_choice": "auto",
        "temperature": 0.8,
        "max_response_output_tokens": "inf",
        "id": "sess_ECq2bEeDvXHEPLH9qCW3W"
    }
}

session.updated

当接受到用户的session.update请求并正确处理后返回。如果出现错误,则直接返回实时多模态事件。

参数

类型

说明

type

string

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

session

object

session配置。

session.temperature

float

模型的温度参数,范围是[0,2)。

session.modalities

array

模型输出模态设置,支持设置["text"](仅输出文本)或["text","audio"](输出音频和文本)。

session.voice

string

模型生成音频的音色,支持:"Chelsie", "Serena", "Ethan", "Cherry"。音色效果请参见:支持的音频音色

session.input_audio_format

string

用户输入音频的格式,当前只支持设置为"pcm16"。

session.output_audio_format

string

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

session.input_audio_transcription

string

对用户输入的音频启用语音转写时相关配置。

session.input_audio_transcription.model

string

语音转写对应模型,当前固定为"gummy-realtime-v1"。

session.turn_detection

object

语音活动检测(VAD)的配置。

设置为 None 表示不启用 VAD,由用户手动发送音频。

session.turn_detection.type

string

服务端VAD类型,目前固定为"server_vad"。

session.turn_detection.threshold

float

VAD检测阈值。建议在嘈杂的环境中增加, 在安静的环境中降低。

  • 取值越接近-1,噪音被判定为语音的概率越大。

  • 取值越接近1,噪音被判定为语音的概率越小。

默认为 0.2, 参数范围:[-1.0, 1.0]。

session.turn_detection.silence_duration_ms

integer

检测语音停止的静音持续时间,超过此值后会触发模型响应。默认值为800,参数范围[200, 6000]。

{
    "event_id": "event_XrsEpDePgfEfHakfTwYoy",
    "type": "session.updated",
    "session": {
        "temperature": 0.8,
        "id": "sess_ECq2bEeDvXHEPLH9qCW3W",
        "object": "realtime.session",
        "model": "qwen-omni-turbo-realtime",
        "modalities": [
            "text",
            "audio"
        ],
        "instructions": "...model instructions here...",
        "voice": "Chelsie",
        "input_audio_format": "pcm16",
        "output_audio_format": "pcm16",
        "input_audio_transcription": {
            "model": "gummy-realtime-v1"
        },
        "turn_detection": {
            "type": "server_vad",
            "threshold": 0.1,
            "prefix_padding_ms": 500,
            "silence_duration_ms": 900,
            "create_response": true,
            "interrupt_response": true
        },
        "max_response_output_tokens": "inf"
    }
}

input_audio_buffer.speech_started

server_vad 模式下,服务器在音频缓冲区中检测到语音开始时,系统会返回服务器 input_audio_buffer.speech_started 事件。

每当音频添加到缓冲区时,此事件都可能发生(除非已检测到语音)。

参数

类型

说明

event_id

string

本次事件的标识。

type

string

事件类型,该事件下固定为input_audio_buffer.speech_started

audio_start_ms

integer

在会话期间,从音频开始写入缓冲区到首次检测到语音时的毫秒数。

item_id

string

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

{
  "event_id": "event_MOcdMTKH1QQRP5mbGWPHA",
  "type": "input_audio_buffer.speech_started",
  "audio_start_ms": 2022,
  "item_id": "item_Fu4bF8iduL8nfJVsbKb3L"
}

input_audio_buffer.speech_stopped

server_vad 模式下,服务器在音频缓冲区中检测到语音结束时,系统会返回服务器 input_audio_buffer.speech_stopped 事件。

服务器还将发送一个带有由音频缓冲区创建的用户消息项的 conversation.item.created 事件。

参数

类型

说明

event_id

string

本次事件的标识。

type

string

事件类型,该事件下固定为input_audio_buffer.speech_stopped

audio_end_ms

integer

自会话开始以来,语音停止时所经过的毫秒数。

item_id

string

当语音停止时将创建的用户消息项的 ID。

{
  "event_id": "event_YmcGFfICPRXBDfgqcpKit",
  "type": "input_audio_buffer.speech_stopped",
  "audio_end_ms": 2823,
  "item_id": "item_Fu4bF8iduL8nfJVsbKb3L"
}

input_audio_buffer.committed

server_vad模式下,当检测到用户说话结束时,服务端会自动提交并返回此事件。在非server_vad模式下,当客户端完成音频发送input_audio_buffer.commit事件的服务端响应。

参数

类型

说明

event_id

string

本次事件的标识。

type

string

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

item_id

string

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

{
  "event_id": "event_WwJkVCuJE8CNaRlMWdn8U",
  "type": "input_audio_buffer.committed",
  "item_id": "item_Fu4bF8iduL8nfJVsbKb3L"
}

input_audio_buffer.cleared

客户端发送input_audio_buffer.clear事件后,服务端将返回input_audio_buffer.cleared事件。

参数

类型

说明

event_id

string

本次事件的标识。

type

string

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

{
  "event_id": "event_1121",
  "type": "input_audio_buffer.cleared"
}

conversation.item.created

当对话项创建时返回该事件。

参数

类型

说明

event_id

string

本次事件的标识。

type

string

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

item

object

要添加到对话中的条目。

item.id

string

对话项的唯一ID。

item.object

string

始终为 realtime.item 。

item.status

string

对话项的状态。

item.role

string

消息发送的角色。

item.content

array

消息的内容。

{
    "event_id": "event_FecZDYhYi4LlVyjsbtyMa",
    "type": "conversation.item.created",
    "item": {
        "id": "item_Dm13TFlfnCx9IrosLFyeX",
        "object": "realtime.item",
        "type": "message",
        "status": "in_progress",
        "role": "assistant",
        "content": [
            {
                "type": "input_audio"
            }
        ]
    }
}

conversation.item.input_audio_transcription.completed

此事件是用户音频写入用户音频缓冲区后的音频转录输出。Realtime 模型可以接收音频输入,输入音频的转录是一个在单独的自动语音识别模型上运行的独立过程,目前始终为 gummy-realtime-v1 。转录文本可能与模型的解释有所不同,可以被作为参考。

参数

类型

说明

event_id

string

本次事件的标识。

type

string

事件类型,该事件下固定为conversation.item.input_audio_transcription.completed

item_id

string

包含音频的用户消息项的 ID。

content_index

integer

包含音频的内容部分的索引。

transcript

string

转录的文本内容。

{
    "event_id": "event_OHMnbeeCHHoVrJDhFBMNY",
    "type": "conversation.item.input_audio_transcription.completed",
    "item_id": "item_Fu4bF8iduL8nfJVsbKb3L",
    "content_index": 0,
    "transcript": "喂,喂。"
}

conversation.item.input_audio_transcription.failed

当开启输入音频转写且用户音频转写失败时,系统会返回服务器 conversation.item.input_audio_transcription.failed 事件。 此事件是与其他 error 事件分开的,以便客户端能够识别相关项。

参数

类型

说明

type

string

事件类型,该事件下固定为conversation.item.input_audio_transcription.failed

item_id

string

用户消息条目的 ID。

content_index

integer

包含音频的内容部分的索引。

error

object

转录的文本内容。

error.code

string

错误码。

error.message

string

错误消息。

error.param

string

错误相关的参数。

{
  "type": "conversation.item.input_audio_transcription.failed",
  "item_id": "<item_id>",
  "content_index": 0,
  "error": {
    "code": "<code>",
    "message": "<message>",
    "param": "<param>"
  }
}

response.created

当服务端生成新的模型响应时,会先发送此事件。

字段

类型

说明

type

string

该事件下固定为response.created

event_id

string

本次事件的标识。

response

object

响应对象。

response.id

string

响应的唯一 ID。

response.conversation_id

string

当前会话的唯一id。

response.object

string

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

response.status

string

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

response.modalities

array

响应的模态。

response.voice

string

模型生成音频的音色。

response.output

array

此事件下目前为空。

{
    "event_id": "event_JIyHMxVNc9gWgflLBiH1w",
    "type": "response.created",
    "response": {
        "id": "resp_Vb3496XSAdbX732ybCL17",
        "object": "realtime.response",
        "conversation_id": "conv_NGFEtyikW1PRDopyZ52Yv",
        "status": "in_progress",
        "modalities": [
            "text",
            "audio"
        ],
        "voice": "Chelsie",
        "output_audio_format": "pcm16",
        "output": []
    }
}

response.done

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

字段

类型

说明

type

string

固定为response.done

response

object

响应对象。

response.id

string

响应的唯一 ID。

response.object

string

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

response.conversation_id

string

当前会话的唯一id。

response.status

string

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

response.modalities

array

响应的模态。

response.voice

string

模型生成音频的音色。

response.output

array

响应的输出。

response.output.id

string

响应输出对应的ID。

response.output.object

string

输出项的对象类型,当前固定为"realtime.item"

response.output.type

string

输出项的类型,当前固定为"message"

response.output.status

string

输出项的状态,取值范围["completed","incompleted"]。

response.output.role

string

输出项的角色,取值范围["user","assistant","system"]。

response.output.content

object

输出项的具体内容。

当用户输入是文本时,则返回type=text,text={大模型推理结果};

当用户输入是音频时, 则返回type=audio, transcript={大模型推理结果};

response.uage

object

本次响应对应的usage信息。

{
    "event_id": "event_W7QDmx8EWyInRnRp1O7Df",
    "type": "response.done",
    "response": {
        "id": "resp_Vb3496XSAdbX732ybCL17",
        "object": "realtime.response",
        "conversation_id": "conv_NGFEtyikW1PRDopyZ52Yv",
        "status": "completed",
        "modalities": [
            "text",
            "audio"
        ],
        "voice": "Chelsie",
        "output_audio_format": "pcm16",
        "output": [
            {
                "id": "item_Ln9VTSx895BCbxuSKWmtg",
                "object": "realtime.item",
                "type": "message",
                "status": "completed",
                "role": "assistant",
                "content": [
                    {
                        "type": "audio",
                        "transcript": "嗯,你要是有啥想法或者问题都可以跟我说呀。"
                    }
                ]
            }
        ],
        "usage": {
            "total_tokens": 261,
            "cached_tokens": 0,
            "input_tokens": 127,
            "output_tokens": 134,
            "input_token_details": {
                "text_tokens": 48,
                "audio_tokens": 79
            },
            "output_token_details": {
                "text_tokens": 14,
                "audio_tokens": 120
            }
        }
    }
}

response.text.delta

模型增量生成新的文本时,系统会返回服务器 response.text.delta 事件。

字段

类型

说明

type

string

固定为response.text.delta

response

object

响应对象。

item_id

string

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

output_index

integer

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

content_index

integer

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

delta

string

返回的增量文本。

{
  "event_id": "event_B1lIeyOXR7qJMEExbqtTG",
  "type": "response.text.delta",
  "response_id": "resp_B1lIdtjF4Noqpn5NOjznj",
  "item_id": "item_B1lIdJsAJlJiFs8ztWpJt",
  "output_index": 0,
  "content_index": 0,
  "delta": "How"
}

response.text.done

当模型生成的文本结束时,系统会返回服务器 response.text.done 事件。

当响应中断、不完整或取消时,系统也会返回此事件。

字段

类型

说明

type

string

固定为response.text.done

response_id

string

响应的ID。

item_id

string

消息项ID。

output_index

integer

响应输出项的索引。

content_index

integer

响应输出项中内部部分的索引。

text

string

模型输出的最终完整文本。

{
  "event_id": "event_B1lIeE2Nac33zn5V7h2mm",
  "type": "response.text.done",
  "response_id": "resp_B1lIdtjF4Noqpn5NOjznj",
  "item_id": "item_B1lIdJsAJlJiFs8ztWpJt",
  "output_index": 0,
  "content_index": 0,
  "text": "How can I assist you today?"
}

response.audio.delta

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

字段

类型

说明

type

string

固定为response.audio.delta

response_id

string

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

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_P79OOMs8LnrXVpiIHUCKR",
  "item_id": "item_OFaPGtzfWCPyGzxnuEX9i",
  "output_index": 0,
  "content_index": 0,
  "delta": "{base64 audio}",
}

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_B1osWMWoDRYyITDyNYcBu",
  "type": "response.audio.done",
  "response_id": "resp_P79OOMs8LnrXVpiIHUCKR",
  "item_id": "item_OFaPGtzfWCPyGzxnuEX9i",
  "output_index": 0,
  "content_index": 0
}

response.audio_transcript.delta

当模型增量生成新的audio对应的文本时,系统会返回服务器 response.audio_transcript.delta 事件。

字段

类型

说明

type

string

固定为response.audio_transcript.delta。

response_id

string

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

item_id

string

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

output_index

integer

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

content_index

integer

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

delta

string

增量文本。

{
    "event_id": "event_OcoAVmmbMQnirKeVFag9x",
    "type": "response.audio_transcript.delta",
    "response_id": "resp_P79OOMs8LnrXVpiIHUCKR",
    "item_id": "item_OFaPGtzfWCPyGzxnuEX9i",
    "output_index": 0,
    "content_index": 0,
    "delta": "你好"
}

response.audio_transcript.done

当模型完成生成新的audio对应的文本时,系统会返回服务器 response.audio_transcript.done 事件。

字段

类型

说明

type

string

固定为response.audio_transcript.done。

response_id

string

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

item_id

string

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

output_index

integer

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

content_index

integer

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

part

object

最终生成的完整文本。

{
    "event_id": "event_VN4Q4GJugLcc1S23viW8E",
    "type": "response.audio_transcript.done",
    "response_id": "resp_P79OOMs8LnrXVpiIHUCKR",
    "item_id": "item_JvJauNH2CTXb1D9WV6pD4",
    "output_index": 0,
    "content_index": 0,
    "part": {
        "type": "audio",
        "text": "你好,我是阿里云研发的大规模语言模型,我叫通义千问。有什么我可以帮助你的吗?"
    }
}

response.output_item.added

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

字段

类型

说明

type

string

固定为response.output_item.added

response_id

string

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

output_index

integer

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

item

object

输出项信息。

item.id

string

输出项的唯一ID。

item.object

string

始终为 realtime.item 。

item.status

string

输出项的状态。

item.role

string

消息发送的角色。

item.content

array

消息的内容。

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

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.role

string

消息发送的角色。

item.content

array

消息的内容。

{
    "event_id": "event_XkiwbYTBC9Wcdwy6uYJ2G",
    "type": "response.output_item.done",
    "response_id": "resp_P79OOMs8LnrXVpiIHUCKR",
    "output_index": 0,
    "item": {
        "id": "item_JvJauNH2CTXb1D9WV6pD4",
        "object": "realtime.item",
        "type": "message",
        "status": "completed",
        "role": "assistant",
        "content": [
            {
                "type": "audio",
                "text": "你好,我是阿里云研发的大规模语言模型,我叫通义千问。有什么我可以帮助你的吗?"
            }
        ]
    }
}

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_J2UixwYKZsXg7c9YXZetL",
    "type": "response.content_part.added",
    "response_id": "resp_P79OOMs8LnrXVpiIHUCKR",
    "item_id": "item_OFaPGtzfWCPyGzxnuEX9i",
    "output_index": 0,
    "content_index": 0,
    "part": {
        "type": "audio",
        "text": ""
    }
}

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_FdVUyXIa8WVk4BZJv8swq",
    "type": "response.content_part.done",
    "response_id": "resp_QeZcSlvzRmmjIURRMafY8",
    "item_id": "item_HvJYzNHXC1MnzvgBfIxJD",
    "output_index": 0,
    "content_index": 0,
    "part": {
        "type": "audio",
        "text": "我也不太清楚现在几点了呢。你可以看看手机或者时钟呀。要是还有别的事想聊聊也可以跟我说哦。"
    }
}