Python
DashScope Python SDK中的SpeechSynthesizer介面已統一為MultiModalConversation,使用新介面只需替換名稱即可,其他參數完全相容。
# DashScope SDK 版本不低於 1.24.5
import os
import dashscope
# 以下為新加坡地區的配置。
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
text = "那我來給大家推薦一款T恤,這款呢真的是超級好看,這個顏色呢很顯氣質,而且呢也是搭配的絕佳單品,大家可以閉眼入,真的是非常好看,對身材的包容性也很好,不管啥身材的寶寶呢,穿上去都是很好看的。推薦寶寶們下單哦。"
# SpeechSynthesizer介面使用方法:dashscope.audio.qwen_tts.SpeechSynthesizer.call(...)
response = dashscope.MultiModalConversation.call(
# 如需使用指令控制功能,請將model替換為qwen3-tts-instruct-flash
model="qwen3-tts-flash",
# 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
# 若沒有配置環境變數,請用阿里雲百鍊API Key將下行替換為:api_key="sk-xxx"
api_key=os.getenv("DASHSCOPE_API_KEY"),
text=text,
voice="Cherry",
# 如需使用指令控制功能,請取消下方注釋,並將model替換為qwen3-tts-instruct-flash
# instructions='語速較快,帶有明顯的上揚語調,適合介紹時尚產品。',
# optimize_instructions=True,
stream=True
)
for chunk in response:
print(chunk)
Java
// DashScope SDK 版本需要不低於 2.19.0
import com.alibaba.dashscope.aigc.multimodalconversation.AudioParameters;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversation;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationParam;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationResult;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.exception.UploadFileException;
import com.alibaba.dashscope.utils.JsonUtils;
import io.reactivex.Flowable;
import com.alibaba.dashscope.utils.Constants;
public class Main {
// 如需使用指令控制功能,請將MODEL替換為qwen3-tts-instruct-flash
private static final String MODEL = "qwen3-tts-flash";
public static void streamCall() throws ApiException, NoApiKeyException, UploadFileException {
MultiModalConversation conv = new MultiModalConversation();
MultiModalConversationParam param = MultiModalConversationParam.builder()
.model(MODEL)
// 新加坡和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
// 若沒有配置環境變數,請用阿里雲百鍊API Key將下行替換為:.apiKey("sk-xxx")
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
.text("Today is a wonderful day to build something people love!")
.voice(AudioParameters.Voice.CHERRY)
.parameter("language_type", "English")
// 如需使用指令控制功能,請取消下方注釋,並將model替換為qwen3-tts-instruct-flash
// .parameter("instructions","語速較快,帶有明顯的上揚語調,適合介紹時尚產品。")
// .parameter("optimize_instructions",true)
.build();
Flowable<MultiModalConversationResult> result = conv.streamCall(param);
result.blockingForEach(r -> {System.out.println(JsonUtils.toJson(r));
});
}
public static void main(String[] args) {
// 以下為新加坡地區的配置。
Constants.baseHttpApiUrl = "https://dashscope-intl.aliyuncs.com/api/v1";
try {
streamCall();
} catch (ApiException | NoApiKeyException | UploadFileException e) {
System.out.println(e.getMessage());
}
System.exit(0);
}
}
curl
# ======= 重要提示 =======
# 以下為新加坡地區的配置。
# 新加坡地區和北京地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
# 若沒有配置環境變數,請用阿里雲百鍊API Key將$DASHSCOPE_API_KEY替換為:sk-xxx。
# === 執行時請刪除該注釋 ===
curl -X POST 'https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation' \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H 'Content-Type: application/json' \
-H 'X-DashScope-SSE: enable' \
-d '{
"model": "qwen3-tts-flash",
"input": {
"text": "那我來給大家推薦一款T恤,這款呢真的是超級好看,這個顏色呢很顯氣質,而且呢也是搭配的絕佳單品,大家可以閉眼入,真的是非常好看,對身材的包容性也很好,不管啥身材的寶寶呢,穿上去都是很好看的。推薦寶寶們下單哦。",
"voice": "Cherry",
"language_type": "Chinese"
}
}'