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
羽毛球分组比赛记分
小程序二维码

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

小夜