Qwen-Audio-3.1-Live
NEW复制成功!
概述
千问实时语音模型服务(Qwen-Audio-3.1-Live)是一款面向实时通话的下一代实时双工语音模型服务。服务采用前台语音模型与后台推理模型协同的架构,并支持按业务场景灵活配置前后台模型,兼顾复杂问题处理能力与自然的双工对话体验:前台模型负责理解用户意图、自然应答和把握发声时机;后台模型负责复杂推理、任务规划与工具调用。后台任务执行期间,前台仍可与用户持续交流,并将任务进展和结果自然融入对话,支持边听、边想、边说、边做。 服务提供两种任务协作模式:Client 模式下,客户应用负责组织对话上下文、调用自有 Agent 或业务流程,并将处理结果交还给前台模型;Responses 模式下,服务根据配置调用后台推理模型,传递所需对话上下文,并将后台结果接入实时对话。两种模式下,客户都可以通过 Function Call 扩展知识查询、订单办理、行程规划等业务工具,并按需配置模型、交互提示词和工具能力,方便客户从实时语音对话逐步扩展到复杂业务执行,让语音助手不仅会聊天,也能把事情办下去。
输入
文本音频
输出
文本音频
功能
定价
- 音频时长¥0.00333每秒
速率限制与上下文
- 最大输入64K
- TPM每分钟 Token 数100000
- RPM每分钟请求数60
API 参考
调用API复制成功!
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
# 安装依赖:python -m pip install "websockets>=15"
# 准备录音:ffmpeg -i input.wav -ar 16000 -ac 1 -f s16le input.pcm
# 设置 DASHSCOPE_API_KEY,再填写同地域的 WORKSPACE_ID。
# 将代码保存为 live_quickstart.py。运行时指定 cn-beijing 或 ap-southeast-1。
# 示例:python live_quickstart.py input.pcm cn-beijing
# 回复音频保存为 reply.wav。
import base64
import json
import os
import sys
import time
import uuid
import wave
from pathlib import Path
from websockets.sync.client import connect
WORKSPACE_ID = "WorkspaceId"
SESSION_MODEL = "qwen-audio-4.0-realtime-preview"
REGIONS = ("cn-beijing", "ap-southeast-1")
REPLY_TIMEOUT_SECONDS = 12
def main():
if len(sys.argv) != 3 or sys.argv[2] not in REGIONS:
raise SystemExit("Usage: python live_quickstart.py input.pcm {cn-beijing|ap-southeast-1}")
region = sys.argv[2]
url = (
f"wss://{WORKSPACE_ID}.{region}.maas.aliyuncs.com/api-ws/v1/live"
"?model=qwen-audio-3.1-live"
)
audio = Path(sys.argv[1]).read_bytes()
if not audio or len(audio) % 2:
raise ValueError("请提供非空的 16 位 PCM 文件")
headers = {"Authorization": f"Bearer {os.environ['DASHSCOPE_API_KEY']}"}
with connect(
url,
additional_headers=headers,
close_timeout=2,
) as ws:
def send(event_type, **payload):
ws.send(json.dumps({
"type": event_type,
"event_id": uuid.uuid4().hex,
**payload,
}))
session = {
"model": SESSION_MODEL,
"modalities": ["text", "audio"],
"turn_detection": {"type": "duplex"},
"delegation": {"type": "client"},
}
send("session.update", session=session)
deadline = time.monotonic() + 20
while True:
event = json.loads(ws.recv(timeout=max(0, deadline - time.monotonic())))
if event["type"] == "session.updated":
break
if event["type"] in ("error", "session.closed"):
raise RuntimeError(event)
audio += bytes(32000 * 3)
with wave.open("reply.wav", "wb") as reply:
reply.setparams((1, 2, 24000, 0, "NONE", "not compressed"))
start = time.monotonic()
offset = 0
deadline = start + len(audio) / 32000 + REPLY_TIMEOUT_SECONDS
while time.monotonic() < deadline:
if offset < len(audio) and time.monotonic() >= start + offset / 32000:
frame = audio[offset:offset + 640]
send("session.input_audio.append",
audio=base64.b64encode(frame).decode("ascii"))
offset += len(frame)
try:
event = json.loads(ws.recv(timeout=0.02 if offset < len(audio) else 0.25))
except TimeoutError:
continue
event_type = event["type"]
if event_type == "session.output_audio.delta":
reply.writeframesraw(base64.b64decode(event["delta"]))
elif event_type == "session.output_transcript.delta":
print(event["delta"], end="", flush=True)
elif event_type == "session.closed":
break
elif event_type == "error":
raise RuntimeError(event["error"])
else:
send("session.close")
close_deadline = time.monotonic() + 2
while time.monotonic() < close_deadline:
try:
event = json.loads(ws.recv(timeout=max(0, close_deadline - time.monotonic())))
except TimeoutError:
break
if event["type"] == "session.closed":
break
if not reply.getnframes():
raise RuntimeError("未收到语音回复")
main()