入门
本节提供使用 Spring AI 的入门指引。
本站(springdoc.cn)中的内容来源于 spring.io ,原始版权归属于 spring.io。由 springdoc.cn 进行翻译,整理。可供个人学习、研究,未经许可,不得进行任何转载、商用或与之相关的行为。 商标声明:Spring 是 Pivotal Software, Inc. 在美国以及其他国家的商标。 |
你应根据自己的需要,按照以下各节中的步骤进行操作。
Spring AI 支持 Spring Boot 3.4.x 版本。待 Spring Boot 3.5.x 发布后,我们也将同步提供支持。 |
Spring Initializr
前往 start.springboot.io,选择要在新应用程序中使用的 AI 模型和向量存储。
组件仓库
快照 - 添加 Snapshot 仓库
要使用快照(和 1.0.0-M6 之前的里程碑)版本,需要在构建文件中添加以下快照仓库。
将以下 Repository 定义添加到 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/'
}
}
NOTE: 使用 Maven 构建 Spring AI 快照版本时,请特别注意镜像配置。若你的 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)声明了指定版本所有依赖的推荐版本。此为纯 BOM 版本,仅包含依赖管理,不涉及插件声明或 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>1.0.0-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
dependencies {
implementation platform("org.springframework.ai:spring-ai-bom:1.0.0-SNAPSHOT")
// 将以下内容替换为你需要使用的特定模块的 starter 依赖
implementation 'org.springframework.ai:spring-ai-openai'
}
Gradle 用户也可通过 Gradle(5.0+)原生支持的 Maven BOM 依赖约束功能使用 Spring AI BOM。具体实现方式是在 Gradle 构建脚本的 dependencies 部分添加 'platform' 依赖处理方法。
Spring AI 示例
更多 Spring AI 相关资源和示例请参阅 此页面。