Azure OpenAI Image Generation

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

Prerequisites

Obtain your Azure OpenAI endpoint and api-key from the Azure OpenAI Service section on the Azure Portal. Spring AI defines a configuration property named spring.ai.azure.openai.api-key that you should set to the value of the API Key obtained from Azure. There is also a configuration property named spring.ai.azure.openai.endpoint that you should set to the endpoint URL obtained when provisioning your model in Azure. Exporting environment variables is one way to set these configuration properties:spring-doc.cn

export SPRING_AI_AZURE_OPENAI_API_KEY=<INSERT KEY HERE>
export SPRING_AI_AZURE_OPENAI_ENDPOINT=<INSERT ENDPOINT URL HERE>

Deployment Name

To use run Azure AI applications, create an Azure AI Deployment through the [Azure AI Portal](oai.azure.com/portal).spring-doc.cn

In Azure, each client must specify a Deployment Name to connect to the Azure OpenAI service.spring-doc.cn

It’s essential to understand that the Deployment Name is different from the model you choose to deployspring-doc.cn

For instance, a deployment named 'MyImgAiDeployment' could be configured to use either the Dalle3 model or the Dalle2 model.spring-doc.cn

For now, to keep things simple, you can create a deployment using the following settings:spring-doc.cn

Deployment Name: MyImgAiDeployment Model Name: Dalle3spring-doc.cn

This Azure configuration will align with the default configurations of the Spring Boot Azure AI Starter and its Autoconfiguration feature.spring-doc.cn

If you use a different Deployment Name, update the configuration property accordingly:spring-doc.cn

spring.ai.azure.openai.image.options.deployment-name=<my deployment name>

The different deployment structures of Azure OpenAI and OpenAI leads to a property in the Azure OpenAI client library named deploymentOrModelName. This is because in OpenAI there is no Deployment Name, only a Model Name.spring-doc.cn

Add Repositories and BOM

Spring AI artifacts are published in Spring Milestone and Snapshot repositories. Refer to the Repositories section to add these repositories to your build system.spring-doc.cn

To help with dependency management, Spring AI provides a BOM (bill of materials) to ensure that a consistent version of Spring AI is used throughout the entire project. Refer to the Dependency Management section to add the Spring AI BOM to your build system.spring-doc.cn

Auto-configuration

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

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

dependencies {
    implementation 'org.springframework.ai:spring-ai-azure-openai-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.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.azure.openai.image.enabledspring-doc.cn

Enable OpenAI image model.spring-doc.cn

truespring-doc.cn

spring.ai.azure.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.azure.openai.image.options.modelspring-doc.cn

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

AzureOpenAiImageOptions.DEFAULT_IMAGE_MODELspring-doc.cn

spring.ai.azure.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.azure.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 Azure OpenAI to monitor and detect abuse.spring-doc.cn

-spring-doc.cn

Connection Properties

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

Propertyspring-doc.cn

Descriptionspring-doc.cn

Defaultspring-doc.cn

spring.ai.azure.openai.endpointspring-doc.cn

The URL to connect tospring-doc.cn

my-dalle3.openai.azure.com/spring-doc.cn

spring.ai.azure.openai.apiKeyspring-doc.cn

The API Keyspring-doc.cn

-spring-doc.cn

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 AzureOpenAiImageModel(OpenAiImageApi openAiImageApi) constructor and the withDefaultOptions(OpenAiImageOptions defaultOptions) method. Alternatively, use the spring.ai.azure.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 = azureOpenaiImageModel.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 AzureOpenAiImageOptions you can use a portable ImageOptions instance, created with the ImageOptionsBuilder#builder().