請求體 | import os
from openai import OpenAI
client = OpenAI(
# 若沒有配置環境變數,請用阿里雲百鍊API Key將下行替換為:api_key="sk-xxx",
api_key=os.getenv("DASHSCOPE_API_KEY"),
# 以下是新加坡地區base_url,如果使用北京地區的模型,需要將base_url替換為:https://dashscope.aliyuncs.com/compatible-mode/v1
base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1",
)
# ----------------音頻輸入 ----------------
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",
},
}
],
}
]
# ----------------視頻輸入(需取消注釋)----------------
# 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"
# },
# }
# ],
# },
# ]
completion = client.chat.completions.create(
model="qwen3-livetranslate-flash",
messages=messages,
modalities=["text", "audio"],
audio={"voice": "Cherry", "format": "wav"},
stream=True,
stream_options={"include_usage": True},
extra_body={"translation_options": {"source_lang": "zh", "target_lang": "en"}},
)
for chunk in completion:
print(chunk)
import OpenAI from "openai";
const client = new OpenAI({
// 若沒有配置環境變數,請用阿里雲百鍊API Key將下行替換為:apiKey: "sk-xxx",
apiKey: process.env.DASHSCOPE_API_KEY,
// 以下是新加坡地區base_url,如果使用北京地區的模型,需要將base_url替換為:https://dashscope.aliyuncs.com/compatible-mode/v1
baseURL: "https://dashscope-intl.aliyuncs.com/compatible-mode/v1",
});
// ---------------- 音頻輸入 ----------------
const 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",
},
},
],
},
];
// ---------------- 視頻輸入(需取消注釋) ----------------
// const 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",
// },
// },
// ],
// },
// ];
async function main() {
const completion = await client.chat.completions.create({
model: "qwen3-livetranslate-flash",
messages: messages,
modalities: ["text", "audio"],
audio: { voice: "Cherry", format: "wav" },
stream: true,
stream_options: { include_usage: true },
translation_options: { source_lang: "zh", target_lang: "en" },
});
for await (const chunk of completion) {
console.log(JSON.stringify(chunk));
}
}
main();
# ======= 重要提示 =======
# 以下是新加坡地區樣本,如果使用北京地區的模型,需要將請求地址替換為:https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions
# === 執行時請刪除該注釋 ===
curl -X POST https://dashscope-intl.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3-livetranslate-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"
}
}
]
}
],
"modalities": ["text", "audio"],
"audio": {
"voice": "Cherry",
"format": "wav"
},
"stream": true,
"stream_options": {
"include_usage": true
},
"translation_options": {
"source_lang": "zh",
"target_lang": "en"
}
}'
|
model string (必選) 模型名稱。支援的模型:qwen3-livetranslate-flash、qwen3-livetranslate-flash-2025-12-01。 |
messages array (必選) 訊息數組,用於向大模型傳遞上下文。僅支援傳入一個 User Message。 訊息類型 User Message object (必選) 使用者訊息。 屬性 content array (必選) 訊息內容。 屬性 type string (必選) 可選值: input_audio
輸入音頻時需設為input_audio。 video_url
輸入視頻檔案時需設為video_url。
input_audio object 輸入的音頻資訊。當type為input_audio時是必選參數。 屬性 format string(必選) 輸入音訊格式,如mp3、wav等。 video_url object 輸入的視頻檔案資訊。當type為video_url時是必選參數。 role string (必選) 使用者訊息的角色,固定為user。 |
stream boolean (必選) 預設值為 false 是否以流式方式輸出回複。模型僅支援流式輸出方式調用,僅可設為true。 |
stream_options object (可選) 流式輸出的配置項,僅在 stream 為 true 時生效。 屬性 include_usage boolean (可選)預設值為 false 是否在最後一個資料區塊包含Token消耗資訊。 可選值: |
modalities array (可選)預設值為["text"] 輸出資料的模態。可選值: |
audio object (可選) 輸出音訊音色與格式。modalities參數需為["text","audio"]。 屬性 voice string (必選) 輸出音訊音色。請參見支援的音色。 format string (必選) 輸出音訊格式,僅支援設定為wav。 |
max_tokens integer (可選) 用於限制模型輸出的最大 Token 數。若產生內容超過此值,響應將被截斷。 預設值與最大值均為模型的最大輸出長度,請參見模型選型。 |
seed integer (可選) 隨機數種子。用於確保在相同輸入和參數下產生結果可複現。若調用時傳入相同的 seed 且其他參數不變,模型將儘可能返回相同結果。 取值範圍:[0,231−1]。 |
temperature float (可選) 預設值為0.000001 採樣溫度,控制模型產生內容的多樣性。temperature越高,產生的內容更多樣,反之更確定。 取值範圍: [0, 2) 為了翻譯的準確性,不建議修改該值。 |
top_p float (可選)預設值為0.8 核採樣的機率閾值,控制模型產生內容的多樣性。 top_p越高,產生的內容更多樣。反之更確定。 取值範圍:(0,1.0] 為了翻譯的準確性,不建議修改該值。 |
presence_penalty float (可選)預設值為0 控制模型產生文本時的內容重複度。 取值範圍:[-2.0, 2.0]。正值降低重複度,負值增加重複度。為了翻譯的準確性,不建議修改該值。 |
top_k integer (可選)預設值為1 產生過程中採樣候選集的大小。例如,取值為50時,僅將單次產生中得分最高的50個Token組成隨機採樣的候選集。取值越大,產生的隨機性越高;取值越小,產生的確定性越高。取值為None或當top_k大於100時,表示不啟用top_k策略,此時僅有top_p策略生效。 取值需要大於或等於0。為了翻譯的準確性,不建議修改該值。 該參數非OpenAI標準參數。通過 Python SDK調用時,請放入 extra_body 對象中,配置方式為:extra_body={"top_k": xxx};通過 Node.js SDK 或 HTTP 方式調用時,請作為頂層參數傳遞。 |
repetition_penalty float (可選)預設值為1.05 模型產生時連續序列中的重複度。提高repetition_penalty時可以降低模型產生的重複度,1.0表示不做懲罰。取值大於0即可。為了翻譯的準確性,不建議修改該值。 該參數非OpenAI標準參數。通過 Python SDK調用時,請放入 extra_body 對象中,配置方式為:extra_body={"repetition_penalty": xxx};通過 Node.js SDK 或 HTTP 方式調用時,請作為頂層參數傳遞。 |
translation_options object (必選) 需配置的翻譯參數。 屬性 source_lang string (可選) 源語言的英文全稱,請參見支援的語種。若不設定,模型會自動識別輸入的語種。 target_lang string (必選) 目標語言的英文全稱,請參見支援的語種。 該參數非OpenAI標準參數。通過 Python SDK調用時,請放入 extra_body 對象中,配置方式為:extra_body={"translation_options": xxx};通過 Node.js SDK 或 HTTP 方式調用時,請作為頂層參數傳遞。 |