入門
本節提供有關如何開始使用 Spring AI 的切入點。
您應根據自己的需求遵循以下各節中的步驟。
| Spring AI 支援 Spring Boot 3.4.x 和 3.5.x。 |
Spring Initializr
前往 start.spring.io 並選擇您要在新應用程式中使用的 AI 模型和向量儲存。
Artifact 倉庫
釋出版 - 使用 Maven Central
Spring AI 1.0.0 及更高版本可在 Maven Central 中獲取。無需額外的倉庫配置。只需確保您的構建檔案中已啟用 Maven Central。
-
Maven
-
Gradle
<!-- 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 之前的舊里程碑版本,您需要在構建檔案中新增以下快照倉庫。
將以下倉庫定義新增到您的 Maven 或 Gradle 構建檔案中
-
Maven
-
Gradle
<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 中配置瞭如下映象
<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 快照倉庫,同時仍將您的映象用於其他依賴項。
依賴管理
Spring AI 物料清單 (BOM) 聲明瞭 Spring AI 特定版本所使用的所有依賴項的推薦版本。這是一個僅限 BOM 的版本,它只包含依賴管理,不包含外掛宣告或對 Spring 或 Spring Boot 的直接引用。您可以使用 Spring Boot 父 POM,或使用 Spring Boot 的 BOM (spring-boot-dependencies) 來管理 Spring Boot 版本。
將 BOM 新增到您的專案
-
Maven
-
Gradle
<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 AI 示例
請參閱此頁面,獲取更多與 Spring AI 相關的資源和示例。