写在前面
本篇开始使用Spring Ai,注意Spring Ai支持Spring Boot3.4.x版本,当Spring Boot 3.5.x版本发布时,也将提供支持。
Spring Initializr
前往start.spring.io,选择你想要在新应用中使用的人工智能模型和向量存储。
Artifact Repositories
Milestones - Use Maven Central
从1.0.0-M6版本开始,发行版已在Maven中央仓库提供。无需修改您的构建文件。
Snapshots - Add Snapshot Repositories
要使用Snapshot(以及1.0.0-M6里程碑之前的)版本,你需要在构建文件中添加以下快照仓库。
将以下仓库定义添加到您的Maven或Gradle构建文件中:
Maven
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| <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
1 2 3 4 5 6 7 8 9
| 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/' } }
|
依赖管理
Spring AI物料清单(BOM)声明了特定版本的Spring AI所使用的所有依赖项的推荐版本。这是一个仅包含BOM的版本,它只包含依赖项管理,不包含插件声明或对Spring或Spring Boot的直接引用。你可以使用Spring Boot父POM,或者使用Spring Boot的BOM(<font style="color:rgb(25, 30, 30);">spring-boot-dependencies</font>)来管理Spring Boot版本。
将BOM添加到您的项目中:
Maven
1 2 3 4 5 6 7 8 9 10 11
| <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
1 2 3 4 5
| dependencies { implementation platform("org.springframework.ai:spring-ai-bom:1.0.0") //用希望使用的特定模块的启动器依赖项替换以下内容 implementation 'org.springframework.ai:spring-ai-openai' }
|
Gradle用户也可以通过利用Gradle(5.0及以上版本)对使用Maven BOM声明依赖约束的原生支持来使用Spring AI BOM。这可以通过在Gradle构建脚本的依赖项部分添加一个“platform”依赖处理方法来实现。
添加特定组件的依赖
文档中的以下各节显示了您需要添加到项目构建系统所需的依赖。
- 聊天模型
- 嵌入模型
- 图像生成模型
- 转录模型
- 文本转语音(TTS)模型
- 向量数据库
示例项目
开发者可以在GitHub上克隆这些项目以开始。
github.com/tzolov/playground-flight-booking
一个AI驱动的系统,可以访问条款和条件(检索增强生成,RAG),访问执行操作的工具(Java方法)(函数调用)并使用大型语言模型与用户交互。
github.com/rd-1-2022/ai-openai-helloworld
github.com/rd-1-2022/ai-azure-openai-helloworld
github.com/Azure-Samples/spring-ai-azure-workshop
入门项目
新建一个名为spring-ai-fly的项目,使用Spring Boot3.5.7 + JDK21版本,然后在其POM文件中新增如下依赖信息:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.5.7</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.gutsyzhan</groupId> <artifactId>spring-ai-fly</artifactId> <version>0.0.1-SNAPSHOT</version> <name>spring-ai-fly</name> <description>spring-ai-fly</description> <url/> <licenses> <license/> </licenses> <developers> <developer/> </developers> <scm> <connection/> <developerConnection/> <tag/> <url/> </scm> <properties> <java.version>21</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
<dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-starter-model-openai</artifactId> </dependency> </dependencies>
<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>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
</project>
|
我们使用的模式是qwen-plus,因此需要在application.yml配置文件中新增如下配置信息:
1 2 3 4 5 6 7 8 9 10 11 12
| spring: application: name: spring-ai-fly ai: openai: api-key: sk-3333333333333fc2a8948f3116 base-url: https://dashscope.aliyuncs.com/compatible-mode chat: options: model: qwen-plus server: port: 8090
|
后面的代码将会在此项目中进行编写。