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

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