DSH 语转文插件:把本地 SenseVoice 暴露成 OpenAI 兼容接口

前言

DSH 自带的语转文能力只通过浏览器 ctx.speechToText 和 /api 的 Remote 通道提供,音频以 base64 传输。第三方服务(如 AstrBot、Open WebUI、LangChain)直接使用 OpenAI 的 /v1/audio/transcriptions 接口,无法复用 DSH 内置的本地模型。这个插件在 DSH 的 Web 服务器上补出这一层,把 DSH 内置的 SenseVoice 语音识别模型暴露为标准的 OpenAI 接口。

这是什么

dsh-asr-openai 是一个 DSH 插件,由 Caiccc-a11y 维护。它让任何 OpenAI 兼容客户端(包括 AstrBot、LangChain、官方 openai SDK)都能通过 /v1/audio/transcriptions 等标准接口调用 DSH 内置的 SenseVoice 模型,无需在第三方服务里部署语音识别组件。

核心功能

  • 提供 OpenAI 兼容的 HTTP 接口:/v1/audio/transcriptions、/v1/audio/translations、/v1/models、/v1/models/{id}、/v1/health。
  • 支持常见的音频格式:mp3、wav、m4a、ogg、webm、flac 等,内部统一用 ffmpeg 转成 16 kHz 单声道 PCM16。
  • 支持多种响应格式:json(默认,返回 {"text":"..."})、text、srt、vtt、verbose_json。
  • 超长音频自动按 segmentSeconds(默认 60 秒)切片,逐段识别后拼接文本。
  • 语言支持:auto、zh、en、yue、ja、ko,也接受 zh-CN、en-US 等变体。
  • 鉴权使用 Authorization: Bearer sk-local(也接受 x-api-key 头),常量时间比较。

安装与启用

仓库地址:https://github.com/Caiccc-a11y/dsh-asr-openai

从 GitHub 拉取(推荐):

git clone https://github.com/Caiccc-a11y/dsh-asr-openai.git ~/dsh-asr-openai
bash ~/dsh-asr-openai/install.sh          # 默认接入 web profile
bash ~/dsh-asr-openai/install.sh myprofile  # 或指定 profile

install.sh 会把它以 link: 形式加进 ~/.dsh/profiles/<profile>/package.json 的 dependencies,并追加到 dsh.profile.bundles(原文件备份为 package.json.bak)。

手动接入(与脚本等价):

  1. 插件目录:本仓库根目录(例如 ~/dsh-asr-openai)。
  2. 在 ~/.dsh/profiles/web/package.json 里:
    - dependencies 增加 "@local/dsh-asr-openai": "link:/绝对路径/dsh-asr-openai"
    - dsh.profile.bundles 增加 "@local/dsh-asr-openai"
  3. 重启 DSH 生效:
   systemctl --user restart dsh-web.service
   # 或在 DSH 里执行 /dsh-restart

典型用法

安装完成后,使用 curl 测试:

curl -s -H 'Authorization: Bearer sk-local' \
     -F 'file=@/path/to/audio.wav' -F 'model=sensevoice-local' \
     http://127.0.0.1:3080/v1/audio/transcriptions

返回格式示例(默认 json):

{ "text": "识别出的文本内容" }

在 AstrBot 中配置(面板 → 服务提供商 → 新增 → 类型选「语音转文字 / OpenAI Whisper API」):

字段 值
API Key sk-local
API Base URL http://127.0.0.1:3080/v1
模型 sensevoice-local

对应 JSON 配置:

{
  "type": "openai_whisper_api",
  "api_key": "sk-local",
  "api_base": "http://127.0.0.1:3080/v1",
  "model": "sensevoice-local",
  "enable": true
}

配置项(profile 的 cordis.patch.yml)

- id: asr-openai
  config:
    apiKey: sk-local        # 共享密钥;改这里同时改 AstrBot 的 API Key
    requireApiKey: true     # false 则不校验(仅建议在纯本机回环时)
    maxUploadBytes: 33554432
    segmentSeconds: 60
    ffmpegPath: ffmpeg      # 可写绝对路径,避免 systemd 下 PATH 差异
    ffmpegTimeoutMs: 120000
    transcriptionTimeoutMs: 300000

排错

现象 原因 / 处理
401 invalid_api_key AstrBot 里的 API Key 与插件 apiKey 不一致
503 ... registry is not mounted 该 profile 没启用 @deepseek-ai/dsh-experimental-voice-input-bundle
503 No speech provider is registered SenseVoice 插件没激活;检查 /v1/health 的 providers
Invalid speech WAV 旧版本问题,已修复(改为自行构造规范 WAV 头)
第一次转写很慢 模型冷启动(约 1 亿参数 INT8),之后 worker 常驻,idleTimeoutMs 默认 5 分钟

适用场景与注意

适合在本地部署 DSH 并需要把内置语音识别能力暴露给第三方服务的场景。插件以当前 DSH 进程权限运行,安装前应检查源码与许可证。模型目录示例:/home/cai/.dsh/speech-to-text/sensevoice/models/sensevoice-onnx(229 MB)。

结尾

这个插件把 DSH 内置的 SenseVoice 模型以 OpenAI 兼容接口对外暴露,让 AstrBot 等第三方客户端可以直接调用,无需额外部署识别组件。更多细节与源码见:

  • 目录页:https://www.skillhub.cn/plugins/Caiccc-a11y/dsh-asr-openai
  • GitHub:https://github.com/Caiccc-a11y/dsh-asr-openai
羽毛球分组比赛记分
小程序二维码

欢迎使用《羽毛球分组比赛记分》微信小程序

Xiaoye