图像与视频¶
client.images()(wanx 系列)与 client.videos()(wan 系列)是异步任务 API:生成请求提交
任务,结果按 task id 轮询。两个资源都提供一步式 generate(提交 + 等待)与手动 jobs() 流程。
计费
生成任务会产生费用。以下代码改编自可运行的 ImagesGenerate 与 VideosGenerate 示例。
完整可运行示例:ImagesGenerate.java(自动同步)
// Copyright (c) Alibaba, Inc. and its affiliates.
package com.alibaba.dashscope.examples.atomic.images;
import com.alibaba.dashscope.DashScopeClient;
import com.alibaba.dashscope.RequestOptions;
import com.alibaba.dashscope.examples.ExampleSupport;
import com.alibaba.dashscope.resources.jobs.Job;
import com.alibaba.dashscope.types.common.Page;
import com.alibaba.dashscope.types.images.ImageGenerateParams;
/**
* Image generation (wanx series): one-call generate (submit + wait), then the manual async-task
* submit/wait/fetch flow, then list tasks. Translation of the Python blueprint example
* 01_atomic_capabilities/image_video_synthesis/images.py.
*/
public final class ImagesGenerate {
public static void main(String[] args) throws Exception {
String model = System.getenv().getOrDefault("DASHSCOPE_MODEL", "wanx2.1-t2i-turbo");
String prompt = "一间有着精致窗户的花店,漂亮的木质门,摆放着花朵";
try (DashScopeClient client = ExampleSupport.client()) {
// One-call generate: submits the task and waits for the result
System.out.println("----- generate (submit + wait) -----");
Job job =
client
.images()
.generate(
ImageGenerateParams.builder()
.model(model)
.prompt(prompt)
.n(1)
.size("1024*1024")
.build());
if (job.isSucceeded()) {
for (String url : job.getImageUrls()) {
System.out.println("image url: " + url);
}
} else {
System.out.println("job failed: " + job.getOutput());
}
// Manual async-task flow: submit first, then wait/fetch by task_id
System.out.println("----- jobs.create + wait + fetch -----");
Job pending =
client
.images()
.jobs()
.create(
ImageGenerateParams.builder()
.model(model)
.prompt(prompt)
.n(1)
.size("1024*1024")
.build());
if (pending.getOutput() == null) {
throw new IllegalStateException("pending.getOutput() is null");
}
String taskId = pending.getTaskId();
System.out.println("submitted task_id: " + taskId);
Job finished = client.images().jobs().wait(taskId, RequestOptions.NONE);
System.out.println("wait finished, succeeded: " + finished.isSucceeded());
Job snapshot = client.images().jobs().fetch(taskId, RequestOptions.NONE);
System.out.println("fetch output: " + snapshot.getOutput());
// List historical tasks
System.out.println("----- jobs.list -----");
Page<Job> result = client.images().jobs().list(null, RequestOptions.NONE);
System.out.println("total tasks: " + result.getTotalCount());
result.getItems().stream()
.limit(3)
.forEach(
task ->
System.out.println(
"task_id=" + task.getTaskId() + ", status=" + task.getTaskStatus()));
}
}
}
完整可运行示例:VideosGenerate.java(自动同步)
// Copyright (c) Alibaba, Inc. and its affiliates.
package com.alibaba.dashscope.examples.atomic.videos;
import com.alibaba.dashscope.DashScopeClient;
import com.alibaba.dashscope.RequestOptions;
import com.alibaba.dashscope.examples.ExampleSupport;
import com.alibaba.dashscope.resources.jobs.Job;
import com.alibaba.dashscope.types.common.Page;
import com.alibaba.dashscope.types.videos.VideoGenerateParams;
/**
* Video generation (wan series): one-call generate (submit + wait), then the manual async-task
* submit/wait/fetch flow, then list tasks. Translation of the Python blueprint example
* 01_atomic_capabilities/image_video_synthesis/videos.py.
*
* <p>Note: video-generation tasks take a while; please be patient.
*/
public final class VideosGenerate {
public static void main(String[] args) throws Exception {
String model = System.getenv().getOrDefault("DASHSCOPE_MODEL", "wan2.7-t2v");
String prompt = "一只小猫在月光下奔跑";
try (DashScopeClient client = ExampleSupport.client()) {
// One-call generate: submits the task and waits for the result
System.out.println("----- generate (submit + wait) -----");
Job job =
client
.videos()
.generate(VideoGenerateParams.builder().model(model).prompt(prompt).build());
if (job.isSucceeded()) {
System.out.println("video url: " + job.getVideoUrl());
} else {
System.out.println("job failed: " + job.getOutput());
}
// Manual async-task flow: submit first, then wait/fetch by task_id
System.out.println("----- jobs.create + wait + fetch -----");
Job pending =
client
.videos()
.jobs()
.create(VideoGenerateParams.builder().model(model).prompt(prompt).build());
if (pending.getOutput() == null) {
throw new IllegalStateException("pending.getOutput() is null");
}
String taskId = pending.getTaskId();
System.out.println("submitted task_id: " + taskId);
Job finished = client.videos().jobs().wait(taskId, RequestOptions.NONE);
System.out.println("wait finished, succeeded: " + finished.isSucceeded());
System.out.println("video url: " + finished.getVideoUrl());
Job snapshot = client.videos().jobs().fetch(taskId, RequestOptions.NONE);
System.out.println("fetch output: " + snapshot.getOutput());
// List historical tasks
System.out.println("----- jobs.list -----");
Page<Job> result = client.videos().jobs().list(null, RequestOptions.NONE);
System.out.println("total tasks: " + result.getTotalCount());
result.getItems().stream()
.limit(3)
.forEach(
task ->
System.out.println(
"task_id=" + task.getTaskId() + ", status=" + task.getTaskStatus()));
}
}
}
图像生成¶
import com.alibaba.dashscope.resources.jobs.Job;
import com.alibaba.dashscope.types.images.ImageGenerateParams;
// 一步式 generate:提交任务并等待结果。
Job job = client.images().generate(
ImageGenerateParams.builder()
.model("wanx2.1-t2i-turbo")
.prompt("一间有着精致窗户的花店,漂亮的木质门,摆放着花朵")
.n(1)
.size("1024*1024")
.build());
if (job.isSucceeded()) {
for (String url : job.getImageUrls()) {
System.out.println("image url: " + url);
}
} else {
System.out.println("job failed: " + job.getOutput());
}
手动异步任务流程¶
先提交,再按 task_id 等待或查询 —— 适合持久化任务 id、由另一个进程轮询的场景:
import com.alibaba.dashscope.RequestOptions;
import com.alibaba.dashscope.types.common.Page;
Job pending = client.images().jobs().create(params);
String taskId = pending.getTaskId();
Job finished = client.images().jobs().wait(taskId, RequestOptions.NONE); // 阻塞直到完成
Job snapshot = client.images().jobs().fetch(taskId, RequestOptions.NONE); // 单次状态查询
Page<Job> history = client.images().jobs().list(null, RequestOptions.NONE); // 历史任务
视频生成¶
形状相同,只是结果访问器不同。视频任务耗时较长 —— wait 请耐心等待:
import com.alibaba.dashscope.types.videos.VideoGenerateParams;
Job job = client.videos().generate(
VideoGenerateParams.builder()
.model("wan2.7-t2v")
.prompt("一只小猫在月光下奔跑")
.build());
if (job.isSucceeded()) {
System.out.println("video url: " + job.getVideoUrl());
}
client.videos().jobs().create / wait / fetch / list 与图像任务流程完全一致。
Job 语义¶
Job.isSucceeded()/isFinished()/getTaskStatus()报告任务状态;失败时getOutput()携带服务端详情。generate= 一次调用完成jobs().create+jobs().wait。- 2.x 的
ImageSynthesis/VideoSynthesis入口映射到这里 —— 见从 2.x 迁移。