From 2861f7b61391576322338f6fd9ecc8d1134565cc Mon Sep 17 00:00:00 2001 From: hanserwei Date: Wed, 22 Oct 2025 15:30:05 +0800 Subject: [PATCH] =?UTF-8?q?feat(dashscope):=20=E5=AE=9E=E7=8E=B0=E6=B5=81?= =?UTF-8?q?=E5=BC=8F=E8=81=8A=E5=A4=A9=E5=93=8D=E5=BA=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 升级 spring-ai-alibaba-bom 版本至1.0.0.4- 修改 /chat 接口支持流式返回 - 使用 Flux 替代 String 返回类型 - 设置 produces 为 TEXT_EVENT_STREAM_VALUE 支持 SSE- 调用 stream() 方法替代 call() 实现流式输出 --- pom.xml | 2 +- .../snailsai/controller/DashScopeController.java | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index 163598e..8ee3128 100644 --- a/pom.xml +++ b/pom.xml @@ -71,7 +71,7 @@ com.alibaba.cloud.ai spring-ai-alibaba-bom - 1.0.0.2 + 1.0.0.4 pom import diff --git a/src/main/java/com/hanserwei/snailsai/controller/DashScopeController.java b/src/main/java/com/hanserwei/snailsai/controller/DashScopeController.java index 16ca13c..21bb4b5 100644 --- a/src/main/java/com/hanserwei/snailsai/controller/DashScopeController.java +++ b/src/main/java/com/hanserwei/snailsai/controller/DashScopeController.java @@ -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 chat(@RequestParam("userPrompt") String userPrompt) { return dashScopeChatClient.prompt() .user(userPrompt) - .call() + .stream() .content(); } }