Qwen-Audio-ASR-Streaming Ruby SDK

更新时间:
复制 MD 格式

使用 Ruby SDK 调用 Qwen-Audio-ASR-Streaming,识别 16 kHz、16 位、单声道裸 PCM 音频。

前提条件

按 Ruby SDK 安装说明 安装 SDK 并配置 API Key。将示例中的 WorkspaceId 替换为华北2(北京)的业务空间 ID,使用同地域的 API Key。

快速开始

将示例保存为 example.rb,运行 ruby example.rb。

require 'dashscope'

client = DashScope::Speech::FunAsrSpeechTranscriber.new(
  model: 'qwen-audio-3.1-asr-flash-streaming',
  url: 'wss://WorkspaceId.cn-beijing.maas.aliyuncs.com/api-ws/v1/inference',
  format: 'pcm',
  sample_rate: 16_000
)
client.on_sentence_end = ->(sentence, _) { puts sentence.text }
client.on_failed = ->(error) { warn error.inspect }
client.transcribe_pcm_file('input.pcm')

接口说明

参数说明
model, url模型 ID 和 WebSocket 接口地址。
format, sample_rate音频格式和采样率。与实际输入保持一致。

模型的参数取值和限制见接口参考。

transcribe_pcm_file 按实时节奏发送音频并等待任务完成。识别定稿通过 on_sentence_end 返回。需要控制发送时,依次调用 start、send_audio 和 stop。通过 on_failed 接收任务错误。

连接超时、任务启动和收尾分别由 connect_timeout、start_timeout、stop_timeout 控制。音频的 format 和 sample_rate 必须与实际输入一致。