Skip to content

API 参考文档

本文档介绍火山引擎实时对话式 AI 的核心 API,包括 StartVoiceChat、UpdateVoiceChat、StopVoiceChat 及相关配置参数。

StartVoiceChat

启动语音会话,返回 RTC 连接凭证。

请求地址POST https://rtc.volcengineapi.com?Action=StartVoiceChat&Version=2024-12-01

请求头:需要使用 AccessKey 进行签名,参考 认证代理服务

请求参数

参数类型必填说明
AppIdstringRTC 应用 ID
RoomIdstring房间 ID
TaskIdstring任务 ID,用于标识会话
AgentConfigobject智能体配置
Configobject会话配置,包含 ASR、TTS、LLM 等参数

AgentConfig

智能体配置:

参数类型必填说明
TargetUserIdstring[]目标用户 ID 列表
UserIdstring智能体用户 ID
WelcomeMessagestring欢迎语
EnableConversationStateCallbackboolean启用会话状态回调
AnsModenumber降噪模式(0-3)
VoicePrintobject声纹识别配置

VoicePrint 配置

参数类型说明
Modenumber声纹识别模式(0: 关闭, 1: 开启)
IdListstring[]声纹 ID 列表

Config

会话配置,包含以下子配置:

json
{
  "ASRConfig": { ... },
  "TTSConfig": { ... },
  "LLMConfig": { ... },
  "InterruptMode": 0
}
参数类型说明
ASRConfigobject语音识别配置
TTSConfigobject语音合成配置
LLMConfigobject大模型配置
InterruptModenumber打断模式(0: 语义打断, 1: 手动打断)

响应

json
{
  "ResponseMetadata": {
    "RequestId": "20250104123456789abcdef01234567",
    "Action": "StartVoiceChat",
    "Version": "2024-12-01",
    "Service": "rtc",
    "Region": "cn-north-1"
  },
  "Result": {
    "AppId": "your-app-id",
    "RoomId": "room-uuid",
    "UserId": "user-uuid",
    "Token": "rtc-token..."
  }
}
字段说明
Result.AppIdRTC 应用 ID
Result.RoomIdRTC 房间 ID
Result.UserIdRTC 用户 ID
Result.TokenRTC 访问令牌(24 小时有效)

官方文档:StartVoiceChat

StopVoiceChat

停止语音会话,释放资源。

请求地址POST https://rtc.volcengineapi.com?Action=StopVoiceChat&Version=2024-12-01

请求参数

参数类型必填说明
AppIdstringRTC 应用 ID
RoomIdstring房间 ID
TaskIdstring任务 ID

响应

json
{
  "ResponseMetadata": {
    "RequestId": "20250104123456789abcdef01234567",
    "Action": "StopVoiceChat",
    "Version": "2024-12-01"
  },
  "Result": {}
}

官方文档:StopVoiceChat

UpdateVoiceChat

更新进行中的语音会话,支持打断、Function calling、自定义播报等操作。

请求地址POST https://rtc.volcengineapi.com?Action=UpdateVoiceChat&Version=2024-12-01

请求参数

参数类型必填说明
AppIdstringRTC 应用 ID
RoomIdstring房间 ID
TaskIdstring任务 ID
Commandstring操作命令
Messagestring播报内容(最长 200 字符)
InterruptModenumber播报优先级

Command 命令类型

Command说明
Interrupt打断当前智能体输出
ExternalTextToSpeech自定义文本转语音播报
FunctionCallResult返回 Function calling 结果

InterruptMode 优先级

用于 ExternalTextToSpeech 命令时指定播报优先级:

说明
1高优先级:终止当前交互,立即播放
2中优先级:等待当前交互结束后播放
3低优先级:如果正在交互则丢弃

使用示例

打断智能体

json
{
  "AppId": "your-app-id",
  "RoomId": "room-uuid",
  "TaskId": "task-id",
  "Command": "Interrupt"
}

自定义播报

json
{
  "AppId": "your-app-id",
  "RoomId": "room-uuid",
  "TaskId": "task-id",
  "Command": "ExternalTextToSpeech",
  "Message": "您有一条新消息",
  "InterruptMode": 1
}

响应

json
{
  "ResponseMetadata": {
    "RequestId": "20250104123456789abcdef01234567",
    "Action": "UpdateVoiceChat",
    "Version": "2024-12-01"
  },
  "Result": {}
}

官方文档:UpdateVoiceChat

ASRConfig

语音识别配置:

参数类型必填说明
Providerstring服务提供商,固定 volcano
ProviderParamsobject提供商参数
VADConfigobject语音活动检测配置
VolumeGainnumber音量增益(0.0-1.0),默认 0.5
TurnDetectionModenumber轮次检测模式
InterruptConfigobject打断配置

ProviderParams

参数类型说明
AppIdstringASR 应用 ID
Modestring识别模式:smallmodel(小模型)、bigmodel(大模型)
Clusterstring服务集群,默认 volcengine_streaming_common
contextstring热词上下文(JSON 格式)
boosting_table_idstring热词表 ID
correct_table_idstring纠错表 ID

VADConfig

语音活动检测配置:

参数类型说明
SilenceTimenumber静音判定时长(毫秒),默认 600
SpeechTimenumber语音判定时长(毫秒)
PrefixTimenumber前缀时长(毫秒)
SuffixTimenumber后缀时长(毫秒)
Sensitivitynumber灵敏度
AIVADboolean是否启用 AI VAD

InterruptConfig

打断配置:

参数类型说明
InterruptSpeechDurationnumber打断语音时长(毫秒),默认 400
InterruptKeywordsstring[]语义打断关键词列表

示例配置

json
{
  "Provider": "volcano",
  "ProviderParams": {
    "AppId": "your-asr-app-id",
    "Mode": "smallmodel",
    "Cluster": "volcengine_streaming_common"
  },
  "VADConfig": {
    "SilenceTime": 600
  },
  "VolumeGain": 0.5,
  "TurnDetectionMode": 0,
  "InterruptConfig": {
    "InterruptSpeechDuration": 400,
    "InterruptKeywords": ["停", "暂停", "等一下", "stop", "wait"]
  }
}

TTSConfig

语音合成配置:

参数类型必填说明
Providerstring服务提供商,固定 volcano
ProviderParamsobject提供商参数
IgnoreBracketTextnumber[]忽略的括号类型

ProviderParams

参数类型说明
appobject应用配置
audioobject音频配置
ResourceIdstringTTS 资源 ID
Additionsobject附加配置

app 配置

参数类型说明
appidstringTTS 应用 ID
tokenstringTTS 应用 Token
clusterstring服务集群,默认 volcano_tts

audio 配置

参数类型说明范围
voice_typestring音色类型见下方音色列表
speed_rationumber语速比例0.5-2.0,默认 1.0
pitch_rationumber音调比例0.5-2.0,默认 1.0
volume_rationumber音量比例0.5-2.0,默认 1.0
emotionstring情感类型happysadangryneutral
emotion_strengthnumber情感强度0.0-1.0,默认 0.8

常用音色

音色 ID说明
BV033_streaming女声,温柔
BV001_streaming男声,磁性
BV700_streaming女声,甜美
BV406_streaming男声,沉稳

更多音色参考:火山引擎 TTS 音色列表

示例配置

json
{
  "Provider": "volcano",
  "ProviderParams": {
    "app": {
      "appid": "your-tts-app-id",
      "token": "your-tts-token",
      "cluster": "volcano_tts"
    },
    "audio": {
      "voice_type": "BV033_streaming",
      "speed_ratio": 1.2,
      "pitch_ratio": 1.1,
      "volume_ratio": 1.0,
      "emotion": "happy",
      "emotion_strength": 0.8
    },
    "ResourceId": "your-resource-id"
  }
}

LLMConfig

大模型配置:

参数类型必填说明
Modestring模式:ArkV3(方舟)或 CustomLLM(自定义)
UrlstringCustomLLM 必填CustomLLM 回调地址
APIKeystringAPI 认证密钥
EndPointIdstringArkV3 必填方舟模型端点 ID
ModelNamestring模型名称
SystemMessagesstring[]系统提示词列表
UserPromptsobject[]预设对话历史
Temperaturenumber采样温度(0.0-1.0),默认 0.5
TopPnumber核采样概率(0.0-1.0),默认 0.9
MaxTokensnumber最大生成 token 数,默认 256
HistoryLengthnumber保留的历史轮数,默认 15
EnableRoundIdboolean启用轮次 ID
VisionConfigobject视觉理解配置
Customstring自定义参数(JSON 字符串),透传给 CustomLLM

VisionConfig

视觉理解配置:

参数类型说明
Enableboolean是否启用视觉理解
SnapshotConfigobject截图配置

UserPrompts

预设对话历史,用于引导对话风格:

json
[
  { "Role": "assistant", "Content": "你好!有什么可以帮助你的?" },
  { "Role": "user", "Content": "你好" }
]

CustomLLM 模式示例

json
{
  "Mode": "CustomLLM",
  "Url": "https://your-server.com/chat-stream",
  "APIKey": "your-api-key",
  "ModelName": "qwen-flash",
  "Temperature": 0.5,
  "TopP": 0.9,
  "MaxTokens": 256,
  "HistoryLength": 15,
  "EnableRoundId": true,
  "VisionConfig": {
    "Enable": false
  },
  "UserPrompts": [
    { "Role": "assistant", "Content": "嗨~我是助手,很高兴见到你!" }
  ]
}

ArkV3 模式示例

json
{
  "Mode": "ArkV3",
  "EndPointId": "your-endpoint-id",
  "Temperature": 0.7,
  "MaxTokens": 512
}

CustomLLM 回调

使用 CustomLLM 模式时,火山引擎会将用户语音识别结果回调到自定义服务。

回调流程

用户语音 → 火山引擎 ASR → CustomLLM 服务 → 火山引擎 TTS → 用户

请求格式

火山引擎发送到 CustomLLM 服务的请求:

http
POST /chat-stream HTTP/1.1
Authorization: Bearer YOUR_API_KEY
Content-Type: application/json

{
  "messages": [
    {"role": "system", "content": "你是一个智能助手"},
    {"role": "user", "content": "你好"}
  ],
  "stream": true,
  "temperature": 0.7,
  "max_tokens": 256,
  "device_id": "custom-device-id"
}

请求字段

字段说明
messages对话历史,OpenAI 格式
stream固定 true,流式响应
temperature采样温度
max_tokens最大生成长度
device_id自定义参数,从 LLMConfig.Custom 透传

响应格式

响应需遵循 OpenAI SSE 格式:

data: {"id":"resp-1","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null}],"model":"qwen-flash","created":1704355200}

data: {"id":"resp-1","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"你好"},"finish_reason":null}],"model":"qwen-flash","created":1704355200}

data: {"id":"resp-1","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"!"},"finish_reason":"stop"}],"model":"qwen-flash","created":1704355200}

data: [DONE]

响应要点

  • 必须返回 SSE 流式响应
  • Content-Type: text/event-stream
  • 每行以 data: 开头
  • 最后一行为 data: [DONE]

官方文档:CustomLLM 接入

RTC Token

客户端加入 RTC 房间需要 Token。Token 由服务端使用 AppKey 生成。

Token 结构

Token = Version + AppId + Base64(Message + Signature)
  • Version:固定值 001
  • AppId:24 位应用标识符
  • Message:二进制编码的负载(RoomId、UserId、过期时间、权限等)
  • Signature:使用 AppKey 进行 HMAC-SHA256 签名

Token 权限

权限说明
PrivPublishStream发布音视频流
PrivSubscribeStream订阅音视频流

有效期

默认 24 小时(86400 秒)。过期后需重新生成。

生成示例

typescript
import { AccessToken } from './rtctoken'

const token = new AccessToken(appId, appKey, roomId, userId)
const expireAt = Math.floor(Date.now() / 1000) + 24 * 3600
token.addPrivilege('PrivPublishStream', expireAt)
token.addPrivilege('PrivSubscribeStream', expireAt)
token.expireTime(expireAt)
const tokenString = token.serialize()

Token 生成库参考 安装与测试 - 生成 RTC Token

错误码

响应格式

json
{
  "ResponseMetadata": {
    "RequestId": "xxx",
    "Action": "StartVoiceChat",
    "Error": {
      "Code": "InvalidParameter",
      "Message": "参数 AppId 不能为空"
    }
  }
}

公共错误码

错误码HTTP 状态码说明
MissingParameter400缺少必要参数(如 Action、Version)
InvalidParameter400参数格式错误或无效
MissingRequestInfo400缺少请求信息(如 X-Date 头)
InvalidTimestamp400请求过期或时间戳无效,检查 UTC 时间格式
InvalidAuthorization400Authorization 头格式错误
InvalidCredential400Credential 格式错误
InvalidAccessKey401AccessKey 无效或格式错误
SignatureDoesNotMatch401签名验证失败,检查 SecretKey
InvalidSecretToken401STS 临时凭证过期或无效
AccessDenied403IAM 权限不足
ServiceNotFound404服务不存在
InvalidActionOrVersion404API Action 或 Version 不存在
FlowLimitExceeded429请求频率超限,降低 QPS
InternalError500内部错误
InternalServiceError502服务网关错误
ServiceUnavailableTemp503服务暂时不可用
InternalServiceTimeout504服务超时

业务错误码

错误码说明
RoomNotExist房间不存在
TaskNotExist任务不存在
InvalidTokenRTC Token 无效或过期

官方文档:公共错误码

相关资源