开始

本节提供了如何开始使用 Spring AI 的起点。spring-doc.cadn.net.cn

应根据需要按照以下各节中的步骤进行作。spring-doc.cadn.net.cn

Spring AI 支持 Spring Boot 3.4.x。当 Spring Boot 3.5.x 发布时,我们也将支持它。

Spring初始化

前往 start.spring.io 并选择要在新应用程序中使用的 AI 模型和矢量存储。spring-doc.cadn.net.cn

工件存储库

里程碑版本 - 使用 Maven Central

从 1.0.0-M6 开始,版本可在 Maven Central 中使用。 无需更改构建文件。spring-doc.cadn.net.cn

快照版本 - 添加快照存储库

若要使用快照(以及 1.0.0-M6 里程碑之前)版本,需要在构建文件中添加以下快照存储库。spring-doc.cadn.net.cn

将以下代码库定义添加到 Maven 或 Gradle 构建文件中:spring-doc.cadn.net.cn

<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像下面这样:spring-doc.cadn.net.cn

<mirror>
    <id>my-mirror</id>
    <mirrorOf>*</mirrorOf>
    <url>https://my-company-repository.com/maven</url>
</mirror>

通配符会将所有存储库请求重定向到镜像,从而阻止访问 Spring 快照存储库。要解决此问题,请修改*mirrorOf配置以排除 Spring 存储库:spring-doc.cadn.net.cn

<mirror>
    <id>my-mirror</id>
    <mirrorOf>*,!spring-snapshots,!central-portal-snapshots</mirrorOf>
    <url>https://my-company-repository.com/maven</url>
</mirror>

此配置允许 Maven 直接访问 Spring 快照存储库,同时仍将镜像用于其他依赖项。spring-doc.cadn.net.cn

依赖关系管理

Spring AI 物料清单 (BOM) 声明了给定版本的 Spring AI 使用的所有依赖项的推荐版本。 这是一个仅 BOM 版本,它仅包含依赖项管理,没有插件声明或对 Spring 或 Spring Boot 的直接引用。 您可以使用 Spring Boot 父 POM,或使用 Spring Boot 中的 BOM (spring-boot-dependencies) 来管理 Spring Boot 版本。spring-doc.cadn.net.cn

将 BOM 添加到项目:spring-doc.cadn.net.cn

<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")
  // Replace the following with the starter dependencies of specific modules you wish to use
  implementation 'org.springframework.ai:spring-ai-openai'
}

Gradle 用户还可以利用 Gradle (5.0+) 原生支持使用 Maven BOM 声明依赖约束,从而使用 Spring AI BOM。这是通过向 Gradle 构建脚本的依赖项部分添加“平台”依赖项处理程序方法来实现的。spring-doc.cadn.net.cn

Spring AI 示例

有关 Spring AI 的更多资源和示例,请参阅此页面spring-doc.cadn.net.cn