—— 阿尔伯特·爱因斯坦

程序员の奇妙冒险

Spring AI AlibabaSpring AI Alibaba人工智能AI
2025-08-07 17:31阅读:136评论:0

基于Spring AI Alibaba 集成通义千问 AI 聊天

创建Spring boot项目 pom配置依赖 application.yml 配置 聊天接口 请求接口 http://localhost:3000/chat/send 响应数据如下 image

_PROTECTED0_PROTECTED2_PROTECTED1__
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
<?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>
 
<groupId>io.github.osinn</groupId>
<artifactId>spring-boot-chat-ai</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-chat-ai</name>
<description>spring-boot-chat-ai</description>
 
<properties>
<java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<spring-ai.version>1.0.0</spring-ai.version>
<spring-ai-alibaba.version>1.0.0.2</spring-ai-alibaba.version>
<spring-boot.version>3.4.5</spring-boot.version>
</properties>
 
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-starter-dashscope</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
 
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-bom</artifactId>
<version>${spring-ai-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<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>
 
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
 
_PROTECTED0__
1
2
3
4
5
6
7
8
9
10
11
12
13
server:
port: 3000
spring:
application:
name: spring-ai-alibaba-example
ai:
dashscope:
# 通义千问 key
api-key: key-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
chat:
options:
model: qwq-plus
 
_PROTECTED0__
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* @author wency_cai
*/
@RestController
public class QWQChatClientController {
 
private static final String DEFAULT_PROMPT = "你好,介绍下你自己!";
 
private final ChatClient chatClient;
 
public QWQChatClientController(ChatModel chatModel) {
this.chatClient = ChatClient.builder(chatModel)
.build();
}
 
@PostMapping("/chat/send")
public Flux<String> streamChat(@RequestParam(value = "input", required = false) String input, HttpServletResponse response) {
response.setCharacterEncoding("UTF-8");
return chatClient.prompt(StringUtils.hasLength(input) ? input : DEFAULT_PROMPT)
.stream()
.content();
}
}
 
  • 请求接口 http://localhost:3000/chat/send
  • 响应数据如下
!image
评论(0)
暂无评论来抢沙发吧~