入门指南
本节提供使用 Spring AI 的快速入门指引。
请根据需求按顺序执行以下步骤:
Spring AI 支持 Spring Boot 3.4.x。当 Spring Boot 3.5.x 发布时,我们将同步支持。
Spring Initializr
访问 start.spring.io,选择要在新应用中使用的 AI 模型和向量存储。
工件仓库
里程碑版本 - 使用 Maven Central
从 1.0.0-M6 开始,版本已发布至 Maven Central。无需修改构建文件。
快照版本 - 添加快照仓库
如需使用快照版本(及 1.0.0-M6 之前的里程碑版本),需在构建文件中添加以下仓库:
在 Maven 或 Gradle 构建文件中添加仓库定义:
Maven
<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>
Gradle
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) 声明了指定版本中所有依赖的推荐版本。此为纯 BOM 版本,仅包含依赖管理,无插件声明或对 Spring/Spring Boot 的直接引用。您可使用 Spring Boot 父 POM,或通过 Spring Boot 的 BOM (spring-boot-dependencies) 管理 Spring Boot 版本。
将 BOM 添加至项目:
Maven
<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
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'
}
添加特定组件的依赖
文档后续章节将说明需添加到项目构建系统的依赖项。
聊天模型
嵌入模型
图像生成模型
语音转录模型
文本转语音 (TTS) 模型
向量数据库
Spring AI 示例
更多 Spring AI 相关资源和示例请参阅此页面。
评论区