3.Spring AI 中文翻译手册之 如何创建您的第一个人工智能应用程序?
Spring AI中文翻译手册之 如何创建您的第一个人工智能应用程序?
1.1 入门
本节提供使用 Spring AI 的入门指南。
Spring AI 支持 Spring Boot 3.4.x 和 3.5.x。
1.2 Spring Initializr 初始化项目
前往start.spring.io,选择您想在新应用程序中使用的 AI 模型和向量存储。
1.2.1 制品仓库
1.2.1.1 发布版本 - 使用 Maven Central
Spring AI 1.0.0 及更高版本已在 Maven Central 上架。无需额外的仓库配置。只需确保已在构建文件中启用 Maven Central 即可。
如果使用的是maven:
<!-- 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>
如果使用的是gradle:
repositories {
mavenCentral()
}
1.2.1.2 快照 - 添加快照存储库
要使用最新的开发版本(例如1.1.0-SNAPSHOT)或 1.0.0 之前的较旧里程碑版本,您需要在构建文件中添加以下快照存储库。
将以下仓库定义添加到您的 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 快照时,请注意 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 快照存储库,同时仍然使用您的镜像来获取其他依赖项。
1.3 依赖关系管理
Spring AI 的物料清单 (BOM) 声明了特定 Spring AI 版本所使用的所有依赖项的推荐版本。这是一个仅包含 BOM 的版本,它只包含依赖项管理,不包含插件声明或对 Spring 或 Spring Boot 的直接引用。您可以使用 Spring Boot 父 POM,或者使用 Spring Boot 自身的 BOMspring-boot-dependencies来管理 Spring Boot 版本。
将物料清单添加到您的项目中:
如果使用的是Maven
<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>
如果使用的是gradle:
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 构建脚本的 dependencies 部分添加一个 ‘platform’ 依赖处理方法来实现。
1.4 添加特定组件的依赖项
文档中的以下每个部分都说明了你需要向项目构建系统中添加哪些依赖项。
1.5 Spring AI 示例
请参阅此页面以获取更多与 Spring AI 相关的资源和示例。
更多推荐

所有评论(0)