You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virtual-patient/virtual-patient-web/src/main/java/com/supervision/service/impl/AskServiceImpl.java

112 lines
5.0 KiB
Java

2 years ago
package com.supervision.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.supervision.exception.BusinessException;
import com.supervision.model.DiagnosisQaRecord;
import com.supervision.model.Process;
import com.supervision.pojo.vo.TalkResultResVO;
import com.supervision.service.AskDefaultQuestionAnswerService;
import com.supervision.service.AskDiseaseQuestionAnswerService;
import com.supervision.service.ProcessService;
import com.supervision.util.*;
2 years ago
import com.supervision.service.AskService;
import com.supervision.pojo.vo.ActionDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
2 years ago
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
2 years ago
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.Random;
2 years ago
@Slf4j
2 years ago
@Service
@RequiredArgsConstructor
2 years ago
public class AskServiceImpl implements AskService {
private final ProcessService processService;
private final AskDiseaseQuestionAnswerService askDiseaseQuestionAnswerService;
private final AskDefaultQuestionAnswerService askDefaultQuestionAnswerService;
2 years ago
@Override
@Transactional(rollbackFor = Exception.class)
public TalkResultResVO receiveVoiceFile(MultipartFile file, String processId, String roomKey, String roomToken) throws IOException {
// 根据processId找到对应的病人
Process process = Optional.ofNullable(processService.getById(processId)).orElseThrow(() -> new BusinessException("未找到诊疗进程"));
// 获取音频对应的文字
String askQuestion = AsrUtil.asrTransformByBytes(file.getBytes());
// 调用rasa获取文字内容
String rasaResult = RasaUtil.talkRasa(askQuestion, UserUtil.getUser().getId(), process.getPatientId());
TalkResultResVO talkResultResVO = new TalkResultResVO();
// 这里校验,rasa回复的结果是不是action
// 这里设置的模板,对于action的动作全部是用ancillary_ | tool_进行标记,详情看生成rasa的yml的代码:RasaServiceImpl.generateDomain
// ancillary_ | tool_
if (rasaResult.startsWith("ancillary_") || rasaResult.startsWith("tool_")) {
List<String> actionList = StrUtil.split(rasaResult, '_');
if (actionList.size() > 1) {
ActionDTO actionDTO = new ActionDTO();
actionDTO.setActionType(actionList.get(0));
actionDTO.setActionId(actionList.get(1));
// 在这里给socket回复,设置为动作
talkResultResVO.setAction(actionDTO);
talkResultResVO.setType(2);
return talkResultResVO;
}
} else {
// 语音消息,这时调用京东的接口进行播放操作
// 这里调用京东数字人接口首先根据token获取房间号
String roomId = HumanUtil.queryRoomId(roomKey, roomToken);
// 区分
List<String> answerIdList = StrUtil.split(rasaResult, '_');
String qaId = null;
String qaType = "miss";
String answer = "您好,我没有听懂您说什么";
if (answerIdList.size() > 1) {
if (rasaResult.startsWith("default_")) {
qaType = "default";
List<String> answerList = Optional.ofNullable(askDefaultQuestionAnswerService.getById(answerIdList.get(1)).getAnswer())
.orElse(CollUtil.newArrayList("您好,我没有听懂您说什么"));
answer = answerList.get(new Random().nextInt(answerList.size()));
qaId = answerIdList.get(1);
2 years ago
} else if (rasaResult.startsWith("disease_")) {
qaType = "disease";
List<String> answerList = Optional.ofNullable(askDiseaseQuestionAnswerService.getById(answerIdList.get(1)).getAnswer())
.orElse(CollUtil.newArrayList("您好,我没有听懂您说什么"));
answer = answerList.get(new Random().nextInt(answerList.size()));
qaId = answerIdList.get(1);
}
}
// 然后这里进行播放
// 这里应该从对话中取结果
HumanUtil.textDriven(rasaResult, roomId);
// 保存记录
DiagnosisQaRecord record = new DiagnosisQaRecord();
record.setProcessId(processId);
record.setQuestionAnswerType(qaType);
record.setQuestionAnswerId(qaId);
record.setQuestion(askQuestion);
record.setAnswer(answer);
record.setCreateUserId(UserUtil.getUser().getId());
record.insert();
}
talkResultResVO.setType(1);
return talkResultResVO;
}
@Override
public String replyVoice() {
String text = "测试:这是文字转语音的测试,测试是否OK";
return TtsUtil.ttsTransform(text);
}
2 years ago
}