This version is still in development and is not considered stable yet. For the latest snapshot version, please use Spring AI 1.0.0-SNAPSHOT!spring-doc.cn

Stability AI Image Generation

Spring AI supports Stability AI’s text to image generation model.spring-doc.cn

Prerequisites

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

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 is one way to set that configuration property.spring-doc.cn

export SPRING_AI_STABILITYAI_API_KEY=<INSERT KEY HERE>

Auto-configuration

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:spring-doc.cn

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

or to your Gradle build.gradle build file.spring-doc.cn

dependencies {
    implementation 'org.springframework.ai:spring-ai-stability-ai-spring-boot-starter'
}
Refer to the Dependency Management section to add the Spring AI BOM to your build file.

Image Generation Properties

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

Property Description Default

spring.ai.stabilityai.base-urlspring-doc.cn

The URL to connect tospring-doc.cn

api.stability.ai/v1spring-doc.cn

spring.ai.stabilityai.api-keyspring-doc.cn

The API Keyspring-doc.cn

-spring-doc.cn

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

Property Description Default

spring.ai.stabilityai.image.enabledspring-doc.cn

Enable Stability AI image model.spring-doc.cn

truespring-doc.cn

spring.ai.stabilityai.image.base-urlspring-doc.cn

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

api.stability.ai/v1spring-doc.cn

spring.ai.stabilityai.image.api-keyspring-doc.cn

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

-spring-doc.cn

spring.ai.stabilityai.image.option.nspring-doc.cn

The number of images to be generated. Must be between 1 and 10.spring-doc.cn

1spring-doc.cn

spring.ai.stabilityai.image.option.modelspring-doc.cn

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

stable-diffusion-v1-6spring-doc.cn

spring.ai.stabilityai.image.option.widthspring-doc.cn

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

512spring-doc.cn

spring.ai.stabilityai.image.option.heightspring-doc.cn

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

512spring-doc.cn

spring.ai.stabilityai.image.option.responseFormatspring-doc.cn

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

-spring-doc.cn

spring.ai.stabilityai.image.option.cfg_scalespring-doc.cn

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

7spring-doc.cn

spring.ai.stabilityai.image.option.clip_guidance_presetspring-doc.cn

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

NONEspring-doc.cn

spring.ai.stabilityai.image.option.samplerspring-doc.cn

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

-spring-doc.cn

spring.ai.stabilityai.image.option.seedspring-doc.cn

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

0spring-doc.cn

spring.ai.stabilityai.image.option.stepsspring-doc.cn

Number of diffusion steps to run. Valid range: 10 to 50.spring-doc.cn

30spring-doc.cn

spring.ai.stabilityai.image.option.style_presetspring-doc.cn

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

-spring-doc.cn

Runtime Options

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

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.spring-doc.cn

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:spring-doc.cn

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

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