主题
入门
本节为如何开始使用 Spring AI 提供了切入点。
你应根据自己的需求,按照以下各节的步骤操作。
NOTE
Spring AI 2.0.x 支持 Spring Boot 4.0.x 和 4.1.x。
Spring Initializr
前往 start.spring.io,选择你想在新应用中使用的 AI 模型和向量数据库。
制品仓库
发布版 - 使用 Maven Central
Spring AI 的制品可在 Maven Central 获取。 无需额外的仓库配置。只要确保你的构建文件中启用了 Maven Central 即可。
xml
<!-- Maven Central is included by default in Maven builds.
You usually don’t need to configure it explicitly,
but it's shown here for clarity. -->
<repositories>
<repository>
<id>central</id>
<url>https://repo.maven.apache.org/maven2</url>
</repository>
</repositories>快照版 - 添加快照仓库
要使用最新的开发版本(例如 2.0.0-SNAPSHOT),你需要在构建文件中添加以下快照仓库。
将以下仓库定义添加到你的 Maven 或 Gradle 构建文件中:
xml
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
<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>注意: 在配合 Spring AI 快照使用 Maven 时,请留意你的 Maven 镜像配置。如果你在 settings.xml 中像这样配置了一个镜像:
xml
<mirror>
<id>my-mirror</id>
<mirrorOf>*</mirrorOf>
<url>https://my-company-repository.com/maven</url>
</mirror>通配符 * 会将所有仓库请求重定向到你的镜像,从而阻止访问 Spring 快照仓库。要修复这个问题,请修改 mirrorOf 配置,将 Spring 仓库排除在外:
xml
<mirror>
<id>my-mirror</id>
<mirrorOf>*,!spring-snapshots,!central-portal-snapshots</mirrorOf>
<url>https://my-company-repository.com/maven</url>
</mirror>这样配置可让 Maven 直接访问 Spring 快照仓库,同时对其他依赖仍使用你的镜像。
依赖管理
Spring AI 物料清单(BOM)声明了给定 Spring AI 版本所使用的所有依赖的推荐版本。 这是一个仅含 BOM 的版本,它只包含依赖管理,不含插件声明,也不直接引用 Spring 或 Spring Boot。 你可以使用 Spring Boot 父 POM,或使用 Spring Boot 的 BOM(spring-boot-dependencies)来管理 Spring Boot 版本。
将 BOM 添加到你的项目中:
xml
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>2.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>为特定组件添加依赖
文档中的以下各节会分别展示你需要向项目构建系统添加哪些依赖。
- 聊天模型(Chat Models)
- 嵌入模型(Embedding Models)
- 图像生成模型(Image Generation Models)
- 转录模型(Transcription Models)
- 文本转语音(TTS)模型
- 向量数据库
Spring AI 示例
更多关于 Spring AI 的资源与示例,请参阅此页面。