Skip to main content

Qwen-Audio-Live Ruby SDK

通過 WebSocket 建立全雙工語音會話,接收回覆字幕和音訊。

前提條件

按 Ruby SDK 安裝說明安裝 1.2.0 並配置 API Key。將示例中的 WorkspaceId 替換為新加坡的業務空間 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.ap-southeast-1.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 連線和伺服器端事件。