Spring AI 支持 OpenAI 的人工智能语言模型 ChatGPT。由于 ChatGPT 创建了业界领先的文本生成模型和嵌入,它在激发人们对人工智能驱动的文本生成的兴趣方面发挥了重要作用。
先决条件
要访问 ChatGPT 模型,您需要与 OpenAI 创建一个 API。在 OpenAI 注册页面创建账户,并在 API Keys 页面生成令牌。Spring AI 项目定义了一个名为 spring.ai.openai.api-key 的配置属性,你应该将其设置为从 openai.com 获取的 API 密钥的值。导出环境变量是设置该配置属性的一种方法:
export SPRING_AI_OPENAI_API_KEY=<INSERT KEY HERE>
添加资源库和 BOM
Spring AI 工件发布在 Spring Milestone 和 Snapshot 资源库中。请参阅 "资源库 "部分,将这些资源库添加到您的构建系统中。
为了帮助进行依赖性管理,Spring AI 提供了一个 BOM(物料清单),以确保在整个项目中使用一致的 Spring AI 版本。请参阅 "依赖关系管理 "部分,将 Spring AI BOM 添加到构建系统中。
自动配置
Spring AI 为 OpenAI 聊天客户端提供 Spring Boot 自动配置功能。要启用它,请在项目的 Maven pom.xml 文件中添加以下依赖项:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
或 Gradle build.gradle 构建文件。
dependencies {
implementation 'org.springframework.ai:spring-ai-openai-spring-boot-starter'
}
请参阅 "依赖关系管理 "部分,将 Spring AI BOM 添加到构建文件中。
聊天属性
重试属性
spring.ai.retry 作为属性前缀,可用于配置 OpenAI 聊天模型的重试机制。
| 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 |
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 |
| spring.ai.retry.on-http-codes |
List of HTTP status codes that should trigger a retry (e.g. to throw TransientAiException). |
empty |

最低0.47元/天 解锁文章
418

被折叠的 条评论
为什么被折叠?



