入门指南
本节提供了关于如何开始使用 Spring AI 的切入点。
您应根据需要按照以下各部分的步骤进行操作。
Spring AI 支持 Spring Boot 3.4.x 和 3.5.x 版本。
Spring Initializr
前往 start.spring.io,选择您希望在新应用中使用的 AI 模型与向量存储。
Artifact 仓库
发行版 - 使用 Maven Central
Spring AI 1.0.0 及更高版本已在 Maven Central 提供。无需额外的仓库配置。只需确保您的构建文件中已启用 Maven Central。
- Maven
- Gradle
<!-- 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>
repositories {
mavenCentral()
}
快照 - 添加快照仓库
要使用最新的开发版本(例如 1.1.0-SNAPSHOT)或 1.0.0 之前的老版本里程碑版本,你需要在构建文件中添加以下快照仓库。
将以下仓库定义添加到您的Maven或Gradle构建文件中:
- Maven
- Gradle
<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>
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 文件中配置了如下所示的镜像:
<mirror>
<id>my-mirror</id>
<mirrorOf>*</mirrorOf>
<url>https://my-company-repository.com/maven</url>
</mirror>
通配符 * 会将所有仓库请求重定向到你的镜像,从而阻止访问 Spring 快照仓库。要解决此问题,请修改 mirrorOf 配置以排除 Spring 仓库:
<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:
- Maven
- Gradle
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>1.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
dependencies {
implementation platform("org.springframework.ai:spring-ai-bom:1.0.0")
// 将以下内容替换为你希望使用的具体模块依赖(例如 spring-ai-openai)或 starter 模块(例如 spring-ai-starter-model-openai)
implementation 'org.springframework.ai:spring-ai-openai'
}
Gradle 用户也可以通过利用 Gradle (5.0+) 原生支持使用 Maven BOM 声明依赖约束的功能来使用 Spring AI BOM。这可以通过在你的 Gradle 构建脚本的依赖项部分添加一个 'platform' 依赖处理器方法来实现。
为特定组件添加依赖项
文档中的以下每个章节都展示了您需要添加到项目构建系统中的依赖项。
Spring AI 示例
请参考此页面获取更多与 Spring AI 相关的资源和示例。