From 4de6a548a3f6c3a4b5be9d9d85b0dfc443c6f15f Mon Sep 17 00:00:00 2001 From: xueqingkun Date: Thu, 22 Feb 2024 14:51:19 +0800 Subject: [PATCH] =?UTF-8?q?web=20:=20=E4=BF=AE=E6=94=B9rasa=20=E5=AF=B9?= =?UTF-8?q?=E8=AF=9D=E6=8E=A5=E5=8F=A3(=E8=AE=BE=E7=BD=AE=E6=9C=AA?= =?UTF-8?q?=E8=AF=86=E5=88=AB=E5=AF=B9=E8=AF=9D=E7=9A=84=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E8=A7=86=E9=A2=91)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/AskServiceImpl.java | 68 +++++++++---------- 1 file changed, 33 insertions(+), 35 deletions(-) diff --git a/virtual-patient-web/src/main/java/com/supervision/service/impl/AskServiceImpl.java b/virtual-patient-web/src/main/java/com/supervision/service/impl/AskServiceImpl.java index b8746adb..c49897e0 100644 --- a/virtual-patient-web/src/main/java/com/supervision/service/impl/AskServiceImpl.java +++ b/virtual-patient-web/src/main/java/com/supervision/service/impl/AskServiceImpl.java @@ -2,21 +2,16 @@ package com.supervision.service.impl; import cn.hutool.core.codec.Base64Encoder; import cn.hutool.core.collection.CollUtil; -import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.IoUtil; +import cn.hutool.core.lang.Assert; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.crypto.digest.MD5; -import cn.hutool.http.HttpUtil; -import cn.hutool.json.JSONObject; -import cn.hutool.json.JSONUtil; -import cn.hutool.setting.dialect.Props; import com.supervision.domain.GlobalResult; import com.supervision.exception.BusinessException; import com.supervision.feign.RasaManageFeignClient; import com.supervision.model.Process; import com.supervision.model.*; -import com.supervision.pojo.vo.TalkReqVO; import com.supervision.pojo.vo.TalkResultResVO; import com.supervision.pojo.vo.TalkVideoReqVO; import com.supervision.service.*; @@ -26,12 +21,8 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; -import javax.servlet.http.HttpServletResponse; -import java.io.File; -import java.io.IOException; import java.io.InputStream; import java.util.List; import java.util.Optional; @@ -55,11 +46,7 @@ public class AskServiceImpl implements AskService { private final RasaManageFeignClient rasaManageFeignClient; - @Value("${answer.defaultNoMatchId}") - private String defaultNoMatchId; - - @Value("${answer.defaultNoMatchAnswerMessage:}") - private String defaultNoMatchAnswerMessage; + private final CommonDicService commonDicService; @Override public String receiveVoiceFile(MultipartFile file) { @@ -127,6 +114,7 @@ public class AskServiceImpl implements AskService { @Override public TalkResultResVO talkByVideo(TalkVideoReqVO talkReqVO) { + // 根据processId找到对应的病人 Process process = Optional.ofNullable(processService.getById(talkReqVO.getProcessId())).orElseThrow(() -> new BusinessException("未找到诊疗进程")); // 调用rasa获取文字内容 @@ -135,9 +123,12 @@ public class AskServiceImpl implements AskService { TalkResultResVO talkResultResVO = new TalkResultResVO(); // 如果rasa没有识别出来,则返回默认值 if (StrUtil.isBlank(rasaResult)) { - talkResultResVO.setVideoBase64(getAnswerVideoBase64OrDefault(defaultNoMatchId)); - talkResultResVO.setAnswerMessage(defaultNoMatchAnswerMessage); - saveQaRecord(talkReqVO.getProcessId(), "default", null, talkReqVO.getText(), null, "您好,我没有听懂您说什么"); + AskPatientAnswer medicalRecErrorAnswer = getMedicalRecErrorAnswer(process.getMedicalRecId()); + talkResultResVO.setVideoBase64(getAnswerVideoBase64OrDefault(medicalRecErrorAnswer.getAnswerResourceId())); + talkResultResVO.setAnswerMessage(medicalRecErrorAnswer.getAnswer()); + saveQaRecord(talkReqVO.getProcessId(), "default", null, talkReqVO.getText(), null, medicalRecErrorAnswer.getAnswer()); + talkResultResVO.setType(1); + return talkResultResVO; } // 这里校验,rasa回复的结果是不是action @@ -157,9 +148,10 @@ public class AskServiceImpl implements AskService { AskTemplateQuestionLibrary library = askTemplateQuestionLibraryService.getById(rasaResult); if (ObjectUtil.isEmpty(library)) { log.info("{}:未从问题库中找到,回答未识别语句", rasaResult); - talkResultResVO.setVideoBase64(getAnswerVideoBase64OrDefault(defaultNoMatchId)); - talkResultResVO.setAnswerMessage(defaultNoMatchAnswerMessage); - saveQaRecord(talkReqVO.getProcessId(), "default", null, talkReqVO.getText(), null, defaultNoMatchAnswerMessage); + AskPatientAnswer medicalRecErrorAnswer = getMedicalRecErrorAnswer(process.getMedicalRecId()); + talkResultResVO.setVideoBase64(getAnswerVideoBase64OrDefault(medicalRecErrorAnswer.getAnswerResourceId())); + talkResultResVO.setAnswerMessage(medicalRecErrorAnswer.getAnswer()); + saveQaRecord(talkReqVO.getProcessId(), "default", null, talkReqVO.getText(), null, medicalRecErrorAnswer.getAnswer()); } else { AskPatientAnswer askPatientAnswer = askPatientAnswerService.lambdaQuery().eq(AskPatientAnswer::getMedicalId, process.getMedicalRecId()) @@ -174,9 +166,10 @@ public class AskServiceImpl implements AskService { saveQaRecord(talkReqVO.getProcessId(), "patient", askPatientAnswer.getId(), talkReqVO.getText(), library, resText); }else { log.info("{}:病历配置,从AskPatientAnswer中未找到回答结果,回复未识别到语句", rasaResult); - talkResultResVO.setVideoBase64(getAnswerVideoBase64OrDefault(defaultNoMatchId)); - talkResultResVO.setAnswerMessage(defaultNoMatchAnswerMessage); - saveQaRecord(talkReqVO.getProcessId(), "default", null, talkReqVO.getText(), library, defaultNoMatchAnswerMessage); + AskPatientAnswer medicalRecErrorAnswer = getMedicalRecErrorAnswer(process.getMedicalRecId()); + talkResultResVO.setVideoBase64(getAnswerVideoBase64OrDefault(medicalRecErrorAnswer.getAnswerResourceId())); + talkResultResVO.setAnswerMessage(medicalRecErrorAnswer.getAnswer()); + saveQaRecord(talkReqVO.getProcessId(), "default", null, talkReqVO.getText(), library, medicalRecErrorAnswer.getAnswer()); } } } @@ -190,17 +183,29 @@ public class AskServiceImpl implements AskService { rasaTalkVo.setSessionId(sessionId); // 默认为1 rasaTalkVo.setModelId("1"); - GlobalResult> talk = rasaManageFeignClient.talk(rasaTalkVo); - log.info("调用rasa对话返回结果:{}", talk); try { + GlobalResult> talk = rasaManageFeignClient.talk(rasaTalkVo); + log.info("调用rasa对话返回结果:{}", talk); if (talk.getCode() != 200 || CollUtil.isEmpty(talk.getData())) { - return "对不起,我没有听懂您说的是什么意思"; + return null; } return CollUtil.getFirst(talk.getData()); } catch (Exception e) { log.error("talkRasa error ", e); } - return "对不起,我没有听懂您说的是什么意思"; + return null; + } + + private AskPatientAnswer getMedicalRecErrorAnswer(String medicalRecId) { + //Optional.ofNullable(medicalRecErrorAnswer).orElseGet(() ->new AskPatientAnswer()).getAnswer() + Assert.notEmpty(medicalRecId, "病历id不能为空"); + CommonDic commonDic = commonDicService.lambdaQuery().eq(CommonDic::getGroupCode, "AQT").eq(CommonDic::getCode, "system_error").one(); + Assert.notNull(commonDic, "字典未配置系统内置异常识别场景"); + + AskTemplateQuestionLibrary askTemplateQuestionLibrary = askTemplateQuestionLibraryService.lambdaQuery().eq(AskTemplateQuestionLibrary::getDictId, commonDic.getId()).one(); + Assert.notNull(askTemplateQuestionLibrary, "知识库未配置系统内置异常识别场景"); + + return askPatientAnswerService.lambdaQuery().eq(AskPatientAnswer::getMedicalId, medicalRecId).eq(AskPatientAnswer::getLibraryQuestionId, askTemplateQuestionLibrary.getId()).one(); } @@ -211,14 +216,7 @@ public class AskServiceImpl implements AskService { * @return 应答视频base64位编码 */ private String getAnswerVideoBase64OrDefault(String fileResourceId) { - if (StrUtil.isBlank(fileResourceId)) { - fileResourceId = defaultNoMatchId; - } FileResource fileResource = fileResourceService.getById(fileResourceId); - if (ObjectUtil.isEmpty(fileResource)) { - log.info("未找到视频,走默认视频"); - fileResource = fileResourceService.getById(defaultNoMatchId); - } try (InputStream inputStream = MinioUtil.download(fileResource.getMinioId())) { return Base64Encoder.encode(IoUtil.readBytes(inputStream)); } catch (Exception e) {