此版本仍在开发中,尚未被视为稳定版。如需最新的快照版本,请使用 Spring AI 1.1.3spring-doc.cadn.net.cn

智谱 AI 图像生成

Spring AI 支持智谱 AI 的图像生成模型 CogView。spring-doc.cadn.net.cn

前置条件

您需要创建一个智谱 AI 的 API 以访问智谱大语言模型。spring-doc.cadn.net.cn

智谱 AI 注册页面 创建账户,并在 API 密钥页面 生成Tokens。spring-doc.cadn.net.cn

The Spring AI 项目定义了一个配置属性名为spring.ai.zhipuai.api-key,你应该将其设置为从API密钥页面获得的API Key的值。spring-doc.cadn.net.cn

您可以在您的application.properties文件中设置此配置属性:spring-doc.cadn.net.cn

spring.ai.zhipuai.api-key=<your-zhipuai-api-key>

处理敏感信息(如API密钥)时,为了增强安全性,您可以使用Spring表达式语言(SpEL)引用自定义环境变量:spring-doc.cadn.net.cn

# In application.yml
spring:
  ai:
    zhipuai:
      api-key: ${ZHIPUAI_API_KEY}
# In your environment or .env file
export ZHIPUAI_API_KEY=<your-zhipuai-api-key>

您也可以在应用程序代码中通过编程方式设置此配置:spring-doc.cadn.net.cn

// Retrieve API key from a secure source or environment variable
String apiKey = System.getenv("ZHIPUAI_API_KEY");

添加仓库和BOM

Spring AI 构件发布在 Maven 中央仓库和 Spring Snapshot 仓库中。 请参阅 构件仓库 部分,将这些仓库添加到您的构建系统中。spring-doc.cadn.net.cn

为了帮助进行依赖管理,Spring AI 提供了一个 BOM(物料清单),以确保整个项目中使用的是相同的 Spring AI 版本。请参阅 依赖管理 部分,将 Spring AI BOM 添加到您的构建系统中。spring-doc.cadn.net.cn

Auto-configuration

Spring AI自动配置和starter模块的artifact名称有了重大变化。 请参阅升级说明获取更多信息。spring-doc.cadn.net.cn

Spring AI 为智谱 AI(ZhiPuAI)聊天客户端提供 Spring Boot 自动配置。 要启用它,请将以下依赖项添加到项目的 Maven pom.xml 文件中:spring-doc.cadn.net.cn

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-starter-model-zhipuai</artifactId>
</dependency>

请将以下内容添加到您的Gradle build.gradle 构建文件中。spring-doc.cadn.net.cn

dependencies {
    implementation 'org.springframework.ai:spring-ai-starter-model-zhipuai'
}
请参阅依赖管理部分,将Spring AI BOM添加到您的构建文件中。

图像生成属性

图像自动配置的启用和禁用现在通过前缀为 spring.ai.model.image 的顶层属性进行配置。spring-doc.cadn.net.cn

要启用,请设置 spring.ai.model.image=stabilityai(默认已启用)spring-doc.cadn.net.cn

要禁用,请设置 spring.ai.model.image=none(或任何不与 stabilityai 匹配的值)spring-doc.cadn.net.cn

此更改是为了允许配置多个模型。spring-doc.cadn.net.cn

前缀 spring.ai.zhipuai.image 是属性前缀,允许您为智谱 AI(ZhiPuAI)配置 ImageModel 实现。spring-doc.cadn.net.cn

<property> </property>spring-doc.cadn.net.cn

<description> </description>spring-doc.cadn.net.cn

默认spring-doc.cadn.net.cn

spring.ai.zhipuai.image.enabled(已移除且不再生效)spring-doc.cadn.net.cn

启用智谱 AI 图像模型。spring-doc.cadn.net.cn

truespring-doc.cadn.net.cn

spring.ai.model.imagespring-doc.cadn.net.cn

启用智谱 AI 图像模型。spring-doc.cadn.net.cn

语雀 AIspring-doc.cadn.net.cn

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

可选地覆盖 spring.ai.zhipuai.base-url 以提供特定于聊天的 URLspring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

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

可选地覆盖 spring.ai.zhipuai.api-key,以提供特定于聊天的 API 密钥。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

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

用于图像生成的模型。spring-doc.cadn.net.cn

cogview-3spring-doc.cadn.net.cn

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

代表最终用户的唯一标识符,可帮助智谱 AI 监控和检测滥用行为。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

连接属性

前缀 spring.ai.zhipuai 用作属性前缀,让您能够连接到智谱 AI(ZhiPuAI)。spring-doc.cadn.net.cn

<property> </property>spring-doc.cadn.net.cn

<description> </description>spring-doc.cadn.net.cn

默认spring-doc.cadn.net.cn

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

连接的URLspring-doc.cadn.net.cn

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

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

API密钥spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

配置属性

重试属性

前缀 spring.ai.retry 用作属性前缀,让您能够配置智谱 AI 图像客户端的重试机制。spring-doc.cadn.net.cn

<property> </property> <description> </description> 默认

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

最大重试尝试次数。spring-doc.cadn.net.cn

10spring-doc.cadn.net.cn

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

指数退避策略的初始睡眠时长。spring-doc.cadn.net.cn

2 秒。spring-doc.cadn.net.cn

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

重试间隔倍数。spring-doc.cadn.net.cn

5spring-doc.cadn.net.cn

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

最大退避持续时间。spring-doc.cadn.net.cn

3 min.spring-doc.cadn.net.cn

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

如果为假,则抛出一个NonTransientAiException,并且不尝试重试4xx客户端错误代码spring-doc.cadn.net.cn

falsespring-doc.cadn.net.cn

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

不应当触发重试的HTTP状态码列表(例如,抛出NonTransientAiException)。spring-doc.cadn.net.cn

<p>空内容</p>spring-doc.cadn.net.cn

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

应触发重试的HTTP状态码列表(例如,抛出TransientAiException)。spring-doc.cadn.net.cn

<p>空内容</p>spring-doc.cadn.net.cn

运行时选项

ZhiPuAiImageOptions.java 提供了模型配置,例如要使用的模型、质量、尺寸等。spring-doc.cadn.net.cn

在启动时,可以使用 ZhiPuAiImageModel(ZhiPuAiImageApi zhiPuAiImageApi) 构造函数和 withDefaultOptions(ZhiPuAiImageOptions defaultOptions) 方法来配置默认选项。或者,也可以使用前面描述的 spring.ai.zhipuai.image.options.* 属性。spring-doc.cadn.net.cn

在运行时,您可以通过向 ImagePrompt 调用添加新的、特定于请求的选项来覆盖默认选项。 例如,要覆盖智谱 AI(ZhiPuAI)特定的选项(如图像质量和要生成的图像数量),请使用以下代码示例:spring-doc.cadn.net.cn

ImageResponse response = zhiPuAiImageModel.call(
        new ImagePrompt("A light cream colored mini golden doodle",
        ZhiPuAiImageOptions.builder()
                .quality("hd")
                .N(4)
                .height(1024)
                .width(1024).build())

);
除了模型特定的 ZhiPuAiImageOptions 之外,您还可以使用通过 ImageOptionsBuilder#builder() 创建的可移植 ImageOptions 实例。