ChatHistoryController.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. package com.rf.AIquantum.dialogue.rest;
  2. import cn.hutool.core.date.DateUtil;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.rf.AIquantum.base.rest.BaseController;
  6. import com.rf.AIquantum.dao.dto.SseResultDataDto;
  7. import com.rf.AIquantum.dialogue.dao.model.ChatHistoryEntity;
  8. import com.rf.AIquantum.dialogue.service.ChatHistoryService;
  9. import com.rf.AIquantum.filter.JwtIgnore;
  10. import com.rf.AIquantum.param.model.ParamEntry;
  11. import com.rf.AIquantum.param.service.ParamService;
  12. import com.rf.AIquantum.utils.Constant;
  13. import com.rf.AIquantum.utils.Result;
  14. import com.rf.AIquantum.utils.SseEmitterService;
  15. import io.swagger.annotations.Api;
  16. import io.swagger.annotations.ApiOperation;
  17. import lombok.extern.slf4j.Slf4j;
  18. import okhttp3.MediaType;
  19. import okhttp3.OkHttpClient;
  20. import okhttp3.Request;
  21. import okhttp3.Response;
  22. import okio.BufferedSource;
  23. import org.apache.commons.lang.StringUtils;
  24. import org.springframework.beans.factory.annotation.Autowired;
  25. import org.springframework.beans.factory.annotation.Value;
  26. import org.springframework.data.domain.Page;
  27. import org.springframework.web.bind.annotation.*;
  28. import javax.servlet.http.HttpServletResponse;
  29. import java.io.BufferedReader;
  30. import java.io.IOException;
  31. import java.util.Collections;
  32. import java.util.Date;
  33. import java.util.List;
  34. import java.util.concurrent.TimeUnit;
  35. /**
  36. * @Description: 聊天记录相关接口
  37. * @Author: zsy
  38. * @Date: 2024/12/4
  39. */
  40. @RestController
  41. @RequestMapping("/chat")
  42. @Slf4j
  43. @Api(tags = "聊天记录相关接口")
  44. public class ChatHistoryController extends BaseController {
  45. @Autowired
  46. private ChatHistoryService chatHistoryService;
  47. @Autowired
  48. private SseEmitterService sseEmitterService;
  49. @Value("${chat_url}")
  50. private String chat_url;
  51. @Autowired
  52. private ParamService paramService;
  53. @GetMapping("/findChats")
  54. @ApiOperation(value = "查询聊天记录", notes = "参数包括:pageNum:页码, pageSize:数量, dialogueId:对话id")
  55. public Result findChatHistorys(@RequestParam int pageNum, @RequestParam int pageSize, @RequestParam String dialogueId) {
  56. Page<ChatHistoryEntity> chatHistoryEntities = chatHistoryService.findByDialogueIdAndStatus(pageNum, pageSize, dialogueId, 1);
  57. return success(chatHistoryEntities);
  58. }
  59. @PostMapping("/updateChatHistory")
  60. @ApiOperation(value = "修改聊天记录信息(点赞、点踩)", notes = "ChatHistory对象,点赞时只需将endorse的值改为2;点赞时只需将endorse的值改为3,如果有反馈意见放到feedback字段;endorse:1:未评价(默认);2:赞同;3:不赞同")
  61. public Result updateChatHistory(@RequestBody String json) {
  62. ChatHistoryEntity chatHistoryEntity = JSONObject.parseObject(json, ChatHistoryEntity.class);
  63. chatHistoryEntity.setUpdateTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
  64. this.chatHistoryService.save(chatHistoryEntity);
  65. return success("修改成功");
  66. }
  67. @PostMapping("/refresh")
  68. @JwtIgnore
  69. @ApiOperation(value = "刷新某条回答", notes = "ChatHistory对象")
  70. public Result refresh(@RequestBody String json) {
  71. ChatHistoryEntity chatHistoryEntity = JSONObject.parseObject(json, ChatHistoryEntity.class);
  72. String dialogueId = chatHistoryEntity.getDialogueId();
  73. String createTime = chatHistoryEntity.getCreateTime();
  74. this.chatHistoryService.deleteByDialogueIdAndCreateTime(dialogueId, createTime);
  75. //调用模型相关操作
  76. List<ChatHistoryEntity> chatHistoryEntities = this.chatHistoryService.findChatHistoryByDialogueIdAndStatus(dialogueId);
  77. JSONArray messages = new JSONArray();
  78. int length = 0;
  79. for (int i=chatHistoryEntities.size() -1 ;i>=0;i--) {
  80. ChatHistoryEntity chatHistory = chatHistoryEntities.get(i);
  81. JSONArray contents = new JSONArray();
  82. JSONObject jsonText = new JSONObject();
  83. jsonText.put("type", "text");
  84. String text = chatHistory.getContent();
  85. if(text.contains("</think>")){
  86. text = text.substring(text.indexOf("</think>")+8);
  87. }
  88. if(length + text.length()>Constant.MESSAGE_MAX_LENGTH){
  89. break;
  90. }
  91. length += text.length();
  92. if (chatHistory.getImage() != null && !chatHistory.getImage().equals("")) {
  93. if(length+chatHistory.getImage().length()>Constant.MESSAGE_MAX_LENGTH){
  94. break;
  95. }
  96. JSONObject jsonImage = new JSONObject();
  97. jsonImage.put("type", "image_url");
  98. JSONObject jsonUrl = new JSONObject();
  99. jsonUrl.put("url", chatHistory.getImage());
  100. length += chatHistory.getImage().length();
  101. jsonImage.put("image_url", jsonUrl);
  102. contents.add(jsonImage);
  103. }
  104. jsonText.put("text", text);
  105. contents.add(jsonText);
  106. JSONObject jsonRole = new JSONObject();
  107. jsonRole.put("role", chatHistory.getRole());
  108. jsonRole.put("content", contents);
  109. messages.add(jsonRole);
  110. }
  111. System.out.println("Message length: " + length);
  112. log.info("Message length: " + length);
  113. //反转顺序
  114. int size = messages.size();
  115. for (int i = 0; i< size; i++){
  116. Object temp = messages.get(i);
  117. messages.set(i,messages.get(size-1-i));
  118. messages.set(size-1-i,temp);
  119. }
  120. JSONObject jsonChat = new JSONObject();
  121. jsonChat.put("messages", messages);
  122. jsonChat.put("stream",true);
  123. // String url = Constant.INVOKE_IP_PROT + Constant.CHAT_PATH;
  124. // String data = HttpClientChat(jsonChat,url);
  125. // JSONObject jsonSystem = JSONObject.parseObject(data);
  126. // if (jsonSystem == null || !jsonSystem.containsKey("response")) {
  127. // return fail("", "模型服务内部错误");
  128. // }
  129. // String content = jsonSystem.getString("response");
  130. SseResultDataDto sseResultDataDto = new SseResultDataDto();
  131. sseResultDataDto.setDialogueId(dialogueId);
  132. OkHttpClient client = new OkHttpClient.Builder().connectTimeout(60, TimeUnit.SECONDS).writeTimeout(60, TimeUnit.SECONDS).readTimeout(60, TimeUnit.SECONDS).build();
  133. StringBuilder stringBuilder = new StringBuilder();
  134. // 创建请求体
  135. okhttp3.RequestBody requestBody = okhttp3.RequestBody.create(jsonChat.toJSONString(), MediaType.parse("application/json"));
  136. ParamEntry paramEntry = this.paramService.findByParamName("chat_url");
  137. if (paramEntry != null) {
  138. chat_url = paramEntry.getParamValue();
  139. }
  140. // 创建请求
  141. Request request = new Request.Builder().header("Content-Type", "application/json").header("Accept", "text/event-stream")
  142. .url(chat_url + Constant.CHAT_PATH)
  143. .post(requestBody)
  144. .build();
  145. // 发送请求并处理响应
  146. try (Response response = client.newCall(request).execute()) {
  147. if (!response.isSuccessful()) {
  148. log.error("Unexpected code " + response);
  149. return fail();
  150. }
  151. // 获取响应流
  152. okhttp3.ResponseBody responseBody = response.body();
  153. String flag = "think";
  154. if (responseBody != null) {
  155. BufferedSource bufferedSource = responseBody.source();
  156. try {
  157. String line;
  158. byte[] buffer = new byte[1024];
  159. while (!bufferedSource.exhausted()) {
  160. int bytesRead = bufferedSource.read(buffer);
  161. line = new String(buffer, 0, bytesRead);
  162. if (sseEmitterService.getEmitter(dialogueId) == null) {
  163. if (flag.equals("think")) {
  164. stringBuilder.append("</").append(flag).append(">");
  165. }
  166. break;
  167. }
  168. sseResultDataDto.setType(flag);
  169. System.out.println("line==" + line);
  170. JSONObject jsonObject = null;
  171. try {
  172. if(StringUtils.isNotEmpty(line)){
  173. jsonObject=JSONObject.parseObject(line);
  174. }
  175. } catch (Exception e) {
  176. log.error(line+"json解析失败",e);
  177. continue;
  178. }
  179. String event = jsonObject.getString("event");
  180. if(event.equals("error")){
  181. if (flag.equals("think")) {
  182. stringBuilder.append("</").append(flag).append(">");
  183. }
  184. break;
  185. }
  186. String data = jsonObject.getString("data");
  187. if (StringUtils.isNotEmpty(data)) {
  188. stringBuilder.append(data);
  189. if(data.contains("</think>")){
  190. flag = "text";
  191. data = data.replace("</think>","");
  192. }
  193. if(data.contains("<think>")){
  194. data = data.replace("<think>","");
  195. }
  196. sseResultDataDto.setContent(data);
  197. sseEmitterService.sendMessage(dialogueId, sseResultDataDto);
  198. }
  199. Thread.sleep(5);
  200. }
  201. } catch (InterruptedException e) {
  202. throw new RuntimeException(e);
  203. }finally {
  204. bufferedSource.close();
  205. }
  206. }
  207. } catch (IOException e) {
  208. e.printStackTrace();
  209. log.error(e.getMessage());
  210. return fail();
  211. }
  212. chatHistoryEntity = new ChatHistoryEntity();
  213. chatHistoryEntity.setDialogueId(dialogueId);
  214. chatHistoryEntity.setRole("system");
  215. chatHistoryEntity.setContent(stringBuilder.toString());
  216. chatHistoryEntity.setStatus(1);
  217. chatHistoryEntity.setEndorse(1);
  218. chatHistoryEntity.setCreateTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
  219. chatHistoryEntity.setUpdateTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
  220. chatHistoryEntity = this.chatHistoryService.save(chatHistoryEntity);
  221. return success(chatHistoryEntity);
  222. }
  223. @GetMapping("/clearChats")
  224. @ApiOperation(value = "清空聊天记录", notes = "参数包括:dialogueId:对话id")
  225. public Result clearChats(@RequestParam String dialogueId) {
  226. this.chatHistoryService.deleteByDialogueId(dialogueId);
  227. return success();
  228. }
  229. @PostMapping("/exportPdf")
  230. @ApiOperation(value = "导出PDF")
  231. public String exportPdf(@RequestBody String json, HttpServletResponse response) {
  232. List<ChatHistoryEntity> chatHistoryEntityList = JSONArray.parseArray(json, ChatHistoryEntity.class);
  233. if (chatHistoryEntityList.size() > 0) {
  234. return "导出内容为空";
  235. /*UserRecordEntity userRecordEntity = this.userRecordService.getUserRecordById(id);
  236. if (userRecordEntity == null) {
  237. return "该记录异常,下载失败";
  238. }
  239. String PDFPath = generatePDFRecord(userRecordEntity, "pdf");
  240. if (PDFPath.equals("false")) {
  241. return "报告版本较早,暂无法下载";
  242. }
  243. // List<String> htmlFilePathList = new ArrayList<>();
  244. // htmlFilePathList.add(PDFPath);
  245. //
  246. // //生成PDF测试记录
  247. // try {
  248. // html2pdf(htmlFilePathList);
  249. // } catch (Exception e) {
  250. // log.error("生成测试文件失败" + e.getMessage());
  251. // e.printStackTrace();
  252. // return "下载失败";
  253. // }
  254. // String[] split = userRecordEntity.getFileName().split("/");
  255. // String fileName = split[split.length - 1];
  256. // String PDFName = fileName.replace(".xlsx", ".pdf");
  257. //下载PDF文件
  258. File z = new File("./h2p/export_pdf/PDFReport/" + PDFPath);
  259. if (!z.exists()) return "下载失败";
  260. try {
  261. FileUtil.downloadFile(response, z, PDFPath, true);
  262. return "下载成功";
  263. } catch (UnsupportedEncodingException e) {
  264. e.printStackTrace();
  265. return "下载失败";
  266. }*/
  267. } else {
  268. return "导出内容为空";
  269. }
  270. }
  271. }