diff --git a/virtual-patient-manage/pom.xml b/virtual-patient-manage/pom.xml index eb948320..d104417f 100644 --- a/virtual-patient-manage/pom.xml +++ b/virtual-patient-manage/pom.xml @@ -34,6 +34,11 @@ nacos-common + + org.springframework.cloud + spring-cloud-starter-openfeign + + com.alibaba.cloud spring-cloud-starter-alibaba-nacos-config @@ -65,9 +70,12 @@ org.springframework.boot spring-boot-maven-plugin - - paketobuildpacks/builder-jammy-base:latest - + + + org.projectlombok + lombok + + diff --git a/virtual-patient-manage/src/main/java/com/supervision/manage/VirtualPatientManageApplication.java b/virtual-patient-manage/src/main/java/com/supervision/manage/VirtualPatientManageApplication.java index b9160a39..fb09bda9 100644 --- a/virtual-patient-manage/src/main/java/com/supervision/manage/VirtualPatientManageApplication.java +++ b/virtual-patient-manage/src/main/java/com/supervision/manage/VirtualPatientManageApplication.java @@ -4,12 +4,14 @@ import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScans; @SpringBootApplication @MapperScan(basePackages = {"com.supervision.**.mapper"}) @ComponentScan(basePackages = {"com.supervision.**"}) +@EnableFeignClients(basePackages = "com.supervision") @EnableDiscoveryClient public class VirtualPatientManageApplication { diff --git a/virtual-patient-model/src/main/java/com/supervision/vo/rasa/RasaTalkVo.java b/virtual-patient-model/src/main/java/com/supervision/vo/rasa/RasaTalkVo.java index f3078789..08a4f6c8 100644 --- a/virtual-patient-model/src/main/java/com/supervision/vo/rasa/RasaTalkVo.java +++ b/virtual-patient-model/src/main/java/com/supervision/vo/rasa/RasaTalkVo.java @@ -19,6 +19,6 @@ public class RasaTalkVo { * 模型id */ @ApiModelProperty(hidden = true) - private String modelId; + private String modelId = "1"; } diff --git a/virtual-patient-rasa/pom.xml b/virtual-patient-rasa/pom.xml index 9f62f611..00705150 100644 --- a/virtual-patient-rasa/pom.xml +++ b/virtual-patient-rasa/pom.xml @@ -40,6 +40,11 @@ nacos-common + + org.springframework.cloud + spring-cloud-starter-openfeign + + com.alibaba.cloud spring-cloud-starter-alibaba-nacos-config diff --git a/virtual-patient-web/pom.xml b/virtual-patient-web/pom.xml index 54172159..bd7054aa 100644 --- a/virtual-patient-web/pom.xml +++ b/virtual-patient-web/pom.xml @@ -42,6 +42,11 @@ spring-cloud-starter-alibaba-nacos-config + + org.springframework.cloud + spring-cloud-starter-openfeign + + diff --git a/virtual-patient-web/src/main/java/com/supervision/VirtualPatientApplication.java b/virtual-patient-web/src/main/java/com/supervision/VirtualPatientApplication.java index 1244c7a3..f1732f2c 100644 --- a/virtual-patient-web/src/main/java/com/supervision/VirtualPatientApplication.java +++ b/virtual-patient-web/src/main/java/com/supervision/VirtualPatientApplication.java @@ -5,6 +5,7 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScans; import org.springframework.scheduling.annotation.EnableScheduling; @@ -13,6 +14,7 @@ import org.springframework.web.socket.config.annotation.EnableWebSocket; @SpringBootApplication @MapperScan(basePackages = {"com.supervision.**.mapper"}) @EnableScheduling +@EnableFeignClients(basePackages = "com.supervision") @EnableDiscoveryClient public class VirtualPatientApplication { diff --git a/virtual-patient-web/src/main/java/com/supervision/controller/RasaController.java b/virtual-patient-web/src/main/java/com/supervision/controller/RasaController.java deleted file mode 100644 index 761a2bfd..00000000 --- a/virtual-patient-web/src/main/java/com/supervision/controller/RasaController.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.supervision.controller; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.supervision.domain.GlobalResult; -import com.supervision.service.RasaService; -import io.swagger.annotations.ApiModelProperty; -import io.swagger.annotations.ApiOperation; -import lombok.RequiredArgsConstructor; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -/** - * @Description: @since 2.0.0接口功能已经过期,功能迁移到rasaManage RasaCmdService.deployRasa 服务中 - */ -@Deprecated -@RestController -@RequestMapping("rasa") -@RequiredArgsConstructor -public class RasaController { - - private final RasaService rasaService; - - @ApiOperation("生成rasa的yml文件") - @GetMapping("generateRasaYml") - public GlobalResult generateRasaYml(String patientId) { - return rasaService.generateRasaYml(patientId); - } - - @ApiOperation("训练rasa") - @GetMapping("trainRasa") - public GlobalResult trainRasa(String patientId) throws JsonProcessingException { - return rasaService.trainRasa(patientId); - } - - @ApiOperation("运行Rasa") - @GetMapping("runRasa") - public GlobalResult runRasa(String patientId) throws JsonProcessingException { - return rasaService.runRasa(patientId); - } - - -} diff --git a/virtual-patient-web/src/main/java/com/supervision/feign/RasaManageFeignClient.java b/virtual-patient-web/src/main/java/com/supervision/feign/RasaManageFeignClient.java new file mode 100644 index 00000000..dda8c97b --- /dev/null +++ b/virtual-patient-web/src/main/java/com/supervision/feign/RasaManageFeignClient.java @@ -0,0 +1,17 @@ +package com.supervision.feign; + +import com.supervision.domain.GlobalResult; +import com.supervision.vo.rasa.RasaTalkVo; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; + +import java.util.List; + +@FeignClient(name = "virtual-patient-rasa", contextId = "VirtualPatientRasa") +public interface RasaManageFeignClient { + @PostMapping("/rasa/talkRasa") + GlobalResult> talk(@RequestBody RasaTalkVo rasaTalkVo); + + +} diff --git a/virtual-patient-web/src/main/java/com/supervision/service/AskService.java b/virtual-patient-web/src/main/java/com/supervision/service/AskService.java index aec660f6..48791bf3 100644 --- a/virtual-patient-web/src/main/java/com/supervision/service/AskService.java +++ b/virtual-patient-web/src/main/java/com/supervision/service/AskService.java @@ -15,8 +15,6 @@ public interface AskService { String receiveVoiceFile(MultipartFile file); - TalkResultResVO talk(TalkReqVO talkReqVO) throws IOException; - TalkResultResVO talkByVideo(TalkVideoReqVO talkReqVO) throws IOException; diff --git a/virtual-patient-web/src/main/java/com/supervision/service/RasaService.java b/virtual-patient-web/src/main/java/com/supervision/service/RasaService.java deleted file mode 100644 index ffc6977f..00000000 --- a/virtual-patient-web/src/main/java/com/supervision/service/RasaService.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.supervision.service; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.supervision.domain.GlobalResult; - -public interface RasaService { - - GlobalResult generateRasaYml(String diseaseId); - - GlobalResult trainRasa(String patientId) throws JsonProcessingException; - - GlobalResult runRasa(String patientId) throws JsonProcessingException; -} 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 60b17864..0038e6b9 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 @@ -1,13 +1,19 @@ 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.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; @@ -15,6 +21,7 @@ import com.supervision.pojo.vo.TalkResultResVO; import com.supervision.pojo.vo.TalkVideoReqVO; import com.supervision.service.*; import com.supervision.util.*; +import com.supervision.vo.rasa.RasaTalkVo; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; @@ -46,6 +53,8 @@ public class AskServiceImpl implements AskService { private final FileResourceService fileResourceService; + private final RasaManageFeignClient rasaManageFeignClient; + @Value("${answer.defaultNoMatchId}") private String defaultNoMatchId; @@ -70,87 +79,6 @@ public class AskServiceImpl implements AskService { return text; } - @Override - @Transactional(rollbackFor = Exception.class) - public TalkResultResVO talk(TalkReqVO talkReqVO) { - // 根据processId找到对应的病人 - Process process = Optional.ofNullable(processService.getById(talkReqVO.getProcessId())).orElseThrow(() -> new BusinessException("未找到诊疗进程")); - // 调用rasa获取文字内容 - String rasaResult = RasaUtil.talkRasa(talkReqVO.getText(), UserUtil.getUser().getId()); - // 如果rasa没有识别出来,则返回默认值 - if (StrUtil.isBlank(rasaResult)) { - // 这里调用京东数字人接口首先根据token获取房间号 - String roomId = HumanUtil.queryRoomId(talkReqVO.getRoomKey(), talkReqVO.getRoomToken()); - HumanUtil.textDriven("您好,我没有听懂您说什么", roomId); - saveQaRecord(talkReqVO.getProcessId(), "default", null, talkReqVO.getText(), null, "您好,我没有听懂您说什么"); - } - TalkResultResVO talkResultResVO = new TalkResultResVO(); - // 这里校验,rasa回复的结果是不是action - // 这里设置的模板,对于action的动作全部是用ancillary_ | tool_进行标记,详情看生成rasa的yml的代码:RasaServiceImpl.generateDomain - // ancillary_ | tool_ - if (rasaResult.startsWith("ancillary_") || rasaResult.startsWith("tool_")) { - log.info("呼出语句:{}", rasaResult); - List actionList = StrUtil.split(rasaResult, '_'); - if (actionList.size() > 1) { - // 在这里给socket回复,设置为动作 - talkResultResVO.setActionId(actionList.get(1)); - talkResultResVO.setType("ancillary".equals(actionList.get(0)) ? 3 : 2); - setActionRelation(talkResultResVO); - return talkResultResVO; - } - } else { - String roomId = HumanUtil.queryRoomId(talkReqVO.getRoomKey(), talkReqVO.getRoomToken()); - AskTemplateQuestionLibrary library = askTemplateQuestionLibraryService.getById(rasaResult); - if (ObjectUtil.isEmpty(library)) { - log.info("{}:未从问题库中找到,回答未识别语句", rasaResult); - HumanUtil.textDriven("您好,我没有听懂您说什么", roomId); - saveQaRecord(talkReqVO.getProcessId(), "default", null, talkReqVO.getText(), null, "您好,我没有听懂您说什么"); - } else { - AskPatientAnswer askPatientAnswer = askPatientAnswerService.lambdaQuery().eq(AskPatientAnswer::getMedicalId, process.getMedicalRecId()) - .eq(AskPatientAnswer::getLibraryQuestionId, library.getId()).last("limit 1").one(); - // 如果没有找到回答,去默认回答里面看看有没有 - if (ObjectUtil.isEmpty(askPatientAnswer)) { - log.info("{}:病历配置,未从AskPatientAnswer中找到回答结果,尝试回答默认答案", rasaResult); - // 首先看看default里面是不是存在,如果存在,就从default里面去找 - if (StrUtil.isNotEmpty(library.getDefaultAnswer())) { - String resText = library.getDefaultAnswer(); - HumanUtil.textDriven(resText, roomId); - // 保存记录 - saveQaRecord(talkReqVO.getProcessId(), "default", library.getId(), talkReqVO.getText(), library, resText); - log.info("{}:找到了默认答案:{}", rasaResult, talkReqVO.getText()); - } else { - log.info("{}:没有从默认答案中找到找到默认内容,回复未识别语句", rasaResult); - HumanUtil.textDriven("您好,我没有听懂您说什么", roomId); - saveQaRecord(talkReqVO.getProcessId(), "default", null, talkReqVO.getText(), library, "您好,我没有听懂您说什么"); - } - } else { - if (StrUtil.isEmpty(askPatientAnswer.getAnswer())) { - log.info("{}:病历配置的回答:{}:为空不为空,但在获取的时候,答案为空,尝试回复默认语句", rasaResult, askPatientAnswer.getId()); - if (StrUtil.isNotEmpty(library.getDefaultAnswer())) { - String resText = library.getDefaultAnswer(); - log.info("{}:病历配置的回答:{}:为空不为空不为空,但在获取的时候,答案为空,开始回复默认语句,默认语句内容:{}", rasaResult, askPatientAnswer.getId(), resText); - HumanUtil.textDriven(resText, roomId); - // 保存记录 - saveQaRecord(talkReqVO.getProcessId(), "default", library.getId(), talkReqVO.getText(), library, resText); - } else { - log.info("{}:病历配置的回答:{}:为空不为空,但在获取的时候,答案为空,但是获取默认语句也为空,那么回复未识别语句", rasaResult, askPatientAnswer.getId()); - HumanUtil.textDriven("您好,我没有听懂您说什么", roomId); - } - - } else { - String resText = askPatientAnswer.getAnswer(); - log.info("{}:找到了病历配置的回答语句:{},回答内容:{}", rasaResult, askPatientAnswer.getId(), resText); - HumanUtil.textDriven(resText, roomId); - // 保存记录 - saveQaRecord(talkReqVO.getProcessId(), "patient", askPatientAnswer.getId(), talkReqVO.getText(), library, resText); - } - - } - } - } - talkResultResVO.setType(1); - return talkResultResVO; - } private void saveQaRecord(String processId, String answerType, String answerId, String question, AskTemplateQuestionLibrary library, String resText) { DiagnosisQaRecord record = new DiagnosisQaRecord(); @@ -195,7 +123,6 @@ public class AskServiceImpl implements AskService { talkResultResVO.setActionType(new MD5().digestHex16(configAncillaryItem.getType())); } } - } @Override @@ -203,7 +130,7 @@ public class AskServiceImpl implements AskService { // 根据processId找到对应的病人 Process process = Optional.ofNullable(processService.getById(talkReqVO.getProcessId())).orElseThrow(() -> new BusinessException("未找到诊疗进程")); // 调用rasa获取文字内容 - String rasaResult = RasaUtil.talkRasa(talkReqVO.getText(), UserUtil.getUser().getId()); + String rasaResult = talkRasa(talkReqVO.getText(), UserUtil.getUser().getId()); log.info("rasa的回复是:{}", rasaResult); TalkResultResVO talkResultResVO = new TalkResultResVO(); // 如果rasa没有识别出来,则返回默认值 @@ -287,6 +214,25 @@ public class AskServiceImpl implements AskService { return talkResultResVO; } + public String talkRasa(String question, String sessionId) { + RasaTalkVo rasaTalkVo = new RasaTalkVo(); + rasaTalkVo.setQuestion(question); + rasaTalkVo.setSessionId(sessionId); + // 默认为1 + rasaTalkVo.setModelId("1"); + GlobalResult> talk = rasaManageFeignClient.talk(rasaTalkVo); + log.info("调用rasa对话返回结果:{}", talk); + try { + if (talk.getCode() != 200 || CollUtil.isEmpty(talk.getData())) { + return "对不起,我没有听懂您说的是什么意思"; + } + return CollUtil.getFirst(talk.getData()); + } catch (Exception e) { + log.error("talkRasa error ", e); + } + return "对不起,我没有听懂您说的是什么意思"; + } + /** * 获取应答视频的base64位编码 diff --git a/virtual-patient-web/src/main/java/com/supervision/service/impl/RasaServiceImpl.java b/virtual-patient-web/src/main/java/com/supervision/service/impl/RasaServiceImpl.java deleted file mode 100644 index 9d849132..00000000 --- a/virtual-patient-web/src/main/java/com/supervision/service/impl/RasaServiceImpl.java +++ /dev/null @@ -1,247 +0,0 @@ -package com.supervision.service.impl; - -import cn.hutool.core.collection.CollUtil; -import cn.hutool.core.io.FileUtil; -import cn.hutool.core.io.IoUtil; -import cn.hutool.core.util.StrUtil; -import cn.hutool.http.HttpRequest; -import cn.hutool.http.HttpResponse; -import cn.hutool.http.HttpUtil; -import cn.hutool.json.JSONUtil; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.supervision.domain.GlobalResult; -import com.supervision.exception.BusinessException; -import com.supervision.model.*; -import com.supervision.pojo.rasa.train.DomainYmlTemplate; -import com.supervision.pojo.rasa.train.NluYmlTemplate; -import com.supervision.pojo.rasa.train.QuestionAnswerDTO; -import com.supervision.pojo.rasa.train.RuleYmlTemplate; -import com.supervision.service.*; -import freemarker.template.Configuration; -import freemarker.template.Template; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Service; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.PrintWriter; -import java.util.*; -import java.util.function.Function; -import java.util.stream.Collectors; -import java.util.zip.ZipEntry; -import java.util.zip.ZipOutputStream; - -@Slf4j -@Service -@RequiredArgsConstructor -public class RasaServiceImpl implements RasaService { - - private final ConfigPhysicalToolService configPhysicalToolService; - - private final ConfigAncillaryItemService configAncillaryItemService; - - private final AskTemplateQuestionLibraryService askTemplateQuestionLibraryService; - - - private static final ObjectMapper objectMapper = new ObjectMapper(); - - @Value("${rasa.base-url}${rasa.saveRasaFile}") - private String saveRasaFileUrl; - - @Value("${rasa.base-url}${rasa.train}") - private String trainRasaUrl; - - @Value("${rasa.base-url}${rasa.run}") - private String runRasaUrl; - - @Override - public GlobalResult generateRasaYml(String patientId) { - - Map ymalFileMap = new HashMap<>(); - // 默认问答MAP - Map questionCodeAndIdMap = new HashMap<>(); - - List ruleList = new ArrayList<>(); - - // 开始生成各种yaml文件 - generateNlu(patientId, questionCodeAndIdMap, ymalFileMap); - generateDomain(questionCodeAndIdMap, ruleList, ymalFileMap); - generateRule(ruleList, ymalFileMap); - // 生成压缩文件 - List tempFile = new ArrayList<>(); - File tempZipFile = FileUtil.createTempFile(".zip", true); - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - try (ZipOutputStream zipOutputStream = new ZipOutputStream(bos)) { - for (Map.Entry fileEntry : ymalFileMap.entrySet()) { - zipOutputStream.putNextEntry(new ZipEntry(fileEntry.getKey())); - IoUtil.copy(FileUtil.getInputStream(fileEntry.getValue()), zipOutputStream); - zipOutputStream.closeEntry(); - tempFile.add(fileEntry.getValue()); - } - zipOutputStream.finish(); - // 调用接口传文件 - HttpRequest request = HttpRequest.post(saveRasaFileUrl); - IoUtil.copy(new ByteArrayInputStream(bos.toByteArray()), FileUtil.getOutputStream(tempZipFile)); - request.form("file", tempZipFile); - request.form("modelId", patientId); - HttpResponse response = request.execute(); - String responseBody = response.body(); - log.info(responseBody); - return objectMapper.readValue(responseBody, new TypeReference>() { - }); - } catch (Exception e) { - log.error("生成ZIP文件失败", e); - throw new BusinessException("生成ZIP文件失败"); - } finally { - // 最后把临时文件删除 - tempFile.forEach(FileUtil::del); - FileUtil.del(tempZipFile); - } - } - - private void generateNlu(String patientId, - Map intentCodeAndIdMap, - Map ymalFileMap) { - // 首先生成根据意图查找到nlu文件 - List nluList = new ArrayList<>(); - - // 默认意图 - List askTemplateQuestionLibraryList = askTemplateQuestionLibraryService.lambdaQuery().list(); - // 生成默认意图的nlu - for (AskTemplateQuestionLibrary questionLibrary : askTemplateQuestionLibraryList) { - // 开始生成 - NluYmlTemplate.Nlu nlu = new NluYmlTemplate.Nlu(); - // 拼接格式:code_id(防止重复) - String intentCode = questionLibrary.getCode() + "_" + questionLibrary.getId(); - nlu.setIntent(intentCode); - nlu.setExamples(questionLibrary.getQuestion()); - nluList.add(nlu); - // 添加到map中,key为意图编码,value为意图ID - intentCodeAndIdMap.put(intentCode, new QuestionAnswerDTO(questionLibrary.getQuestion(), CollUtil.newArrayList( questionLibrary.getId()), questionLibrary.getDescription())); - } - - // 这里处理呼出的问题(code和问题不能为空) - List physicalToolList = configPhysicalToolService.lambdaQuery() - .isNotNull(ConfigPhysicalTool::getCode) - .isNotNull(ConfigPhysicalTool::getCallOutQuestion).list(); - - for (ConfigPhysicalTool tool : physicalToolList) { - // 把呼出的问题全部加进去 - NluYmlTemplate.Nlu nlu = new NluYmlTemplate.Nlu(); - String toolIntent = "tool_" + tool.getCode(); - nlu.setIntent(toolIntent); - nlu.setExamples(tool.getCallOutQuestion()); - nluList.add(nlu); - // answer格式为:---tool---工具ID - intentCodeAndIdMap.put(toolIntent, - new QuestionAnswerDTO(tool.getCallOutQuestion(), - CollUtil.newArrayList("tool_" + tool.getId()), "tool-" + tool.getToolName())); - } - - // 生成呼出的辅助检查 - List ancillaryItemList = configAncillaryItemService.lambdaQuery() - .isNotNull(ConfigAncillaryItem::getCode) - .isNotNull(ConfigAncillaryItem::getCallOutQuestion).list(); - - for (ConfigAncillaryItem ancillary : ancillaryItemList) { - // 把辅助问诊的问题全部加进去 - NluYmlTemplate.Nlu nlu = new NluYmlTemplate.Nlu(); - String itemIntent = "ancillary_" + ancillary.getCode(); - nlu.setIntent(itemIntent); - nlu.setExamples(ancillary.getCallOutQuestion()); - nluList.add(nlu); - // answer格式为:---ancillary---工具ID - intentCodeAndIdMap.put(itemIntent, - new QuestionAnswerDTO(ancillary.getCallOutQuestion(), - CollUtil.newArrayList("ancillary_" + ancillary.getId()), "呼出-ancillary-" + ancillary.getItemName())); - } - NluYmlTemplate nluYmlTemplate = new NluYmlTemplate(); - nluYmlTemplate.setNlu(nluList); - - // 生成后生成yml文件 - createYmlFile(NluYmlTemplate.class, "nlu.ftl", nluYmlTemplate, "nlu.yml", ymalFileMap); - - } - - - public void generateDomain(Map questionCodeAndIdMap, - List ruleList, Map ymalFileMap) { - LinkedHashMap> responses = new LinkedHashMap<>(); - for (Map.Entry entry : questionCodeAndIdMap.entrySet()) { - String intentCode = entry.getKey(); - QuestionAnswerDTO value = entry.getValue(); - String utter = "utter_" + intentCode; - responses.put(utter, CollUtil.newArrayList(value.getAnswerList())); - ruleList.add(new RuleYmlTemplate.Rule(value.getDesc(), intentCode, utter)); - } - - - DomainYmlTemplate domainYmlTemplate = new DomainYmlTemplate(); - // 意图 - List intentList = new ArrayList<>(questionCodeAndIdMap.keySet()); - domainYmlTemplate.setIntents(intentList); - // 回复 - domainYmlTemplate.setResponses(responses); - // action - List actionList = new ArrayList<>(responses.keySet()); - domainYmlTemplate.setActions(actionList); - // 生成yml文件 - createYmlFile(DomainYmlTemplate.class, "domain.ftl", domainYmlTemplate, "domain.yml", ymalFileMap); - } - - /** - * 生成rule - */ - public void generateRule(List ruleList, Map ymalFileMap) { - RuleYmlTemplate ruleYmlTemplate = new RuleYmlTemplate(); - ruleYmlTemplate.setRules(ruleList); - // 生成yml文件 - createYmlFile(RuleYmlTemplate.class, "rules.ftl", ruleYmlTemplate, "rules.yml", ymalFileMap); - } - - private void createYmlFile(Class clazz, String ftlName, Object data, String ymlName, Map ymalFileMap) { - try { - // 这个版本和maven依赖的版本一致 - Configuration configuration = new Configuration(Configuration.VERSION_2_3_31); - configuration.setClassForTemplateLoading(clazz, "/templates"); // 模板文件的所在目录 - // 获取模板 - Template template = configuration.getTemplate(ftlName); - File tempFile = FileUtil.createTempFile(".yml", true); - // 创建输出文件 - try (PrintWriter out = new PrintWriter(tempFile);) { - // 填充并生成输出 - template.process(data, out); - } catch (Exception e) { - log.error("文件生成失败"); - } - ymalFileMap.put(ymlName, tempFile); - } catch (Exception e) { - log.error("导出模板失败", e); - } - - - } - - @Override - public GlobalResult trainRasa(String patientId) throws JsonProcessingException { - Map param = new HashMap<>(); - param.put("modelId", patientId); - String responseBody = HttpUtil.post(trainRasaUrl, JSONUtil.toJsonStr(param)); - return objectMapper.readValue(responseBody, new TypeReference>() { - }); - } - - @Override - public GlobalResult runRasa(String patientId) throws JsonProcessingException { - Map param = new HashMap<>(); - param.put("modelId", patientId); - String responseBody = HttpUtil.post(runRasaUrl, JSONUtil.toJsonStr(param)); - return objectMapper.readValue(responseBody, new TypeReference>() { - }); - } -} diff --git a/virtual-patient-web/src/main/java/com/supervision/util/RasaUtil.java b/virtual-patient-web/src/main/java/com/supervision/util/RasaUtil.java deleted file mode 100644 index 644545b7..00000000 --- a/virtual-patient-web/src/main/java/com/supervision/util/RasaUtil.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.supervision.util; - -import cn.hutool.core.collection.CollUtil; -import cn.hutool.core.util.ObjectUtil; -import cn.hutool.http.HttpUtil; -import cn.hutool.json.JSONObject; -import cn.hutool.json.JSONUtil; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.supervision.pojo.paddlespeech.res.AsrResultDTO; -import com.supervision.pojo.paddlespeech.res.PaddleSpeechResDTO; -import com.supervision.pojo.rasa.train.dto.RasaReqDTO; -import com.supervision.pojo.rasa.train.dto.RasaResDTO; -import com.supervision.vo.rasa.RasaTalkVo; -import lombok.extern.slf4j.Slf4j; -import org.springframework.core.env.Environment; - -import java.util.List; -import java.util.stream.Collectors; - -@Slf4j -public class RasaUtil { - - private static final Environment environment = SpringBeanUtil.getBean(Environment.class); - private static final String RASA_TALK_URL = environment.getProperty("rasa.base-url") + environment.getProperty("rasa.talk"); - - public static String talkRasa(String question, String sessionId) { - RasaTalkVo rasaTalkVo = new RasaTalkVo(); - rasaTalkVo.setQuestion(question); - rasaTalkVo.setSessionId(sessionId); - // 默认为1 - rasaTalkVo.setModelId("1"); - String post = HttpUtil.post(RASA_TALK_URL, JSONUtil.toJsonStr(rasaTalkVo)); - log.info("调用rasa对话返回结果:{}", post); - try { - JSONObject entries = JSONUtil.parseObj(post); - List list = JSONUtil.toList(entries.getStr("data"), String.class); - if (CollUtil.isEmpty(list)) { - return "对不起,我没有听懂您说的是什么意思"; - } - return CollUtil.getFirst(list); - } catch (Exception e) { - log.error("talkRasa error ", e); - } - return "对不起,我没有听懂您说的是什么意思"; - } - - -} diff --git a/virtual-patient-web/src/main/resources/application-prod.yml b/virtual-patient-web/src/main/resources/application-prod.yml index 398222d1..4bce6a98 100644 --- a/virtual-patient-web/src/main/resources/application-prod.yml +++ b/virtual-patient-web/src/main/resources/application-prod.yml @@ -67,22 +67,7 @@ paddle-speech: # https://github.com/PaddlePaddle/PaddleSpeech/wiki/PaddleSpeech-Server-RESTful-API tts: http://virtual-patient-paddle-speech:8090/paddlespeech/tts asr: http://virtual-patient-paddle-speech:8090/paddlespeech/asr -rasa: - base-url: http://virtual-patient-rasa:8890/virtual-patient-rasa/ - talk: rasa/talkRasa - saveRasaFile: rasaFile/saveRasaFile - train: rasaCmd/trainExec - run: rasaCmd/runExec -human: - base-url: https://digital-human.jd.com - room-id: /getRoomId - text-driven: /text_driven - talk-status: /talkStatus - resourceMaxNumber: 5 -ws: - # nginx的wss地址(如果是wss的,那么带不带s都可以访问) - nginx-ip: virtual-patient-nginx - nginx-port: 443 + answer: # 对于没有匹配上的缺省回答ID,关联的是vp_file_resource的ID defaultNoMatchId: 1739173836351885313