入門指南

本節提供瞭如何開始使用 Spring AI 的指引。

您應根據需要遵循以下各節中的步驟。

Spring AI 支援 Spring Boot 3.4.x。Spring Boot 3.5.x 釋出後,我們也將支援它。

Spring Initializr

前往 start.spring.io 並選擇您希望在新應用中使用的 AI 模型和向量資料庫。

Artifact 倉庫

里程碑版本 - 使用 Maven Central

從 1.0.0-M6 版本開始,釋出版本已在 Maven Central 提供。您的構建檔案無需任何更改。

Snapshot 版本 - 新增 Snapshot 倉庫

要使用 Snapshot 版本(以及 1.0.0-M6 之前的里程碑版本),您需要在構建檔案中新增以下 Snapshot 倉庫。

將以下倉庫定義新增到您的 Maven 或 Gradle 構建檔案中

Maven
<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>
Gradle
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 Snapshot 版本時,請注意您的 Maven 映象配置。如果您在 settings.xml 中配置瞭如下所示的映象:

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

萬用字元 * 將把所有倉庫請求重定向到您的映象,從而阻止訪問 Spring Snapshot 倉庫。要解決此問題,請修改 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 Snapshot 倉庫,同時仍使用您的映象獲取其他依賴項。

依賴管理

Spring AI 物料清單 (BOM) 聲明瞭給定 Spring AI 版本所使用的所有依賴項的推薦版本。這是一個僅包含 BOM 的版本,它只包含依賴管理,不包含外掛宣告或對 Spring 或 Spring Boot 的直接引用。您可以使用 Spring Boot 父級 POM,或者使用 Spring Boot 的 BOM(spring-boot-dependencies)來管理 Spring Boot 版本。

將 BOM 新增到您的專案

Maven
<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>
Gradle
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 構建指令碼的 dependencies 部分新增 'platform' 依賴處理方法來實現。

新增特定元件的依賴項

文件中的以下各節都說明了您需要新增到專案構建系統中的依賴項。

Spring AI 示例

請參閱 此頁面 以獲取更多與 Spring AI 相關的資源和示例。