|
|
@ -1,6 +1,7 @@
|
|
|
|
package com.supervision.service.impl;
|
|
|
|
package com.supervision.service.impl;
|
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.codec.Base64;
|
|
|
|
import cn.hutool.core.codec.Base64;
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
import com.supervision.dto.dify.ChatResDTO;
|
|
|
|
import com.supervision.dto.dify.ChatResDTO;
|
|
|
|
import com.supervision.dto.paddlespeech.res.TtsResultDTO;
|
|
|
|
import com.supervision.dto.paddlespeech.res.TtsResultDTO;
|
|
|
|
import com.supervision.dto.robot.AnswerInfo;
|
|
|
|
import com.supervision.dto.robot.AnswerInfo;
|
|
|
@ -9,21 +10,27 @@ import com.supervision.dto.robot.RobotTalkDTO;
|
|
|
|
import com.supervision.model.RobotTalkReq;
|
|
|
|
import com.supervision.model.RobotTalkReq;
|
|
|
|
import com.supervision.model.dify.DIFYChatReqInputVO;
|
|
|
|
import com.supervision.model.dify.DIFYChatReqInputVO;
|
|
|
|
import com.supervision.model.dify.DifyChatReqVO;
|
|
|
|
import com.supervision.model.dify.DifyChatReqVO;
|
|
|
|
|
|
|
|
import com.supervision.model.dify.StreamResponse;
|
|
|
|
import com.supervision.service.IChatService;
|
|
|
|
import com.supervision.service.IChatService;
|
|
|
|
import com.supervision.util.AsrUtil;
|
|
|
|
import com.supervision.util.AsrUtil;
|
|
|
|
import com.supervision.util.DifyApiUtil;
|
|
|
|
import com.supervision.util.DifyApiUtil;
|
|
|
|
import com.supervision.util.TtsUtil;
|
|
|
|
import com.supervision.util.TtsUtil;
|
|
|
|
import jakarta.annotation.Resource;
|
|
|
|
|
|
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
|
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import net.sourceforge.pinyin4j.PinyinHelper;
|
|
|
|
import net.sourceforge.pinyin4j.PinyinHelper;
|
|
|
|
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
|
|
|
|
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
|
|
|
|
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
|
|
|
|
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
|
|
|
|
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
|
|
|
|
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
|
|
|
|
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
|
|
|
|
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
|
|
|
import org.springframework.http.MediaType;
|
|
|
|
|
|
|
|
import org.springframework.http.codec.ServerSentEvent;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import org.springframework.util.StopWatch;
|
|
|
|
import org.springframework.util.StopWatch;
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
import org.springframework.web.reactive.function.client.WebClient;
|
|
|
|
|
|
|
|
import reactor.core.publisher.Flux;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashMap;
|
|
|
@ -34,14 +41,70 @@ import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
@Slf4j
|
|
|
|
@Service
|
|
|
|
@Service
|
|
|
|
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
public class ChatServiceImpl implements IChatService {
|
|
|
|
public class ChatServiceImpl implements IChatService {
|
|
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
@Value("${dify.url}")
|
|
|
|
private DifyApiUtil difyApiUtil;
|
|
|
|
private String difyUrl;
|
|
|
|
|
|
|
|
@Value("${dify.app-auth}")
|
|
|
|
|
|
|
|
private String difyAppAuth;
|
|
|
|
|
|
|
|
private final WebClient webClient;
|
|
|
|
|
|
|
|
private final DifyApiUtil difyApiUtil;
|
|
|
|
Map<String, String> voiceCache = new HashMap<>();
|
|
|
|
Map<String, String> voiceCache = new HashMap<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public Flux<ServerSentEvent<Map<String, String>>> streamingMessage(String query) {
|
|
|
|
|
|
|
|
DifyChatReqVO difyChatReqVO = new DifyChatReqVO();
|
|
|
|
|
|
|
|
difyChatReqVO.setUser("admin");
|
|
|
|
|
|
|
|
DIFYChatReqInputVO inputs = new DIFYChatReqInputVO();
|
|
|
|
|
|
|
|
difyChatReqVO.setQuery(query);
|
|
|
|
|
|
|
|
// difyChatReqVO.setQuery("尽可能详细的介绍一下勐赫小镇的医疗服务");
|
|
|
|
|
|
|
|
difyChatReqVO.setInputs(inputs);
|
|
|
|
|
|
|
|
StringBuilder sentence = new StringBuilder();
|
|
|
|
|
|
|
|
log.info("query:{}", query);
|
|
|
|
|
|
|
|
return webClient.post()
|
|
|
|
|
|
|
|
.uri(difyUrl)
|
|
|
|
|
|
|
|
.headers(httpHeaders -> {
|
|
|
|
|
|
|
|
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
|
|
|
|
httpHeaders.setBearerAuth(difyAppAuth);
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.bodyValue(JSON.toJSONString(difyChatReqVO))
|
|
|
|
|
|
|
|
.retrieve()
|
|
|
|
|
|
|
|
.bodyToFlux(StreamResponse.class)
|
|
|
|
|
|
|
|
.map(response -> {
|
|
|
|
|
|
|
|
Map<String, String> map = new HashMap<>();
|
|
|
|
|
|
|
|
map.put("event", response.getEvent());
|
|
|
|
|
|
|
|
if (response.getEvent().equals("message") && response.getAnswer() != null) {
|
|
|
|
|
|
|
|
String voiceBaseId = UUID.randomUUID().toString();
|
|
|
|
|
|
|
|
//遍历answer中的每一个字符,判断是否为标点符号,如果是,说明是句子的结尾,将标点符号前的文本拼接到sentence中,并打印,然后清空sentence,如果标点符号后还有文本,将文本拼接到sentence中
|
|
|
|
|
|
|
|
for (char ch : response.getAnswer().toCharArray()) {
|
|
|
|
|
|
|
|
sentence.append(ch);
|
|
|
|
|
|
|
|
if (ch == '。' || ch == '!' || ch == '?' || ch == ',' || ch == '、' || ch == '‘' || ch == '’' || ch == '“' || ch == '”') { // Check for punctuation marks
|
|
|
|
|
|
|
|
log.info(sentence.toString());
|
|
|
|
|
|
|
|
TtsResultDTO ttsResultDTO = TtsUtil.ttsTransform(sentence.toString());
|
|
|
|
|
|
|
|
voiceCache.put(voiceBaseId, ttsResultDTO.getAudio());
|
|
|
|
|
|
|
|
map.put("audioId", voiceBaseId);
|
|
|
|
|
|
|
|
sentence.setLength(0); // Clear the sentence
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.getEvent().equals("message_end") && !sentence.isEmpty()) {
|
|
|
|
|
|
|
|
log.info(sentence.toString());
|
|
|
|
|
|
|
|
TtsResultDTO ttsResultDTO = TtsUtil.ttsTransform(sentence.toString());
|
|
|
|
|
|
|
|
voiceCache.put(voiceBaseId, ttsResultDTO.getAudio());
|
|
|
|
|
|
|
|
map.put("audioId", voiceBaseId);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return ServerSentEvent.builder(map).build();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return ServerSentEvent.builder(map).build();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public String asr(MultipartFile file) throws IOException {
|
|
|
|
|
|
|
|
return replaceTown(AsrUtil.asrTransformByBytes(file.getBytes()));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public RobotTalkDTO talk(MultipartFile file, RobotTalkReq robotTalkReq) {
|
|
|
|
public RobotTalkDTO talk(MultipartFile file, RobotTalkReq robotTalkReq) {
|
|
|
|
log.info("robotTalkReq:{}", robotTalkReq);
|
|
|
|
log.info("robotTalkReq:{}", robotTalkReq);
|
|
|
|