快速开始
本节提供了如何开始使用 Spring AI 的切入点。
你可以根据自己的需要,遵循以下各节中的步骤。
Spring AI 2.0.x 支持 Spring Boot 4.0.x 和 4.1.x。
Spring Initializr
前往 start.spring.io,选择要在新应用中使用的 AI Models 和 Vector Stores。
Artifact 仓库
版本发布 - 使用 Maven Central
Spring AI 构件可在 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()
}
快照 - 添加快照仓库
要使用最新的开发版本(例如 2.0.0-SNAPSHOT),你需要在构建文件中添加以下快照仓库。
将以下仓库定义添加到你的 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 snapshot repositories,同时其他依赖仍使用你的镜像。
依赖管理
Spring AI Bill of Materials (BOM) 声明了某个给定 Spring AI 版本所使用的所有 dependencies 的推荐版本。这是一个仅包含 BOM 的版本,它只包含 dependency management,不包含 plugin declarations,也没有对 Spring 或 Spring Boot 的直接引用。你可以使用 Spring Boot parent 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>2.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
dependencies {
implementation platform("org.springframework.ai:spring-ai-bom:2.0.0")
// Replace the following with the specific module dependencies (e.g., spring-ai-openai) or starter modules (e.g., spring-ai-starter-model-openai) that you wish to use
implementation 'org.springframework.ai:spring-ai-openai'
}
Gradle 用户也可以利用 Gradle 原生支持,通过 Maven BOM 声明依赖约束来使用 Spring AI BOM。具体实现方式是在 Gradle 构建脚本的 dependencies 部分添加 'platform' 依赖处理方法。
为特定组件添加依赖
文档中的以下每个部分都会显示你需要添加到项目构建系统中的依赖项。
Spring AI 示例
请参阅 此页面 获取更多与 Spring AI 相关的资源和示例。