OpenAI Image Generation

Spring AI supports DALL-E, the Image generation model from OpenAI.spring-doc.cn

Prerequisites

You will need to create an API key with OpenAI to access ChatGPT models. Create an account at OpenAI signup page and generate the token on the API Keys page. The Spring AI project defines a configuration property named spring.ai.openai.api-key that you should set to the value of the API Key obtained from openai.com. Exporting an environment variable is one way to set that configuration property:spring-doc.cn

export SPRING_AI_OPENAI_API_KEY=<INSERT KEY HERE>

Auto-configuration

Spring AI provides Spring Boot auto-configuration for the OpenAI 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-openai-spring-boot-starter</artifactId>
</dependency>

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

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

Image Generation Properties

Connection Properties

The prefix spring.ai.openai is used as the property prefix that lets you connect to OpenAI.spring-doc.cn

Propertyspring-doc.cn

Descriptionspring-doc.cn

Defaultspring-doc.cn

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

The URL to connect tospring-doc.cn

api.openai.comspring-doc.cn

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

The API Keyspring-doc.cn

-spring-doc.cn

spring.ai.openai.organization-idspring-doc.cn

Optionally you can specify which organization used for an API request.spring-doc.cn

-spring-doc.cn

spring.ai.openai.project-idspring-doc.cn

Optionally, you can specify which project is used for an API request.spring-doc.cn

-spring-doc.cn

For users that belong to multiple organizations (or are accessing their projects through their legacy user API key), optionally, you can specify which organization and project is used for an API request. Usage from these API requests will count as usage for the specified organization and project.

Retry Properties

The prefix spring.ai.retry is used as the property prefix that lets you configure the retry mechanism for the OpenAI Image client.spring-doc.cn

Property Description Default

spring.ai.retry.max-attemptsspring-doc.cn

Maximum number of retry attempts.spring-doc.cn

10spring-doc.cn

spring.ai.retry.backoff.initial-intervalspring-doc.cn

Initial sleep duration for the exponential backoff policy.spring-doc.cn

2 sec.spring-doc.cn

spring.ai.retry.backoff.multiplierspring-doc.cn

Backoff interval multiplier.spring-doc.cn

5spring-doc.cn

spring.ai.retry.backoff.max-intervalspring-doc.cn

Maximum backoff duration.spring-doc.cn

3 min.spring-doc.cn

spring.ai.retry.on-client-errorsspring-doc.cn

If false, throw a NonTransientAiException, and do not attempt retry for 4xx client error codesspring-doc.cn

falsespring-doc.cn

spring.ai.retry.exclude-on-http-codesspring-doc.cn

List of HTTP status codes that should not trigger a retry (e.g. to throw NonTransientAiException).spring-doc.cn

emptyspring-doc.cn

spring.ai.retry.on-http-codesspring-doc.cn

List of HTTP status codes that should trigger a retry (e.g. to throw TransientAiException).spring-doc.cn

emptyspring-doc.cn

Configuration Properties

The prefix spring.ai.openai.image is the property prefix that lets you configure the ImageModel implementation for OpenAI.spring-doc.cn

Propertyspring-doc.cn

Descriptionspring-doc.cn

Defaultspring-doc.cn

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

Enable OpenAI image model.spring-doc.cn

truespring-doc.cn

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

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

-spring-doc.cn

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

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

-spring-doc.cn

spring.ai.openai.image.organization-idspring-doc.cn

Optionally you can specify which organization used for an API request.spring-doc.cn

-spring-doc.cn

spring.ai.openai.image.project-idspring-doc.cn

Optionally, you can specify which project is used for an API request.spring-doc.cn

-spring-doc.cn

spring.ai.openai.image.options.nspring-doc.cn

The number of images to generate. Must be between 1 and 10. For dall-e-3, only n=1 is supported.spring-doc.cn

-spring-doc.cn

spring.ai.openai.image.options.modelspring-doc.cn

The model to use for image generation.spring-doc.cn

OpenAiImageApi.DEFAULT_IMAGE_MODELspring-doc.cn

spring.ai.openai.image.options.qualityspring-doc.cn

The quality of the image that will be generated. HD creates images with finer details and greater consistency across the image. This parameter is only supported for dall-e-3.spring-doc.cn

-spring-doc.cn

spring.ai.openai.image.options.response_formatspring-doc.cn

The format in which the generated images are returned. Must be one of URL or b64_json.spring-doc.cn

-spring-doc.cn

spring.ai.openai.image.options.sizespring-doc.cn

The size of the generated images. Must be one of 256x256, 512x512, or 1024x1024 for dall-e-2. Must be one of 1024x1024, 1792x1024, or 1024x1792 for dall-e-3 models.spring-doc.cn

-spring-doc.cn

spring.ai.openai.image.options.size_widthspring-doc.cn

The width of the generated images. Must be one of 256, 512, or 1024 for dall-e-2.spring-doc.cn

-spring-doc.cn

spring.ai.openai.image.options.size_heightspring-doc.cn

The height of the generated images. Must be one of 256, 512, or 1024 for dall-e-2.spring-doc.cn

-spring-doc.cn

spring.ai.openai.image.options.stylespring-doc.cn

The style of the generated images. Must be one of vivid or natural. Vivid causes the model to lean towards generating hyper-real and dramatic images. Natural causes the model to produce more natural, less hyper-real looking images. This parameter is only supported for dall-e-3.spring-doc.cn

-spring-doc.cn

spring.ai.openai.image.options.userspring-doc.cn

A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.spring-doc.cn

-spring-doc.cn

You can override the common spring.ai.openai.base-url, spring.ai.openai.api-key, spring.ai.openai.organization-id and spring.ai.openai.project-id properties. The spring.ai.openai.image.base-url, spring.ai.openai.image.api-key, spring.ai.openai.image.organization-id and spring.ai.openai.image.project-id properties if set take precedence over the common properties. This is useful if you want to use different OpenAI accounts for different models and different model endpoints.
All properties prefixed with spring.ai.openai.image.options can be overridden at runtime.

Runtime Options

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

On start-up, the default options can be configured with the OpenAiImageModel(OpenAiImageApi openAiImageApi) constructor and the withDefaultOptions(OpenAiImageOptions defaultOptions) method. 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 OpenAI specific options such as quality and the number of images to create, use the following code example:spring-doc.cn

ImageResponse response = openaiImageModel.call(
        new ImagePrompt("A light cream colored mini golden doodle",
        OpenAiImageOptions.builder()
                .withQuality("hd")
                .withN(4)
                .withHeight(1024)
                .withWidth(1024).build())

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