在用户与智能体互动的过程中,你可能需要打断功能来中止当前智能体的回答,开启新一轮对话。RTC 提供手动打断和自动打断两种打断方式,你可根据场景不同自由选择使用,提升对话的流畅度和自然度。
场景 | 描述 |
---|---|
客服对话 | 使用较短的自动打断时长,配合手动打断,实现快速响应和精准控制。 |
在线教育 | 采用较长的自动打断时长,避免误打断学生发言,教师可使用手动打断进行必要干预。 |
多人会议 | 结合自动打断和手动打断,灵活管理发言顺序和时长。 |
你已参考场景搭建 了解 AI 应用的整体构建流程。
自动打断由系统根据用户的音频输入,自动判断是否需要中止智能体的输出。支持两种方式:
StartVoiceChat
接口,Config.InterruptMode
设置为 0
,开启语音自动打断功能。此时用户发出声音智能体便立刻停止输出,等待用户语音输入开启新一轮对话。调用StartVoiceChat
接口,Config.ASRConfig.InterruptConfig.InterruptSpeechDuration
设置为指定值,实现基于用户输入音频时长判定的自动打断。此时只有房间内用户持续说话时间达到设定值时,智能体才会停止输出。该参数取值范围为[200,3000]
,单位为 ms。
手动打断为用户主动发起打断请求,通过点击按钮或快捷键方式,主动打断智能体的回答。
通过服务端或客户端均可实现手动打断操作,你可根据业务请求端种类选择对应的方式。例如你在开发 AI 应用时选择服务端响应请求,则使用服务端实现手动打断,降低请求延迟。
调用 UpdateVoiceChat
接口,设置以下参数打断智能体输出:
参数 | 类型 | 描述 |
---|---|---|
AppId | String | RTC 应用 AppId,参看获取 AppId。 |
RoomId | String | 自定义会话房间 ID。 |
TaskId | String | 自定义会话任务 ID。 |
Command | String | 填入interrupt ,表示打断智能体。 |
你可参看以下示例从服务端实现打断操作:
POST https://rtc.volcengineapi.com?Action=UpdateVoiceChat&Version=2024-12-01 { "AppId": "661e****543cf", "RoomId": "Room1", "TaskId": "task1", "Command": "interrupt" }
使用 SendUserBinaryMessage
接口实现打断操作。该接口的 buffer
参数需要传入特定格式的内容,下图展示了 buffer
参数的格式:
参数名 | 类型 | 描述 |
---|---|---|
magic_number | binary | 消息格式标识符,当前场景消息格式固定为 ctrl ,用于标识该消息为控制消息。 |
length | binary | 打断消息长度,单位为字节,采用大端序(Big-endian)存储方式,用于说明 control_message 字段的字节长度。 |
control_message | binary | 打断行为配置信息,采用 JSON 格式,具体内容格式参看 control_message 格式。 |
control_message:
参数名 | 类型 | 描述 |
---|---|---|
Command | String | 控制命令,此处填入 interrupt ,表示打断智能体。 |
你可参看以下示例从客户端实现打断操作:
// 发送打断指令 void sendInterruptMessage(const std::string &uid) { nlohmann::json json_data; json_data["Command"] = "interrupt"; sendUserBinaryMessage(uid, json_data.dump()); } void buildBinaryMessage(const std::string& magic_number, const std::string& message, size_t& binary_message_length, std::shared_ptr<uint8_t[]>& binary_message) { //将字符串包装成 TLV auto magic_number_length = magic_number.size(); auto message_length = message.size(); binary_message_length = magic_number_length + 4 + message_length; binary_message = std::shared_ptr<uint8_t[]>(new uint8_t[binary_message_length]); std::memcpy(binary_message.get(), magic_number.data(), magic_number_length); binary_message[magic_number_length] = static_cast<uint8_t>((message_length >> 24) & 0xFF); binary_message[magic_number_length+1] = static_cast<uint8_t>((message_length >> 16) & 0xFF); binary_message[magic_number_length+2] = static_cast<uint8_t>((message_length >> 8) & 0xFF); binary_message[magic_number_length+3] = static_cast<uint8_t>(message_length & 0xFF); std::memcpy(binary_message.get()+magic_number_length+4, message.data(), message_length); } int sendUserBinaryMessage(const std::string &uid, const std::string& message) { if (rtcRoom_ != nullptr) { size_t length = 0; std::shared_ptr<uint8_t[]> binary_message = nullptr; buildBinaryMessage("ctrl", message, length, binary_message); return rtcRoom_->sendUserBinaryMessage(uid.c_str(), static_cast<int>(length), binary_message.get()); } return -1;