跳到主要内容

DashScope Image Generation

Spring AI Alibaba 支持来自阿里云 DashScope 的图像生成模型,包括通义万相(Wanx)和通义千问图像(Qwen Image)系列模型。

Prerequisites

您需要使用阿里云 DashScope 创建 API Key 才能访问 DashScope 图像生成模型。

阿里云 DashScope 控制台 创建账户,并在 API Keys 页面 生成 API Key。

Spring AI Alibaba 项目定义了一个名为 spring.ai.dashscope.api-key 的配置属性,您应将其设置为从 DashScope 控制台获得的 API Key 值。

您可以在 application.properties 文件中设置此配置属性:

spring.ai.dashscope.api-key=<your-dashscope-api-key>

为了在处理敏感信息(如 API keys)时增强安全性,您可以使用 Spring Expression Language (SpEL) 来引用自定义环境变量:

# In application.yml
spring:
ai:
dashscope:
api-key: ${AI_DASHSCOPE_API_KEY}
# In your environment or .env file
export AI_DASHSCOPE_API_KEY=<your-dashscope-api-key>

您也可以在应用程序代码中以编程方式设置此配置:

// Retrieve API key from a secure source or environment variable
String apiKey = System.getenv("AI_DASHSCOPE_API_KEY");

Auto-configuration

Spring AI Alibaba 为 DashScope Image Generation Client 提供 Spring Boot 自动配置。 要启用它,请将以下依赖项添加到项目的 Maven pom.xml 文件中:

<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-starter-dashscope</artifactId>
</dependency>

或添加到 Gradle build.gradle 构建文件中。

dependencies {
implementation 'com.alibaba.cloud.ai:spring-ai-alibaba-starter-dashscope'
}

TIP: 请参考 Dependency Management 部分,将 Spring AI Alibaba BOM 添加到您的构建文件中。

Image Generation Properties

Connection Properties

前缀 spring.ai.dashscope 用作允许您连接到 DashScope 的属性前缀。

PropertyDescriptionDefault
spring.ai.dashscope.base-url连接的 URLhttps://dashscope.aliyuncs.com
spring.ai.dashscope.api-keyAPI Key-
spring.ai.dashscope.work-space-id可选,您可以指定用于 API 请求的工作空间 ID-

TIP: 对于属于多个工作空间的用户,您可以可选地指定用于 API 请求的工作空间 ID。 这些 API 请求的使用量将计入指定工作空间的使用量。

Retry Properties

前缀 spring.ai.retry 用作允许您配置 DashScope Image 客户端的重试机制的属性前缀。

PropertyDescriptionDefault
spring.ai.retry.max-attempts最大重试尝试次数10
spring.ai.retry.backoff.initial-interval指数退避策略的初始睡眠持续时间2 sec.
spring.ai.retry.backoff.multiplier退避间隔乘数5
spring.ai.retry.backoff.max-interval最大退避持续时间3 min.
spring.ai.retry.on-client-errors如果为 false,抛出 NonTransientAiException,并且不对 4xx 客户端错误代码尝试重试false
spring.ai.retry.exclude-on-http-codes不应触发重试的 HTTP 状态代码列表(例如,抛出 NonTransientAiException)empty
spring.ai.retry.on-http-codes应触发重试的 HTTP 状态代码列表(例如,抛出 TransientAiException)empty

Configuration Properties

NOTE

图像自动配置的启用和禁用现在通过前缀为 spring.ai.model.image 的顶级属性进行配置。

要启用,spring.ai.model.image=dashscope

要禁用,spring.ai.model.image=none(或任何与 dashscope 不匹配的值)

此更改是为了允许配置多个模型。

前缀 spring.ai.dashscope.image 是允许您配置 DashScope 的 ImageModel 实现的属性前缀。

PropertyDescriptionDefault
spring.ai.model.image启用 DashScope 图像模型dashscope
spring.ai.dashscope.image.base-url可选覆盖 spring.ai.dashscope.base-url 以提供图像特定的 URL-
spring.ai.dashscope.image.api-key可选覆盖 spring.ai.dashscope.api-key 以提供图像特定的 API Key-
spring.ai.dashscope.image.work-space-id可选,您可以指定用于 API 请求的工作空间 ID-
spring.ai.dashscope.image.options.model用于图像生成的模型。可用模型:qwen-image(默认)、qwen-image-plusqwen-image-editqwen-mt-imagewan2.2-t2i-pluswan2.2-t2i-flashwanx2.1-imageeditwan2.2-t2i-previewqwen-image
spring.ai.dashscope.image.options.n要生成的图像数量。必须在 1 到 4 之间1
spring.ai.dashscope.image.options.width生成的图像宽度。必须是 720、1024 或 1280 之一-
spring.ai.dashscope.image.options.height生成的图像高度。必须是 720、1024 或 1280 之一-
spring.ai.dashscope.image.options.size生成的图像大小。必须是 1024*1024720*12801280*720 之一-
spring.ai.dashscope.image.options.style生成的图像样式。必须是 photographyportrait3d cartoonanimeoil paintingwatercolorsketchchinese paintingflat illustrationauto 之一-
spring.ai.dashscope.image.options.seed用于生成的随机种子。必须在 0 到 4294967290 之间-
spring.ai.dashscope.image.options.ref-img参考图像 URL。支持 jpg、png、tiff、webp 格式-
spring.ai.dashscope.image.options.ref-strength参考强度。必须在 0.0 到 1.0 之间-
spring.ai.dashscope.image.options.ref-mode参考模式。必须是 repaintrefonly 之一-
spring.ai.dashscope.image.options.negative-prompt负面提示词-
spring.ai.dashscope.image.options.prompt-extend是否启用提示词扩展-
spring.ai.dashscope.image.options.watermark是否添加水印-
spring.ai.dashscope.image.options.function功能类型-
spring.ai.dashscope.image.options.base-image-url基础图像 URL(用于图像编辑)-
spring.ai.dashscope.image.options.mask-image-url遮罩图像 URL(用于图像编辑)-
spring.ai.dashscope.image.options.sketch-image-url草图图像 URL(用于图像生成)-
spring.ai.dashscope.image.options.sketch-weight草图权重-
spring.ai.dashscope.image.options.sketch-extraction是否启用草图提取-
spring.ai.dashscope.image.options.sketch-color草图颜色(二维数组)-
spring.ai.dashscope.image.options.mask-color遮罩颜色(二维数组)-
spring.ai.dashscope.image.options.response-format返回生成的图像的格式。必须是 urlb64_json 之一url

NOTE: 您可以覆盖通用的 spring.ai.dashscope.base-urlspring.ai.dashscope.api-keyspring.ai.dashscope.work-space-id 属性。 如果设置了 spring.ai.dashscope.image.base-urlspring.ai.dashscope.image.api-keyspring.ai.dashscope.image.work-space-id 属性,它们优先于通用属性。 如果您想为不同的模型和不同的模型端点使用不同的 DashScope 账户,这很有用。

TIP: 所有以 spring.ai.dashscope.image.options 为前缀的属性都可以在运行时覆盖。

Runtime Options

DashScopeImageOptions 类提供在进行图像生成请求时使用的选项。 在启动时,使用 spring.ai.dashscope.image 指定的选项,但您可以在运行时覆盖这些选项。

DashScopeImageOptions 类实现 ImageOptions 接口,提供可移植和 DashScope 特定的配置选项。

例如,要覆盖 DashScope 特定选项(如样式和要创建的图像数量),请使用以下代码示例:

DashScopeImageOptions imageOptions = DashScopeImageOptions.builder()
.model(DashScopeModel.ImageModel.QWEN_IMAGE.getValue())
.n(2)
.width(1024)
.height(1024)
.style("photography")
.seed(12345)
.build();

ImagePrompt imagePrompt = new ImagePrompt("A beautiful sunset over the ocean", imageOptions);
ImageResponse response = dashScopeImageModel.call(imagePrompt);

Manual Configuration

spring-ai-alibaba-dashscope 依赖项添加到项目的 Maven pom.xml 文件中:

<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-dashscope</artifactId>
</dependency>

或添加到 Gradle build.gradle 构建文件中。

dependencies {
implementation 'com.alibaba.cloud.ai:spring-ai-alibaba-dashscope'
}

TIP: 请参考 Dependency Management 部分,将 Spring AI Alibaba BOM 添加到您的构建文件中。

接下来,创建一个 DashScopeImageModel

var dashScopeImageApi = DashScopeImageApi.builder()
.apiKey(System.getenv("AI_DASHSCOPE_API_KEY"))
.build();

var dashScopeImageModel = new DashScopeImageModel(dashScopeImageApi);

var imageOptions = DashScopeImageOptions.builder()
.model(DashScopeModel.ImageModel.QWEN_IMAGE.getValue())
.n(1)
.width(1024)
.height(1024)
.style("photography")
.build();

var imagePrompt = new ImagePrompt("A light cream colored mini golden doodle", imageOptions);
ImageResponse response = dashScopeImageModel.call(imagePrompt);

List<ImageGeneration> images = response.getResults();

Supported Models

DashScope 支持多种图像生成模型:

Qwen Image 系列

  • qwen-image - 通义千问图像生成模型(默认)
  • qwen-image-plus - 增强版通义千问图像生成模型
  • qwen-image-edit - 通义千问图像编辑模型
  • qwen-mt-image - 通义千问多语言图像生成模型

Wanx 系列

  • wan2.2-t2i-plus - 通义万相文本到图像增强版
  • wan2.2-t2i-flash - 通义万相文本到图像快速版
  • wanx2.1-imageedit - 通义万相图像编辑模型
  • wan2.2-t2i-preview - 通义万相文本到图像预览版

更多模型信息请参考 DashScope 模型列表

Advanced Features

Image-to-Image Generation

使用参考图像进行图像生成,可以基于现有图像生成新图像:

DashScopeImageOptions options = DashScopeImageOptions.builder()
.model(DashScopeModel.ImageModel.QWEN_IMAGE.getValue())
.refImg("https://example.com/reference-image.jpg")
.refStrength(0.7f) // 参考强度 0.0-1.0
.refMode("repaint") // 或 "refonly"
.build();

ImagePrompt prompt = new ImagePrompt("Generate a similar image with different colors", options);
ImageResponse response = dashScopeImageModel.call(prompt);

Image Editing

使用基础图像和遮罩图像进行图像编辑:

DashScopeImageOptions options = DashScopeImageOptions.builder()
.model(DashScopeModel.ImageModel.QWEN_IMAGE_EDIT.getValue())
.baseImageUrl("https://example.com/base-image.jpg")
.maskImageUrl("https://example.com/mask-image.png")
.build();

ImagePrompt prompt = new ImagePrompt("Replace the background with a beach scene", options);
ImageResponse response = dashScopeImageModel.call(prompt);

Sketch-to-Image Generation

使用草图图像生成最终图像:

DashScopeImageOptions options = DashScopeImageOptions.builder()
.model(DashScopeModel.ImageModel.QWEN_IMAGE.getValue())
.sketchImageUrl("https://example.com/sketch.jpg")
.sketchWeight(80) // 草图权重
.sketchExtraction(true) // 启用草图提取
.build();

ImagePrompt prompt = new ImagePrompt("A detailed illustration based on the sketch", options);
ImageResponse response = dashScopeImageModel.call(prompt);

Style Control

通过样式参数控制生成图像的风格:

DashScopeImageOptions options = DashScopeImageOptions.builder()
.model(DashScopeModel.ImageModel.QWEN_IMAGE.getValue())
.style("anime") // 可选:photography, portrait, 3d cartoon, anime, oil painting, watercolor, sketch, chinese painting, flat illustration, auto
.build();

ImagePrompt prompt = new ImagePrompt("A beautiful landscape", options);
ImageResponse response = dashScopeImageModel.call(prompt);

Negative Prompt

使用负面提示词来排除不想要的元素:

DashScopeImageOptions options = DashScopeImageOptions.builder()
.model(DashScopeModel.ImageModel.QWEN_IMAGE.getValue())
.negativePrompt("blurry, low quality, distorted")
.build();

ImagePrompt prompt = new ImagePrompt("A high-quality portrait photo", options);
ImageResponse response = dashScopeImageModel.call(prompt);

Seed for Reproducibility

使用种子值来获得可重现的结果:

DashScopeImageOptions options = DashScopeImageOptions.builder()
.model(DashScopeModel.ImageModel.QWEN_IMAGE.getValue())
.seed(12345) // 固定种子值
.build();

ImagePrompt prompt = new ImagePrompt("A futuristic city", options);
ImageResponse response = dashScopeImageModel.call(prompt);
// 使用相同的种子和提示词会生成相似的结果

Watermark

为生成的图像添加水印:

DashScopeImageOptions options = DashScopeImageOptions.builder()
.model(DashScopeModel.ImageModel.QWEN_IMAGE.getValue())
.watermark(true)
.build();

ImagePrompt prompt = new ImagePrompt("A professional product photo", options);
ImageResponse response = dashScopeImageModel.call(prompt);

Asynchronous Image Generation

DashScope 图像生成采用异步任务模式。当您调用 call() 方法时,系统会:

  1. 提交图像生成任务并获取 taskId
  2. 轮询任务状态直到完成
  3. 返回生成的图像结果

任务状态包括:

  • SUCCEEDED - 任务成功完成
  • FAILED - 任务失败
  • UNKNOWN - 未知状态

您可以通过响应元数据获取任务相关信息:

ImageResponse response = dashScopeImageModel.call(imagePrompt);
ImageResponseMetadata metadata = response.getMetadata();

String taskStatus = (String) metadata.get("taskStatus");
String requestId = (String) metadata.get("requestId");
Integer imageCount = (Integer) metadata.get("imageCount");

Example Code

完整的示例代码可以参考项目中的测试文件,展示了如何使用 DashScope Image API 的各种功能。

Spring AI Alibaba 开源项目基于 Spring AI 构建,是阿里云通义系列模型及服务在 Java AI 应用开发领域的最佳实践,提供高层次的 AI API 抽象与云原生基础设施集成方案,帮助开发者快速构建 AI 应用。