入门指南

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

你应该按照以下步骤,在每个后续章节中,根据您的需求遵循。spring-doc.cadn.net.cn

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

Spring Boot 初始化器Spring Boot 初始化器

前往 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()
}

快照版本 - 添加快照库Snapshot - Add Snapshot Repositories

要使用最新的开发版本(例如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号Maven镜像配置中设置了类似以下内容: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</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 BOM 来声明依赖约束,来使用 Spring AI BOM。这是通过在你的 Gradle 构建脚本的依赖部分添加一个 '平台' 依赖处理器方法来实现的。spring-doc.cadn.net.cn

Spring AI 样本

请参考this page以获取更多关于Spring AI的资源和样本。spring-doc.cadn.net.cn