OpenAI Image Generation

Spring AI 支持 DALL-E,这是 OpenAI 的图像生成模型。

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

Prerequisites

您需要使用 OpenAI 创建 API 密钥来访问 ChatGPT 模型。在 OpenAI signup page上创建一个帐户并在 API Keys page上生成令牌。Spring AI 项目定义了一个配置属性,名为 spring.ai.openai.api-key,您应将其设置为从 openai.com 获得的 `API Key`的值。导出环境变量是设置该配置属性的一种方法:

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:

export SPRING_AI_OPENAI_API_KEY=<INSERT KEY HERE>

Auto-configuration

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

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:

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

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

or to your Gradle build.gradle build file.

dependencies {
    implementation 'org.springframework.ai:spring-ai-openai-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.openai.image 是允许你配置适用于 OpenAI 的 ImageClient 实现的属性前缀。

The prefix spring.ai.openai.image is the property prefix that lets you configure the ImageClient implementation for OpenAI.

Property

Description

Default

spring.ai.openai.image.enabled

Enable OpenAI image client.

true

spring.ai.openai.image.base-url

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

-

spring.ai.openai.image.api-key

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

-

spring.ai.openai.image.options.n

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

-

spring.ai.openai.image.options.model

The model to use for image generation.

OpenAiImageApi.DEFAULT_IMAGE_MODEL

spring.ai.openai.image.options.quality

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.ai.openai.image.options.response_format

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

-

spring.ai.openai.image.options.size

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.ai.openai.image.options.size_width

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

-

spring.ai.openai.image.options.size_height

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

-

spring.ai.openai.image.options.style

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.ai.openai.image.options.user

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

-

Connection Properties

spring.ai.openai 前缀用作可让你连接到 Open AI 的属性前缀。

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

Property

Description

Default

spring.ai.openai.base-url

The URL to connect to

[role="bare"]https://api.openai.com

spring.ai.openai.api-key

The API Key

-

Configuration Properties

Retry Properties

spring.ai.retry 前缀用作可让你配置 OpenAI Image 客户端的重试机制的属性前缀。

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

Property Description Default

spring.ai.retry.max-attempts

Maximum number of retry attempts.

10

spring.ai.retry.backoff.initial-interval

Initial sleep duration for the exponential backoff policy.

2 sec.

spring.ai.retry.backoff.multiplier

Backoff interval multiplier.

5

spring.ai.retry.backoff.max-interval

Maximum backoff duration.

3 min.

spring.ai.retry.on-client-errors

If false, throw a NonTransientAiException, and do not attempt retry for 4xx client error codes

false

spring.ai.retry.exclude-on-http-codes

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

empty

Runtime Options

OpenAiImageOptions.java提供模型配置,例如使用的模型、质量、大小等。

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

启动时,可以使用 OpenAiImageClient(OpenAiImageApi openAiImageApi) 构造函数和 withDefaultOptions(OpenAiImageOptions defaultOptions) 方法配置默认选项。或者,使用前面介绍的 spring.ai.openai.image.options.* 属性。

On start-up, the default options can be configured with the OpenAiImageClient(OpenAiImageApi openAiImageApi) constructor and the withDefaultOptions(OpenAiImageOptions defaultOptions) method. Alternatively, use the spring.ai.openai.image.options.* properties described previously.

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

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:

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

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

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