Stability AI Image Generation

Spring AI 支持 Stability AI 的 text to image generation model

Spring AI supports Stability AI’s text to image generation model.

Prerequisites

您需要使用 Stability AI 的 API 密钥来访问其 AI 模型,请关注他们的 Getting Started documentation

You will need to create an API key with Stability AI to access their AI models, follow their Getting Started documentation.

Spring AI 项目定义了一个名为 spring.ai.stabilityai.api-key 的配置属性,你应将其设置为从 Stability AI 获得的 API Key 的值。导出环境变量是一种设置该配置属性的方法。

The Spring AI project defines a configuration property named spring.ai.stabilityai.api-key that you should set to the value of the API Key obtained from Stability AI. Exporting an environment variable in one way to set that configuration property.

export SPRING_AI_STABILITYAI_API_KEY=<INSERT KEY HERE>

Auto-configuration

Spring AI 提供适用于 Stability AI 图像生成客户端的 Spring Boot 自动配置。要启用它,请将以下依赖项添加到项目的 Maven pom.xml 文件中:

Spring AI provides Spring Boot auto-configuration for the Stability AI Image Generation Client. To enable it add the following dependency to your project’s Maven pom.xml file:

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-stability-ai-spring-boot-starter</artifactId>
</dependency>

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

or to your Gradle build.gradle build file.

dependencies {
    implementation 'org.springframework.ai:spring-ai-stability-ai-spring-boot-starter'
}
  1. 参见 Dependency Management 部分,将 Spring AI BOM 添加到你的构建文件中。

Refer to the Dependency Management section to add the Spring AI BOM to your build file.

Image Generation Properties

前缀 spring.ai.stabilityai 用作允许你连接到 Stability AI 的属性前缀。

The prefix spring.ai.stabilityai is used as the property prefix that lets you connect to Stability AI.

Property Description Default

spring.ai.stabilityai.base-url

The URL to connect to

[role="bare"]https://api.stability.ai/v1

spring.ai.stabilityai.api-key

The API Key

-

前缀 spring.ai.stabilityai.image 是允许你配置适用于 Stability AI 的 ImageClient 实现的属性前缀。

The prefix spring.ai.stabilityai.image is the property prefix that lets you configure the ImageClient implementation for Stability AI.

Property Description Default

spring.ai.stabilityai.image.enabled

Enable Stability AI image client.

true

spring.ai.stabilityai.image.base-url

Optional overrides the spring.ai.openai.base-url to provide a specific url

https://api.stability.ai/v1

spring.ai.stabilityai.image.api-key

Optional overrides the spring.ai.openai.api-key to provide a specific api-key

-

spring.ai.stabilityai.image.option.n

The number of images to be generated. Must be between 1 and 10.

1

spring.ai.stabilityai.image.option.model

The engine/model to use in Stability AI. The model is passed in the URL as a path parameter.

stable-diffusion-v1-6

spring.ai.stabilityai.image.option.width

Width of the image to generate, in pixels, in an increment divisible by 64. Engine-specific dimension validation applies.

512

spring.ai.stabilityai.image.option.height

Height of the image to generate, in pixels, in an increment divisible by 64. Engine-specific dimension validation applies.

512

spring.ai.stabilityai.image.option.responseFormat

The format in which the generated images are returned. Must be "application/json" or "image/png".

-

spring.ai.stabilityai.image.option.cfg_scale

The strictness level of the diffusion process adherence to the prompt text. Range: 0 to 35.

7

spring.ai.stabilityai.image.option.clip_guidance_preset

Pass in a style preset to guide the image model towards a particular style. This list of style presets is subject to change.

NONE

spring.ai.stabilityai.image.option.sampler

Which sampler to use for the diffusion process. If this value is omitted, an appropriate sampler will be automatically selected.

-

spring.ai.stabilityai.image.option.seed

Random noise seed (omit this option or use 0 for a random seed). Valid range: 0 to 4294967295.

0

spring.ai.stabilityai.image.option.steps

Number of diffusion steps to run. Valid range: 10 to 50.

30

spring.ai.stabilityai.image.option.style_preset

Pass in a style preset to guide the image model towards a particular style. This list of style presets is subject to change.

-

Image Options

StabilityAiImageOptions.java提供模型配置,例如使用的模型、样式、大小等。

The StabilityAiImageOptions.java provides model configurations, such as the model to use, the style, the size, etc.

在启动时,可以使用 StabilityAiImageClient(StabilityAiApi stabilityAiApi, StabilityAiImageOptions options) 构造函数配置默认选项。或者,使用之前描述的 spring.ai.openai.image.options.* 属性。

On start-up, the default options can be configured with the StabilityAiImageClient(StabilityAiApi stabilityAiApi, StabilityAiImageOptions options) constructor. Alternatively, use the spring.ai.openai.image.options.* properties described previously.

在运行时,你可以通过将新的特定于请求的选项添加到 ImagePrompt 调用来覆盖默认选项。例如,要覆盖特定于 Stability AI 的选项,例如质量和要创建的图像数量,请使用以下代码示例:

At runtime, you can override the default options by adding new, request specific, options to the ImagePrompt call. For example to override the Stability AI specific options such as quality and the number of images to create, use the following code example:

ImageResponse response = openaiImageClient.call(
        new ImagePrompt("A light cream colored mini golden doodle",
        StabilityAiImageOptions.builder()
                .withStylePreset("cinematic")
                .withN(4)
                .withHeight(1024)
                .withWidth(1024).build())

);
  1. 除了模型特定的 StabilityAiImageOptions 之外,你还可以使用用 ImageOptionsBuilder#builder() 创建的便携式 ImageOptions 实例。

In addition to the model specific StabilityAiImageOptions you can use a portable ImageOptions instance, created with the ImageOptionsBuilder#builder().