Skip to content

Add doubao asr|增加豆包语音识别模型支持 - #3330

Open
kakuibeyond wants to merge 3 commits into
QuantumNous:mainfrom
kakuibeyond:add_doubao_asr
Open

Add doubao asr|增加豆包语音识别模型支持#3330
kakuibeyond wants to merge 3 commits into
QuantumNous:mainfrom
kakuibeyond:add_doubao_asr

Conversation

@kakuibeyond

@kakuibeyond kakuibeyond commented Mar 19, 2026

Copy link
Copy Markdown

改动描述

VolcEngine 渠道新增了 Doubao ASR 支持,使项目可以通过现有 OpenAI 兼容接口 /v1/audio/transcriptions 转发到豆包语音识别服务,实现“上传音频文件,返回转录文本”。

实现目标是保持用户侧调用方式不变:

  • 上游客户端继续使用 OpenAI/Whisper 兼容的 multipart/form-data
  • 渠道侧继续使用 appid|access_token 配置方式
  • new-api 内部自动将请求转换为 VolcEngine/Doubao ASR 的官方请求格式

改动内容

  1. 新增 VolcEngine ASR 转换逻辑
  • 新增 relay/channel/volcengine/asr.go
  • RelayModeAudioTranscription 下:
    • 从 multipart 请求中读取 file
    • 将音频内容转为 base64
    • 推断音频格式
    • 构造 Doubao ASR 请求体
    • 转发到官方 flash ASR 接口
  1. 扩展 VolcEngine adaptor
  • 修改 relay/channel/volcengine/adaptor.go
  • 新增 /v1/audio/transcriptions 对应的 VolcEngine 分支:
    • 请求 URL 改为 Doubao ASR 官方地址
    • 请求头改为 X-Api-App-Key / X-Api-Access-Key / X-Api-Resource-Id / X-Api-Request-Id / X-Api-Sequence
    • 响应根据 response_format 映射回 OpenAI 兼容输出
  1. 新增模型可见性
  • 修改 relay/channel/volcengine/constants.go
  • bigmodel(doubao asr 默认模型名称) 加入 VolcEngine 模型列表,便于渠道配置和模型选择
  1. 更新 API 文档
  • 修改 docs/openapi/relay.json
  • /v1/audio/transcriptions 说明中补充:该接口除 Whisper 外,也可通过 VolcEngine/Doubao 语音渠道转发到豆包 ASR

接口映射说明

客户端入口仍为:

  • POST /v1/audio/transcriptions

本 PR 内部映射到 Doubao 官方 flash ASR 接口:

  • POST https://openspeech.bytedance.com/api/v3/auc/bigmodel/recognize/flash

当前支持的返回格式:

  • response_format 为空或 json:返回 {"text":"..."}
  • response_format=text:返回纯文本

参考的 Doubao 官方文档

  1. Doubao 语音识别极速版接口
  1. 识别结果字段说明

另外也核对了标准版两段式接口文档,但考虑到该方式需要调两次api(submit/query)缺乏通用性,因此本 PR 未采用该方案:

为什么采用极速版接口

  • 更符合 new-api 当前 /v1/audio/transcriptions 的同步接口语义,即一次请求直接返回结果
  • 无需引入 submit/query 两阶段任务轮询逻辑

测试方案

  1. 单元测试
    已执行:
go test ./relay/channel/volcengine -run 'TestVolcengineASR' -v
go test ./relay/channel/volcengine -v

结果:

  • 全部通过
  1. 文档结构校验
    已执行:
jq empty docs/openapi/relay.json

结果:

  • 通过
  1. 官方上游联调验证
    使用真实 Doubao 凭证和测试音频文件,直接请求官方 ASR 接口:
  1. 本地环境验证方案
    本地启动后可这样验证:
curl http://localhost:3000/v1/audio/transcriptions \
  -H "Authorization: Bearer <your-token>" \
  -F "file=@/absolute/path/to/welcome.mp3" \
  -F "model=bigmodel"

预期结果:

{"text":"欢迎使用阿里云。"}

客户端测试

  1. 启动本地服务
  2. 打开 http://localhost:3000, 按页面提示完成初始化,创建一个管理员账号
  3. 渠道管理,新建一个渠道:

渠道类型:字节火山方舟、豆包通用
Key:appid|acess_token
模型:bigmodel
Base URL 保持默认

  1. 令牌管理,创建一个 API Token
  2. 测本地 ASR:
curl http://localhost:3000/v1/audio/transcriptions \
  -H "Authorization: Bearer 你的Token" \
  -F "file=@/absolute/path/to/welcome.mp3" \
  -F "model=bigmodel"

Summary by CodeRabbit

  • New Features

    • Added audio transcription relay that supports OpenAI Whisper request format and can route transcriptions via VolcEngine and Doubao ASR channels.
    • Added support for an additional VolcEngine ASR model identifier ("bigmodel") to improve upstream model selection.
  • Documentation

    • Updated transcription endpoint docs to clarify Whisper compatibility and available ASR routing options.

@coderabbitai

coderabbitai Bot commented Mar 19, 2026

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 858dffaa-3ef3-425c-8574-1b1f9691bf78

📥 Commits

Reviewing files that changed from the base of the PR and between 1f83eaf and 8100a41.

📒 Files selected for processing (1)
  • relay/channel/volcengine/asr.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • relay/channel/volcengine/asr.go

Walkthrough

Adds Volcengine ASR (audio transcription) support: new ASR request/response types and build/handle logic, adapter branching for a transcription relay mode, audio format detection, header/URL routing, and updated OpenAPI description for the transcription endpoint. (50 words)

Changes

Cohort / File(s) Summary
Documentation
docs/openapi/relay.json
Rewrote POST /v1/audio/transcriptions description to state OpenAI Whisper compatibility and that requests may be proxied/routed to VolcEngine/Doubao ASR.
New ASR Integration
relay/channel/volcengine/asr.go
New file: defines Volcengine ASR request/response DTOs, multipart form parsing, audio file base64 encoding, format detection, model resolution, request ID generation, and response handling that emits OpenAI-style JSON or plain text.
Adapter Modifications
relay/channel/volcengine/adaptor.go
Added handling for RelayModeAudioTranscription in ConvertAudioRequest, GetRequestURL, SetupRequestHeader, and DoResponse; switched JSON marshal/unmarshal calls to shared common.Marshal/common.Unmarshal.
Model Support
relay/channel/volcengine/constants.go
Prepended "bigmodel" to ModelList to include the ASR model identifier.

Sequence Diagram

sequenceDiagram
    participant Client
    participant Relay as Relay Adapter\n(Volcengine)
    participant ASR as Volcengine ASR\nService

    Client->>Relay: POST /v1/audio/transcriptions (multipart/form + file)
    Relay->>Relay: buildASRRequest() - parse form, read file, base64 encode, detect format, resolve model
    Relay->>Relay: ConvertAudioRequest() - branch on RelayModeAudioTranscription, set responseFormat context
    Relay->>Relay: SetupRequestHeader() - set ASR-specific headers (X-Api-*)
    Relay->>ASR: POST bigmodel/recognize/flash (JSON body + headers)
    ASR-->>Relay: HTTP response (X-Api-Status-Code, code, result.text)
    Relay->>Relay: handleASRResponse() - unmarshal, extract transcript, format as JSON or plain text
    Relay-->>Client: Transcription response (transcript [+ usage])
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Suggested reviewers

  • creamlike1024
  • Calcium-Ion

Poem

🐰 I nibble bytes and listen close,

I turn recorded hush to prose,
Base64 hops, formats traced with care,
Headers lined up, transcripts in the air,
A little rabbit clap — your words laid bare.

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Add doubao asr' clearly summarizes the main change—adding Doubao ASR (audio transcription) support to the system.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Tip

CodeRabbit can scan for known vulnerabilities in your dependencies using OSV Scanner.

OSV Scanner will automatically detect and report security vulnerabilities in your project's dependencies. No additional configuration is required.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
relay/channel/volcengine/asr.go (1)

178-201: Note: srt and vtt response formats are not supported.

OpenAI Whisper supports srt and vtt formats in addition to json, verbose_json, and text. These are handled gracefully with an error message, which is acceptable. Consider documenting this limitation if users expect full Whisper API compatibility.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@relay/channel/volcengine/asr.go` around lines 178 - 201, The ASR handler's
responseFormat switch (checking variable responseFormat in volcengine ASR code)
does not support "srt" and "vtt"; add explicit documentation and a clearer error
message: update the project docs/README and the comment for the volcengine ASR
handler to state that "srt" and "vtt" are not supported, and modify the
unsupported-response_format error text returned by this switch to list the
unsupported formats (e.g., include "srt" and "vtt") so callers see that
limitation when invoking the handler.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@relay/channel/volcengine/asr.go`:
- Around line 134-143: In handleASRResponse ensure resp.Body is always closed by
moving the defer resp.Body.Close() to immediately after obtaining resp (before
calling io.ReadAll); this guarantees the response body is closed even if
io.ReadAll(resp.Body) returns an error so that no resource leak occurs when
handling volcengine ASR responses.

---

Nitpick comments:
In `@relay/channel/volcengine/asr.go`:
- Around line 178-201: The ASR handler's responseFormat switch (checking
variable responseFormat in volcengine ASR code) does not support "srt" and
"vtt"; add explicit documentation and a clearer error message: update the
project docs/README and the comment for the volcengine ASR handler to state that
"srt" and "vtt" are not supported, and modify the unsupported-response_format
error text returned by this switch to list the unsupported formats (e.g.,
include "srt" and "vtt") so callers see that limitation when invoking the
handler.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: c5ba1ceb-709f-4a7b-9f80-91ed72ed142c

📥 Commits

Reviewing files that changed from the base of the PR and between d096a2e and 1f83eaf.

📒 Files selected for processing (4)
  • docs/openapi/relay.json
  • relay/channel/volcengine/adaptor.go
  • relay/channel/volcengine/asr.go
  • relay/channel/volcengine/constants.go

Comment thread relay/channel/volcengine/asr.go Outdated
@ghost

This comment was marked as spam.

@bilibilipala

Copy link
Copy Markdown

主分支里好像没更新这块的内容,不过好在我在你的分支里找到了这段代码,感谢大佬解决了这个问题

@bilibilipala

Copy link
Copy Markdown

但是目前new-api在语音这块的计费还没办法按小时计费,而且目前豆包语音也不是异步实现的,还要改好多

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants