Qwen-Audio-3.1-Live

NEW
复制成功!
加入对比

概述

千问实时语音模型服务(Qwen-Audio-3.1-Live)是一款面向实时通话的下一代实时双工语音模型服务。服务采用前台语音模型与后台推理模型协同的架构,并支持按业务场景灵活配置前后台模型,兼顾复杂问题处理能力与自然的双工对话体验:前台模型负责理解用户意图、自然应答和把握发声时机;后台模型负责复杂推理、任务规划与工具调用。后台任务执行期间,前台仍可与用户持续交流,并将任务进展和结果自然融入对话,支持边听、边想、边说、边做。 服务提供两种任务协作模式:Client 模式下,客户应用负责组织对话上下文、调用自有 Agent 或业务流程,并将处理结果交还给前台模型;Responses 模式下,服务根据配置调用后台推理模型,传递所需对话上下文,并将后台结果接入实时对话。两种模式下,客户都可以通过 Function Call 扩展知识查询、订单办理、行程规划等业务工具,并按需配置模型、交互提示词和工具能力,方便客户从实时语音对话逐步扩展到复杂业务执行,让语音助手不仅会聊天,也能把事情办下去。

输入

文本音频

输出

文本音频

功能

前缀补全

调用通义千问 API 时启用 Partial Mode,可让模型严格接续您提供的前缀文本进行生成。查看文档

函数调用

通过函数调用将大模型与外部工具和系统连接起来。查看文档

缓存

Context Cache 会缓存长上下文请求的公共前缀,减少重复计算、提升响应速度并降低成本。查看文档

结构化输出

结构化输出可确保模型返回符合预期格式的 JSON 字符串。查看文档

批量任务

异步批量处理请求,降低调用成本。查看文档

联网搜索

启用联网搜索后,模型可基于实时检索的数据进行回答。查看文档

微调

基于样本数据训练模型,使其更适配特定任务。查看文档

定价

  • 音频时长
    ¥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()

关闭服务

关闭后,Wan系列模型将在所有已开通地域停止服务,您将无法通过体验中心或API调用,请谨慎操作。如需继续使用,可重新开启。查看详情