From ccda6b3c12a4cf9e521c81586ae2518b60f34875 Mon Sep 17 00:00:00 2001 From: PL <774412461@qq.com> Date: Fri, 30 Aug 2024 10:34:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=92=E6=9F=A5=E6=B5=8B=E8=AF=95=E6=97=A0?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=EF=BC=8C=E6=AD=A3=E5=BC=8F=E7=AB=99=E8=AE=BF?= =?UTF-8?q?=E9=97=AE=E5=BC=82=E5=B8=B8=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/ChatMessageServiceImpl.java | 136 +++++++++--------- 1 file changed, 71 insertions(+), 65 deletions(-) diff --git a/src/main/java/cn/teammodel/service/impl/ChatMessageServiceImpl.java b/src/main/java/cn/teammodel/service/impl/ChatMessageServiceImpl.java index 20dde7c..9b4578c 100644 --- a/src/main/java/cn/teammodel/service/impl/ChatMessageServiceImpl.java +++ b/src/main/java/cn/teammodel/service/impl/ChatMessageServiceImpl.java @@ -74,26 +74,30 @@ public class ChatMessageServiceImpl implements ChatMessageService { */ @Override public SseEmitter chatComments(ChatCommentsDto chatCommentsDto, String userId, String userName) { - // 目前仅使用讯飞星火大模型 - String appId = chatCommentsDto.getAppId(); - String text = commentsTemplate(chatCommentsDto); - if (!StringUtils.isEmpty(text)) { - chatCommentsDto.setText(text); - } else { - throw new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), "参数错误"); - } + try { + // 目前仅使用讯飞星火大模型 + String appId = chatCommentsDto.getAppId(); + String text = commentsTemplate(chatCommentsDto); + if (!StringUtils.isEmpty(text)) { + chatCommentsDto.setText(text); + } else { + throw new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), "参数错误"); + } - SseEmitter sseEmitter; - if (StringUtils.isEmpty(appId)) { - sseEmitter = commentsBySession(chatCommentsDto, userId, userName); - } else { - ChatCompletionReqDto chatCompletionReqDto = new ChatCompletionReqDto(); - chatCompletionReqDto.setAppId(chatCommentsDto.getAppId()); - chatCompletionReqDto.setSessionId(chatCommentsDto.getSessionId()); - chatCompletionReqDto.setText(chatCommentsDto.getText()); - sseEmitter = completionByApp(chatCompletionReqDto, false); + SseEmitter sseEmitter; + if (StringUtils.isEmpty(appId)) { + sseEmitter = commentsBySession(chatCommentsDto, userId, userName); + } else { + ChatCompletionReqDto chatCompletionReqDto = new ChatCompletionReqDto(); + chatCompletionReqDto.setAppId(chatCommentsDto.getAppId()); + chatCompletionReqDto.setSessionId(chatCommentsDto.getSessionId()); + chatCompletionReqDto.setText(chatCommentsDto.getText()); + sseEmitter = completionByApp(chatCompletionReqDto, false); + } + return sseEmitter; + } catch (Exception e) { + throw new ServiceException(ErrorCode.OPERATION_ERROR.getCode(), e.getMessage()); } - return sseEmitter; } /** @@ -290,47 +294,45 @@ public class ChatMessageServiceImpl implements ChatMessageService { /** * 评语调用 聊天模型 * 待优化 + * * @return */ private String commentsTemplate(ChatCommentsDto chatCommentsDto) { StringBuilder builder = new StringBuilder(); String strData = JSON.toJSONString(chatCommentsDto.getData()); - - File file = new File(ClassLoader.getSystemResource("Json/ChatModel.json").getPath()); //相对路径获取文件信息 - //File file = new File("src/main/resources/Json/ChatModel.json"); //绝对路径获取文件信息 - List chatModels = new ArrayList<>(); ChatModelDto chatModel = null; + + try { + File file = new File(ClassLoader.getSystemResource("Json/ChatModel.json").getPath()); //相对路径获取文件信息 + //File file = new File("src/main/resources/Json/ChatModel.json"); //绝对路径获取文件信息 + //chatModel = readerMethod(file); - chatModels = readerMethod(file); - if(chatModels.size() <= 0) - { - throw new ServiceException(ErrorCode.NOT_FOUND_ERROR.getCode(), "评语模版未配置"); + chatModels = readerMethod(file); + } catch (Exception e) { + throw new ServiceException(ErrorCode.OPERATION_ERROR.getCode(), e.getMessage()); } //循环查找对应的模型数据 - for(ChatModelDto chatModelTemp : chatModels) - { + for (ChatModelDto chatModelTemp : chatModels) { //判断评语类型 - if(chatCommentsDto.getType().equals(chatModelTemp.getType())) - { + if (chatCommentsDto.getType().equals(chatModelTemp.getType())) { chatModel = chatModelTemp; break; } } - if(chatModel != null) - { + if (chatModel != null) { //角色条件 - builder.append(String.format(chatModel.getRole(),chatCommentsDto.getPeriod(),chatCommentsDto.getSubject())); + builder.append(String.format(chatModel.getRole(), chatCommentsDto.getPeriod(), + chatCommentsDto.getSubject())); } ChatModelDto finalChatModel = chatModel; //模版 switch (chatCommentsDto.getType()) { //智育 总体评语模版 - case "wisdom": - { + case "wisdom": { WisdomCommentsDto wisdomComments; //转换问题 try { @@ -339,8 +341,7 @@ public class ChatMessageServiceImpl implements ChatMessageService { } catch (Exception e) { throw new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), "类型转换失败"); } - if(wisdomComments.getName() == null) - { + if (wisdomComments.getName() == null) { throw new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), "请求参数异常"); } @@ -384,63 +385,66 @@ public class ChatMessageServiceImpl implements ChatMessageService { item.data[4] )); }); - builder.append(String.format(finalChatModel.getEnd(),wisdomComments.getName())); + builder.append(String.format(finalChatModel.getEnd(), wisdomComments.getName())); break; } //智育 表现模版 - case "wisdomExam": - { + case "wisdomExam": { List examComments = new ArrayList<>(); try { - examComments = JSON.parseObject(strData, new TypeReference>() {}); + examComments = JSON.parseObject(strData, new TypeReference>() { + }); } catch (Exception e) { throw new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), "类型转换失败"); } - if(examComments.size() <= 1){ - if (examComments.isEmpty()){ + if (examComments.size() <= 1) { + if (examComments.isEmpty()) { throw new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), "请求参数异常"); } - if (examComments.get(0).name == null){ + if (examComments.get(0).name == null) { throw new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), "请求参数异常"); } } int count = 1; for (WisdomExamCommentsDto examComment : examComments) { - builder.append(String.format(finalChatModel.getCycleChats().get(0), count, examComment.getName(), examComment.getTime(), examComment.getScore(), examComment.getScoreRate(), examComment.getRanking())); - if (count < examComments.size()) - { + builder.append(String.format(finalChatModel.getCycleChats().get(0), count, examComment.getName(), + examComment.getTime(), examComment.getScore(), examComment.getScoreRate(), + examComment.getRanking())); + if (count < examComments.size()) { count++; } } - builder.append(String.format(finalChatModel.getEnd(),count)); + builder.append(String.format(finalChatModel.getEnd(), count)); break; } // 智育 学科评语模版 - case "windomSubject": - { + case "windomSubject": { List subjectComments; try { - subjectComments = JSON.parseObject(strData, new TypeReference>() {}); - } - catch (Exception e) { + subjectComments = JSON.parseObject(strData, new TypeReference>() { + }); + } catch (Exception e) { throw new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), "请求参数异常"); } - if(subjectComments.size() <= 1){ - if (subjectComments.isEmpty()){ + if (subjectComments.size() <= 1) { + if (subjectComments.isEmpty()) { throw new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), "请求参数异常"); } } builder.append(finalChatModel.getChat()); //拼接学科数组 - for (WisdomSubjectComments comments : subjectComments){ + for (WisdomSubjectComments comments : subjectComments) { builder.append(String.format(finalChatModel.getCycleChats().get(0), comments.subjectName)); - for (WisdomSubjectComments.StuInfo stuInfo : comments.getRankings()){ - builder.append(String.format(finalChatModel.getCycleChats().get(1), stuInfo.ranking, stuInfo.name, stuInfo.scoreRate)); + for (WisdomSubjectComments.StuInfo stuInfo : comments.getRankings()) { + builder.append(String.format(finalChatModel.getCycleChats().get(1), stuInfo.ranking, + stuInfo.name, stuInfo.scoreRate)); } - builder.append(String.format(finalChatModel.getCycleChats().get(2), comments.getClaasRanking().ranking, comments.getClaasRanking().scoreRate,comments.getClaasRanking().average)); + builder.append(String.format(finalChatModel.getCycleChats().get(2), + comments.getClaasRanking().ranking, comments.getClaasRanking().scoreRate, + comments.getClaasRanking().average)); } - builder.append(String.format(finalChatModel.getEnd(),subjectComments.size())); + builder.append(String.format(finalChatModel.getEnd(), subjectComments.size())); break; } //艺术 考核指标纬度评语 @@ -462,11 +466,11 @@ public class ChatMessageServiceImpl implements ChatMessageService { throw new ServiceException(ErrorCode.PARAMS_ERROR.getCode(), "评语类型异常"); } - if (chatCommentsDto.getSize() > 0 ){ + if (chatCommentsDto.getSize() > 0) { builder.append("字数限制在:") .append(chatCommentsDto.getSize()) .append("字左右"); - }else { + } else { builder.append("字数限制在:200字左右"); } @@ -475,13 +479,14 @@ public class ChatMessageServiceImpl implements ChatMessageService { /** * 读取文件信息并转换为智能对话模型数组对象 + * * @param file * @return */ - private static List readerMethod(File file) { + private static List readerMethod(File file) { //读取文件信息,并返回string字符串 ,并改成json格式 String fileTxt = FileUtil.readFile(file); - String strData =JSON.toJSONString(fileTxt); + String strData = JSON.toJSONString(fileTxt); //获取聊天字段中的数据 Object str = JSON.parseObject(strData).get("chatModel"); @@ -491,7 +496,8 @@ public class ChatMessageServiceImpl implements ChatMessageService { //转换问题 try { //转换方式 - chatModelDtos = JSON.parseObject(strData2, new TypeReference>() {}); + chatModelDtos = JSON.parseObject(strData2, new TypeReference>() { + }); } catch (Exception e) { throw new ServiceException(ErrorCode.OPERATION_ERROR.getCode(), "类型转换失败"); }