package com.rf.AIquantum.dialogue.rest; import cn.hutool.core.date.DateUtil; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.auth0.jwt.interfaces.DecodedJWT; import com.rf.AIquantum.base.rest.BaseController; import com.rf.AIquantum.dialogue.dao.model.ChatHistoryEntity; import com.rf.AIquantum.dialogue.dao.model.DialogueEntity; import com.rf.AIquantum.dialogue.service.ChatHistoryService; import com.rf.AIquantum.user.dao.model.UserEntity; import com.rf.AIquantum.user.service.UserService; import com.rf.AIquantum.utils.Constant; import com.rf.AIquantum.utils.JWTUtil; import com.rf.AIquantum.utils.Result; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.util.DigestUtils; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets; import java.util.Date; import java.util.List; import static com.rf.AIquantum.dialogue.rest.DialogueController.HttpClientChat; /** * @Description: 聊天记录相关接口 * @Author: zsy * @Date: 2024/12/4 */ @RestController @RequestMapping("/chat") @Api(tags = "聊天记录相关接口") public class ChatHistoryController extends BaseController { @Autowired private ChatHistoryService chatHistoryService; @GetMapping("/findChats") @ApiOperation(value = "查询聊天记录",notes = "参数包括:pageNum:页码, pageSize:数量, dialogueId:对话id") public Result findChatHistorys(@RequestParam int pageNum, @RequestParam int pageSize, @RequestParam String dialogueId){ Page chatHistoryEntities = chatHistoryService.findByDialogueIdAndStatus(pageNum,pageSize,dialogueId,1); return success(chatHistoryEntities); } @PostMapping("/updateChatHistory") @ApiOperation(value = "修改聊天记录信息(点赞、点踩)",notes = "ChatHistory对象,点赞时只需将endorse的值改为2;点赞时只需将endorse的值改为3,如果有反馈意见放到feedback字段;endorse:1:未评价(默认);2:赞同;3:不赞同") public Result updateChatHistory(@RequestBody String json) { ChatHistoryEntity chatHistoryEntity = JSONObject.parseObject(json,ChatHistoryEntity.class); chatHistoryEntity.setUpdateTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss.SSS")); this.chatHistoryService.save(chatHistoryEntity); return success("修改成功"); } @PostMapping("/refresh") @ApiOperation(value = "刷新某条回答",notes = "ChatHistory对象") public Result refresh(@RequestBody String json) throws UnsupportedEncodingException { ChatHistoryEntity chatHistoryEntity = JSONObject.parseObject(json,ChatHistoryEntity.class); String dialogueId = chatHistoryEntity.getDialogueId(); String createTime = chatHistoryEntity.getCreateTime(); this.chatHistoryService.deleteByDialogueIdAndCreateTime(dialogueId,createTime); //调用模型相关操作 List chatHistoryEntities = this.chatHistoryService.findChatHistoryByDialogueIdAndStatus(dialogueId); JSONArray messages = new JSONArray(); for (ChatHistoryEntity chatHistory : chatHistoryEntities) { JSONArray contents = new JSONArray(); JSONObject jsonText = new JSONObject(); jsonText.put("type","text"); jsonText.put("text",chatHistory.getContent()); if (chatHistory.getImage() != null && !chatHistory.getImage().equals("")) { JSONObject jsonImage = new JSONObject(); jsonImage.put("type","image_url"); JSONObject jsonUrl = new JSONObject(); jsonUrl.put("url",chatHistory.getImage()); jsonImage.put("image_url",jsonUrl); contents.add(jsonImage); } contents.add(jsonText); JSONObject jsonRole = new JSONObject(); jsonRole.put("role",chatHistory.getRole()); jsonRole.put("content",contents); messages.add(jsonRole); } JSONObject jsonChat = new JSONObject(); jsonChat.put("messages",messages); String url = Constant.INVOKE_IP_PROT + Constant.CHAT_PATH; String data = HttpClientChat(jsonChat,url); JSONObject jsonSystem = JSONObject.parseObject(data); if (jsonSystem == null || !jsonSystem.containsKey("response")) { return fail("", "模型服务内部错误"); } String content = jsonSystem.getString("response"); chatHistoryEntity = new ChatHistoryEntity(); chatHistoryEntity.setDialogueId(dialogueId); chatHistoryEntity.setRole("system"); chatHistoryEntity.setContent(content); chatHistoryEntity.setStatus(1); chatHistoryEntity.setEndorse(1); chatHistoryEntity.setCreateTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss.SSS")); chatHistoryEntity.setUpdateTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss.SSS")); chatHistoryEntity = this.chatHistoryService.save(chatHistoryEntity); return success(chatHistoryEntity); } @GetMapping("/clearChats") @ApiOperation(value = "清空聊天记录",notes = "参数包括:dialogueId:对话id") public Result clearChats(@RequestParam String dialogueId){ this.chatHistoryService.deleteByDialogueId(dialogueId); return success(); } @PostMapping("/exportPdf") @ApiOperation(value = "导出PDF") public String exportPdf(@RequestBody String json, HttpServletResponse response) { List chatHistoryEntityList = JSONArray.parseArray(json,ChatHistoryEntity.class); if (chatHistoryEntityList.size() > 0) { return "导出内容为空"; /*UserRecordEntity userRecordEntity = this.userRecordService.getUserRecordById(id); if (userRecordEntity == null) { return "该记录异常,下载失败"; } String PDFPath = generatePDFRecord(userRecordEntity, "pdf"); if (PDFPath.equals("false")) { return "报告版本较早,暂无法下载"; } // List htmlFilePathList = new ArrayList<>(); // htmlFilePathList.add(PDFPath); // // //生成PDF测试记录 // try { // html2pdf(htmlFilePathList); // } catch (Exception e) { // log.error("生成测试文件失败" + e.getMessage()); // e.printStackTrace(); // return "下载失败"; // } // String[] split = userRecordEntity.getFileName().split("/"); // String fileName = split[split.length - 1]; // String PDFName = fileName.replace(".xlsx", ".pdf"); //下载PDF文件 File z = new File("./h2p/export_pdf/PDFReport/" + PDFPath); if (!z.exists()) return "下载失败"; try { FileUtil.downloadFile(response, z, PDFPath, true); return "下载成功"; } catch (UnsupportedEncodingException e) { e.printStackTrace(); return "下载失败"; }*/ }else { return "导出内容为空"; } } }