萬相映像產生模型支援影像編輯、圖文混排輸出,滿足多樣化產生與整合需求。
模型概覽
|
模型名稱 |
模型簡介 |
輸出映像規格 |
|
wan2.6-image |
萬相2.6 image 支援影像編輯和圖文混排輸出 |
圖片格式:PNG。 映像解析度和尺寸請參見size參數。 |
調用前,請查閱各地區支援的模型列表與價格。
前提條件
您需要已擷取API Key與API Host並配置API Key到環境變數(準備下線,併入配置 API Key)。請將範例程式碼中的 DASHSCOPE_API_HOST 替換為擷取的 API Host。
新加坡、維吉尼亞和北京地區擁有獨立的 API Key 與請求地址,不可混用,跨地區調用將導致鑒權失敗或服務報錯,詳情請參見選擇部署模式。
HTTP同步調用
一次請求即可獲得結果,流程簡單,推薦大多數情境使用。
新加坡地區:POST https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation
維吉尼亞地區:POST https://dashscope-us.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation
北京地區:POST https://dashscope.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation
請求參數 |
影像編輯
圖文混排(僅支援流式)同步介面在啟用圖文混排輸出(即
|
要求標頭(Headers) |
|
|
Content-Type 請求內容類型。此參數必須設定為 |
|
|
Authorization 請求身份認證。介面使用阿里雲百鍊API-Key進行身份認證。樣本值:Bearer sk-xxxx。 |
|
|
X-DashScope-Sse 用於啟用流式輸出。
|
|
請求體(Request Body) |
|
|
model 模型名稱。設定為wan2.6-image。 |
|
|
input 輸入的基本資料。 |
|
|
parameters 影像處理參數。 |
響應參數 |
任務執行成功任務資料(如任務狀態、映像URL等)僅保留24小時,逾時後會被自動清除。請您務必及時儲存產生的映像。
任務執行成功(流式輸出)任務資料(如任務狀態、映像URL等)僅保留24小時,逾時後會被自動清除。請您務必及時儲存產生的映像。
任務執行異常如果因為某種原因導致任務執行失敗,將返回相關資訊,可以通過code和message欄位明確指示錯誤原因。請參見錯誤資訊進行解決。
|
|
output 任務輸出資訊。 |
|
|
usage 輸出資訊統計。只對成功的結果計數。 |
|
|
request_id 請求唯一標識。可用於請求明細溯源和問題排查。 |
|
|
code 請求失敗的錯誤碼。請求成功時不會返回此參數,詳情請參見錯誤資訊。 |
|
|
message 請求失敗的詳細資料。請求成功時不會返回此參數,詳情請參見錯誤資訊。 |
HTTP非同步呼叫
由於映像產生任務耗時較長(通常為1-2分鐘),API採用非同步呼叫以避免請求逾時。整個流程包含 “建立任務 -> 輪詢擷取” 兩個核心步驟,具體如下:
具體耗時受限於排隊任務數和服務執行情況,請在擷取結果時耐心等待。
步驟1:建立任務擷取任務ID
新加坡地區:POST https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/image-generation/generation
維吉尼亞地區:POST https://dashscope-us.aliyuncs.com/api/v1/services/aigc/image-generation/generation
北京地區:POST https://dashscope.aliyuncs.com/api/v1/services/aigc/image-generation/generation
建立成功後,使用介面返回的
task_id查詢結果,task_id 有效期間為 24 小時。請勿重複建立任務,輪詢擷取即可。新手指引請參見Postman。
請求參數 |
影像編輯
圖文混排輸出
|
要求標頭(Headers) |
|
|
Content-Type 請求內容類型。此參數必須設定為 |
|
|
Authorization 請求身份認證。介面使用阿里雲百鍊API-Key進行身份認證。樣本值:Bearer sk-xxxx。 |
|
|
X-DashScope-Async 非同步處理配置參數,必須設定為 重要
缺少此要求標頭將報錯:“current user api does not support synchronous calls”。 |
|
請求體(Request Body) |
|
|
model 模型名稱。 樣本值:wan2.6-image。 |
|
|
input 輸入的基本資料。 |
|
|
parameters 影像處理參數。 |
響應參數 |
成功響應請儲存 task_id,用於查詢任務狀態與結果。 異常響應建立任務失敗,請參見錯誤資訊進行解決。 |
|
output 任務輸出資訊。 |
|
|
request_id 請求唯一標識。可用於請求明細溯源和問題排查。 |
|
|
code 請求失敗的錯誤碼。請求成功時不會返回此參數,詳情請參見錯誤資訊。 |
|
|
message 請求失敗的詳細資料。請求成功時不會返回此參數,詳情請參見錯誤資訊。 |
步驟2:根據任務ID查詢結果
新加坡地區:GET https://dashscope-intl.aliyuncs.com/api/v1/tasks/{task_id}
維吉尼亞地區:GET https://dashscope-us.aliyuncs.com/api/v1/tasks/{task_id}
北京地區:GET https://dashscope.aliyuncs.com/api/v1/tasks/{task_id}
-
輪詢建議:映像產生過程較長,建議採用輪詢機制,並設定合理的查詢間隔(如 10 秒)來擷取結果。
-
任務狀態流轉:PENDING(排隊中)→ RUNNING(處理中)→ SUCCEEDED(成功)/ FAILED(失敗)。
-
結果連結:任務成功後返回映像連結,有效期間為 24 小時。建議在擷取連結後立即下載並轉存至永久儲存(如阿里雲 OSS)。
請求參數 |
查詢任務結果將 |
要求標頭(Headers) |
|
|
Authorization 請求身份認證。介面使用阿里雲百鍊API-Key進行身份認證。樣本值:Bearer sk-xxxx。 |
|
URL路徑參數(Path parameters) |
|
|
task_id 任務ID。 |
響應參數 |
任務執行成功任務資料(如任務狀態、映像URL等)僅保留24小時,逾時後會被自動清除。請您務必及時儲存產生的映像。
任務執行異常如果因為某種原因導致任務執行失敗,將返回相關資訊,可以通過code和message欄位明確指示錯誤原因。請參見錯誤資訊進行解決。
|
|
output 任務輸出資訊。 |
|
|
usage 輸出資訊統計。只對成功的結果計數。 |
|
|
request_id 請求唯一標識。可用於請求明細溯源和問題排查。 |
|
|
code 請求失敗的錯誤碼。請求成功時不會返回此參數,詳情請參見錯誤資訊。 |
|
|
message 請求失敗的詳細資料。請求成功時不會返回此參數,詳情請參見錯誤資訊。 |
DashScope Python SDK調用
SDK 的參數命名與HTTP介面基本一致,參數結構根據語言特性進行封裝。
由於任務耗時較長,SDK 在底層封裝了 HTTP 非同步呼叫流程,支援同步、非同步兩種調用方式。
具體耗時受限於排隊任務數和服務執行情況,請在擷取結果時耐心等待。
請確保 DashScope Python SDK 版本不低於 1.25.8,再運行以下代碼。更新請參考安裝SDK。
各地區的base_url和 API Key 不通用,以下樣本以新加坡地區為例進行調用:
新加坡地區:https://dashscope-intl.aliyuncs.com/api/v1
維吉尼亞地區:https://dashscope-us.aliyuncs.com/api/v1
北京地區:https://dashscope.aliyuncs.com/api/v1
影像編輯
同步調用
請求樣本
import os
import dashscope
from dashscope.aigc.image_generation import ImageGeneration
from dashscope.api_entities.dashscope_response import Message
# 以下為新加坡地區base_url,各地區的base_url不同
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
# 若沒有配置環境變數,請用百鍊API Key將下行替換為:api_key="sk-xxx"
# 各地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
api_key = os.getenv("DASHSCOPE_API_KEY")
message = Message(
role="user",
# 支援本地檔案 如 "image": "file://umbrella1.png"
content=[
{
"text": "參考圖1的風格和圖2的背景,產生番茄炒蛋"
},
{
"image": "https://cdn.wanx.aliyuncs.com/tmp/pressure/umbrella1.png"
},
{
"image": "https://img.alicdn.com/imgextra/i3/O1CN01SfG4J41UYn9WNt4X1_!!6000000002530-49-tps-1696-960.webp"
}
]
)
print("----sync call, please wait a moment----")
rsp = ImageGeneration.call(
model='wan2.6-image',
api_key=api_key,
messages=[message],
negative_prompt="",
prompt_extend=True,
watermark=False,
n=1,
enable_interleave=False,
size="1280*1280"
)
print(rsp)
響應樣本
url 有效期間24小時,請及時下載映像。
{
"status_code": 200,
"request_id": "b6a4c68d-3a91-4018-ae96-3cf373xxxxxx",
"code": "",
"message": "",
"output": {
"text": null,
"finish_reason": null,
"choices": [
{
"finish_reason": "stop",
"message": {
"role": "assistant",
"content": [
{
"image": "https://dashscope-result-bj.oss-cn-beijing.aliyuncs.com/xxxxxx.png?Expires=xxxxxx",
"type": "image"
}
]
}
}
],
"audio": null,
"finished": true
},
"usage": {
"input_tokens": 0,
"output_tokens": 0,
"characters": 0,
"image_count": 1,
"size": "1280*1280",
"total_tokens": 0
}
}
非同步呼叫
請求樣本
import os
import dashscope
from dashscope.aigc.image_generation import ImageGeneration
from dashscope.api_entities.dashscope_response import Message
from http import HTTPStatus
# 以下為新加坡地區base_url,各地區的base_url不同
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
# 若沒有配置環境變數,請用百鍊API Key將下行替換為:api_key="sk-xxx"
# 各地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
api_key = os.getenv("DASHSCOPE_API_KEY")
# 建立非同步任務
def create_async_task():
print("Creating async task...")
message = Message(
role="user",
content=[
{'text': '參考圖1的風格和圖2的背景,產生番茄炒蛋'},
{'image': 'https://cdn.wanx.aliyuncs.com/tmp/pressure/umbrella1.png'},
{'image': 'https://img.alicdn.com/imgextra/i3/O1CN01SfG4J41UYn9WNt4X1_!!6000000002530-49-tps-1696-960.webp'}
]
)
response = ImageGeneration.async_call(
model="wan2.6-image",
api_key=api_key,
messages=[message],
negative_prompt="",
prompt_extend=True,
watermark=False,
n=1,
enable_interleave=False,
size="1280*1280"
)
if response.status_code == 200:
print("Task created successfully:", response)
return response # 返回任務ID
else:
raise Exception(f"Failed to create task: {response.code} - {response.message}")
# 等待任務完成
def wait_for_completion(task_response):
print("Waiting for task completion...")
status = ImageGeneration.wait(task=task_response, api_key=api_key)
if status.output.task_status == "SUCCEEDED":
print("Task succeeded!")
print("Response:", status)
else:
raise Exception(f"Task failed with status: {status.output.task_status}")
# 擷取非同步任務資訊
def fetch_task_status(task):
print("Fetching task status...")
status = ImageGeneration.fetch(task=task, api_key=api_key)
if status.status_code == HTTPStatus.OK:
print("Task status:", status.output.task_status)
print("Response details:", status)
else:
print(f"Failed to fetch status: {status.code} - {status.message}")
# 取消非同步任務
def cancel_task(task):
print("Canceling task...")
response = ImageGeneration.cancel(task=task, api_key=api_key)
if response.status_code == HTTPStatus.OK:
print("Task canceled successfully:", response.output.task_status)
else:
print(f"Failed to cancel task: {response.code} - {response.message}")
# 主執行流程
if __name__ == "__main__":
task = create_async_task()
wait_for_completion(task)
響應樣本
1、建立任務的響應樣本
{
"status_code": 200,
"request_id": "4fb3050f-de57-4a24-84ff-e37ee5xxxxxx",
"code": "",
"message": "",
"output": {
"text": null,
"finish_reason": null,
"choices": null,
"audio": null,
"task_id": "127ec645-118f-4884-955d-0eba8dxxxxxx",
"task_status": "PENDING"
},
"usage": {
"input_tokens": 0,
"output_tokens": 0,
"characters": 0
}
}
2、查詢任務結果的響應樣本
url 有效期間24小時,請及時下載映像。
{
"status_code": 200,
"request_id": "b2a7fab4-5e00-4b0a-86fe-8b9964xxxxxx",
"code": null,
"message": "",
"output": {
"text": null,
"finish_reason": null,
"choices": [
{
"finish_reason": "stop",
"message": {
"role": "assistant",
"content": [
{
"image": "https://dashscope-result-bj.oss-cn-beijing.aliyuncs.com/xxxxxx.png?Expires=xxxxxx",
"type": "image"
}
]
}
}
],
"audio": null,
"task_id": "127ec645-118f-4884-955d-0eba8xxxxxx",
"task_status": "SUCCEEDED",
"submit_time": "2026-01-09 17:52:04.136",
"scheduled_time": "2026-01-09 17:52:04.164",
"end_time": "2026-01-09 17:52:25.408",
"finished": true
},
"usage": {
"input_tokens": 0,
"output_tokens": 0,
"characters": 0,
"size": "1280*1280",
"total_tokens": 0,
"image_count": 1
}
}
圖文混合輸出
同步調用(僅支援流式)
請求樣本
import os
import dashscope
from dashscope.aigc.image_generation import ImageGeneration
from dashscope.api_entities.dashscope_response import Message
# 以下為新加坡地區url,各地區的base_url不同
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
# 若沒有配置環境變數,請用百鍊API Key將下行替換為:api_key="sk-xxx"
# 各地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
api_key = os.getenv("DASHSCOPE_API_KEY")
def sync_call_with_stream():
print("\n========== 同步調用 - 流式圖文輸出 ==========")
image_message = Message(
role="user",
content=[
{
"text": "給我一個3張圖辣椒炒肉教程"
}
]
)
image_stream_res = ImageGeneration.call(
model="wan2.6-image",
api_key=api_key,
messages=[image_message],
stream=True, # 僅支援流式調用
negative_prompt="",
enable_interleave=True,
max_images=3,
size="1280*1280"
)
print("流式輸出結果:")
for stream_res in image_stream_res:
print(stream_res)
if __name__ == "__main__":
sync_call_with_stream()
響應樣本
url 有效期間24小時,請及時下載映像。
{"status_code": 200, "request_id": "5b98e8f3-aeff-4c20-a26c-499a7525axxx", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": [{"type": "text", "text": "辣椒"}]}}], "audio": null, "finished": false}, "usage": {"input_tokens": 28, "output_tokens": 0, "characters": 0, "total_tokens": 28, "image_count": 0, "size": "0*0"}}
{"status_code": 200, "request_id": "5b98e8f3-aeff-4c20-a26c-499a7525axxx", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": [{"type": "text", "text": "炒"}]}}], "audio": null, "finished": false}, "usage": {"input_tokens": 28, "output_tokens": 1, "characters": 0, "total_tokens": 29, "image_count": 0, "size": "0*0"}}
{"status_code": 200, "request_id": "5b98e8f3-aeff-4c20-a26c-499a7525axxx", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": [{"type": "text", "text": "肉"}]}}], "audio": null, "finished": false}, "usage": {"input_tokens": 28, "output_tokens": 2, "characters": 0, "total_tokens": 30, "image_count": 0, "size": "0*0"}}
......
{"status_code": 200, "request_id": "5b98e8f3-aeff-4c20-a26c-499a7525axxx", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "null", "message": {"role": "assistant", "content": [{"type": "text", "text": "。"}]}}], "audio": null, "finished": false}, "usage": {"input_tokens": 28, "output_tokens": 398, "characters": 0, "total_tokens": 426, "image_count": 2, "size": "1280*1280"}}
{"status_code": 200, "request_id": "5b98e8f3-aeff-4c20-a26c-499a7525axxx", "code": "", "message": "", "output": {"text": null, "finish_reason": null, "choices": [{"finish_reason": "stop", "message": {"role": "assistant", "content": [{"type": "image", "image": "https://dashscope-result-sh.oss-cn-shanghai.aliyuncs.com/xxx.png?Expires=xxx"}]}}], "audio": null, "finished": true}, "usage": {"input_tokens": 28, "output_tokens": 523, "characters": 0, "total_tokens": 551, "image_count": 3, "size": "1280*1280"}}
非同步呼叫
請求樣本
注意:非同步呼叫不需要設定stream參數
import os
import dashscope
from dashscope.aigc.image_generation import ImageGeneration
from dashscope.api_entities.dashscope_response import Message
from http import HTTPStatus
# 以下為新加坡地區url,各地區的base_url不同
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
# 若沒有配置環境變數,請用百鍊API Key將下行替換為:api_key="sk-xxx"
# 各地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
api_key = os.getenv("DASHSCOPE_API_KEY")
def main():
"""非同步呼叫 - 圖文輸出"""
print("========== wan2.6-image 非同步呼叫 - 圖文輸出 ==========")
image_message = Message(
role="user",
content=[
{
"text": "給我一個3張圖的辣椒炒肉教程"
}
]
)
# 建立非同步任務
print("---async call, creating task----")
response = ImageGeneration.async_call(
model="wan2.6-image",
api_key=api_key,
messages=[image_message],
# 非同步呼叫不需要設定stream參數
negative_prompt="",
enable_interleave=True,
max_images=3,
size="1280*1280"
)
if response.status_code == HTTPStatus.OK:
print(f"任務建立成功:")
print(response)
# 等待任務完成
print("\n---waiting for task completion----")
status = ImageGeneration.wait(task=response, api_key=api_key)
if status.output.task_status == "SUCCEEDED":
print("任務完成!")
print(f"結果:")
print(status)
else:
print(f"任務失敗,狀態: {status.output.task_status}")
else:
print(f"任務建立失敗: {response.code} - {response.message}")
if __name__ == "__main__":
try:
main()
except Exception as e:
print(f"執行出錯: {str(e)}")
import traceback
traceback.print_exc()
響應樣本
1、建立任務的響應樣本
{
"status_code": 200,
"request_id": "4fb3050f-de57-4a24-84ff-e37ee5xxxxxx",
"code": "",
"message": "",
"output": {
"text": null,
"finish_reason": null,
"choices": null,
"audio": null,
"task_id": "5c67585e-a3be-4943-b04d-c3fbb2xxxxxx",
"task_status": "PENDING"
},
"usage": {
"input_tokens": 0,
"output_tokens": 0,
"characters": 0
}
}
2、查詢任務結果的響應樣本
url 有效期間24小時,請及時下載映像。
{
"status_code": 200,
"request_id": "997a759b-fbb9-4b35-9a4d-6dab1xxxxxx",
"code": null,
"message": "",
"output": {
"text": null,
"finish_reason": null,
"choices": [
{
"finish_reason": "stop",
"message": {
"role": "assistant",
"content": [
{
"text": "辣椒炒肉是湘菜中的經典,也是許多人心中的家常美味。它以鮮辣爽口、肉質鮮嫩而聞名,製作過程簡單,卻能帶來極致的味蕾享受。今天,我們就來一起學習這道菜的製作方法。\n\n首先,準備好所有食材是成功的第一步。新鮮的豬肉、紅綠辣椒、蒜瓣和薑片是必不可少的。將豬肉切成薄片,辣椒切段,蒜和薑切片備用。",
"type": "text"
},
{
"image": "https://dashscope-result-sh.oss-cn-shanghai.aliyuncs.com/xxx.png?Expires=xxx",
"type": "image"
},
{
"text": "接下來是炒制的關鍵環節。熱鍋涼油,放入蒜片和薑片爆香,接著倒入切好的肉片,快速翻炒至肉片變色。肉片炒香後,加入切好的辣椒段,繼續翻炒,讓辣椒的香氣充分釋放出來,與肉片的鮮美完美融合。",
"type": "text"
},
{
"image": "https://dashscope-result-sh.oss-cn-shanghai.aliyuncs.com/xxx.png?Expires=xxx",
"type": "image"
},
{
"text": "最後,加入適量的生抽、老抽、蠔油和少許糖調味,快速翻炒均勻,讓每一片肉和辣椒都裹上醬汁。待湯汁收濃,即可關火出鍋。這道色香味俱全的辣椒炒肉就大功告成了!",
"type": "text"
},
{
"image": "https://dashscope-result-sh.oss-cn-shanghai.aliyuncs.com/xxx.png?Expires=xxx",
"type": "image"
}
]
}
}
],
"audio": null,
"task_id": "5c67585e-a3be-4943-b04d-c3fbb2xxxxxx",
"task_status": "SUCCEEDED",
"submit_time": "2026-01-16 17:47:39.469",
"scheduled_time": "2026-01-16 17:47:39.804",
"end_time": "2026-01-16 17:49:46.736",
"finished": true
},
"usage": {
"input_tokens": 29,
"output_tokens": 477,
"characters": 0,
"size": "1280*1280",
"total_tokens": 506,
"image_count": 3
}
}
DashScope Java SDK調用
SDK 的參數命名與HTTP介面基本一致,參數結構根據語言特性進行封裝。
由於任務耗時較長,SDK 在底層封裝了 HTTP 非同步呼叫流程,支援同步、非同步兩種調用方式。
具體耗時受限於排隊任務數和服務執行情況,請在擷取結果時耐心等待。
請確保 DashScope Java SDK 版本不低於 2.22.6,再運行以下代碼。
各地區的base_url和 API Key 不通用,以下樣本以新加坡地區為例進行調用:
新加坡地區:https://dashscope-intl.aliyuncs.com/api/v1
維吉尼亞地區:https://dashscope-us.aliyuncs.com/api/v1
北京地區:https://dashscope.aliyuncs.com/api/v1
影像編輯
同步調用
請求樣本
import com.alibaba.dashscope.aigc.imagegeneration.*;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.exception.UploadFileException;
import com.alibaba.dashscope.utils.Constants;
import com.alibaba.dashscope.utils.JsonUtils;
import java.util.Arrays;
import java.util.Collections;
/**
* wan2.6-image 影像編輯 - 同步調用樣本
*/
public class Main {
static {
// 以下為新加坡地區url,各地區的base_url不同
Constants.baseHttpApiUrl = "https://dashscope-intl.aliyuncs.com/api/v1";
}
// 若沒有配置環境變數,請用百鍊API Key將下行替換為:apiKey="sk-xxx"
// 各地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
static String apiKey = System.getenv("DASHSCOPE_API_KEY");
public static void basicCall() throws ApiException, NoApiKeyException, UploadFileException {
// 構建多圖輸入訊息
ImageGenerationMessage message = ImageGenerationMessage.builder()
.role("user")
.content(Arrays.asList(
// 支援多圖輸入,可以提供多張參考圖片
Collections.singletonMap("text", "參考圖1的風格和圖2的背景,產生番茄炒蛋"),
Collections.singletonMap("image", "https://cdn.wanx.aliyuncs.com/tmp/pressure/umbrella1.png"),
Collections.singletonMap("image", "https://img.alicdn.com/imgextra/i3/O1CN01SfG4J41UYn9WNt4X1_!!6000000002530-49-tps-1696-960.webp")
)).build();
// 影像編輯使用普通同步調用,不需要設定stream和enable_interleave
ImageGenerationParam param = ImageGenerationParam.builder()
.apiKey(apiKey)
.model("wan2.6-image")
.messages(Collections.singletonList(message))
.n(1)
.size("1280*1280")
.negativePrompt("")
.promptExtend(true)
.build();
ImageGeneration imageGeneration = new ImageGeneration();
ImageGenerationResult result = null;
try {
System.out.println("---sync call for image editing, please wait a moment----");
result = imageGeneration.call(param);
} catch (ApiException | NoApiKeyException | UploadFileException e) {
throw new RuntimeException(e.getMessage());
}
System.out.println(JsonUtils.toJson(result));
}
public static void main(String[] args) {
try {
basicCall();
} catch (ApiException | NoApiKeyException | UploadFileException e) {
System.out.println(e.getMessage());
}
}
}
響應樣本
url 有效期間24小時,請及時下載映像。
{
"requestId": "b148327e-830f-414c-a8df-724dec28exxx",
"usage": {
"input_tokens": 0,
"output_tokens": 0,
"total_tokens": 0,
"image_count": 1,
"size": "1280*1280"
},
"output": {
"choices": [
{
"finish_reason": "stop",
"message": {
"role": "assistant",
"content": [
{
"image": "https://dashscope-result-sh.oss-cn-shanghai.aliyuncs.com/xxx.png?Expires=xxx",
"type": "image"
}
]
}
}
],
"finished": true
},
"status_code": 200,
"code": "",
"message": ""
}
非同步呼叫
請求樣本
import com.alibaba.dashscope.aigc.imagegeneration.*;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.exception.UploadFileException;
import com.alibaba.dashscope.utils.Constants;
import com.alibaba.dashscope.utils.JsonUtils;
import java.util.Arrays;
import java.util.Collections;
/**
* wan2.6-image 影像編輯 - 非同步呼叫樣本
*/
public class Main {
static {
// 以下為新加坡地區url,各地區的base_url不同
Constants.baseHttpApiUrl = "https://dashscope-intl.aliyuncs.com/api/v1";
}
// 若沒有配置環境變數,請用百鍊API Key將下行替換為:apiKey="sk-xxx"
// 各地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
static String apiKey = System.getenv("DASHSCOPE_API_KEY");
public static void asyncCall() throws ApiException, NoApiKeyException, UploadFileException {
// 構建多圖輸入訊息
ImageGenerationMessage message = ImageGenerationMessage.builder()
.role("user")
.content(Arrays.asList(
// 支援多圖輸入,可以提供多張參考圖片
Collections.singletonMap("text", "參考圖1的風格和圖2的背景,產生番茄炒蛋"),
Collections.singletonMap("image", "https://cdn.wanx.aliyuncs.com/tmp/pressure/umbrella1.png"),
Collections.singletonMap("image", "https://img.alicdn.com/imgextra/i3/O1CN01SfG4J41UYn9WNt4X1_!!6000000002530-49-tps-1696-960.webp")
)).build();
ImageGenerationParam param = ImageGenerationParam.builder()
.apiKey(apiKey)
.model("wan2.6-image")
.n(1)
.size("1280*1280")
.negativePrompt("")
.promptExtend(true)
.messages(Arrays.asList(message))
.build();
ImageGeneration imageGeneration = new ImageGeneration();
ImageGenerationResult result = null;
try {
System.out.println("---async call for image editing, creating task----");
result = imageGeneration.asyncCall(param);
} catch (ApiException | NoApiKeyException | UploadFileException e) {
throw new RuntimeException(e.getMessage());
}
System.out.println("任務建立結果:");
System.out.println(JsonUtils.toJson(result));
String taskId = result.getOutput().getTaskId();
// 等待任務完成
waitTask(taskId);
}
public static void waitTask(String taskId) throws ApiException, NoApiKeyException {
ImageGeneration imageGeneration = new ImageGeneration();
System.out.println("\n---waiting for task completion----");
ImageGenerationResult result = imageGeneration.wait(taskId, apiKey);
System.out.println("任務完成結果:");
System.out.println(JsonUtils.toJson(result));
}
public static void main(String[] args) {
try {
asyncCall();
} catch (ApiException | NoApiKeyException | UploadFileException e) {
System.out.println(e.getMessage());
}
}
}
響應樣本
1、建立任務的響應樣本
{
"status_code": 200,
"request_id": "9cd85950-2e26-4b2c-b562-1694cf928xxx",
"code": "",
"message": "",
"output": {
"task_id": "4c861fbe-af89-4a2f-8fc5-4bb15c313xxx",
"task_status": "PENDING"
},
"usage": null
}
2、查詢任務結果的響應樣本
url 有效期間24小時,請及時下載映像。
{
"status_code": 200,
"request_id": "cbdf1424-306e-4a52-82f3-8bf5d8a99xxx",
"code": "",
"message": "",
"output": {
"choices": [
{
"finish_reason": "stop",
"message": {
"role": "assistant",
"content": [
{
"image": "https://dashscope-result-sh.oss-cn-shanghai.aliyuncs.com/xxx.png?Expires=xxx",
"type": "image"
}
]
}
}
],
"task_id": "4c861fbe-af89-4a2f-8fc5-4bb15c3139ba",
"task_status": "SUCCEEDED",
"submit_time": "2026-01-16 16:36:06.556",
"scheduled_time": "2026-01-16 16:36:06.591",
"end_time": "2026-01-16 16:36:25.190",
"finished": true
},
"usage": {
"input_tokens": 0,
"output_tokens": 0,
"size": "1280*1280",
"total_tokens": 0,
"image_count": 1
}
}
圖文混合輸出
同步調用(僅支援流式)
請求樣本
import com.alibaba.dashscope.aigc.imagegeneration.*;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.exception.UploadFileException;
import com.alibaba.dashscope.utils.Constants;
import com.alibaba.dashscope.utils.JsonUtils;
import io.reactivex.Flowable;
import java.util.Collections;
/**
* wan2.6-image 圖文輸出 - 流式調用樣本
*/
public class Main {
static {
// 以下為新加坡地區url,各地區的base_url不同
Constants.baseHttpApiUrl = "https://dashscope-intl.aliyuncs.com/api/v1";
}
// 若沒有配置環境變數,請用百鍊API Key將下行替換為:apiKey="sk-xxx"
// 各地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
static String apiKey = System.getenv("DASHSCOPE_API_KEY");
public static void streamCall() throws ApiException, NoApiKeyException, UploadFileException {
ImageGenerationMessage message = ImageGenerationMessage.builder()
.role("user")
.content(Collections.singletonList(
Collections.singletonMap("text", "給我一個3張圖辣椒炒肉教程")
)).build();
// 圖文輸出必須使用流式調用
ImageGenerationParam param = ImageGenerationParam.builder()
.apiKey(apiKey)
.model("wan2.6-image")
.messages(Collections.singletonList(message))
.stream(true) // 必須開啟流式輸出
.enableInterleave(true)
.size("1280*1280")
.negativePrompt("")
.maxImages(3)
.build();
ImageGeneration imageGeneration = new ImageGeneration();
try {
System.out.println("---stream call for image interleave----");
Flowable<ImageGenerationResult> resultFlowable = imageGeneration.streamCall(param);
resultFlowable.blockingForEach(result -> {
System.out.println(JsonUtils.toJson(result));
});
} catch (ApiException | NoApiKeyException | UploadFileException e) {
throw new RuntimeException(e.getMessage());
}
}
public static void main(String[] args) {
try {
streamCall();
} catch (ApiException | NoApiKeyException | UploadFileException e) {
System.out.println(e.getMessage());
}
System.exit(0);
}
}
響應樣本
url 有效期間24小時,請及時下載映像。
{"requestId":"12c7432c-8028-4289-a97c-4e22df98bxxx","usage":{"input_tokens":28,"output_tokens":0,"total_tokens":28,"image_count":0,"size":"0*0"},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":[{"type":"text","text":"辣椒"}]}}],"finished":false},"status_code":200,"code":"","message":""}
{"requestId":"12c7432c-8028-4289-a97c-4e22df98bxxx","usage":{"input_tokens":28,"output_tokens":1,"total_tokens":29,"image_count":0,"size":"0*0"},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":[{"type":"text","text":"炒"}]}}],"finished":false},"status_code":200,"code":"","message":""}
{"requestId":"12c7432c-8028-4289-a97c-4e22df98bxxx","usage":{"input_tokens":28,"output_tokens":2,"total_tokens":30,"image_count":0,"size":"0*0"},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":[{"type":"text","text":"肉"}]}}],"finished":false},"status_code":200,"code":"","message":""}
......
{"requestId":"12c7432c-8028-4289-a97c-4e22df98bxxx","usage":{"input_tokens":28,"output_tokens":73,"total_tokens":101,"image_count":0,"size":"0*0"},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":[{"type":"text","text":"。"}]}}],"finished":false},"status_code":200,"code":"","message":""}
{"requestId":"12c7432c-8028-4289-a97c-4e22df98bxxx","usage":{"input_tokens":28,"output_tokens":198,"total_tokens":226,"image_count":1,"size":"1280*1280"},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":[{"type":"image","image":"https://dashscope-result-sh.oss-cn-shanghai.aliyuncs.com/xxx.png?Expires=xxx"}]}}],"finished":false},"status_code":200,"code":"","message":""}
{"requestId":"12c7432c-8028-4289-a97c-4e22df98bxxx","usage":{"input_tokens":28,"output_tokens":199,"total_tokens":227,"image_count":1,"size":"1280*1280"},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":[{"type":"text","text":"接下來"}]}}],"finished":false},"status_code":200,"code":"","message":""}
......
{"requestId":"12c7432c-8028-4289-a97c-4e22df98bxxx","usage":{"input_tokens":28,"output_tokens":245,"total_tokens":273,"image_count":1,"size":"1280*1280"},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":[{"type":"text","text":"。"}]}}],"finished":false},"status_code":200,"code":"","message":""}
{"requestId":"12c7432c-8028-4289-a97c-4e22df98bxxx","usage":{"input_tokens":28,"output_tokens":368,"total_tokens":396,"image_count":2,"size":"1280*1280"},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":[{"type":"image","image":"https://dashscope-result-sh.oss-cn-shanghai.aliyuncs.com/xxx.png?Expires=xxx"}]}}],"finished":false},"status_code":200,"code":"","message":""}
{"requestId":"12c7432c-8028-4289-a97c-4e22df98bxxx","usage":{"input_tokens":28,"output_tokens":369,"total_tokens":397,"image_count":2,"size":"1280*1280"},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":[{"type":"text","text":"最後"}]}}],"finished":false},"status_code":200,"code":"","message":""}
......
{"requestId":"12c7432c-8028-4289-a97c-4e22df98bxxx","usage":{"input_tokens":28,"output_tokens":416,"total_tokens":444,"image_count":2,"size":"1280*1280"},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":[{"type":"text","text":"鍋"}]}}],"finished":false},"status_code":200,"code":"","message":""}
{"requestId":"12c7432c-8028-4289-a97c-4e22df98bxxx","usage":{"input_tokens":28,"output_tokens":417,"total_tokens":445,"image_count":2,"size":"1280*1280"},"output":{"choices":[{"finish_reason":"null","message":{"role":"assistant","content":[{"type":"text","text":"。"}]}}],"finished":false},"status_code":200,"code":"","message":""}
{"requestId":"12c7432c-8028-4289-a97c-4e22df98bxxx","usage":{"input_tokens":28,"output_tokens":541,"total_tokens":569,"image_count":3,"size":"1280*1280"},"output":{"choices":[{"finish_reason":"stop","message":{"role":"assistant","content":[{"type":"image","image":"https://dashscope-result-sh.oss-cn-shanghai.aliyuncs.com/xxx.png?Expires=xxx"}]}}],"finished":true},"status_code":200,"code":"","message":""}
非同步呼叫
請求樣本
注意:非同步呼叫不需要設定stream參數
import com.alibaba.dashscope.aigc.imagegeneration.*;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.exception.UploadFileException;
import com.alibaba.dashscope.utils.Constants;
import com.alibaba.dashscope.utils.JsonUtils;
import java.util.Collections;
/**
* wan2.6-image 圖文輸出 - 非同步呼叫樣本
*/
public class Main {
static {
// 以下為新加坡地區url,各地區的base_url不同
Constants.baseHttpApiUrl = "https://dashscope-intl.aliyuncs.com/api/v1";
}
// 若沒有配置環境變數,請用百鍊API Key將下行替換為:apiKey="sk-xxx"
// 各地區的API Key不同。擷取API Key:https://www.alibabacloud.com/help/zh/model-studio/get-api-key
static String apiKey = System.getenv("DASHSCOPE_API_KEY");
public static void asyncCall() throws ApiException, NoApiKeyException, UploadFileException {
ImageGenerationMessage message = ImageGenerationMessage.builder()
.role("user")
.content(Collections.singletonList(
Collections.singletonMap("text", "給我一個3張圖的辣椒炒肉教程")
)).build();
ImageGenerationParam param = ImageGenerationParam.builder()
.apiKey(apiKey)
.model("wan2.6-image")
.size("1280*1280")
.enableInterleave(true)
.maxImages(3)
.negativePrompt("")
.messages(Collections.singletonList(message))
.build();
ImageGeneration imageGeneration = new ImageGeneration();
ImageGenerationResult result = null;
try {
System.out.println("---async call for image interleave, creating task----");
result = imageGeneration.asyncCall(param);
} catch (ApiException | NoApiKeyException | UploadFileException e) {
throw new RuntimeException(e.getMessage());
}
System.out.println("任務建立結果:");
System.out.println(JsonUtils.toJson(result));
String taskId = result.getOutput().getTaskId();
// 等待任務完成
waitTask(taskId);
}
public static void waitTask(String taskId) throws ApiException, NoApiKeyException {
ImageGeneration imageGeneration = new ImageGeneration();
System.out.println("\n---waiting for task completion----");
ImageGenerationResult result = imageGeneration.wait(taskId, apiKey);
System.out.println("任務完成結果:");
System.out.println(JsonUtils.toJson(result));
}
public static void main(String[] args) {
try {
asyncCall();
} catch (ApiException | NoApiKeyException | UploadFileException e) {
System.out.println(e.getMessage());
}
}
}
響應樣本
1、建立任務的響應樣本
{
"requestId": "7d6c5760-334b-48c4-9b1e-08ee9c7fexxx",
"output": {
"task_id": "1bb9d9fa-bf1a-43dc-b5fe-366c1dc70xxx",
"task_status": "PENDING"
},
"status_code": 200,
"code": "",
"message": ""
}
2、查詢任務結果的響應樣本
url 有效期間24小時,請及時下載映像。
{
"requestId": "6ed62b00-2225-4fc3-8ee3-2aed0b484xxx",
"usage": {
"input_tokens": 29,
"output_tokens": 471,
"total_tokens": 500,
"image_count": 3,
"size": "1280*1280"
},
"output": {
"choices": [
{
"finish_reason": "stop",
"message": {
"role": "assistant",
"content": [
{
"text": "辣椒炒肉是湘菜中的經典,也是許多人心中的家常美味。它以鮮辣爽口、肉質鮮嫩而聞名,製作過程簡單,卻能帶來極致的味蕾享受。今天,我們就來一起學習這道菜的製作方法。\n\n首先,準備好所有食材是成功的第一步。新鮮的豬肉、紅綠辣椒、蒜瓣和薑片是必不可少的。將豬肉切成薄片,辣椒切段,蒜和薑切片備用。",
"type": "text"
},
{
"image": "https://dashscope-result-sh.oss-cn-shanghai.aliyuncs.com/xxx.png?Expires=xxx",
"type": "image"
},
{
"text": "接下來是醃制豬肉。將切好的豬肉片放入碗中,加入少許生抽、料酒、澱粉和食用油,用手抓勻,醃制10-15分鐘。這樣處理過的肉片會更加滑嫩入味。",
"type": "text"
},
{
"image": "https://dashscope-result-sh.oss-cn-shanghai.aliyuncs.com/xxx.png?Expires=xxx",
"type": "image"
},
{
"text": "最後,熱鍋涼油,放入醃制好的肉片快速滑炒至變色,盛出備用。鍋中留底油,放入蒜片和薑片爆香,再加入辣椒段翻炒出香味。隨後倒入肉片,加入生抽、老抽、少許糖和蠔油,快速翻炒均勻,撒上蔥花即可出鍋。",
"type": "text"
},
{
"image": "https://dashscope-result-sh.oss-cn-shanghai.aliyuncs.com/xxx.png?Expires=xxx",
"type": "image"
}
]
}
}
],
"task_id": "1bb9d9fa-bf1a-43dc-b5fe-366c1dc70836",
"task_status": "SUCCEEDED",
"finished": true,
"submit_time": "2026-01-16 18:26:32.082",
"scheduled_time": "2026-01-16 18:26:32.133",
"end_time": "2026-01-16 18:28:41.748"
},
"status_code": 200,
"code": "",
"message": ""
}
使用限制
資料時效:任務
task_id和 映像url均只保留 24 小時,到期後將無法查詢或下載。內容審核:輸入的
prompt和輸出的映像均會經過Alibaba Content Security Service審核,包含違規內容的請求將報錯“IPInfringementSuspect”或“DataInspectionFailed”,具體參見錯誤資訊。網路訪問配置:映像連結儲存於阿里雲 OSS,如果業務系統因安全性原則無法訪問外部OSS連結,請將以下 OSS 網域名稱加入網路訪問白名單。
# OSS網域名稱列表 dashscope-result-bj.oss-cn-beijing.aliyuncs.com dashscope-result-hz.oss-cn-hangzhou.aliyuncs.com dashscope-result-sh.oss-cn-shanghai.aliyuncs.com dashscope-result-wlcb.oss-cn-wulanchabu.aliyuncs.com dashscope-result-zjk.oss-cn-zhangjiakou.aliyuncs.com dashscope-result-sz.oss-cn-shenzhen.aliyuncs.com dashscope-result-hy.oss-cn-heyuan.aliyuncs.com dashscope-result-cd.oss-cn-chengdu.aliyuncs.com dashscope-result-gz.oss-cn-guangzhou.aliyuncs.com dashscope-result-wlcb-acdr-1.oss-cn-wulanchabu-acdr-1.aliyuncs.com
計費與限流
錯誤碼
如果模型調用失敗並返回報錯資訊,請參見錯誤資訊進行解決。
常見問題
Q: 如何查看模型的推理費用和調用量?
A: 詳情請參見賬單查詢與成本管理。
Q:為什麼程式碼範例無法調用?
A:請升級您的SDK版本到最新版,更新請參考安裝SDK。