Qwen-Audio-Live Ruby SDK

更新时间:
复制 MD 格式

通过 WebSocket 建立全双工语音会话,接收回复字幕和音频。

前提条件

按 Ruby SDK 安装说明安装 1.2.0 并配置 API Key。将示例中的 WorkspaceId 替换为华北2(北京)的业务空间 ID。Qwen-Audio-Live 使用业务空间专属域名。

准备 input.pcm,格式为 16 kHz、16 位小端、单声道裸 PCM。可用 FFmpeg 转换录音。

ffmpeg -i input.wav -ar 16000 -ac 1 -f s16le input.pcm

快速开始

将以下代码保存为 example.rb,执行 ruby example.rb。

require 'dashscope'

def send_frame(client, frame, interval)
  deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + interval
  client.append_audio(frame)
  while (remaining = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC)).positive?
    client.pump(remaining)
  end
end

input_size = File.size('input.pcm')
unless input_size.positive? && input_size.even?
  raise ArgumentError, 'input.pcm must contain 16-bit PCM samples.'
end

live = DashScope::Qwen::Live.new(
  url: 'wss://WorkspaceId.cn-beijing.maas.aliyuncs.com/api-ws/v1/realtime',
  model: 'qwen-audio-3.1-live',
  front_model: 'qwen-audio-4.0-realtime-preview',
  turn_detection_mode: :duplex,
  voice: 'yunshu',
  delegation: :client
)

File.open('reply.pcm', 'wb') do |output|
  live.on_assistant_text_delta = ->(text, _, _) { print text }
  live.on_assistant_audio_chunk = ->(pcm, _, _) { output.write(pcm) }
  live.on_error = ->(error) { warn error.inspect }
  session_updated = false
  live.on_raw_event = ->(type, _) { session_updated = true if type == 'session.updated' }
  live.connect
  live.pump_until(10) { session_updated }
  unless session_updated
    live.abort
    raise 'Timed out waiting for session.updated.'
  end

  File.open('input.pcm', 'rb') do |input|
    while (frame = input.read(1280))
    send_frame(live, frame, 0.04)
    end
  end

  25.times do
    send_frame(live, "\0" * 1280, 0.04)
  end

  deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + 30
  live.pump(0.1) while Process.clock_gettime(Process::CLOCK_MONOTONIC) < deadline
  live.finish
end

puts "\nSession duration: #{live.usage&.seconds} s"

终端显示回复字幕,reply.pcm 保存 24 kHz、16 位小端、单声道语音。示例为回复预留 30 秒。实际应用应在接收完本轮回复后再关闭会话。

会话参数

参数说明
model连接使用的模型 ID,填 qwen-audio-3.1-live。
front_model直接与用户对话的模型。示例使用 qwen-audio-4.0-realtime-preview。
turn_detection_mode示例使用 duplex,让模型在回复时继续接收用户语音。
voice选择前台模型支持的音色。示例使用 yunshu。
delegation示例使用 Client 模式。若选 Responses 模式,需要配置后台模型。

更多会话配置见 Qwen-Audio-Live API 参考。

方法与事件

方法用途
connect建立连接,等待 session.created,然后发送 session.update。
append_audio发送一帧音频。
mute_input / unmute_input暂停或恢复输入。
send_user_text / send_item将文本或自定义消息写入会话。
send_function_call_output将前台函数调用的结果回传到会话。
send_thinking / send_instruction / send_commentary向前台模型回传委派任务的进度、指令或说明。
append_backend_item / create_backend_response向后台追加消息或触发后台响应。
send_raw_event发送 Live 客户端事件。
finish发送 session.close,等待 session.closed 后断开。
abort立即断开,会话用量的最终值可能尚未返回。
事件或回调说明
on_session_ready收到会话 ID 时触发。发送音频前,先等服务端返回 session.updated。
on_user_transcript_delta / on_assistant_text_delta用户转写或回复字幕增量。
on_assistant_audio_chunk回复音频分片。
on_delegation_created任务委派摘要。
on_backend_eventResponses 模式下的后台事件。
on_function_call前台函数调用。
on_usage_updated / on_closed累计用量更新或会话结束。结束事件包含最终用量。
on_raw_event / on_error其他服务端事件或错误。
  • 委派摘要提供 delegation_id、task_id、summary、offset_ms 和 context。回传委派进度时使用委派 ID。
  • usage 返回最近一次用量,包括 seconds 和可选的上下文窗口占用比例。正常关闭时,从 session.closed 获取最终用量。

音频通过 session.input_audio.append 发送。连接、关闭及事件字段见 WebSocket 连接和服务端事件。