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 获取您的 API 密钥。

You will need to create an API key with Stability AI to access their AI models. Follow their Getting Started documentation to obtain your API key.

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.

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

You can set this configuration property in your application.properties file:

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

为了在处理 API 密钥等敏感信息时增强安全性,您可以使用 Spring 表达式语言 (SpEL) 引用自定义环境变量:

For enhanced security when handling sensitive information like API keys, you can use Spring Expression Language (SpEL) to reference a custom environment variable:

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

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

You can also set this configuration programmatically in your application code:

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

Auto-configuration

Spring AI 自动配置、启动器模块的工件名称发生了重大变化。请参阅 upgrade notes 以获取更多信息。

There has been a significant change in the Spring AI auto-configuration, starter modules' artifact names. Please refer to the upgrade notes for more information.

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-starter-model-stability-ai</artifactId>
</dependency>

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

or to your Gradle build.gradle build file.

dependencies {
    implementation 'org.springframework.ai:spring-ai-starter-model-stability-ai'
}
  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.model.image 的顶级属性进行配置。

Enabling and disabling of the image auto-configurations are now configured via top level properties with the prefix spring.ai.model.image.

要启用,spring.ai.model.image=stabilityai(默认启用)

To enable, spring.ai.model.image=stabilityai (It is enabled by default)

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

To disable, spring.ai.model.image=none (or any value which doesn’t match stabilityai)

此更改旨在允许配置多个模型。

This change is done to allow configuration of multiple models.

前缀 spring.ai.stabilityai.image 是属性前缀,可用于配置 Stability AI 的 ImageModel 实现。

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

Property Description Default

spring.ai.stabilityai.image.enabled (Removed and no longer valid)

Enable Stability AI image model.

true

spring.ai.model.image

Enable Stability AI image model.

stabilityai

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.

-

Runtime Options

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

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

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

On start-up, the default options can be configured with the StabilityAiImageModel(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 = stabilityaiImageModel.call(
        new ImagePrompt("A light cream colored mini golden doodle",
        StabilityAiImageOptions.builder()
                .stylePreset("cinematic")
                .N(4)
                .height(1024)
                .width(1024).build())

);

除了模型特定的 StabilityAiImageOptions ,您还可以使用通过 ImageOptionsBuilder#builder() 创建的可移植 ImageOptions 实例。

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