如何开始使用 Spring AI ?我们可以根据自身需求,按照以下各部分的步骤操作。
Spring AI 支持 Spring Boot 3.4.x 和 3.5.x。
1、Spring初始化器
前往 start.spring.io,选择你想在新应用中使用的AI模型和矢量存储。
2、文物库
发行 - 使用 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-快照)或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 快照仓库。要解决这个问题,可以修改*镜像的配置以排除 Spring 仓库:
<mirror> <id>my-mirror</id> <mirrorOf>*,!spring-snapshots,!central-portal-snapshots</mirrorOf> <url>https://my-company-repository.com/maven</url></mirror>
这种配置允许 Maven 直接访问 Spring 快照仓库,同时仍能使用镜像处理其他依赖。
3、依赖管理
Spring AI 物料清单(BOM)声明了该版本 Spring AI 所使用的所有依赖的推荐版本。 这是一个仅包含BOM的版本,仅包含依赖管理,没有插件声明或直接引用Spring或Spring Boot。 你可以使用Spring Boot的父POM,或者使用Spring Boot的BOM (Spring Boot依赖关系)来管理 Spring Boot 版本。
将物料清单添加到你的项目中:
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") // 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(5.0+)原生支持,使用 Maven 物料清单来声明依赖约束,从而使用春 AI 物料清单。这是通过在 Gradle 构建脚本的依赖部分添加一个“平台”依赖处理方法实现的。
4、为特定组件添加依赖
后续继续学习需要添加到项目构建系统中的依赖关系。
聊天模型
嵌入模型
图像生成模型
转录模型
文本转语音(TTS)模型
向量数据库