微服务改造

dev_2.1.0
liu 1 year ago
parent e5b92b5759
commit 1e2b755174

@ -34,6 +34,11 @@
<artifactId>nacos-common</artifactId> <artifactId>nacos-common</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency> <dependency>
<groupId>com.alibaba.cloud</groupId> <groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
@ -65,9 +70,12 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId> <artifactId>spring-boot-maven-plugin</artifactId>
<configuration> <configuration>
<image> <excludes>
<builder>paketobuildpacks/builder-jammy-base:latest</builder> <exclude>
</image> <groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>

@ -4,12 +4,14 @@ import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans; import org.springframework.context.annotation.ComponentScans;
@SpringBootApplication @SpringBootApplication
@MapperScan(basePackages = {"com.supervision.**.mapper"}) @MapperScan(basePackages = {"com.supervision.**.mapper"})
@ComponentScan(basePackages = {"com.supervision.**"}) @ComponentScan(basePackages = {"com.supervision.**"})
@EnableFeignClients(basePackages = "com.supervision")
@EnableDiscoveryClient @EnableDiscoveryClient
public class VirtualPatientManageApplication { public class VirtualPatientManageApplication {

@ -19,6 +19,6 @@ public class RasaTalkVo {
* id * id
*/ */
@ApiModelProperty(hidden = true) @ApiModelProperty(hidden = true)
private String modelId; private String modelId = "1";
} }

@ -40,6 +40,11 @@
<artifactId>nacos-common</artifactId> <artifactId>nacos-common</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency> <dependency>
<groupId>com.alibaba.cloud</groupId> <groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>

@ -42,6 +42,11 @@
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency> <dependency>

@ -5,6 +5,7 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans; import org.springframework.context.annotation.ComponentScans;
import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.EnableScheduling;
@ -13,6 +14,7 @@ import org.springframework.web.socket.config.annotation.EnableWebSocket;
@SpringBootApplication @SpringBootApplication
@MapperScan(basePackages = {"com.supervision.**.mapper"}) @MapperScan(basePackages = {"com.supervision.**.mapper"})
@EnableScheduling @EnableScheduling
@EnableFeignClients(basePackages = "com.supervision")
@EnableDiscoveryClient @EnableDiscoveryClient
public class VirtualPatientApplication { public class VirtualPatientApplication {

@ -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.0rasaManage RasaCmdService.deployRasa
*/
@Deprecated
@RestController
@RequestMapping("rasa")
@RequiredArgsConstructor
public class RasaController {
private final RasaService rasaService;
@ApiOperation("生成rasa的yml文件")
@GetMapping("generateRasaYml")
public GlobalResult<String> generateRasaYml(String patientId) {
return rasaService.generateRasaYml(patientId);
}
@ApiOperation("训练rasa")
@GetMapping("trainRasa")
public GlobalResult<String> trainRasa(String patientId) throws JsonProcessingException {
return rasaService.trainRasa(patientId);
}
@ApiOperation("运行Rasa")
@GetMapping("runRasa")
public GlobalResult<String> runRasa(String patientId) throws JsonProcessingException {
return rasaService.runRasa(patientId);
}
}

@ -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<List<String>> talk(@RequestBody RasaTalkVo rasaTalkVo);
}

@ -15,8 +15,6 @@ public interface AskService {
String receiveVoiceFile(MultipartFile file); String receiveVoiceFile(MultipartFile file);
TalkResultResVO talk(TalkReqVO talkReqVO) throws IOException;
TalkResultResVO talkByVideo(TalkVideoReqVO talkReqVO) throws IOException; TalkResultResVO talkByVideo(TalkVideoReqVO talkReqVO) throws IOException;

@ -1,13 +0,0 @@
package com.supervision.service;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.supervision.domain.GlobalResult;
public interface RasaService {
GlobalResult<String> generateRasaYml(String diseaseId);
GlobalResult<String> trainRasa(String patientId) throws JsonProcessingException;
GlobalResult<String> runRasa(String patientId) throws JsonProcessingException;
}

@ -1,13 +1,19 @@
package com.supervision.service.impl; package com.supervision.service.impl;
import cn.hutool.core.codec.Base64Encoder; import cn.hutool.core.codec.Base64Encoder;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.MD5; 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 cn.hutool.setting.dialect.Props;
import com.supervision.domain.GlobalResult;
import com.supervision.exception.BusinessException; import com.supervision.exception.BusinessException;
import com.supervision.feign.RasaManageFeignClient;
import com.supervision.model.Process; import com.supervision.model.Process;
import com.supervision.model.*; import com.supervision.model.*;
import com.supervision.pojo.vo.TalkReqVO; 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.pojo.vo.TalkVideoReqVO;
import com.supervision.service.*; import com.supervision.service.*;
import com.supervision.util.*; import com.supervision.util.*;
import com.supervision.vo.rasa.RasaTalkVo;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
@ -46,6 +53,8 @@ public class AskServiceImpl implements AskService {
private final FileResourceService fileResourceService; private final FileResourceService fileResourceService;
private final RasaManageFeignClient rasaManageFeignClient;
@Value("${answer.defaultNoMatchId}") @Value("${answer.defaultNoMatchId}")
private String defaultNoMatchId; private String defaultNoMatchId;
@ -70,87 +79,6 @@ public class AskServiceImpl implements AskService {
return text; 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<String> 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) { private void saveQaRecord(String processId, String answerType, String answerId, String question, AskTemplateQuestionLibrary library, String resText) {
DiagnosisQaRecord record = new DiagnosisQaRecord(); DiagnosisQaRecord record = new DiagnosisQaRecord();
@ -195,7 +123,6 @@ public class AskServiceImpl implements AskService {
talkResultResVO.setActionType(new MD5().digestHex16(configAncillaryItem.getType())); talkResultResVO.setActionType(new MD5().digestHex16(configAncillaryItem.getType()));
} }
} }
} }
@Override @Override
@ -203,7 +130,7 @@ public class AskServiceImpl implements AskService {
// 根据processId找到对应的病人 // 根据processId找到对应的病人
Process process = Optional.ofNullable(processService.getById(talkReqVO.getProcessId())).orElseThrow(() -> new BusinessException("未找到诊疗进程")); Process process = Optional.ofNullable(processService.getById(talkReqVO.getProcessId())).orElseThrow(() -> new BusinessException("未找到诊疗进程"));
// 调用rasa获取文字内容 // 调用rasa获取文字内容
String rasaResult = RasaUtil.talkRasa(talkReqVO.getText(), UserUtil.getUser().getId()); String rasaResult = talkRasa(talkReqVO.getText(), UserUtil.getUser().getId());
log.info("rasa的回复是:{}", rasaResult); log.info("rasa的回复是:{}", rasaResult);
TalkResultResVO talkResultResVO = new TalkResultResVO(); TalkResultResVO talkResultResVO = new TalkResultResVO();
// 如果rasa没有识别出来,则返回默认值 // 如果rasa没有识别出来,则返回默认值
@ -287,6 +214,25 @@ public class AskServiceImpl implements AskService {
return talkResultResVO; return talkResultResVO;
} }
public String talkRasa(String question, String sessionId) {
RasaTalkVo rasaTalkVo = new RasaTalkVo();
rasaTalkVo.setQuestion(question);
rasaTalkVo.setSessionId(sessionId);
// 默认为1
rasaTalkVo.setModelId("1");
GlobalResult<List<String>> 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 * base64

@ -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<String> generateRasaYml(String patientId) {
Map<String, File> ymalFileMap = new HashMap<>();
// 默认问答MAP
Map<String, QuestionAnswerDTO> questionCodeAndIdMap = new HashMap<>();
List<RuleYmlTemplate.Rule> ruleList = new ArrayList<>();
// 开始生成各种yaml文件
generateNlu(patientId, questionCodeAndIdMap, ymalFileMap);
generateDomain(questionCodeAndIdMap, ruleList, ymalFileMap);
generateRule(ruleList, ymalFileMap);
// 生成压缩文件
List<File> tempFile = new ArrayList<>();
File tempZipFile = FileUtil.createTempFile(".zip", true);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try (ZipOutputStream zipOutputStream = new ZipOutputStream(bos)) {
for (Map.Entry<String, File> 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<GlobalResult<String>>() {
});
} 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<String, QuestionAnswerDTO> intentCodeAndIdMap,
Map<String, File> ymalFileMap) {
// 首先生成根据意图查找到nlu文件
List<NluYmlTemplate.Nlu> nluList = new ArrayList<>();
// 默认意图
List<AskTemplateQuestionLibrary> 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<ConfigPhysicalTool> 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<ConfigAncillaryItem> 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<String, QuestionAnswerDTO> questionCodeAndIdMap,
List<RuleYmlTemplate.Rule> ruleList, Map<String, File> ymalFileMap) {
LinkedHashMap<String, List<String>> responses = new LinkedHashMap<>();
for (Map.Entry<String, QuestionAnswerDTO> 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<String> intentList = new ArrayList<>(questionCodeAndIdMap.keySet());
domainYmlTemplate.setIntents(intentList);
// 回复
domainYmlTemplate.setResponses(responses);
// action
List<String> actionList = new ArrayList<>(responses.keySet());
domainYmlTemplate.setActions(actionList);
// 生成yml文件
createYmlFile(DomainYmlTemplate.class, "domain.ftl", domainYmlTemplate, "domain.yml", ymalFileMap);
}
/**
* rule
*/
public void generateRule(List<RuleYmlTemplate.Rule> ruleList, Map<String, File> 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<String, File> 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<String> trainRasa(String patientId) throws JsonProcessingException {
Map<String, Object> param = new HashMap<>();
param.put("modelId", patientId);
String responseBody = HttpUtil.post(trainRasaUrl, JSONUtil.toJsonStr(param));
return objectMapper.readValue(responseBody, new TypeReference<GlobalResult<String>>() {
});
}
@Override
public GlobalResult<String> runRasa(String patientId) throws JsonProcessingException {
Map<String, Object> param = new HashMap<>();
param.put("modelId", patientId);
String responseBody = HttpUtil.post(runRasaUrl, JSONUtil.toJsonStr(param));
return objectMapper.readValue(responseBody, new TypeReference<GlobalResult<String>>() {
});
}
}

@ -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<String> 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 "对不起,我没有听懂您说的是什么意思";
}
}

@ -67,22 +67,7 @@ paddle-speech:
# https://github.com/PaddlePaddle/PaddleSpeech/wiki/PaddleSpeech-Server-RESTful-API # https://github.com/PaddlePaddle/PaddleSpeech/wiki/PaddleSpeech-Server-RESTful-API
tts: http://virtual-patient-paddle-speech:8090/paddlespeech/tts tts: http://virtual-patient-paddle-speech:8090/paddlespeech/tts
asr: http://virtual-patient-paddle-speech:8090/paddlespeech/asr 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: answer:
# 对于没有匹配上的缺省回答ID,关联的是vp_file_resource的ID # 对于没有匹配上的缺省回答ID,关联的是vp_file_resource的ID
defaultNoMatchId: 1739173836351885313 defaultNoMatchId: 1739173836351885313

Loading…
Cancel
Save