Spring AI之开始

快速入门

本节提供了有关如何开始使用 Spring AI 的起点。
您应该根据自己的需要,按照以下每个部分中的步骤进行操作。

Spring AI 支持 Spring Boot 3.4.x。当 Spring Boot 3.5.x 发布时,我们也将支持它。

Spring Initializr

前往 start.spring.io(https://start.spring.io/) 并选择要在新应用程序中使用的 AI 模型和矢量存储。

构件仓库 (Artifact Repositories)

里程碑版 (Milestones) - 直接使用Maven中央仓库

从 1.0.0-M6 版本开始,所有的里程碑版(Milestone)和正式版(GA)都会发布到 Maven 中央仓库 (Maven Central)。

**个人理解:**这意味着,如果你使用的 Spring AI 版本 大于或等于 1.0.0-M6,那么你不需要在你的构建文件(如 Maven 的 pom.xml 或 Gradle 的 build.gradle)中添加任何特殊的仓库地址。Maven 和 Gradle 默认就会从中央仓库查找和下载依赖,一切都会自动完成。这大大简化了项目配置。

快照版 (Snapshots) - 需要添加快照仓库

如果你想使用最新的快照版 (SNAPSHOT),或者需要使用 1.0.0-M6 之前的旧里程碑版,你就必须在你的构建文件中手动添加 Spring 的快照仓库地址。

**什么是快照版 (SNAPSHOT)? **
这通常是开发过程中的版本,包含了最新的代码提交,但可能不稳定。开发者用它来测试新功能或修复。因为它们不是正式发布的,所以不会被放到 Maven 中央仓库。

为什么要添加仓库?
你需要明确告诉你的构建工具(Maven/Gradle):“除了默认的中央仓库,请也去下面这个 Spring 自己的仓库地址找一下我需要的依赖。” 否则,构建会因为找不到依赖而失败。

**因此: **
**用新版(≥ 1.0.0-M6)? -> 无需任何配置。 **
**尝鲜用快照版 (SNAPSHOT) 或用非常旧的版本(< 1.0.0-M6)? -> 必须手动在项目中添加指定的仓库地址。 **

请将以下仓库定义添加到您的 Maven 或 Gradle 构建文件中:

对于 Maven (pom.xml):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<repositories>
<!-- Spring 快照仓库 -->
<repository>
<id>spring-snapshots</id> //仓库的唯一标识符,在项目中不能重复
<name>Spring Snapshots</name> //仓库的可读名称,用于日志和错误信息中,方便识别
<url>https://repo.spring.io/snapshot</url> //Maven下载依赖的url
<releases> //禁止Maven在这个地址(https://repo.spring.io/snapshot)寻找任何正式版(Release)的依赖
<enabled>false</enabled>
</releases>
</repository>
<!-- Sonatype 中央快照门户仓库 -->
<repository>
<name>Central Portal Snapshots</name>
<id>central-portal-snapshots</id>
<url>https://central.sonatype.com/repository/maven-snapshots/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots> //要在这个仓库里查找快照版本
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>

我一开始是觉得配置两个依赖有问题,觉得没必要,但是想了想得出一下结论

因为你不能百分百确定你需要的那个快照版依赖(或者这个依赖所依赖的其他快照版)到底在哪个依赖里面

对于 Gradle (build.gradle 或 build.gradle.kts):

1
2
3
4
5
6
7
8
9
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/milestone' }
maven { url 'https://repo.spring.io/snapshot' }
maven {
name = 'Central Portal Snapshots'
url = 'https://central.sonatype.com/repository/maven-snapshots/'
}
}

注意: 将 Maven 与 Spring AI 快照一起使用时,请注意您的 Maven 镜像配置。如果您在 settings.xml 中配置了镜像,如下所示:

1
2
3
4
5
<mirror>
<id>my-mirror</id>
<mirrorOf>*</mirrorOf>
<url>https://my-company-repository.com/maven</url>
</mirror>

通配符 * 会将所有存储库请求重定向到您的镜像,从而阻止访问 Spring 快照存储库。要解决此问题,请修改 mirrorOf 配置以排除 Spring 存储库:

1
2
3
4
5
<mirror>
<id>my-mirror</id>
<mirrorOf>*,!spring-snapshots,!central-portal-snapshots</mirrorOf>
<url>https://my-company-repository.com/maven</url>
</mirror>

依赖管理 (Dependency Management)

Spring AI 的物料清单(BOM - Bill of Materials)声明了给定 Spring AI 版本所使用的所有依赖项的推荐版本。这是一个纯 BOM 的版本,它只包含依赖管理,没有插件声明或对 Spring 或 Spring Boot 的直接引用。您可以使用 Spring Boot 的父级 POM,或者使用 Spring Boot 的 BOM(spring-boot-dependencies)来管理 Spring Boot 的版本。

将 BOM 添加到您的项目中:

对于 Maven (pom.xml):

1
2
3
4
5
6
7
8
9
10
11
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>1.0.0-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

对于 Gradle (build.gradle 或 build.gradle.kts):

1
2
3
4
5
dependencies {
implementation platform("org.springframework.ai:spring-ai-bom:1.0.0-SNAPSHOT")
// Replace the following with the starter dependencies of specific modules you wish to use
implementation 'org.springframework.ai:spring-ai-openai'
}

为特定组件添加依赖项

文档中的以下每个部分都显示了您需要添加到项目构建系统中的依赖项。

聊天模型(https://docs.spring.io/spring-ai/reference/api/chatmodel.html)

嵌入模型(https://docs.spring.io/spring-ai/reference/api/embeddings.html)

图像生成模型(https://docs.spring.io/spring-ai/reference/api/imageclient.html)

转录模型(https://docs.spring.io/spring-ai/reference/api/audio/transcriptions.html)

文本到语音转换 (TTS) 模型(https://docs.spring.io/spring-ai/reference/api/audio/speech.html)

矢量数据库(https://docs.spring.io/spring-ai/reference/api/vectordbs.html)

Spring AI 示例

有关与 Spring AI 相关的更多资源和示例,请参阅此页面 (https://github.com/danvega/awesome-spring-ai)。