|
@@ -0,0 +1,92 @@
|
|
|
+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 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.Arrays;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+@RestController
|
|
|
+@RequestMapping("/ai")
|
|
|
+public class AliAiController {
|
|
|
+
|
|
|
+ @Value("${ai.api.key}")
|
|
|
+ private String apiKey;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private Generation generation;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AiQuestionMapper aiQuestionMapper;
|
|
|
+
|
|
|
+ @PostMapping(value = "/aliTyqw")
|
|
|
+ public String send(@RequestBody String content) throws NoApiKeyException, InputRequiredException {
|
|
|
+
|
|
|
+ // 用户与模型的对话历史
|
|
|
+ Message userMessage = Message.builder()
|
|
|
+ .role(Role.USER.getValue())
|
|
|
+ .content(content)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ GenerationParam param = GenerationParam.builder()
|
|
|
+ .model("qwen-turbo")
|
|
|
+ .messages(Arrays.asList(userMessage))
|
|
|
+ .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();
|
|
|
+
|
|
|
+ //获取当前登录用户的信息
|
|
|
+ Long userId = SecurityUtils.getUserId();
|
|
|
+ String userName = SecurityUtils.getUsername();
|
|
|
+
|
|
|
+ // 创建实体并保存到数据库
|
|
|
+ AiQuestion aiQuestion = new AiQuestion();
|
|
|
+ aiQuestion.setUserId(userId);
|
|
|
+ aiQuestion.setUserName(userName);
|
|
|
+ aiQuestion.setQuestion(content);
|
|
|
+ aiQuestion.setAnswer(answer);
|
|
|
+ aiQuestion.setAskTime(LocalDateTime.now());
|
|
|
+ aiQuestionMapper.save(aiQuestion);
|
|
|
+
|
|
|
+ return answer;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查询所有数据
|
|
|
+ @GetMapping("/questions")
|
|
|
+ public List<AiQuestion> getAllQuestions() {
|
|
|
+ return aiQuestionMapper.findAll();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 根据 userId 查询数据
|
|
|
+ @GetMapping("/questions/user/{userId}")
|
|
|
+ public List<AiQuestion> getQuestionsByUserId(@PathVariable Long userId) {
|
|
|
+ return aiQuestionMapper.findByUserId(userId);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 根据 id 删除数据
|
|
|
+ @DeleteMapping("/questions/deleted/{id}")
|
|
|
+ public String deleteQuestion(@PathVariable Long id) {
|
|
|
+ aiQuestionMapper.deleteById(id);
|
|
|
+ return "Question with id " + id + " deleted successfully.";
|
|
|
+ }
|
|
|
+}
|