Skip to main content

Qwen-Audio-ASR-Message Ruby SDK

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

前提条件

按 Ruby SDK 安装说明 安装 SDK 并配置 API Key。将示例中的 WorkspaceId 替换为新加坡的业务空间 ID,使用同地域的 API Key。

快速开始

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

client = DashScope::Speech::FunAsrSpeechTranscriber.new(
  model: 'qwen-audio-3.1-asr-flash-message',
  url: 'wss://WorkspaceId.ap-southeast-1.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 接收任务错误。 该模型默认返回每句话的最终结果。Ruby SDK 1.2.0 无法通过 intermediate_result_enabled 开启中间结果。如需中间结果,请在 客户端事件 的 run-task 中设置该参数。 连接超时、任务启动和收尾分别由 connect_timeout、start_timeout、stop_timeout 控制。音频的 format 和 sample_rate 必须与实际输入一致。