ZhiPuAI Image Generation

Spring AI supports CogView, the Image generation model from ZhiPuAI.spring-doc.cn

Prerequisites

You will need to create an API with ZhiPuAI to access ZhiPu AI language models.spring-doc.cn

Create an account at ZhiPu AI registration page and generate the token on the API Keys page. The Spring AI project defines a configuration property named spring.ai.zhipuai.api-key that you should set to the value of the API Key obtained from API Keys page. Exporting an environment variable is one way to set that configuration property:spring-doc.cn

export SPRING_AI_ZHIPU_AI_API_KEY=<INSERT KEY HERE>

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 ZhiPuAI 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-zhipuai-spring-boot-starter</artifactId>
</dependency>

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

dependencies {
    implementation 'org.springframework.ai:spring-ai-zhipuai-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.zhipuai.image is the property prefix that lets you configure the ImageModel implementation for ZhiPuAI.spring-doc.cn

Propertyspring-doc.cn

Descriptionspring-doc.cn

Defaultspring-doc.cn

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

Enable ZhiPuAI image model.spring-doc.cn

truespring-doc.cn

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

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

-spring-doc.cn

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

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

-spring-doc.cn

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

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

cogview-3spring-doc.cn

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

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

-spring-doc.cn

Connection Properties

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

Propertyspring-doc.cn

Descriptionspring-doc.cn

Defaultspring-doc.cn

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

The URL to connect tospring-doc.cn

open.bigmodel.cn/api/paasspring-doc.cn

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

The API Keyspring-doc.cn

-spring-doc.cn

Configuration Properties

Retry Properties

The prefix spring.ai.retry is used as the property prefix that lets you configure the retry mechanism for the ZhiPuAI 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

Runtime Options

The ZhiPuAiImageOptions.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 ZhiPuAiImageModel(ZhiPuAiImageApi zhiPuAiImageApi) constructor and the withDefaultOptions(ZhiPuAiImageOptions defaultOptions) method. Alternatively, use the spring.ai.zhipuai.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 ZhiPuAI specific options such as quality and the number of images to create, use the following code example:spring-doc.cn

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

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