Spring AI 入门

前置知识参考:Spring AI 中文网
用于快速接入大模型

以下是 Spring AI、Spring Boot 与 JDK 的版本兼容性整理:

Spring AI 版本 Spring Boot 版本 JDK 版本
1.0.x 3.1.x - 3.5.x Java 17
1.1.x 3.5.x Java 17
2.x 4.0.x 及以上 Java 21

大模型选型

  1. 自研大模型,AI算法岗(985,211)
  2. 云端大模型,阿里百炼
  3. 本地 ollama 部署开源大模型

github 大模型评分

调用大模型

使用百炼大模型创建API-KEY
导入maven:

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
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.3</version>
<relativePath/>
</parent>
<properties>
<java.version>17</java.version>
<spring-ai.version>1.0.0-M6</spring-ai.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>${spring-ai.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
</dependencies>

设置application.yml配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
spring:
application:
name: spring-ai-promote
ai:
openai:
#去掉v1,springai会自动加。加上的话在运行时会报404 NOT_FOUND from POST https://dashscope.aliyuncs.com/compatible-mode/v1/v1/chat/completions
baseurl: https://dashscope.aliyuncs.com/compatible-mode
# 为了防止泄密,这里建议使用环境变量的方式,将 API Key 配置在环境变量中
api-key: ${OPENAI_API_KEY}
chat:
options:
#这个模型支持非思考模式,适用于快速响应的场景
model: qwen-max