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:
2
pom.xml
2
pom.xml
@@ -71,7 +71,7 @@
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud.ai</groupId>
|
||||
<artifactId>spring-ai-alibaba-bom</artifactId>
|
||||
<version>1.0.0.2</version>
|
||||
<version>1.0.0.4</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user