Files
snails-ai-backend/src/main/java/com/hanserwei/snailsai/controller/DashScopeController.java
Hanserwei 40c05838f7 feat(user): 添加用户查询工具和测试数据接口
- 新增 QueryTool 类,提供 findAll 和 findAllByIdIn 方法用于查询用户
- 在 ChatClientConfiguration 中注册 QueryTool 为默认工具
- 创建 TestDataController,提供生成测试用户数据的接口- 新增 UserService 和 UserRepository,实现用户数据的批量插入和查询功能
- 将 ChatMessageDTO 从 model 包移动到 dto 包,优化包结构
-为 UserEntity 添加 createTime 和 updateTime 字段,完善实体类审计信息
- 新增 RedisConfig 配置类,为后续 Redis 功能做准备
2025-10-23 18:08:30 +08:00

42 lines
1.5 KiB
Java

package com.hanserwei.snailsai.controller;
import com.hanserwei.snailsai.model.AIResponse;
import com.hanserwei.snailsai.dto.ChatMessageDTO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
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.web.bind.annotation.*;
import reactor.core.publisher.Flux;
@Slf4j
@RequestMapping("/dashscope")
@RestController
@CrossOrigin
public class DashScopeController {
@Resource
private ChatClient dashScopeChatClient;
@PostMapping(value = "/generateStream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<AIResponse> generateStream(@RequestBody ChatMessageDTO chatMessageDTO) {
// 构建提示词
Prompt prompt = new Prompt(new UserMessage(chatMessageDTO.getMessage()));
// 流式输出
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();
});
}
}