跳到正文

配置参考

配置

config.json 按域分组:server / concurrency / socket / logging / recording / asr / vad。各组字段如下(键名不带组前缀)。

server — 监听

字段类型默认值说明
hoststring"127.0.0.1"实际监听地址
portuint1610096实际监听端口
healthPathstring"/health"HTTP 健康检查路径
wsPathstring"/audio"WebSocket 升级路径
wsSubprotocolstring"audio.drachtio.org"要求客户端声明的子协议;为空则不强制

concurrency — 并发

字段类型默认值说明
maxSessionsint3216并发会话上限;超过后新连接返回 503
ioWorkersint32CPU 核心数I/O 工作线程数;运行时会按 FD_SETSIZE 自动抬升下限
sessionPoolSizeint3216预热的 ASR session 数;超过 maxSessions 会被裁剪
numThreadsint321ONNX 推理线程数
acceptBacklogint32SOMAXCONNlisten(backlog)
workerPollTimeoutMsint3210工作线程单轮 select 超时;夹紧到 [1, 1000]

socket — 套接字缓冲

字段类型默认值说明
tcpNoDelaybooltrue启用 TCP_NODELAY
maxFrameBytesint324194304单个 WebSocket 帧最大字节数;超过即 1002 协议错误
readBufferBytesint3265536每连接 recv 缓冲
writeBufferBytesint3265536每连接 send 缓冲(预留)
recvBufferBytesint32262144SO_RCVBUF 设置(0 表示不调整)
sendBufferBytesint32262144SO_SNDBUF 设置(0 表示不调整)

logging — 日志

字段类型默认值说明
dirstring"logs"日志目录;相对路径按运行目录解析,按天滚动
debugTextFramesboolfalse打印每条 metadata 文本帧
debugAudioFramesboolfalse每个二进制帧落一条识别状态日志
debugRecognitionStateboolfalse每次识别状态变化都落日志

recording — 录音

字段类型默认值说明
enabledboolfalse启用上行音频录制
dirstring"recordings"录制目录(相对路径按配置文件所在目录解析)

asr — 识别

字段类型默认值说明
activeModelstringsherpa-onnx-streaming-zipformer-zh-int8-2025-06-30当前加载的 ASR profile ID;必须存在于 models
modelsobject见默认 config.jsonASR profile 集合;服务启动时只加载 active profile
providerstring"cpu"ONNX 推理后端
sampleRateint3216000上行采样率;当前只支持 16kHz
debugboolfalsesherpa-onnx 内部 debug
sendPartialResultsbooltrue是否下发 partial 文本帧
decodingMethodstringgreedy_searchsherpa-onnx 解码方式;显式配置时原样使用。未配置且 hotwordProfiles 非空时自动改为 modified_beam_search
hotwordsScorefloat1.5sherpa hotword boost 分数;对所有 hotword profile 生效
hotwordsFilestring"hotword.json"独立热词配置文件路径(默认 hotword.json
defaultHotwordProfilestring"default"metadata 未指定 hotwordProfile 时默认生效的 profile ID;为空则不启用热词
hotwordProfilesobject{}静态 hotword profile 集合(推荐在 hotword.json 中配置,也兼容内嵌于 config.json
partialMinIntervalMsint32300同一句 partial 文本最小下发间隔
decodeBatchMsint3280批量 decode 节奏
idleFinalizeMsint321200有未结算音频且已有 partial 时,若“最后一帧音频”空闲,或“最后一次 speech_active=true / partial 文本变化”空闲达到该时长,则主动 flush 并下发 flushReason=audio-idlespeech_started / speech_segment_detected 不刷新该空闲计时;0 表示关闭

vad — 语音活动检测(强制启用,无开关)

字段类型默认值说明
modelstringsilero_vad.onnxsilero-vad onnx
bufferSizeSecondsfloat30.0silero-vad ring buffer
thresholdfloat0.5VAD 置信度阈值
minSilenceDurationfloat0.5触发段尾的最小静音时长
minSpeechDurationfloat0.25视为有效语音的最小时长
windowSizeint32512VAD 窗大小(采样点)
maxSpeechDurationfloat20.0单段最大语音时长,超过强制切段

asr.models 每个 profile 的字段:

字段类型说明
modelDirstring模型目录;相对 onnx-platform/models/ 解析,绝对路径原样使用
encoder / decoder / joiner / tokensstring必填模型文件名或路径;相对 modelDir 解析
bpeVocabstring可选 BPE 词表;相对 modelDir 解析
modelTypestringsherpa-onnx 模型类型提示,例如 zipformer;空字符串表示交给模型 metadata 自动识别
modelingUnitstring建模单元,例如 cjkchar

默认配置包含两个 profile:

  • sherpa-onnx-streaming-zipformer-zh-int8-2025-06-30:默认中文 int8 streaming zipformer,文件为 encoder.int8.onnx / decoder.onnx / joiner.int8.onnx / tokens.txtmodelType 默认为空以使用 metadata 自动识别
  • sherpa-onnx-streaming-zipformer-bilingual-zh-en:中英双语 streaming zipformer,文件为 encoder-epoch-99-avg-1.onnx / decoder-epoch-99-avg-1.onnx / joiner-epoch-99-avg-1.onnx / tokens.txtmodelTypezipformer

切换到中英双语模型:

json
{
  "asr": {
    "activeModel": "sherpa-onnx-streaming-zipformer-bilingual-zh-en"
  }
}

如果需要加载其他模型,请在 asr.models 中新增 profile,并把 asr.activeModel 改为对应 ID。旧的 asrModelName / asrEncoder / asrDecoder / asrJoiner / asrTokens / asrBpeVocab / asrModelType / asrModelingUnit 字段不再 兼容;缺少 asr.activeModel 或找不到对应 profile 时服务会启动失败。

热词配置与独立 hotword.json

热词配置已独立为单文件(默认 hotword.json),config.json 中只需通过 asr.hotwordsFileasr.defaultHotwordProfile 指定:

json
{
  "asr": {
    "decodingMethod": "modified_beam_search",
    "hotwordsScore": 1.5,
    "hotwordsFile": "hotword.json",
    "defaultHotwordProfile": "default"
  }
}

hotword.json 结构示例:

json
{
  "defaultHotwordProfile": "default",
  "hotwordProfiles": {
    "default": {
      "words": [
        "接收whatsapp验证码",
        "微信",
        "WhatsApp",
        "白班值",
        "华法林"
      ]
    },
    "sales": {
      "words": ["智能客服", "FreeSWITCH", "sherpa-onnx"]
    }
  }
}
  • words:字符串数组,启动时用 \n 拼接为 sherpa-compatible UTF-8 hotword lines
  • file:UTF-8 hotword 文本文件,路径相对 hotword.json 所在目录解析
  • hotword 文本不会写入 OnlineRecognizerConfig.hotwords_file;服务会在每个 WebSocket session 选定 profile 或收到动态热词后调用 CreateStream(hotwords)
  • sherpa-onnx 要求通过 CreateStream(hotwords) 使用 session 级 hotwords 时,decodingMethod 必须为 modified_beam_search。如果未显式配置 asr.decodingMethod,服务会在热词非空时自动切换;如果显式配成 greedy_search,后续启用 hotwords 的连接仍可能在创建 stream 时失败或崩溃
  • 修改 hotword.json 或热词文件后需要重启服务,不支持运行时热加载

twoPass — 句尾 2-pass 二次离线精修识别(可选)

当开启 twoPass.enabled: true 时,服务端在流式推流期间由 Pass 1(流式 Zipformer)输出实时 partial 增量字幕;当 VAD 判定句尾静音或推流结束时,自动调度离线大模型重算整句音频并输出 final 纠错终态文本。

字段类型默认值说明
enabledbooltrue是否启用 2-pass 句尾精修识别
activeModelstring"sherpa-onnx-zipformer-multi-zh-hans-2023-9-2"当前加载的 2-pass 离线模型 Profile ID;必须存在于 twoPass.models
modelsobjectconfig.json2-pass 离线模型 Profile 集合
recognizerPoolSizeint324离线识别器实例池容量,并发支撑 final 句级精修
numThreadsint321每个离线识别实例计算线程数
providerstring"cpu"ONNX 推理后端("cpu"
hotwordsScorefloat1.5离线热词加权分数
fallbackToFirstPassbooltrue当 2-pass 识别异常或返回空时,是否自动回退 Pass 1 的识别文本
debugboolfalse是否打印 2-pass 内部调试日志

twoPass.models 支持 Transducer 离线大模型SenseVoice / Paraformer 离线模型 两类配置:

1. Transducer 架构(离线 Zipformer 大模型)

字段类型说明
modelDirstring离线模型目录;相对 onnx-platform/models/ 解析
encoder / decoder / joiner / tokensstring必填模型文件与词表;相对 modelDir 解析
bpeVocabstring可选 BPE 词表(如 bpe.modelbbpe.model);相对 modelDir 解析
modelTypestring固定为 "transducer"(或留空自动探测)
modelingUnitstring建模单元(如 "cjkchar""bpe"

2. SenseVoice / Paraformer 架构

字段类型说明
modelDirstring离线模型目录;相对 onnx-platform/models/ 解析
modelstring离线模型文件名(如 model.int8.onnx);相对 modelDir 解析
tokensstring词表文件名(如 tokens.txt);相对 modelDir 解析
modelTypestring模型架构类型:"sense_voice""paraformer"
languagestring目标识别语种("zh""en""auto" 等,仅 SenseVoice 生效)
useItnbool是否启用逆文本归一化(ITN,仅 SenseVoice 生效)

预配置离线模型选型对比

  1. sherpa-onnx-zipformer-multi-zh-hans-2023-9-2(默认推荐):
    • 中文多数据集离线 Zipformer 大模型(Transducer 架构),高准确率,与流式中文模型语种对齐。
  2. sherpa-onnx-zipformer-zh-en-2023-11-22
    • 中英双语离线 Zipformer 大模型(Transducer 架构 + BPE 建模),适合中英混合通话场景。
  3. sherpa-onnx-sense-voice
    • SenseVoice 多语种离线大模型,自带 ITN 逆文本归一化与情绪/事件检测能力。
  4. sherpa-onnx-paraformer-zh
    • 阿里 Paraformer 高精度纯中文离线模型。

吞吐建议

调优时优先调整:

  • concurrency.ioWorkers:与并发连接数匹配,每个工作线程受 FD_SETSIZE 约束 (Windows 默认 1024,本进程编译期强制设到 1024,单工作线程上限约 992 连接)
  • concurrency.sessionPoolSize:建议接近或等于 concurrency.maxSessions,避免 连接抖动反复冷启动 recognizer / VAD
  • concurrency.numThreads:ONNX 推理线程,配合 CPU 调整
  • asr.partialMinIntervalMsasr.decodeBatchMs:影响 partial 实时性与 CPU 占用
  • recording.enabled:默认关闭;只在排查问题时打开

文档与代码在同一仓库维护,现有 Markdown 是唯一内容源。