For the latest snapshot version, please use Spring AI 1.1.3!spring-doc.cn

Azure OpenAI Image Generation

Spring AI supports the gpt-image-1-mini 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-doc.cn

Spring AI defines two configuration properties:spring-doc.cn

  1. spring.ai.azure.openai.api-key: Set this to the value of the API Key obtained from Azure.spring-doc.cn

  2. spring.ai.azure.openai.endpoint: Set this to the endpoint URL obtained when provisioning your model in Azure.spring-doc.cn

You can set these configuration properties in your application.properties file:spring-doc.cn

spring.ai.azure.openai.api-key=<your-azure-openai-api-key>
spring.ai.azure.openai.endpoint=<your-azure-openai-endpoint>

For enhanced security when handling sensitive information like API keys, you can use Spring Expression Language (SpEL) to reference custom environment variables:spring-doc.cn

# In application.yml
spring:
  ai:
    azure:
      openai:
        api-key: ${AZURE_OPENAI_API_KEY}
        endpoint: ${AZURE_OPENAI_ENDPOINT}
# In your environment or .env file
export AZURE_OPENAI_API_KEY=<your-azure-openai-api-key>
export AZURE_OPENAI_ENDPOINT=<your-azure-openai-endpoint>

You can also set these configurations programmatically in your application code:spring-doc.cn

// Retrieve API key and endpoint from secure sources or environment variables
String apiKey = System.getenv("AZURE_OPENAI_API_KEY");
String endpoint = System.getenv("AZURE_OPENAI_ENDPOINT");

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 'gpt-image-1-mini' 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: gpt-image-1-minispring-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 Maven Central and Spring Snapshot repositories. Refer to the Artifact 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

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

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-starter-model-azure-openai</artifactId>
</dependency>

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

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

Image Generation Properties

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

To enable, spring.ai.model.image=azure-openai (It is enabled by default)spring-doc.cn

To disable, spring.ai.model.image=none (or any value which doesn’t match azure-openai)spring-doc.cn

This change is done to allow configuration of multiple models.spring-doc.cn

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

Propertyspring-doc.cn

Descriptionspring-doc.cn

Defaultspring-doc.cn

spring.ai.azure.openai.image.enabled (Removed and no longer valid)spring-doc.cn

Enable Azure OpenAI image model.spring-doc.cn

truespring-doc.cn

spring.ai.model.imagespring-doc.cn

Enable image model. Set to azure-openai for Azure OpenAI.spring-doc.cn

azure-openaispring-doc.cn

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

The number of images to generate (e.g. 1 for gpt-image-1-mini).spring-doc.cn

-spring-doc.cn

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

The model to use for image generation (e.g. gpt-image-1-mini).spring-doc.cn

gpt-image-1-minispring-doc.cn

spring.ai.azure.openai.image.options.deployment-namespring-doc.cn

The deployment name as defined in Azure AI Studio for your image model.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.azure.openai.image.options.sizespring-doc.cn

The size of the generated images (e.g. 1024x1024). Check Azure documentation for supported sizes for your model.spring-doc.cn

-spring-doc.cn

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

The width of the generated images.spring-doc.cn

-spring-doc.cn

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

The height of the generated images.spring-doc.cn

-spring-doc.cn

spring.ai.azure.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.azure.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 to (e.g. <your-resource>.openai.azure.com/)spring-doc.cn

-spring-doc.cn

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

The API Keyspring-doc.cn

-spring-doc.cn

Runtime Options

The AzureOpenAiImageOptions provides model configurations, such as the deployment name, model, and image size.spring-doc.cn

On start-up, the default options can be configured with the AzureOpenAiImageModel(OpenAIClient openAIClient, AzureOpenAiImageOptions options) constructor. 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 request-specific options to the ImagePrompt call. For example, to use the gpt-image-1-mini model with a custom size:spring-doc.cn

ImageResponse response = azureOpenAiImageModel.call(
        new ImagePrompt("A light cream colored mini golden doodle",
        AzureOpenAiImageOptions.builder()
                .model("gpt-image-1-mini")
                .deploymentName("gpt-image-1-mini")
                .height(1024)
                .width(1024)
                .build())
);
In addition to the model specific AzureOpenAiImageOptions you can use a portable ImageOptions instance, created with the ImageOptionsBuilder#builder().