feat(dashscope): 实现流式聊天响应

- 升级 spring-ai-alibaba-bom 版本至1.0.0.4- 修改 /chat 接口支持流式返回
- 使用 Flux<String> 替代 String 返回类型
- 设置 produces 为 TEXT_EVENT_STREAM_VALUE 支持 SSE- 调用 stream() 方法替代 call() 实现流式输出
This commit is contained in:
hanserwei
2025-10-22 15:30:05 +08:00
parent 4207c85202
commit 2861f7b613
2 changed files with 6 additions and 4 deletions

View File

@@ -71,7 +71,7 @@
<dependency> <dependency>
<groupId>com.alibaba.cloud.ai</groupId> <groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-bom</artifactId> <artifactId>spring-ai-alibaba-bom</artifactId>
<version>1.0.0.2</version> <version>1.0.0.4</version>
<type>pom</type> <type>pom</type>
<scope>import</scope> <scope>import</scope>
</dependency> </dependency>

View File

@@ -2,10 +2,12 @@ package com.hanserwei.snailsai.controller;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient; import org.springframework.ai.chat.client.ChatClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
@RequestMapping("/dashscope") @RequestMapping("/dashscope")
@RestController @RestController
@@ -14,11 +16,11 @@ public class DashScopeController {
@Resource @Resource
private ChatClient dashScopeChatClient; private ChatClient dashScopeChatClient;
@GetMapping("/chat") @GetMapping(value = "/chat",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public String chat(@RequestParam("userPrompt") String userPrompt) { public Flux<String> chat(@RequestParam("userPrompt") String userPrompt) {
return dashScopeChatClient.prompt() return dashScopeChatClient.prompt()
.user(userPrompt) .user(userPrompt)
.call() .stream()
.content(); .content();
} }
} }