此版本仍在开发中,尚未被视为稳定版。如需最新的快照版本,请使用 Spring AI 1.1.3spring-doc.cadn.net.cn

快速开始

此部分提供了入门Spring AI的相关起点。spring-doc.cadn.net.cn

您应根据需要遵循每个部分中的步骤。spring-doc.cadn.net.cn

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

Spring Initializr

请前往start.spring.io,并选择您希望在新应用中使用的AI模型和向量存储。spring-doc.cadn.net.cn

artifact repositories

发布版本 - 使用Maven中央仓库

Spring AI 1.0.0 及其后续版本已在 Maven Central 上可用。 无需额外的仓库配置。请确保在构建文件中启用了 Maven Central。spring-doc.cadn.net.cn

<!-- 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-SNAPSHOT)或在1.0.0之前的里程碑版本,您需要在构建文件中添加以下快照仓库。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 Bill of Materials (BOM) 声明了一个特定版本的 Spring AI 使用的所有依赖项的推荐版本。 这只是一个 BOM 版本,仅包含依赖管理而不包含任何插件声明或直接引用 Spring 或 Spring Boot。 您可以使用 Spring Boot 父 POM,或者使用来自 Spring Boot (spring-boot-dependencies) 的 BOM 来管理 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</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+)原生支持的声明依赖约束功能,使用 Spring AI BOM。这是通过在 Gradle 构建脚本的依赖部分添加一个 'platform' 依赖处理方法实现的。spring-doc.cadn.net.cn

Spring AI 样例

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