Qwen-Omni 接受文本、圖片、音頻、視頻的多模態輸入,輸出文本或語音回複。支援數十種語言和方言,適用於內容審核、文本創作、視覺識別、音視頻互動等情境。
支援的地區:新加坡、北京,需使用各地區的API Key。
快速開始
前提條件- 已配置API Key並配置API Key到環境變數。
- Qwen-Omni 模型僅支援 OpenAI 相容方式調用,需要安裝最新版SDK。OpenAI Python SDK最低版本為 1.52.0,Node.js SDK最低版本為 4.68.0。
說明運行前安裝依賴:pip install numpy soundfile openai(Python)或 npm install openai wav(Node.js)。
以下樣本向 Qwen-Omni 發送一條簡訊,流式接收文本和音頻回複,流結束後將音頻解碼並儲存為 WAV 檔案。
import os
import base64
import soundfile as sf
import numpy as np
from openai import OpenAI
# 1. 初始化用戶端
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"), # 確認已配置環境變數
# 以下為新加坡地區的URL。請將 {WorkspaceId} 替換為您的百鍊業務空間ID,各地區的URL不同。
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)
# 2. 發起請求
try:
completion = client.chat.completions.create(
model="qwen3.5-omni-plus",
messages=[{"role": "user", "content": "你是誰"}],
modalities=["text", "audio"], # 指定輸出文本和音頻
audio={"voice": "Tina", "format": "wav"},
stream=True, # 必須設定為 True
stream_options={"include_usage": True},
)
# 3. 處理流式響應並解碼音頻
print("模型回複:")
audio_base64_string = ""
for chunk in completion:
# 處理文本部分
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")
# 收集音頻部分
if chunk.choices and hasattr(chunk.choices[0].delta, "audio") and chunk.choices[0].delta.audio:
audio_base64_string += chunk.choices[0].delta.audio.get("data", "")
# 4. 儲存音頻檔案
if audio_base64_string:
wav_bytes = base64.b64decode(audio_base64_string)
audio_np = np.frombuffer(wav_bytes, dtype=np.int16)
sf.write("audio_assistant.wav", audio_np, samplerate=24000)
print("\n音頻檔案已儲存至:audio_assistant.wav")
except Exception as e:
print(f"請求失敗: {e}")
// 運行前的準備工作:
// Windows/Mac/Linux 通用:
// 1. 確保已安裝 Node.js (建議版本 >= 14)
// 2. 運行以下命令安裝必要的依賴:
// npm install openai wav
import OpenAI from "openai";
import { createWriteStream } from 'node:fs';
import { Writer } from 'wav';
// 定義音頻轉換函式:將Base64字串轉換並儲存為標準的 WAV 音頻檔案
async function convertAudio(audioString, audioPath) {
try {
// 解碼Base64字串為Buffer
const wavBuffer = Buffer.from(audioString, 'base64');
// 建立WAV檔案寫入流
const writer = new Writer({
sampleRate: 24000, // 採樣率
channels: 1, // 單聲道
bitDepth: 16 // 16位元深度
});
// 建立輸出檔案流並建立管道串連
const outputStream = createWriteStream(audioPath);
writer.pipe(outputStream);
// 寫入PCM資料並結束寫入
writer.write(wavBuffer);
writer.end();
// 使用Promise等待檔案寫入完成
await new Promise((resolve, reject) => {
outputStream.on('finish', resolve);
outputStream.on('error', reject);
});
// 添加額外等待時間確保音頻完整
await new Promise(resolve => setTimeout(resolve, 800));
console.log(`\n音頻檔案已成功儲存為 ${audioPath}`);
} catch (error) {
console.error('處理過程中發生錯誤:', error);
}
}
// 1. 初始化用戶端
const openai = new OpenAI(
{
// 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
apiKey: process.env.DASHSCOPE_API_KEY,
// 以下為新加坡地區URL,調用時請將{WorkspaceId}替換為真實的業務空間ID,各地區的URL不同。
baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
}
);
// 2. 發起請求
const completion = await openai.chat.completions.create({
model: "qwen3.5-omni-plus",
messages: [
{
"role": "user",
"content": "你是誰?"
}],
stream: true,
stream_options: {
include_usage: true
},
modalities: ["text", "audio"],
audio: { voice: "Tina", format: "wav" }
});
let audioString = "";
console.log("大模型的回複:")
// 3. 處理流式響應並解碼音頻
for await (const chunk of completion) {
if (Array.isArray(chunk.choices) && chunk.choices.length > 0) {
// 處理常值內容
if (chunk.choices[0].delta.content) {
process.stdout.write(chunk.choices[0].delta.content);
}
// 處理音頻內容
if (chunk.choices[0].delta.audio) {
if (chunk.choices[0].delta.audio["data"]) {
audioString += chunk.choices[0].delta.audio["data"];
}
}
}
}
// 4. 儲存音頻檔案
convertAudio(audioString, "audio_assistant.wav");
# ======= 重要提示 =======
# 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
# 以下為新加坡地區URL,調用時請將{WorkspaceId}替換為真實的業務空間ID,各地區的URL不同。
# === 執行時請刪除該注釋 ===
curl -X POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3.5-omni-plus",
"messages": [
{
"role": "user",
"content": "你是誰?"
}
],
"stream":true,
"stream_options":{
"include_usage":true
},
"modalities":["text","audio"],
"audio":{"voice":"Tina","format":"wav"}
}'
返回結果
運行Python或Node.js代碼後,控制台輸出模型的文本回複,同目錄下產生audio_assistant.wav音頻檔案。
大模型的回複:
我是阿里雲研發的大規模語言模型,我叫千問。有什麼我可以協助你的嗎?
運行HTTP代碼直接返迴文本和Base64編碼的音頻資料(audio欄位)。
data: {"choices":[{"delta":{"content":"我"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1757647879,"system_fingerprint":null,"model":"qwen3.5-omni-plus","id":"chatcmpl-a68eca3b-c67e-4666-a72f-73c0b4919860"}
data: {"choices":[{"delta":{"content":"是"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1757647879,"system_fingerprint":null,"model":"qwen3.5-omni-plus","id":"chatcmpl-a68eca3b-c67e-4666-a72f-73c0b4919860"}
......
data: {"choices":[{"delta":{"audio":{"data":"/v8AAAAAAAAAAAAAAA...","expires_at":1757647879,"id":"audio_a68eca3b-c67e-4666-a72f-73c0b4919860"}},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1757647879,"system_fingerprint":null,"model":"qwen3.5-omni-plus","id":"chatcmpl-a68eca3b-c67e-4666-a72f-73c0b4919860"}
data: {"choices":[{"finish_reason":"stop","delta":{"content":""},"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1764763585,"system_fingerprint":null,"model":"qwen3.5-omni-plus","id":"chatcmpl-e8c82e9e-073e-4289-a786-a20eb444ac9c"}
data: {"choices":[],"object":"chat.completion.chunk","usage":{"prompt_tokens":207,"completion_tokens":103,"total_tokens":310,"completion_tokens_details":{"audio_tokens":83,"text_tokens":20},"prompt_tokens_details":{"text_tokens":207}},"created":1757940330,"system_fingerprint":null,"model":"qwen3.5-omni-plus","id":"chatcmpl-9cdd5a26-f9e9-4eff-9dcc-93a878165afc"}
模型選型
-
Qwen3.5-Omni 系列:適用於長視頻分析、會議紀要、字幕產生、內容審核、音視頻互動等情境。
- 輸入限制:3 小時音頻或 1 小時視頻
- 音頻控制:支援通過指令調節音量、語速、情緒
- 視覺能力:與 Qwen3.5 同等水平,可理解畫面、語音、音效等多模態資訊
- 多模態組合輸入:支援文本與圖片、音頻、視頻的任意組合約時輸入,不限於單一模態
- 聲音複刻:支援自訂音色(僅qwen3.5-omni-plus、qwen3.5-omni-flash支援,快照版本暫不支援),詳情請參見聲音複刻
-
Qwen3-Omni-Flash 系列:適用於短視頻分析、成本敏感情境。
- 輸入限制:150 秒以內音視頻
- 思考模式:Qwen-Omni 系列中唯一支援思考模式的系列
- 輸入模態:僅支援文本與單一其他模態(圖片、音頻或視頻)的組合輸入
-
Qwen-Omni-Turbo 系列
已停止更新,功能受限。建議遷移至 Qwen3.5-Omni 系列或 Qwen3-Omni-Flash 系列。
| 模型系列 | 音視頻描述能力 | 深度思考 | 連網搜尋 | 輸入音頻語種 | 輸出音頻語種 | 音色數量 |
Qwen3.5-Omni 最新一代全模態模型 | 強 | 不支援 | 支援 | 113 種 含74 種語言、39 種方言 語言:中文、英語、德語、法語、意大利語、捷克語、印尼語、泰語、韓語、波蘭語、日語、越南語、芬蘭語、葡萄牙語、西班牙語、荷蘭語、俄語、馬來語、加泰羅尼亞語、瑞典語、土耳其語、烏克蘭語、羅馬尼亞語、斯洛伐克語、丹麥語、冰島語、挪威語(博克馬爾)、馬其頓語、希臘語、匈牙利語、加利西亞語、菲律賓語、克羅地亞語、波斯尼亞語、斯洛文尼亞語、保加利亞語、哈薩克語、白俄羅斯語、拉脫維亞語、愛沙尼亞語、阿塞拜疆語、維吾爾語、斯瓦希裡語、印地語、世界語、柯爾克孜語、塔吉克語、宿務語、南非語、阿拉伯語、立陶宛語、爪哇語、孟加拉語、波斯語、希伯來語、旁遮普語、古吉拉特語、蒙古語、阿斯圖里亞斯語、卡納達語、馬拉地語、國際語、馬拉雅拉姆語、馬爾他語、新挪威語、泰盧固語、烏爾都語、格魯吉亞語、巴斯克語、泰米爾語、奧裡亞語、塞爾維亞語、毛利語 方言: | 36 種 含 29 種語言、7 種方言 語言: 方言: | 55 種 |
Qwen3-Omni-Flash 混合思考模型 | 較弱 | 支援 | 不支援 | 19 種 含 11 種語言、8 種方言 語言: 中文、英語、德語、法語、意大利語、泰語、韓語、日語、俄語、西班牙語、葡萄牙語 方言:四川話、上海話、粵語、閩南語、陝西話、南京話、天津話、北京話 | 19 種 含 11 種語言、8 種方言 語言: 中文、英語、德語、法語、意大利語、泰語、韓語、日語、俄語、西班牙語、葡萄牙語 方言:四川話、上海話、粵語、閩南語、陝西話、南京話、天津話、北京話 | 17~49 種
|
Qwen-Omni-Turbo 已停止更新 | 無 | 不支援 | 不支援 | 中文、英語 | 中文、英語 | 4 種 |
模型名稱、上下文長度、價格及快照版本請參見百鍊控制台;並發限流規則請參見 限流 。
模型效果
音視頻內容分析
| 00:00.000 – 00:02.500 雨夜中的城市街道填滿寬銀幕畫面。長時間曝光下,紅藍車燈拖出光軌滑過,在濕漉漉的路面上閃爍。一位身穿深色及膝風衣、內搭淺色襯衫領帶的孤獨男子,沿著右側行人路向鏡頭走來。雨滴凝結在他的肩膀和頭髮上;每一步都在潮濕的混凝土上發出沉悶的啪嗒聲。低沉不祥的電子嗡鳴支撐著環境音,同時穩定的雨聲四處劈啪作響。他身旁的磚牆上貼滿彩色塗鴉,街區遠處幾塊霓虹燈招牌發光——一塊清晰顯示橙紅色草書體 "Nice",另一塊豎置招牌的末端露出字母 "AT"。 ...... 00:12.300 – 00:14.533鏡頭向下傾斜。在手電筒光圈內,一張泛黃老化的單頁紙躺在淺水窪中,邊緣燒焦且邊角捲曲。墨跡段落覆蓋大部分表面,但潮濕和焦痕使文字無法辨認。男子跪下,戴手套的手指伸向檔案;手靠近時水波向外蕩漾。警笛達到峰值,隨後開始略微消退,而環境嗡鳴保持穩定。畫面在顫抖的紙頁即將被觸碰前定格,片段結束。
|
多方言理解
| 這段音頻由多位來自中國不同地區的歌手用各自的方言進行饒舌,展現了中國方言的豐富多樣性。以下是每位歌手的聲音特徵、情緒狀態及對應歌詞的詳細描述: 杭州話(吳語)聲音特徵:這位男歌手的聲音較為柔和,帶有吳語特有的軟糯和溫婉。他的語調起伏平緩,節奏輕快,給人一種娓娓道來的感覺。 情緒狀態:悠閑、愜意且充滿生活氣息。他描繪了杭州的慢生活和城市變遷,情緒中帶著對家鄉的眷戀和對現代發展的感慨,整體感覺舒適而親切。 對應歌詞:> 杭州話,聽聽看,我們格文化,包括家門口的兒化音 > 像花開在西湖邊,不搞文化調查,只有我們搞得懂 > 你們都不懂,三不搭門,格兒,老兒,粉兒,伢兒,這外人分得清 > 出發,變化快,地鐵交通發達,據說要去看看 > 包容新馬泰,看十三太,為城市驕傲,性格直給 > 感情你只為過橋,而且味道不一樣,你不來嘞
|
歌詞字幕產生
| [00:00:12,680 --> 00:00:16,960] 貓線繞過樹搖晃的月光 [00:00:18,400 --> 00:00:22,800] 暖氣片哼著九八年排行 [00:00:24,160 --> 00:00:28,080] 時間撥開雲霧般的熱浪 [00:00:28,920 --> 00:00:33,000] 螢幕裡的霓虹曬在鼻樑 ...... [00:03:16,720 --> 00:03:21,680] 我們窩在年輪最柔軟一牆 [00:03:22,400 --> 00:03:27,000] 呼吸被餘溫釀成蜂蜜糖 [00:03:28,160 --> 00:03:33,200] 沙發陷落成雲絮的形狀 [00:03:34,000 --> 00:03:38,800] 每個毛孔都曬著晴朗 [00:04:09,000 --> 00:04:10,020] (End)
|
音視頻編程
使用方式
流式輸出
Qwen-Omni 的所有請求必須設定 stream=True。
模型配置
根據使用情境配置參數、提示詞和音視頻長度,在成本、速度與效果之間取得平衡。
音視頻理解
| 使用情境 | 推薦視頻長度 | Prompt 建議 | max_pixels 推薦參數值 |
快速審核,成本低 | ≤60分鐘 | 50 個詞以內的簡單 Prompt | 230,400 |
內容提取(長視頻分段) | ≤60分鐘 | 921,600~2,073,600 | |
標準分析(短視頻打標) | ≤4分鐘 | 使用下方的結構化 Prompt 建議Prompt | 921,600~2,073,600 |
精細分析(多說話人/複雜情境) | ≤2分鐘 | 2,073,600 |
說明長視頻需要細粒度描述時,建議分段處理。
音頻理解
控制音頻長度和提示詞複雜度,平衡成本與效果。
| 使用情境 | 推薦音頻長度 | Prompt 建議 |
快速審核、低成本 | ≤60分鐘 | 50 個詞以內的簡單 Prompt |
內容提取(長音頻分段) | ≤60分鐘 | |
標準分析(音頻打標) | ≤2分鐘 | 使用結構化 Prompt 結構化Prompt |
精細分析(多說話人/複雜情境) | ≤1分鐘 |
說明長音頻需要細粒度描述時,建議分段處理。
多模態組合輸入
說明多模態組合輸入僅 Qwen3.5-Omni 系列支援,可在同一請求中同時傳入多種模態資料(如圖片+音頻+文本、視頻+圖片+文本等任意組合)。
以下樣本展示如何在一個請求中同時傳入圖片和音頻,由模型綜合分析多模態內容。
OpenAI 相容
import os
from openai import OpenAI
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)
completion = client.chat.completions.create(
model="qwen3.5-omni-plus",
messages=[
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {
"url": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241022/emyrja/dog_and_girl.jpeg"
},
},
{
"type": "input_audio",
"input_audio": {
"data": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20250211/tixcef/cherry.wav",
"format": "wav"
},
},
{"type": "text", "text": "請描述圖片內容,並告訴我音頻在說什麼。"},
],
},
],
modalities=["text", "audio"],
audio={"voice": "Tina", "format": "wav"},
stream=True,
stream_options={"include_usage": True},
)
for chunk in completion:
if chunk.choices:
print(chunk.choices[0].delta)
else:
print(chunk.usage)
import OpenAI from "openai";
const openai = new OpenAI(
{
// 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
apiKey: process.env.DASHSCOPE_API_KEY,
// 以下為新加坡地區URL,調用時請將{WorkspaceId}替換為真實的業務空間ID,各地區的URL不同。
baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
}
);
const completion = await openai.chat.completions.create({
model: "qwen3.5-omni-plus",
messages: [
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": { "url": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241022/emyrja/dog_and_girl.jpeg" },
},
{
"type": "input_audio",
"input_audio": {
"data": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20250211/tixcef/cherry.wav",
"format": "wav"
},
},
{ "type": "text", "text": "請描述圖片內容,並告訴我音頻在說什麼。" }
]
}
],
stream: true,
stream_options: {
include_usage: true
},
modalities: ["text", "audio"],
audio: { voice: "Tina", format: "wav" }
});
for await (const chunk of completion) {
if (Array.isArray(chunk.choices) && chunk.choices.length > 0) {
console.log(chunk.choices[0].delta);
} else {
console.log(chunk.usage);
}
}
# 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
# 以下為新加坡地區URL,調用時請將{WorkspaceId}替換為真實的業務空間ID,各地區的URL不同。
curl -X POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3.5-omni-plus",
"messages": [
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {
"url": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241022/emyrja/dog_and_girl.jpeg"
}
},
{
"type": "input_audio",
"input_audio": {
"data": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20250211/tixcef/cherry.wav",
"format": "wav"
}
},
{
"type": "text",
"text": "請描述圖片內容,並告訴我音頻在說什麼。"
}
]
}
],
"stream": true,
"stream_options": {
"include_usage": true
},
"modalities": ["text", "audio"],
"audio": {"voice": "Tina", "format": "wav"}
}'
單一模態輸入
以下情境中,每次請求僅傳入文本與一種其他模態(視頻、音頻或圖片)的組合,所有 Qwen-Omni 模型均支援。
視頻+文本輸入
視頻檔案形式(可理解視頻中的音頻)
-
檔案數量:
- Qwen3.5-Omni系列:使用公網URL方式,最多可傳入 512 個;使用Base64編碼方式,最多可傳入 250 個。
- Qwen3-Omni-Flash系列、Qwen-Omni-Turbo系列:僅支援輸入一個;
-
檔案大小:
-
使用公網URL方式:
- Qwen3.5-Omni系列:限制為 2GB
- Qwen3-Omni-Flash:限制為 256 MB
- Qwen-Omni-Turbo:限制為 150 MB
-
使用 Base 64 編碼方式:編碼後的 Base64 字串大小必須小於 10MB
-
-
時間長度限制:
- Qwen3.5-Omni系列:1 小時
- Qwen3-Omni-Flash:150 秒
- Qwen-Omni-Turbo:40 秒
-
檔案格式:MP4、AVI、MKV、MOV、FLV、WMV 等。
-
視頻檔案中的視覺資訊與音頻資訊會分開計費。
OpenAI 相容
import os
from openai import OpenAI
client = OpenAI(
# 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
api_key=os.getenv("DASHSCOPE_API_KEY"),
# 以下為新加坡地區URL,調用時請將{WorkspaceId}替換為真實的業務空間ID,各地區的URL不同。
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)
completion = client.chat.completions.create(
model="qwen3.5-omni-plus", # 模型為Qwen3-Omni-Flash時,請在非思考模式下運行
messages=[
{
"role": "user",
"content": [
{
"type": "video_url",
"video_url": {
"url": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241115/cqqkru/1.mp4"
},
},
{"type": "text", "text": "視頻的內容是什麼?"},
],
},
],
# 設定輸出資料的模態,當前支援兩種:["text","audio"]、["text"]
modalities=["text", "audio"],
audio={"voice": "Tina", "format": "wav"},
# stream 必須設定為 True,否則會報錯
stream=True,
stream_options={"include_usage": True},
)
for chunk in completion:
if chunk.choices:
print(chunk.choices[0].delta)
else:
print(chunk.usage)
import OpenAI from "openai";
const openai = new OpenAI(
{
// 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
apiKey: process.env.DASHSCOPE_API_KEY,
// 以下為新加坡地區URL,調用時請將{WorkspaceId}替換為真實的業務空間ID,各地區的URL不同。
baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
}
);
const completion = await openai.chat.completions.create({
model: "qwen3.5-omni-plus", // 模型為Qwen3-Omni-Flash時,請在非思考模式下運行
messages: [
{
"role": "user",
"content": [{
"type": "video_url",
"video_url": { "url": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241115/cqqkru/1.mp4" },
},
{ "type": "text", "text": "視頻的內容是什麼?" }]
}],
stream: true,
stream_options: {
include_usage: true
},
modalities: ["text", "audio"],
audio: { voice: "Tina", format: "wav" }
});
for await (const chunk of completion) {
if (Array.isArray(chunk.choices) && chunk.choices.length > 0) {
console.log(chunk.choices[0].delta);
} else {
console.log(chunk.usage);
}
}
# ======= 重要提示 =======
# 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
# 以下為新加坡地區URL,調用時請將{WorkspaceId}替換為真實的業務空間ID,各地區的URL不同。
# === 執行時請刪除該注釋 ===
curl -X POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3.5-omni-plus",
"messages": [
{
"role": "user",
"content": [
{
"type": "video_url",
"video_url": {
"url": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241115/cqqkru/1.mp4"
}
},
{
"type": "text",
"text": "視頻的內容是什麼"
}
]
}
],
"stream":true,
"stream_options": {
"include_usage": true
},
"modalities":["text","audio"],
"audio":{"voice":"Tina","format":"wav"}
}'
圖片列表形式
圖片數量- Qwen3.5-Omni系列:最少傳入 2 張圖片,最多可傳入 2048 張圖片
- Qwen3-Omni-Flash:最少傳入 2 張圖片,最多可傳入 128 張圖片
- Qwen-Omni-Turbo:最少傳入 4 張圖片,最多可傳入 80 張圖片
OpenAI 相容
import os
from openai import OpenAI
client = OpenAI(
# 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
api_key=os.getenv("DASHSCOPE_API_KEY"),
# 以下為新加坡地區URL,調用時請將{WorkspaceId}替換為真實的業務空間ID,各地區的URL不同。
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)
completion = client.chat.completions.create(
model="qwen3.5-omni-plus", # 模型為Qwen3-Omni-Flash時,請在非思考模式下運行
messages=[
{
"role": "user",
"content": [
{
"type": "video",
"video": [
"https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241108/xzsgiz/football1.jpg",
"https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241108/tdescd/football2.jpg",
"https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241108/zefdja/football3.jpg",
"https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241108/aedbqh/football4.jpg",
],
},
{"type": "text", "text": "描述這個視頻的具體過程"},
],
}
],
# 設定輸出資料的模態,當前支援兩種:["text","audio"]、["text"]
modalities=["text", "audio"],
audio={"voice": "Tina", "format": "wav"},
# stream 必須設定為 True,否則會報錯
stream=True,
stream_options={"include_usage": True},
)
for chunk in completion:
if chunk.choices:
print(chunk.choices[0].delta)
else:
print(chunk.usage)
import OpenAI from "openai";
const openai = new OpenAI(
{
// 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
apiKey: process.env.DASHSCOPE_API_KEY,
// 以下為新加坡地區URL,調用時請將{WorkspaceId}替換為真實的業務空間ID,各地區的URL不同。
baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
}
);
const completion = await openai.chat.completions.create({
model: "qwen3.5-omni-plus", //模型為Qwen3-Omni-Flash時,請在非思考模式下運行
messages: [{
role: "user",
content: [
{
type: "video",
video: [
"https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241108/xzsgiz/football1.jpg",
"https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241108/tdescd/football2.jpg",
"https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241108/zefdja/football3.jpg",
"https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241108/aedbqh/football4.jpg"
]
},
{
type: "text",
text: "描述這個視頻的具體過程"
}
]
}],
stream: true,
stream_options: {
include_usage: true
},
modalities: ["text", "audio"],
audio: { voice: "Tina", format: "wav" }
});
for await (const chunk of completion) {
if (Array.isArray(chunk.choices) && chunk.choices.length > 0) {
console.log(chunk.choices[0].delta);
} else {
console.log(chunk.usage);
}
}
# ======= 重要提示 =======
# 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
# 以下為新加坡地區URL,調用時請將{WorkspaceId}替換為真實的業務空間ID,各地區的URL不同。
# === 執行時請刪除該注釋 ===
curl -X POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3.5-omni-plus",
"messages": [
{
"role": "user",
"content": [
{
"type": "video",
"video": [
"https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241108/xzsgiz/football1.jpg",
"https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241108/tdescd/football2.jpg",
"https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241108/zefdja/football3.jpg",
"https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241108/aedbqh/football4.jpg"
]
},
{
"type": "text",
"text": "描述這個視頻的具體過程"
}
]
}
],
"stream": true,
"stream_options": {
"include_usage": true
},
"modalities": ["text", "audio"],
"audio": {
"voice": "Tina",
"format": "wav"
}
}'
音頻+文本輸入
-
檔案數量:
- Qwen3.5-Omni系列:使用公網URL方式,最多可傳入 2048 個;使用Base64編碼方式,最多可傳入 250 個;
- Qwen3-Omni-Flash系列、Qwen-Omni-Turbo系列:僅支援輸入一個;
-
檔案大小:
-
使用公網URL方式:
- Qwen3.5-Omni系列:不超過 2GB
- Qwen3-Omni-Flash:不超過 100MB
- Qwen-Omni-Turbo:不超過 10MB
-
使用 Base 64 編碼方式:編碼後的 Base64 字串大小必須小於 10MB
-
-
時間長度限制:
- Qwen3.5-Omni系列:最長 3 小時
- Qwen3-Omni-Flash:最長 20 分鐘
- Qwen-Omni-Turbo:最長 3 分鐘
-
檔案格式:支援AMR、 WAV、 3GP、 3GPP、 AAC、 MP3等主流格式
以下範例程式碼以傳入音頻公網URL為例,傳入本地音頻請參見:輸入 Base 64 編碼的本地檔案。當前只支援以流式輸出的方式進行調用。
OpenAI 相容
import os
from openai import OpenAI
client = OpenAI(
# 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
api_key=os.getenv("DASHSCOPE_API_KEY"),
# 以下為新加坡地區URL,調用時請將{WorkspaceId}替換為真實的業務空間ID,各地區的URL不同。
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)
completion = client.chat.completions.create(
model="qwen3.5-omni-plus",# 模型為Qwen3-Omni-Flash時,請在非思考模式下運行
messages=[
{
"role": "user",
"content": [
{
"type": "input_audio",
"input_audio": {
"data": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20250211/tixcef/cherry.wav",
"format": "wav",
},
},
{"type": "text", "text": "這段音頻在說什麼"},
],
},
],
# 設定輸出資料的模態,當前支援兩種:["text","audio"]、["text"]
modalities=["text", "audio"],
audio={"voice": "Tina", "format": "wav"},
# stream 必須設定為 True,否則會報錯
stream=True,
stream_options={"include_usage": True},
)
for chunk in completion:
if chunk.choices:
print(chunk.choices[0].delta)
else:
print(chunk.usage)
import OpenAI from "openai";
const openai = new OpenAI(
{
// 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
apiKey: process.env.DASHSCOPE_API_KEY,
// 以下為新加坡地區URL,調用時請將{WorkspaceId}替換為真實的業務空間ID,各地區的URL不同。
baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
}
);
const completion = await openai.chat.completions.create({
model: "qwen3.5-omni-plus", // 模型為Qwen3-Omni-Flash時,請在非思考模式下運行
messages: [
{
"role": "user",
"content": [{
"type": "input_audio",
"input_audio": { "data": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20250211/tixcef/cherry.wav", "format": "wav" },
},
{ "type": "text", "text": "這段音頻在說什麼" }]
}],
stream: true,
stream_options: {
include_usage: true
},
modalities: ["text", "audio"],
audio: { voice: "Tina", format: "wav" }
});
for await (const chunk of completion) {
if (Array.isArray(chunk.choices) && chunk.choices.length > 0) {
console.log(chunk.choices[0].delta);
} else {
console.log(chunk.usage);
}
}
# ======= 重要提示 =======
# 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
# 以下為新加坡地區URL,調用時請將{WorkspaceId}替換為真實的業務空間ID,各地區的URL不同。
# === 執行時請刪除該注釋 ===
curl -X POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3.5-omni-plus",
"messages": [
{
"role": "user",
"content": [
{
"type": "input_audio",
"input_audio": {
"data": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20250211/tixcef/cherry.wav",
"format": "wav"
}
},
{
"type": "text",
"text": "這段音頻在說什麼"
}
]
}
],
"stream":true,
"stream_options":{
"include_usage":true
},
"modalities":["text","audio"],
"audio":{"voice":"Tina","format":"wav"}
}'
圖片+文本輸入
Qwen-Omni 模型支援傳入多張圖片。對輸入圖片的要求如下:
-
圖片數量:
- 公網URL傳入:最多可傳入 2048 張
- Base 64 編碼:最多可傳入 250 張
-
映像大小:
-
使用公網URL方式:
- Qwen3.5-Omni系列:單個圖片檔案的大小不超過 20MB
- Qwen3-Omni-Flash系列、Qwen-Omni-Turbo系列:單個圖片檔案的大小不超過 10MB
-
使用 Base 64 編碼方式:編碼後的 Base64 字串大小必須小於 10MB;
-
-
圖片的寬度和高度均應大於 10 像素,寬高比不應超過 200:1 或 1:200
-
支援的圖片類型請參見映像與視頻理解
以下範例程式碼以傳入圖片公網URL為例,傳入本地圖片請參見:輸入 Base 64 編碼的本地檔案。當前只支援以流式輸出的方式進行調用。
OpenAI 相容
import os
from openai import OpenAI
client = OpenAI(
# 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
api_key=os.getenv("DASHSCOPE_API_KEY"),
# 以下為新加坡地區URL,調用時請將{WorkspaceId}替換為真實的業務空間ID,各地區的URL不同。
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)
completion = client.chat.completions.create(
model="qwen3.5-omni-plus", # 模型為Qwen3-Omni-Flash時,請在非思考模式下運行
messages=[
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {
"url": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241022/emyrja/dog_and_girl.jpeg"
},
},
{"type": "text", "text": "圖中描繪的是什麼景象?"},
],
},
],
# 設定輸出資料的模態,當前支援兩種:["text","audio"]、["text"]
modalities=["text", "audio"],
audio={"voice": "Tina", "format": "wav"},
# stream 必須設定為 True,否則會報錯
stream=True,
stream_options={
"include_usage": True
}
)
for chunk in completion:
if chunk.choices:
print(chunk.choices[0].delta)
else:
print(chunk.usage)
import OpenAI from "openai";
const openai = new OpenAI(
{
// 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
apiKey: process.env.DASHSCOPE_API_KEY,
// 以下為新加坡地區URL,調用時請將{WorkspaceId}替換為真實的業務空間ID,各地區的URL不同。
baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
}
);
const completion = await openai.chat.completions.create({
model: "qwen3.5-omni-plus", // 模型為Qwen3-Omni-Flash時,請在非思考模式下運行
messages: [
{
"role": "user",
"content": [{
"type": "image_url",
"image_url": { "url": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241022/emyrja/dog_and_girl.jpeg" },
},
{ "type": "text", "text": "圖中描繪的是什麼景象?" }]
}],
stream: true,
stream_options: {
include_usage: true
},
modalities: ["text", "audio"],
audio: { voice: "Tina", format: "wav" }
});
for await (const chunk of completion) {
if (Array.isArray(chunk.choices) && chunk.choices.length > 0) {
console.log(chunk.choices[0].delta);
} else {
console.log(chunk.usage);
}
}
# ======= 重要提示 =======
# 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
# 以下為新加坡地區URL,調用時請將{WorkspaceId}替換為真實的業務空間ID,各地區的URL不同。
# === 執行時請刪除該注釋 ===
curl -X POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3.5-omni-plus",
"messages": [
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {
"url": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241022/emyrja/dog_and_girl.jpeg"
}
},
{
"type": "text",
"text": "圖中描繪的是什麼景象?"
}
]
}
],
"stream":true,
"stream_options":{
"include_usage":true
},
"modalities":["text","audio"],
"audio":{"voice":"Tina","format":"wav"}
}'
連網搜尋
Qwen3.5-Omni 系列支援連網搜尋,擷取即時資訊後進行推理分析。
- 連網搜尋功能僅在 Qwen3.5-Omni 系列模型中支援,僅支援
agent搜尋策略。 - 計費請參考計費說明中的
agent策略。
通過 enable_search 參數開啟連網搜尋。以下樣本查詢即時資訊:
OpenAI 相容
# 運行前的準備工作:
# pip install openai
import os
from openai import OpenAI
# 初始化用戶端
client = OpenAI(
# 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
api_key=os.getenv("DASHSCOPE_API_KEY"),
# 以下為新加坡地區URL,調用時請將{WorkspaceId}替換為真實的業務空間ID,各地區的URL不同。
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)
# 發起請求(開啟連網搜尋)
try:
completion = client.chat.completions.create(
model="qwen3.5-omni-plus",
messages=[{
"role": "user",
"content": "請查詢今天的日期和星期,並告訴我今天有哪些重要節日"
}],
stream=True,
stream_options={"include_usage": True},
# 開啟連網搜尋
extra_body={
"enable_search": True
}
)
print("模型回複(包含即時資訊):")
for chunk in completion:
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")
print()
except Exception as e:
print(f"請求失敗: {e}")
// 運行前的準備工作:
// npm install openai
import OpenAI from "openai";
// 初始化用戶端
const openai = new OpenAI({
// 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
apiKey: process.env.DASHSCOPE_API_KEY,
// 以下為新加坡地區URL,調用時請將{WorkspaceId}替換為真實的業務空間ID,各地區的URL不同。
baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
});
// 發起請求(開啟連網搜尋)
const completion = await openai.chat.completions.create({
model: "qwen3.5-omni-plus",
messages: [{
"role": "user",
"content": "請查詢今天的日期和星期,並告訴我今天有哪些重要節日"
}],
stream: true,
stream_options: {
include_usage: true
},
// 開啟連網搜尋
extra_body: {
enable_search: true
}
});
console.log("模型回複(包含即時資訊):");
for await (const chunk of completion) {
if (Array.isArray(chunk.choices) && chunk.choices.length > 0) {
if (chunk.choices[0].delta.content) {
process.stdout.write(chunk.choices[0].delta.content);
}
}
}
console.log();
# ======= 重要提示 =======
# 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
# 以下為新加坡地區URL,調用時請將{WorkspaceId}替換為真實的業務空間ID,各地區的URL不同。
# === 執行時請刪除該注釋 ===
curl -X POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3.5-omni-plus",
"messages": [
{
"role": "user",
"content": "請查詢今天的日期和星期,並告訴我今天有哪些重要節日"
}
],
"stream": true,
"stream_options": {
"include_usage": true
},
"enable_search": true
}'
開啟/關閉思考模式
Qwen-Omni 系列中,僅 Qwen3-Omni-Flash 屬於混合思考模型,通過 enable_thinking 參數控制思考模式:
true:開啟思考模式false(預設):關閉思考模式
在思考模式下,不支援輸出音頻。
OpenAI 相容
import os
from openai import OpenAI
client = OpenAI(
# 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
api_key=os.getenv("DASHSCOPE_API_KEY"),
# 以下為新加坡地區URL,調用時請將{WorkspaceId}替換為真實的業務空間ID,各地區的URL不同。
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)
completion = client.chat.completions.create(
model="qwen3-omni-flash",
messages=[{"role": "user", "content": "你是誰"}],
# 開啟/關閉思考模式,在思考模式下不支援輸出音頻;qwen-omni-turbo不支援設定enable_thinking。
extra_body={'enable_thinking': True},
# 設定輸出資料的模態,非思考模式下當前支援兩種:["text","audio"]、["text"],思考模式僅支援:["text"]
modalities=["text"],
# 設定音色,思考模式下不支援設定audio參數
# audio={"voice": "Tina", "format": "wav"},
# stream 必須設定為 True,否則會報錯
stream=True,
stream_options={"include_usage": True},
)
for chunk in completion:
if chunk.choices:
print(chunk.choices[0].delta)
else:
print(chunk.usage)
import OpenAI from "openai";
const openai = new OpenAI(
{
// 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
apiKey: process.env.DASHSCOPE_API_KEY,
// 以下為新加坡地區URL,調用時請將{WorkspaceId}替換為真實的業務空間ID,各地區的URL不同。
baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
}
);
const completion = await openai.chat.completions.create({
model: "qwen3-omni-flash",
messages: [
{ role: "user", content: "你是誰?" }
],
// stream 必須設定為 True,否則會報錯
stream: true,
stream_options: {
include_usage: true
},
// 開啟/關閉思考模式,在思考模式下不支援輸出音頻;qwen-omni-turbo不支援設定enable_thinking。
extra_body:{'enable_thinking': true},
// 設定輸出資料的模態,非思考模式下當前支援兩種:["text","audio"]、["text"],思考模式僅支援:["text"]
modalities: ["text"],
// 設定音色,思考模式下不支援設定audio參數
//audio: { voice: "Tina", format: "wav" }
});
for await (const chunk of completion) {
if (Array.isArray(chunk.choices) && chunk.choices.length > 0) {
console.log(chunk.choices[0].delta);
} else {
console.log(chunk.usage);
}
}
# ======= 重要提示 =======
# 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
# 以下為新加坡地區URL,調用時請將{WorkspaceId}替換為真實的業務空間ID,各地區的URL不同。
# === 執行時請刪除該注釋 ===
curl -X POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3-omni-flash",
"messages": [
{
"role": "user",
"content": "你是誰?"
}
],
"stream":true,
"stream_options":{
"include_usage":true
},
"modalities":["text"],
"enable_thinking": true
}'
返回結果
data: {"choices":[{"delta":{"content":null,"role":"assistant","reasoning_content":""},"index":0,"logprobs":null,"finish_reason":null}],"object":"chat.completion.chunk","usage":null,"created":1757937336,"system_fingerprint":null,"model":"qwen3-omni-flash","id":"chatcmpl-ce3d6fe5-e717-4b7e-8b40-3aef12288d4c"}
data: {"choices":[{"finish_reason":null,"logprobs":null,"delta":{"content":null,"reasoning_content":"嗯"},"index":0}],"object":"chat.completion.chunk","usage":null,"reated":1757937336,"system_fingerprint":null,"model":"qwen3-omni-flash","id":"chatcmpl-ce3d6fe5-e717-4b7e-8b40-3aef12288d4c"}
data: {"choices":[{"delta":{"content":null,"reasoning_content":","},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"reated":1757937336,"system_fingerprint":null,"model":"qwen3-omni-flash","id":"chatcmpl-ce3d6fe5-e717-4b7e-8b40-3aef12288d4c"}
......
data: {"choices":[{"delta":{"content":"告訴我"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1757937336,"tem_fingerprint":null,"model":"qwen3-omni-flash","id":"chatcmpl-ce3d6fe5-e717-4b7e-8b40-3aef12288d4c"}
data: {"choices":[{"delta":{"content":"!"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1757937336,"systm_fingerprint":null,"model":"qwen3-omni-flash","id":"chatcmpl-ce3d6fe5-e717-4b7e-8b40-3aef12288d4c"}
data: {"choices":[{"finish_reason":"stop","delta":{"content":"","reasoning_content":null},"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1757937336,"system_fingerprint":null,"model":"qwen3-omni-flash","id":"chatcmpl-ce3d6fe5-e717-4b7e-8b40-3aef12288d4c"}
data: {"choices":[],"object":"chat.completion.chunk","usage":{"prompt_tokens":11,"completion_tokens":363,"total_tokens":374,"completion_tokens_details":{"reasoning_tokens":195,"text_tokens":168},"prompt_tokens_details":{"text_tokens":11}},"created":1757937336,"system_fingerprint":null,"model":"qwen3-omni-flash","id":"chatcmpl-ce3d6fe5-e717-4b7e-8b40-3aef12288d4c"}
多輪對話
使用 Qwen-Omni 多輪對話時,注意以下限制:
-
Assistant Message
messages 數組中的 Assistant Message 只能包含文本資料。
-
User Message
每條 User Message 只能包含文本和一種模態資料,多輪對話中可在不同輪次傳入不同模態。
OpenAI 相容
import os
from openai import OpenAI
client = OpenAI(
# 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
api_key=os.getenv("DASHSCOPE_API_KEY"),
# 以下為新加坡地區URL,調用時請將{WorkspaceId}替換為真實的業務空間ID,各地區的URL不同。
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)
completion = client.chat.completions.create(
model="qwen3.5-omni-plus", # 模型為Qwen3-Omni-Flash時,請在非思考模式下運行
messages=[
{
"role": "user",
"content": [
{
"type": "input_audio",
"input_audio": {
"data": "https://dashscope.oss-cn-beijing.aliyuncs.com/audios/welcome.mp3",
"format": "mp3",
},
},
{"type": "text", "text": "這段音頻在說什麼"},
],
},
{
"role": "assistant",
"content": [{"type": "text", "text": "這段音頻在說:歡迎使用阿里雲"}],
},
{
"role": "user",
"content": [{"type": "text", "text": "介紹一下這家公司?"}],
},
],
# 設定輸出資料的模態,當前支援兩種:["text","audio"]、["text"]
modalities=["text"],
# stream 必須設定為 True,否則會報錯
stream=True,
stream_options={"include_usage": True},
)
for chunk in completion:
if chunk.choices:
print(chunk.choices[0].delta)
else:
print(chunk.usage)
import OpenAI from "openai";
const openai = new OpenAI(
{
// 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
apiKey: process.env.DASHSCOPE_API_KEY,
// 以下為新加坡地區URL,調用時請將{WorkspaceId}替換為真實的業務空間ID,各地區的URL不同。
baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
}
);
const completion = await openai.chat.completions.create({
model: "qwen3.5-omni-plus", // 模型為Qwen3-Omni-Flash時,請在非思考模式下運行
messages: [
{
"role": "user",
"content": [
{
"type": "input_audio",
"input_audio": {
"data": "https://dashscope.oss-cn-beijing.aliyuncs.com/audios/welcome.mp3",
"format": "mp3",
},
},
{ "type": "text", "text": "這段音頻在說什麼" },
],
},
{
"role": "assistant",
"content": [{ "type": "text", "text": "這段音頻在說:歡迎使用阿里雲" }],
},
{
"role": "user",
"content": [{ "type": "text", "text": "介紹一下這家公司?" }]
}],
stream: true,
stream_options: {
include_usage: true
},
modalities: ["text"]
});
for await (const chunk of completion) {
if (Array.isArray(chunk.choices) && chunk.choices.length > 0) {
console.log(chunk.choices[0].delta);
} else {
console.log(chunk.usage);
}
}
# ======= 重要提示 =======
# 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
# 以下為新加坡地區URL,調用時請將{WorkspaceId}替換為真實的業務空間ID,各地區的URL不同。
# === 執行時請刪除該注釋 ===
curl -X POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3.5-omni-plus",
"messages": [
{
"role": "user",
"content": [
{
"type": "input_audio",
"input_audio": {
"data": "https://dashscope.oss-cn-beijing.aliyuncs.com/audios/welcome.mp3"
}
},
{
"type": "text",
"text": "這段音頻在說什麼"
}
]
},
{
"role": "assistant",
"content": [
{
"type": "text",
"text": "這段音頻在說:歡迎使用阿里雲"
}
]
},
{
"role": "user",
"content": [
{
"type": "text",
"text": "介紹一下這家公司?"
}
]
}
],
"stream": true,
"stream_options": {
"include_usage": true
},
"modalities": ["text"]
}'
解析輸出的Base 64 編碼的音頻資料
Qwen-Omni 以流式輸出 Base 64 編碼的音頻資料,有兩種處理方式:
- 方式一(推薦):收集各 chunk 的 Base64 資料,流結束後統一解碼儲存為音頻檔案。
- 方式二:逐 chunk 即時解碼並播放,需額外安裝 pyaudio。
# Installation instructions for pyaudio:
# APPLE Mac OS X
# brew install portaudio
# pip install pyaudio
# Debian/Ubuntu
# sudo apt-get install python-pyaudio python3-pyaudio
# or
# pip install pyaudio
# CentOS
# sudo yum install -y portaudio portaudio-devel && pip install pyaudio
# Microsoft Windows
# python -m pip install pyaudio
import os
from openai import OpenAI
import base64
import numpy as np
import soundfile as sf
client = OpenAI(
# 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
api_key=os.getenv("DASHSCOPE_API_KEY"),
# 以下為新加坡地區URL,調用時請將{WorkspaceId}替換為真實的業務空間ID,各地區的URL不同。
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)
completion = client.chat.completions.create(
model="qwen3.5-omni-plus", # 模型為Qwen3-Omni-Flash時,請在非思考模式下運行
messages=[{"role": "user", "content": "你是誰"}],
# 設定輸出資料的模態,當前支援兩種:["text","audio"]、["text"]
modalities=["text", "audio"],
audio={"voice": "Tina", "format": "wav"},
# stream 必須設定為 True,否則會報錯
stream=True,
stream_options={"include_usage": True},
)
# 方式1: 待產生結束後再進行解碼
audio_string = ""
for chunk in completion:
if chunk.choices:
if hasattr(chunk.choices[0].delta, "audio"):
try:
audio_string += chunk.choices[0].delta.audio["data"]
except Exception as e:
print(chunk.choices[0].delta.content)
else:
print(chunk.usage)
wav_bytes = base64.b64decode(audio_string)
audio_np = np.frombuffer(wav_bytes, dtype=np.int16)
sf.write("audio_assistant_py.wav", audio_np, samplerate=24000)
# 方式2: 邊產生邊解碼(使用方式2請將方式1的代碼進行注釋)
# # 初始化 PyAudio
# import pyaudio
# import time
# p = pyaudio.PyAudio()
# # 建立音頻流
# stream = p.open(format=pyaudio.paInt16,
# channels=1,
# rate=24000,
# output=True)
# for chunk in completion:
# if chunk.choices:
# if hasattr(chunk.choices[0].delta, "audio"):
# try:
# audio_string = chunk.choices[0].delta.audio["data"]
# wav_bytes = base64.b64decode(audio_string)
# audio_np = np.frombuffer(wav_bytes, dtype=np.int16)
# # 直接播放音頻資料
# stream.write(audio_np.tobytes())
# except Exception as e:
# print(chunk.choices[0].delta.content)
# time.sleep(0.8)
# # 清理資源
# stream.stop_stream()
# stream.close()
# p.terminate()
// 運行前的準備工作:
// Windows/Mac/Linux 通用:
// 1. 確保已安裝 Node.js (建議版本 >= 14)
// 2. 運行以下命令安裝必要的依賴:
// npm install openai wav
//
// 如果要使用即時播放功能 (方式2), 還需要:
// Windows:
// npm install speaker
// Mac:
// brew install portaudio
// npm install speaker
// Linux (Ubuntu/Debian):
// sudo apt-get install libasound2-dev
// npm install speaker
import OpenAI from "openai";
const openai = new OpenAI(
{
// 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
apiKey: process.env.DASHSCOPE_API_KEY,
// 以下為新加坡地區URL,調用時請將{WorkspaceId}替換為真實的業務空間ID,各地區的URL不同。
baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
}
);
const completion = await openai.chat.completions.create({
model: "qwen3.5-omni-plus", // 模型為Qwen3-Omni-Flash時,請在非思考模式下運行
messages: [
{
"role": "user",
"content": "你是誰?"
}],
stream: true,
stream_options: {
include_usage: true
},
modalities: ["text", "audio"],
audio: { voice: "Tina", format: "wav" }
});
// 方式1: 待產生結束後再進行解碼
// 需要安裝: npm install wav
import { createWriteStream } from 'node:fs'; // node:fs 是 Node.js 內建模組,無需安裝
import { Writer } from 'wav';
async function convertAudio(audioString, audioPath) {
try {
// 解碼Base64字串為Buffer
const wavBuffer = Buffer.from(audioString, 'base64');
// 建立WAV檔案寫入流
const writer = new Writer({
sampleRate: 24000, // 採樣率
channels: 1, // 單聲道
bitDepth: 16 // 16位元深度
});
// 建立輸出檔案流並建立管道串連
const outputStream = createWriteStream(audioPath);
writer.pipe(outputStream);
// 寫入PCM資料並結束寫入
writer.write(wavBuffer);
writer.end();
// 使用Promise等待檔案寫入完成
await new Promise((resolve, reject) => {
outputStream.on('finish', resolve);
outputStream.on('error', reject);
});
// 添加額外等待時間確保音頻完整
await new Promise(resolve => setTimeout(resolve, 800));
console.log(`音頻檔案已成功儲存為 ${audioPath}`);
} catch (error) {
console.error('處理過程中發生錯誤:', error);
}
}
let audioString = "";
for await (const chunk of completion) {
if (Array.isArray(chunk.choices) && chunk.choices.length > 0) {
if (chunk.choices[0].delta.audio) {
if (chunk.choices[0].delta.audio["data"]) {
audioString += chunk.choices[0].delta.audio["data"];
}
}
} else {
console.log(chunk.usage);
}
}
// 執行轉換
convertAudio(audioString, "audio_assistant_mjs.wav");
// 方式2: 邊產生邊即時播放
// 需要先按照上方系統對應的說明安裝必要組件
// import Speaker from 'speaker'; // 引入音頻播放庫
// // 建立擴音器執行個體(配置與 WAV 檔案參數一致)
// const speaker = new Speaker({
// sampleRate: 24000, // 採樣率
// channels: 1, // 聲道數
// bitDepth: 16, // 位深
// signed: true // 有符號 PCM
// });
// for await (const chunk of completion) {
// if (Array.isArray(chunk.choices) && chunk.choices.length > 0) {
// if (chunk.choices[0].delta.audio) {
// if (chunk.choices[0].delta.audio["data"]) {
// const pcmBuffer = Buffer.from(chunk.choices[0].delta.audio.data, 'base64');
// // 直接寫入擴音器播放
// speaker.write(pcmBuffer);
// }
// }
// } else {
// console.log(chunk.usage);
// }
// }
// speaker.on('finish', () => console.log('播放完成'));
// speaker.end(); // 根據實際 API 流結束情況調用
# Installation instructions for pyaudio:
# APPLE Mac OS X
# brew install portaudio
# pip install pyaudio
# Debian/Ubuntu
# sudo apt-get install python-pyaudio python3-pyaudio
# or
# pip install pyaudio
# CentOS
# sudo yum install -y portaudio portaudio-devel && pip install pyaudio
# Microsoft Windows
# python -m pip install pyaudio
import os
from openai import OpenAI
import base64
import numpy as np
import soundfile as sf
import queue
import threading
client = OpenAI(
# 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
api_key=os.getenv("DASHSCOPE_API_KEY"),
# 以下為新加坡地區URL,調用時請將{WorkspaceId}替換為真實的業務空間ID,各地區的URL不同。
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)
# 方式2: 邊產生邊解碼(使用方式2請將方式1的代碼進行注釋)
# # 初始化 PyAudio
import pyaudio
import time
# 建立一個隊列用於儲存音頻資料
audio_queue = queue.Queue()
# 設定是否已經開始播放
started_playing = False
# 設定緩衝時間(秒)
buffer_time = 5
# 音頻播放函數(將在單獨線程中運行)
def play_audio():
global started_playing
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16,
channels=1,
rate=24000,
output=True)
# 收集的音頻資料(用於緩衝)
buffer_data = bytearray()
try:
while True:
# 如果隊列為空白且已經開始播放,等待一小段時間
if audio_queue.empty():
if started_playing:
time.sleep(0.1)
# 如果隊列持續為空白,可能意味著音頻結束了
if audio_queue.empty():
# 播放剩餘的緩衝資料
if buffer_data:
stream.write(bytes(buffer_data))
buffer_data = bytearray()
continue
else:
time.sleep(0.1)
continue
# 從隊列擷取音頻資料
audio_np = audio_queue.get()
# 將資料添加到緩衝區
buffer_data.extend(audio_np.tobytes())
# 如果還沒開始播放且緩衝區大小足夠,開始播放
samples_per_second = 24000 * 2 # 採樣率 * 每個樣本的位元組數(16位=2位元組)
buffer_size_threshold = int(samples_per_second * buffer_time)
if not started_playing and len(buffer_data) >= buffer_size_threshold:
started_playing = True
# 如果已經開始播放,按塊播放資料
if started_playing:
# 每次播放一小塊資料(例如0.1秒的資料)
chunk_size = int(samples_per_second * 0.1)
while len(buffer_data) >= chunk_size:
chunk = buffer_data[:chunk_size]
buffer_data = buffer_data[chunk_size:]
stream.write(bytes(chunk))
# 標記任務完成
audio_queue.task_done()
finally:
# 清理資源
stream.stop_stream()
stream.close()
p.terminate()
# 啟動播放線程
audio_thread = threading.Thread(target=play_audio, daemon=True)
audio_thread.start()
completion = client.chat.completions.create(
model="qwen3.5-omni-plus",
messages=[{"role": "user", "content": "你是誰"}],
# 設定輸出資料的模態,當前支援兩種:["text","audio"]、["text"]
modalities=["text", "audio"],
audio={"voice": "Tina", "format": "wav"},
# stream 必須設定為 True,否則會報錯
stream=True,
stream_options={"include_usage": True},
)
# 接收音頻資料並放入隊列
for chunk in completion:
if chunk.choices:
if hasattr(chunk.choices[0].delta, "audio"):
try:
audio_string = chunk.choices[0].delta.audio["data"]
wav_bytes = base64.b64decode(audio_string)
audio_np = np.frombuffer(wav_bytes, dtype=np.int16)
# 將音頻資料放入隊列,而不是直接播放
audio_queue.put(audio_np)
except Exception as e:
print(chunk.choices[0].delta.audio["transcript"])
# 等待所有音頻資料播放完畢
audio_queue.join()
# 額外等待一段時間,確保最後的音頻都播放完畢
time.sleep(2)
輸入 Base 64 編碼的本地檔案
使用 Base 64 編碼方式傳入檔案時,編碼後的 Base64 字串大小必須小於 10MB。
圖片
以儲存在本地的eagle.png為例。
import os
from openai import OpenAI
import base64
client = OpenAI(
# 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
api_key=os.getenv("DASHSCOPE_API_KEY"),
# 以下為新加坡地區URL,調用時請將{WorkspaceId}替換為真實的業務空間ID,各地區的URL不同。
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)
# Base 64 編碼格式
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
base64_image = encode_image("eagle.png")
completion = client.chat.completions.create(
model="qwen3.5-omni-plus", # 模型為Qwen3-Omni-Flash時,請在非思考模式下運行
messages=[
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {"url": f"data:image/png;base64,{base64_image}"},
},
{"type": "text", "text": "圖中描繪的是什麼景象?"},
],
},
],
# 設定輸出資料的模態,當前支援兩種:["text","audio"]、["text"]
modalities=["text", "audio"],
audio={"voice": "Tina", "format": "wav"},
# stream 必須設定為 True,否則會報錯
stream=True,
stream_options={"include_usage": True},
)
for chunk in completion:
if chunk.choices:
print(chunk.choices[0].delta)
else:
print(chunk.usage)
import OpenAI from "openai";
import { readFileSync } from 'fs';
const openai = new OpenAI(
{
// 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
apiKey: process.env.DASHSCOPE_API_KEY,
// 以下為新加坡地區URL,調用時請將{WorkspaceId}替換為真實的業務空間ID,各地區的URL不同。
baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
}
);
const encodeImage = (imagePath) => {
const imageFile = readFileSync(imagePath);
return imageFile.toString('base64');
};
const base64Image = encodeImage("eagle.png")
const completion = await openai.chat.completions.create({
model: "qwen3.5-omni-plus",// 模型為Qwen3-Omni-Flash時,請在非思考模式下運行
messages: [
{
"role": "user",
"content": [{
"type": "image_url",
"image_url": { "url": `data:image/png;base64,${base64Image}` },
},
{ "type": "text", "text": "圖中描繪的是什麼景象?" }]
}],
stream: true,
stream_options: {
include_usage: true
},
modalities: ["text", "audio"],
audio: { voice: "Tina", format: "wav" }
});
for await (const chunk of completion) {
if (Array.isArray(chunk.choices) && chunk.choices.length > 0) {
console.log(chunk.choices[0].delta);
} else {
console.log(chunk.usage);
}
}
音頻
以儲存在本地的welcome.mp3為例。
import os
from openai import OpenAI
import base64
import numpy as np
import soundfile as sf
import requests
client = OpenAI(
# 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
api_key=os.getenv("DASHSCOPE_API_KEY"),
# 以下為新加坡地區URL,調用時請將{WorkspaceId}替換為真實的業務空間ID,各地區的URL不同。
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)
def encode_audio(audio_path):
with open(audio_path, "rb") as audio_file:
return base64.b64encode(audio_file.read()).decode("utf-8")
base64_audio = encode_audio("welcome.mp3")
completion = client.chat.completions.create(
model="qwen3.5-omni-plus", # 模型為Qwen3-Omni-Flash時,請在非思考模式下運行
messages=[
{
"role": "user",
"content": [
{
"type": "input_audio",
"input_audio": {
"data": f"data:;base64,{base64_audio}",
"format": "mp3",
},
},
{"type": "text", "text": "這段音頻在說什麼"},
],
},
],
# 設定輸出資料的模態,當前支援兩種:["text","audio"]、["text"]
modalities=["text", "audio"],
audio={"voice": "Tina", "format": "wav"},
# stream 必須設定為 True,否則會報錯
stream=True,
stream_options={"include_usage": True},
)
for chunk in completion:
if chunk.choices:
print(chunk.choices[0].delta)
else:
print(chunk.usage)
import OpenAI from "openai";
import { readFileSync } from 'fs';
const openai = new OpenAI(
{
// 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
apiKey: process.env.DASHSCOPE_API_KEY,
// 以下為新加坡地區URL,調用時請將{WorkspaceId}替換為真實的業務空間ID,各地區的URL不同。
baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
}
);
const encodeAudio = (audioPath) => {
const audioFile = readFileSync(audioPath);
return audioFile.toString('base64');
};
const base64Audio = encodeAudio("welcome.mp3")
const completion = await openai.chat.completions.create({
model: "qwen3.5-omni-plus", // 模型為Qwen3-Omni-Flash時,請在非思考模式下運行
messages: [
{
"role": "user",
"content": [{
"type": "input_audio",
"input_audio": { "data": `data:;base64,${base64Audio}`, "format": "mp3" },
},
{ "type": "text", "text": "這段音頻在說什麼" }]
}],
stream: true,
stream_options: {
include_usage: true
},
modalities: ["text", "audio"],
audio: { voice: "Tina", format: "wav" }
});
for await (const chunk of completion) {
if (Array.isArray(chunk.choices) && chunk.choices.length > 0) {
console.log(chunk.choices[0].delta);
} else {
console.log(chunk.usage);
}
}
視頻
視頻檔案
以儲存在本地的spring_mountain.mp4為例。
import os
from openai import OpenAI
import base64
import numpy as np
import soundfile as sf
client = OpenAI(
# 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
api_key=os.getenv("DASHSCOPE_API_KEY"),
# 以下為新加坡地區URL,調用時請將{WorkspaceId}替換為真實的業務空間ID,各地區的URL不同。
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)
# Base 64 編碼格式
def encode_video(video_path):
with open(video_path, "rb") as video_file:
return base64.b64encode(video_file.read()).decode("utf-8")
base64_video = encode_video("spring_mountain.mp4")
completion = client.chat.completions.create(
model="qwen3.5-omni-plus", # 模型為Qwen3-Omni-Flash時,請在非思考模式下運行
messages=[
{
"role": "user",
"content": [
{
"type": "video_url",
"video_url": {"url": f"data:;base64,{base64_video}"},
},
{"type": "text", "text": "她在唱什麼"},
],
},
],
# 設定輸出資料的模態,當前支援兩種:["text","audio"]、["text"]
modalities=["text", "audio"],
audio={"voice": "Tina", "format": "wav"},
# stream 必須設定為 True,否則會報錯
stream=True,
stream_options={"include_usage": True},
)
for chunk in completion:
if chunk.choices:
print(chunk.choices[0].delta)
else:
print(chunk.usage)
import OpenAI from "openai";
import { readFileSync } from 'fs';
const openai = new OpenAI(
{
// 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
apiKey: process.env.DASHSCOPE_API_KEY,
// 以下為新加坡地區URL,調用時請將{WorkspaceId}替換為真實的業務空間ID,各地區的URL不同。
baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
}
);
const encodeVideo = (videoPath) => {
const videoFile = readFileSync(videoPath);
return videoFile.toString('base64');
};
const base64Video = encodeVideo("spring_mountain.mp4")
const completion = await openai.chat.completions.create({
model: "qwen3.5-omni-plus", // 模型為Qwen3-Omni-Flash時,請在非思考模式下運行
messages: [
{
"role": "user",
"content": [{
"type": "video_url",
"video_url": { "url": `data:;base64,${base64Video}` },
},
{ "type": "text", "text": "她在唱什麼" }]
}],
stream: true,
stream_options: {
include_usage: true
},
modalities: ["text", "audio"],
audio: { voice: "Tina", format: "wav" }
});
for await (const chunk of completion) {
if (Array.isArray(chunk.choices) && chunk.choices.length > 0) {
console.log(chunk.choices[0].delta);
} else {
console.log(chunk.usage);
}
}
圖片列表
以儲存在本地的football1.jpg、football2.jpg、football3.jpg與football4.jpg為例。
import os
from openai import OpenAI
import base64
import numpy as np
import soundfile as sf
client = OpenAI(
# 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
api_key=os.getenv("DASHSCOPE_API_KEY"),
# 以下為新加坡地區URL,調用時請將{WorkspaceId}替換為真實的業務空間ID,各地區的URL不同。
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)
# Base 64 編碼格式
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
base64_image_1 = encode_image("football1.jpg")
base64_image_2 = encode_image("football2.jpg")
base64_image_3 = encode_image("football3.jpg")
base64_image_4 = encode_image("football4.jpg")
completion = client.chat.completions.create(
model="qwen3.5-omni-plus", # 模型為Qwen3-Omni-Flash時,請在非思考模式下運行
messages=[
{
"role": "user",
"content": [
{
"type": "video",
"video": [
f"data:image/jpeg;base64,{base64_image_1}",
f"data:image/jpeg;base64,{base64_image_2}",
f"data:image/jpeg;base64,{base64_image_3}",
f"data:image/jpeg;base64,{base64_image_4}",
],
},
{"type": "text", "text": "描述這個視頻的具體過程"},
],
}
],
# 設定輸出資料的模態,當前支援兩種:["text","audio"]、["text"]
modalities=["text", "audio"],
audio={"voice": "Tina", "format": "wav"},
# stream 必須設定為 True,否則會報錯
stream=True,
stream_options={"include_usage": True},
)
for chunk in completion:
if chunk.choices:
print(chunk.choices[0].delta)
else:
print(chunk.usage)
import OpenAI from "openai";
import { readFileSync } from 'fs';
const openai = new OpenAI(
{
// 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
apiKey: process.env.DASHSCOPE_API_KEY,
// 以下為新加坡地區URL,調用時請將{WorkspaceId}替換為真實的業務空間ID,各地區的URL不同。
baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
}
);
const encodeImage = (imagePath) => {
const imageFile = readFileSync(imagePath);
return imageFile.toString('base64');
};
const base64Image1 = encodeImage("football1.jpg")
const base64Image2 = encodeImage("football2.jpg")
const base64Image3 = encodeImage("football3.jpg")
const base64Image4 = encodeImage("football4.jpg")
const completion = await openai.chat.completions.create({
model: "qwen3.5-omni-plus", // 模型為Qwen3-Omni-Flash時,請在非思考模式下運行
messages: [{
role: "user",
content: [
{
type: "video",
video: [
`data:image/jpeg;base64,${base64Image1}`,
`data:image/jpeg;base64,${base64Image2}`,
`data:image/jpeg;base64,${base64Image3}`,
`data:image/jpeg;base64,${base64Image4}`
]
},
{
type: "text",
text: "描述這個視頻的具體過程"
}
]
}],
stream: true,
stream_options: {
include_usage: true
},
modalities: ["text", "audio"],
audio: { voice: "Tina", format: "wav" }
});
for await (const chunk of completion) {
if (Array.isArray(chunk.choices) && chunk.choices.length > 0) {
console.log(chunk.choices[0].delta);
} else {
console.log(chunk.usage);
}
API參考
Qwen-Omni 模型的輸入輸出參數詳情,請參見OpenAI相容-Chat。
計費與限流
計費規則Qwen-Omni 根據不同模態(音頻、映像、視頻)的 Token 數計費。詳情請參見百鍊控制台。
音頻、圖片與視頻轉換為Token數的規則
音頻
-
Qwen3.5-Omni系列:- 輸入音頻計算公式:
總 Tokens 數 = 音頻時間長度(單位:秒)* 7 - 輸出音頻計算公式:
總 Tokens 數 = 音頻時間長度(單位:秒)* 12.5
- 輸入音頻計算公式:
-
Qwen3-Omni-Flash:輸入與輸出音訊計算公式均為總 Tokens 數 = 音頻時間長度(單位:秒)* 12.5 -
Qwen-Omni-Turbo:輸入與輸出音訊計算公式均為總 Tokens 數 = 音頻時間長度(單位:秒)* 25
不足 1 秒的音頻按 1 秒計算。
圖片
Qwen3.5-Omni系列、Qwen3-Omni-Flash模型:每32x32像素對應 1 個 TokenQwen-Omni-Turbo模型:每28x28像素對應 1 個 Token
Qwen3.5-Omni 系列每張圖最少 24 個 Token,其他模型最少 4 個 Token;預設上限 1280 個 Token。Qwen3.5-Omni 系列可通過 vl_high_resolution_images 參數將上限提升至 16384 個 Token(Qwen-Omni-Turbo 和 Qwen3-Omni-Flash 不支援)。以下代碼可估算單張圖片消耗的 Token 數:
import math
from PIL import Image # pip install Pillow
# ============ 模型參數配置(按需修改) ============
# 映像因子:Qwen3.5-Omni系列、Qwen3-Omni-Flash 為 32;Qwen-Omni-Turbo 為 28
IMAGE_FACTOR = 32
# Token 下限:Qwen3.5-Omni系列為 24;Qwen-Omni-Turbo、Qwen3-Omni-Flash 為 4
MIN_TOKENS = 24
# 高解析度模式(僅 Qwen3.5-Omni 系列支援,Qwen-Omni-Turbo 和 Qwen3-Omni-Flash 不支援)
# True → Token 上限 16384
# False → Token 上限 1280(預設)
VL_HIGH_RESOLUTION_IMAGES = False
# ============ 像素範圍(由上方參數自動計算) ============
MIN_PIXELS = MIN_TOKENS * IMAGE_FACTOR * IMAGE_FACTOR
MAX_PIXELS = (16384 if VL_HIGH_RESOLUTION_IMAGES else 1280) * IMAGE_FACTOR * IMAGE_FACTOR
def smart_resize(height, width, factor=IMAGE_FACTOR,
min_pixels=MIN_PIXELS, max_pixels=MAX_PIXELS):
"""將映像寬高對齊到 factor 整數倍,並縮放到 [min_pixels, max_pixels] 範圍內。"""
h_bar = max(factor, round(height / factor) * factor)
w_bar = max(factor, round(width / factor) * factor)
if h_bar * w_bar > max_pixels:
beta = math.sqrt((height * width) / max_pixels)
h_bar = math.floor(height / beta / factor) * factor
w_bar = math.floor(width / beta / factor) * factor
elif h_bar * w_bar < min_pixels:
beta = math.sqrt(min_pixels / (height * width))
h_bar = math.ceil(height * beta / factor) * factor
w_bar = math.ceil(width * beta / factor) * factor
return h_bar, w_bar
if __name__ == "__main__":
image = Image.open("xxx/test.jpg")
print(f"原始大小:{image.width}x{image.height}")
resized_h, resized_w = smart_resize(image.height, image.width)
token = int(resized_h * resized_w / (IMAGE_FACTOR * IMAGE_FACTOR)) + 2
print(f"縮放後尺寸:{resized_w}x{resized_h},Token 數:{token}")
視頻
視頻檔案的 Token 分為視覺部分 video_tokens 和音頻部分 audio_tokens。
-
video_tokens計算過程較為複雜。請參見以下代碼:
# pip install opencv-python
import math
import cv2
# ============ 模型參數配置(按需修改) ============
# 映像因子:Qwen3.5-Omni系列、Qwen3-Omni-Flash 為 32;Qwen-Omni-Turbo 為 28
IMAGE_FACTOR = 32
FRAME_FACTOR = 2
FPS = 2
MAX_RATIO = 200
# 視訊框架的像素下限
VIDEO_MIN_PIXELS = 64 * IMAGE_FACTOR * IMAGE_FACTOR
# 視訊框架的像素上限
# Qwen3.5-Omni系列:640 * 32 * 32
# Qwen3-Omni-Flash:768 * 32 * 32
# Qwen-Omni-Turbo:768 * 28 * 28
VIDEO_MAX_PIXELS = 640 * IMAGE_FACTOR * IMAGE_FACTOR
# 最少抽取幀數:Qwen3.5-Omni系列、Qwen3-Omni-Flash 為 2;Qwen-Omni-Turbo 為 4
FPS_MIN_FRAMES = 2
# 最大抽取幀數:Qwen3.5-Omni系列為 2048;Qwen3-Omni-Flash 為 128;Qwen-Omni-Turbo 為 80
FPS_MAX_FRAMES = 2048
# 視頻輸入的最大像素值
# Qwen3.5-Omni系列:180224 * 32 * 32
# Qwen3-Omni-Flash:16384 * 32 * 32
# Qwen-Omni-Turbo:16384 * 28 * 28
VIDEO_TOTAL_PIXELS = 180224 * IMAGE_FACTOR * IMAGE_FACTOR
# ============ 核心函數 ============
def get_video_info(video_path):
"""讀取視頻的基本資料:高度、寬度、總幀數、幀率。"""
cap = cv2.VideoCapture(video_path)
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
fps = cap.get(cv2.CAP_PROP_FPS)
cap.release()
return height, width, total_frames, fps
def smart_nframes(total_frames, video_fps):
"""根據視頻時間長度和幀率,計算實際抽取的幀數。"""
min_frames = math.ceil(FPS_MIN_FRAMES / FRAME_FACTOR) * FRAME_FACTOR
max_frames = min(FPS_MAX_FRAMES, total_frames) // FRAME_FACTOR * FRAME_FACTOR
duration = total_frames / video_fps if video_fps else 0
if duration - int(duration) > (1 / FPS):
total_frames = math.ceil(duration * video_fps)
else:
total_frames = math.ceil(int(duration) * video_fps)
nframes = total_frames / video_fps * FPS
nframes = int(min(max(nframes, min_frames), max_frames, total_frames))
if not (FRAME_FACTOR <= nframes <= total_frames):
raise ValueError(f"nframes should in [{FRAME_FACTOR}, {total_frames}], got {nframes}")
return nframes
def smart_resize(height, width, nframes, factor=IMAGE_FACTOR):
"""將視訊框架縮放到合理的像素範圍內,寬高對齊到 factor 整數倍。"""
max_pixels = max(
min(VIDEO_MAX_PIXELS, VIDEO_TOTAL_PIXELS / nframes * FRAME_FACTOR),
int(VIDEO_MIN_PIXELS * 1.05)
)
if max(height, width) / min(height, width) > MAX_RATIO:
raise ValueError(f"aspect ratio exceeds {MAX_RATIO}")
h_bar = max(factor, round(height / factor) * factor)
w_bar = max(factor, round(width / factor) * factor)
if h_bar * w_bar > max_pixels:
beta = math.sqrt((height * width) / max_pixels)
h_bar = math.floor(height / beta / factor) * factor
w_bar = math.floor(width / beta / factor) * factor
elif h_bar * w_bar < VIDEO_MIN_PIXELS:
beta = math.sqrt(VIDEO_MIN_PIXELS / (height * width))
h_bar = math.ceil(height * beta / factor) * factor
w_bar = math.ceil(width * beta / factor) * factor
return h_bar, w_bar
# ============ 計算 Token ============
if __name__ == "__main__":
video_path = "spring_mountain.mp4"
height, width, total_frames, video_fps = get_video_info(video_path)
print(f"視頻資訊:{width}x{height},{total_frames} 幀,{video_fps:.1f} fps")
nframes = smart_nframes(total_frames, video_fps)
resized_h, resized_w = smart_resize(height, width, nframes)
video_tokens = int(
math.ceil(nframes / FPS) * resized_h / IMAGE_FACTOR * resized_w / IMAGE_FACTOR
) + 2
print(f"抽取幀數:{nframes},縮放後尺寸:{resized_w}x{resized_h},video_tokens:{video_tokens}")
-
audio_tokens-
Qwen3.5-Omni系列:- 輸入音頻計算公式:
總 Tokens 數 = 音頻時間長度(單位:秒)* 7 - 輸出音頻計算公式:
總 Tokens 數 = 音頻時間長度(單位:秒)* 12.5
- 輸入音頻計算公式:
-
Qwen3-Omni-Flash:輸入與輸出音訊計算公式均為總 Tokens 數 = 音頻時間長度(單位:秒)* 12.5 -
Qwen-Omni-Turbo:輸入與輸出音訊計算公式均為總 Tokens 數 = 音頻時間長度(單位:秒)* 25
若音頻時間長度不足1秒,則按 1 秒計算。
-
免費額度的領取、查詢和使用方法,請參見新人免費額度。
限流限流規則及常見問題請參見限流。
錯誤碼
如果模型調用失敗並返回報錯資訊,請參見錯誤碼進行解決。
音色列表
Qwen-Omni 支援的音色列表請參見音色列表。