| 
					
				 | 
			
			
				@@ -0,0 +1,181 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+package com.usky.ai.controller.web; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.alibaba.dashscope.aigc.generation.Generation; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.alibaba.dashscope.aigc.generation.GenerationParam; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.alibaba.dashscope.aigc.generation.GenerationResult; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.alibaba.dashscope.common.Message; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.alibaba.dashscope.common.Role; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.alibaba.dashscope.exception.InputRequiredException; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.alibaba.dashscope.exception.NoApiKeyException; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.usky.ai.mapper.AiQuestionMapper; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.usky.ai.service.AiQuestion; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.usky.common.security.utils.SecurityUtils; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import lombok.extern.slf4j.Slf4j; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.springframework.beans.factory.annotation.Autowired; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.springframework.beans.factory.annotation.Value; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.springframework.web.bind.annotation.*; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import javax.annotation.Resource; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.time.LocalDateTime; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.List; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.stream.Collectors; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+@Slf4j 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+@RestController 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+@RequestMapping("/ai") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+public class AiQuestionController { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Value("${ai.api.key}") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private String apiKey; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Resource 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private Generation generation; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private AiQuestionMapper aiQuestionMapper; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    // 查询所有数据 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @GetMapping("/all") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public List<AiQuestion> getAllQuestions() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return aiQuestionMapper.findAll(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    // 根据 userId 查询数据 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @GetMapping("/user/{userId}") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public List<AiQuestion> getQuestionsByUserId(@PathVariable Long userId) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return aiQuestionMapper.findByUserId(userId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    // 根据 id 删除数据 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @DeleteMapping("/deleted/{id}") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public String deleteQuestion(@PathVariable Long id) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        aiQuestionMapper.deleteById(id); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return "Question with id " + id + " deleted successfully."; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    // 根据 userId 和 sessionId 查询数据 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @GetMapping("/session/{sessionId}/user/{userId}") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public List<AiQuestion> getQuestionsByUserIdAndSessionId(@PathVariable String sessionId, @PathVariable Long userId) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return aiQuestionMapper.findByUserIdAndSessionId(sessionId, userId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    //阿里百炼通义千问大模型 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @PostMapping(value = "/aliTyqw") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public String send1(@RequestBody String content, @RequestParam(required = false) String sessionId) throws NoApiKeyException, InputRequiredException { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        //获取当前登录用户的信息 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Long userId = SecurityUtils.getUserId(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        String userName = SecurityUtils.getLoginUser().getSysUser().getNickName(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // 如果没有传入 sessionId,则创建一个新的会话ID 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (sessionId == null || sessionId.isEmpty()) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            sessionId = java.util.UUID.randomUUID().toString(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // 获取当前用户的对话历史 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        log.info("会话ID: {}, 用户ID: {}", sessionId, userId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<AiQuestion> conversationHistory = aiQuestionMapper.findByUserIdAndSessionId(sessionId, userId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // 构建对话历史消息 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<Message> messages = conversationHistory.stream() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .map(q -> Message.builder() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        .role(q.getUserId().equals(userId) ? Role.USER.getValue() : Role.ASSISTANT.getValue()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        .content(q.getUserId().equals(userId) ? q.getQuestion() : q.getAnswer()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        .build()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .collect(Collectors.toList()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // 用户与模型的对话历史 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // 添加用户的新消息 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Message userMessage = Message.builder() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .role(Role.USER.getValue()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .content(content) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .build(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        messages.add(userMessage); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        GenerationParam param = GenerationParam.builder() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .model("qwen-turbo") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .messages(messages) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .resultFormat(GenerationParam.ResultFormat.MESSAGE) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .topP(0.8) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .apiKey(apiKey) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .enableSearch(true) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .build(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        GenerationResult generationResult = generation.call(param); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // 获取回答内容 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        String answer = generationResult.getOutput().getChoices().get(0).getMessage().getContent(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // 创建实体并保存到数据库 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        AiQuestion aiQuestion = new AiQuestion(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        aiQuestion.setModel("qwen-turbo"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        aiQuestion.setSessionId(sessionId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        aiQuestion.setUserId(userId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        aiQuestion.setUserName(userName); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        aiQuestion.setQuestion(content); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        aiQuestion.setAnswer(answer); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        aiQuestion.setAskTime(LocalDateTime.now()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        aiQuestionMapper.save(aiQuestion); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return answer; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    //阿里百炼DeepSeek大模型 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @PostMapping(value = "/aliDeepSeek") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public String send2(@RequestBody String content, @RequestParam(required = false) String sessionId) throws NoApiKeyException, InputRequiredException { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        //获取当前登录用户的信息 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Long userId = SecurityUtils.getUserId(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        String userName = SecurityUtils.getLoginUser().getSysUser().getNickName(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // 如果没有传入 sessionId,则创建一个新的会话ID 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (sessionId == null || sessionId.isEmpty()) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            sessionId = java.util.UUID.randomUUID().toString(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // 获取当前用户的对话历史 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        log.info("会话ID: {}, 用户ID: {}", sessionId, userId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<AiQuestion> conversationHistory = aiQuestionMapper.findByUserIdAndSessionId(sessionId, userId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // 构建对话历史消息 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<Message> messages = conversationHistory.stream() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .map(q -> Message.builder() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        .role(q.getUserId().equals(userId) ? Role.USER.getValue() : Role.ASSISTANT.getValue()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        .content(q.getUserId().equals(userId) ? q.getQuestion() : q.getAnswer()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        .build()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .collect(Collectors.toList()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // 用户与模型的对话历史 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // 添加用户的新消息 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Message userMessage = Message.builder() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .role(Role.USER.getValue()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .content(content) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .build(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        messages.add(userMessage); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        GenerationParam param = GenerationParam.builder() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .model("deepseek-v3") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .messages(messages) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .resultFormat(GenerationParam.ResultFormat.MESSAGE) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .apiKey(apiKey) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .build(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        GenerationResult generationResult = generation.call(param); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // 获取回答内容 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        String answer = generationResult.getOutput().getChoices().get(0).getMessage().getContent(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // 创建实体并保存到数据库 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        AiQuestion aiQuestion = new AiQuestion(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        aiQuestion.setModel("deepseek-v3"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        aiQuestion.setSessionId(sessionId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        aiQuestion.setUserId(userId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        aiQuestion.setUserName(userName); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        aiQuestion.setQuestion(content); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        aiQuestion.setAnswer(answer); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        aiQuestion.setAskTime(LocalDateTime.now()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        aiQuestionMapper.save(aiQuestion); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return answer; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 |