Lzh on GitHub

入门指南

本节提供了使用 Spring AI 的入门指南和起点。

本节提供了使用 Spring AI 的入门指南和起点。

你可以根据自己的需求,按照以下各节中的步骤进行操作。

Spring AI 支持 Spring Boot 3.4.x 和 3.5.x。

Spring Initializr

前往 start.spring.io,选择你希望在新应用中使用的 AI 模型向量存储(Vector Stores)

构件仓库

版本发布(Releases) - 使用 Maven Central

Spring AI 1.0.0 及更高版本已在 Maven Central 上提供,无需额外的仓库配置。只需确保在构建文件中启用了 Maven Central 即可。

<!-- 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>

快照版本(Snapshots) - 添加 Snapshot 仓库

若要使用最新的开发版本(例如 1.1.0-SNAPSHOT)或 1.0.0 之前的旧里程碑版本,需要在构建文件中添加以下 Snapshot 仓库

将以下仓库定义添加到你的 MavenGradle 构建文件中:

<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>

注意:在使用 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 快照仓库,同时仍然使用你的镜像来获取其他依赖。

依赖管理(Dependency Management)

Spring AI 的 依赖清单(Bill of Materials,BOM) 声明了某个 Spring AI 版本所使用的所有依赖的推荐版本。该 BOM 仅用于依赖管理,不包含插件声明,也不直接引用 Spring 或 Spring Boot。你可以使用 Spring Boot 的父 POM,或者使用 Spring Boot 的 BOM(spring-boot-dependencies)来管理 Spring Boot 的版本。

将 BOM 添加到你的项目中:

<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>

Gradle 用户也可以通过利用 Gradle(5.0 及以上版本)对使用 Maven BOM 声明依赖约束的原生支持来使用 Spring AI BOM。这可以通过在 Gradle 构建脚本的 dependencies 部分添加 platform 依赖处理方法来实现。

为特定组件添加依赖

文档中的每个以下章节都会说明你需要添加到项目构建系统的依赖项:

Spring AI 示例(Spring AI samples)

有关 Spring AI 的更多资源和示例,请参考此 页面