feat(dashscope): 实现流式响应接口并返回AIResponse对象
- 新增AIResponse模型类用于封装流式响应内容- 修改DashScopeController以支持流式输出 - 将原有的chat方法改为generateStream方法 - 使用Prompt构建提示词并获取流式响应- 映射chatResponse到AIResponse对象- 添加@Slf4j注解以支持日志记录- 引入必要的Spring AI相关类和Lombok注解
This commit is contained in:
@@ -1,13 +1,19 @@
|
|||||||
package com.hanserwei.snailsai.controller;
|
package com.hanserwei.snailsai.controller;
|
||||||
|
|
||||||
|
import com.hanserwei.snailsai.model.AIResponse;
|
||||||
import com.hanserwei.snailsai.model.ChatMessageDTO;
|
import com.hanserwei.snailsai.model.ChatMessageDTO;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.ai.chat.client.ChatClient;
|
import org.springframework.ai.chat.client.ChatClient;
|
||||||
import org.springframework.ai.chat.memory.ChatMemory;
|
import org.springframework.ai.chat.memory.ChatMemory;
|
||||||
|
import org.springframework.ai.chat.messages.UserMessage;
|
||||||
|
import org.springframework.ai.chat.model.Generation;
|
||||||
|
import org.springframework.ai.chat.prompt.Prompt;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import reactor.core.publisher.Flux;
|
import reactor.core.publisher.Flux;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
@RequestMapping("/dashscope")
|
@RequestMapping("/dashscope")
|
||||||
@RestController
|
@RestController
|
||||||
@CrossOrigin
|
@CrossOrigin
|
||||||
@@ -16,12 +22,20 @@ public class DashScopeController {
|
|||||||
@Resource
|
@Resource
|
||||||
private ChatClient dashScopeChatClient;
|
private ChatClient dashScopeChatClient;
|
||||||
|
|
||||||
@PostMapping(value = "/chat",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
|
@PostMapping(value = "/generateStream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
|
||||||
public Flux<String> chat(@RequestBody ChatMessageDTO chatMessageDTO) {
|
public Flux<AIResponse> generateStream(@RequestBody ChatMessageDTO chatMessageDTO) {
|
||||||
return dashScopeChatClient.prompt()
|
// 构建提示词
|
||||||
.user(chatMessageDTO.getMessage())
|
Prompt prompt = new Prompt(new UserMessage(chatMessageDTO.getMessage()));
|
||||||
.advisors(e-> e.param(ChatMemory.CONVERSATION_ID,chatMessageDTO.getConversionId()))
|
|
||||||
.stream()
|
// 流式输出
|
||||||
.content();
|
return dashScopeChatClient.prompt(prompt)
|
||||||
|
.advisors(a -> a.param(ChatMemory.CONVERSATION_ID, chatMessageDTO.getConversionId()))
|
||||||
|
.stream() // 流式输出
|
||||||
|
.chatResponse()
|
||||||
|
.mapNotNull(chatResponse -> {
|
||||||
|
Generation generation = chatResponse.getResult();
|
||||||
|
String text = generation.getOutput().getText();
|
||||||
|
return AIResponse.builder().v(text).build();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
15
src/main/java/com/hanserwei/snailsai/model/AIResponse.java
Normal file
15
src/main/java/com/hanserwei/snailsai/model/AIResponse.java
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package com.hanserwei.snailsai.model;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class AIResponse {
|
||||||
|
// 流式响应内容
|
||||||
|
private String v;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user