|
|
|
@ -2,9 +2,12 @@ package com.supervision.service.impl;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.codec.Base64;
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
|
|
import cn.hutool.core.date.TimeInterval;
|
|
|
|
|
import cn.hutool.core.lang.Assert;
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
|
|
import cn.hutool.crypto.digest.MD5;
|
|
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
|
import com.supervision.dto.dify.ChatResDTO;
|
|
|
|
|
import com.supervision.dto.paddlespeech.res.TtsResultDTO;
|
|
|
|
@ -16,10 +19,7 @@ import com.supervision.model.dify.DIFYChatReqInputVO;
|
|
|
|
|
import com.supervision.model.dify.DifyChatReqVO;
|
|
|
|
|
import com.supervision.model.dify.StreamResponse;
|
|
|
|
|
import com.supervision.service.IChatService;
|
|
|
|
|
import com.supervision.util.AsrUtil;
|
|
|
|
|
import com.supervision.util.DifyApiUtil;
|
|
|
|
|
import com.supervision.util.TtsUtil;
|
|
|
|
|
import com.supervision.util.WavUtil;
|
|
|
|
|
import com.supervision.util.*;
|
|
|
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
@ -36,8 +36,10 @@ import org.springframework.util.StopWatch;
|
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
import org.springframework.web.reactive.function.client.WebClient;
|
|
|
|
|
import reactor.core.publisher.Flux;
|
|
|
|
|
import reactor.core.publisher.Mono;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.time.Duration;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
@ -54,9 +56,12 @@ public class ChatServiceImpl implements IChatService {
|
|
|
|
|
|
|
|
|
|
@Value("${dify.url}")
|
|
|
|
|
private String difyUrl;
|
|
|
|
|
|
|
|
|
|
@Value("${dify.app-auth}")
|
|
|
|
|
private String difyAppAuth;
|
|
|
|
|
|
|
|
|
|
private final WebClient webClient;
|
|
|
|
|
|
|
|
|
|
private final DifyApiUtil difyApiUtil;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -83,7 +88,12 @@ public class ChatServiceImpl implements IChatService {
|
|
|
|
|
StringBuilder sentence = new StringBuilder();
|
|
|
|
|
log.info("query:{}", query);
|
|
|
|
|
|
|
|
|
|
return webClient.post()
|
|
|
|
|
PredefinedAnswerAssistant predefinedAnswerAssistant = new PredefinedAnswerAssistant();
|
|
|
|
|
TimeInterval timeInterval = DateUtil.timer();
|
|
|
|
|
timeInterval.start();
|
|
|
|
|
timeInterval.start("start");
|
|
|
|
|
log.info("request:时间:{}", DateUtil.now());
|
|
|
|
|
Flux<StreamResponse> flux = webClient.post()
|
|
|
|
|
.uri(difyUrl)
|
|
|
|
|
.headers(httpHeaders -> {
|
|
|
|
|
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
@ -91,28 +101,41 @@ public class ChatServiceImpl implements IChatService {
|
|
|
|
|
})
|
|
|
|
|
.bodyValue(JSON.toJSONString(difyChatReqVO))
|
|
|
|
|
.retrieve()
|
|
|
|
|
.bodyToFlux(StreamResponse.class)
|
|
|
|
|
.mapNotNull(response -> {
|
|
|
|
|
.bodyToFlux(StreamResponse.class);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Flux<StreamResponse> timeoutMono1 = Flux.interval(Duration.ofMillis(1000)).take(8).map(aLong -> {
|
|
|
|
|
StreamResponse timeoutResponse = new StreamResponse();
|
|
|
|
|
timeoutResponse.setEvent("timeout");
|
|
|
|
|
return timeoutResponse;
|
|
|
|
|
});
|
|
|
|
|
Flux<StreamResponse> mergedFlux = flux.mergeWith(timeoutMono1);
|
|
|
|
|
return mergedFlux.mapNotNull(response -> {
|
|
|
|
|
log.info("response:时间:{},响应回答:{}", timeInterval.intervalMs(), JSONUtil.toJsonStr(response));
|
|
|
|
|
Map<String, String> map = new HashMap<>();
|
|
|
|
|
map.put("event", response.getEvent());
|
|
|
|
|
if (response.getEvent().equals("message") && response.getAnswer() != null) {
|
|
|
|
|
log.info("response:时间:{}", DateUtil.now());
|
|
|
|
|
log.info("response:{}", response);
|
|
|
|
|
//遍历answer中的每一个字符,判断是否为标点符号,如果是,说明是句子的结尾,将标点符号前的文本拼接到sentence中,并打印,然后清空sentence,如果标点符号后还有文本,将文本拼接到sentence中
|
|
|
|
|
for (char ch : response.getAnswer().toCharArray()) {
|
|
|
|
|
sentence.append(ch);
|
|
|
|
|
fullAskString.append(ch);
|
|
|
|
|
if (ch == '。' || ch == '!' || ch == '?' || ch == ',' || ch == '、' || ch == '‘' || ch == '’' || ch == '“' || ch == '”') { // Check for punctuation marks
|
|
|
|
|
log.info(sentence.toString());
|
|
|
|
|
log.info("响应回答:{}", sentence);
|
|
|
|
|
TtsResultDTO ttsResultDTO = TtsUtil.ttsTransform(sentence.toString());
|
|
|
|
|
String voiceBaseId = UUID.randomUUID().toString();
|
|
|
|
|
audioList.add(voiceBaseId);
|
|
|
|
|
audioCache.put(voiceBaseId, ttsResultDTO.getAudio());
|
|
|
|
|
map.put("audioId", voiceBaseId);
|
|
|
|
|
sentence.setLength(0);
|
|
|
|
|
predefinedAnswerAssistant.closePreviewAnswer();
|
|
|
|
|
return ServerSentEvent.builder(map).build();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (response.getEvent().equals("message_end")) {
|
|
|
|
|
predefinedAnswerAssistant.closePreviewAnswer();
|
|
|
|
|
map.put("sessionId", response.getConversation_id());
|
|
|
|
|
if (!sentence.isEmpty()) {
|
|
|
|
|
log.info(sentence.toString());
|
|
|
|
@ -126,7 +149,7 @@ public class ChatServiceImpl implements IChatService {
|
|
|
|
|
String uuid = cn.hutool.core.lang.UUID.randomUUID().toString();
|
|
|
|
|
List<String> collect = audioList.stream().map(audioCache::get).filter(Objects::nonNull).collect(Collectors.toList());
|
|
|
|
|
String fullAnswer = WavUtil.mergeWavFilesToBase64(collect);
|
|
|
|
|
audioCache.put(uuid,fullAnswer);
|
|
|
|
|
audioCache.put(uuid, fullAnswer);
|
|
|
|
|
|
|
|
|
|
builder.answerInfo(AnswerInfo.builder().contentType(2).message(fullAskString.toString()).voiceBaseId(uuid).build());
|
|
|
|
|
builder.sessionId(response.getConversation_id());
|
|
|
|
@ -134,6 +157,29 @@ public class ChatServiceImpl implements IChatService {
|
|
|
|
|
this.appendDialogCache(response.getConversation_id(), builder.build());
|
|
|
|
|
return ServerSentEvent.builder(map).build();
|
|
|
|
|
}
|
|
|
|
|
if (predefinedAnswerAssistant.overThreshold()) {
|
|
|
|
|
if (StrUtil.isEmpty(difyChatReqVO.getConversation_id()) && !predefinedAnswerAssistant.isSkipFirst()) {
|
|
|
|
|
predefinedAnswerAssistant.skipFirst();
|
|
|
|
|
String text = predefinedAnswerAssistant.getHelloAnswer();
|
|
|
|
|
predefinedAnswerAssistant.resetInitTime(text);
|
|
|
|
|
log.info("超过阈值,返回预定义打招呼回答:{}", text);
|
|
|
|
|
String voiceBaseId = MD5.create().digestHex(text);
|
|
|
|
|
map.put("audioId", voiceBaseId);
|
|
|
|
|
log.info("response前置回答id:{}", voiceBaseId);
|
|
|
|
|
audioCache.put(voiceBaseId, predefinedAnswerAssistant.getHelloAnswerAudio(text));
|
|
|
|
|
return ServerSentEvent.builder(map).build();
|
|
|
|
|
} else {
|
|
|
|
|
String text = predefinedAnswerAssistant.getPredefinedAnswer();
|
|
|
|
|
log.info("超过阈值,返回预定义回答:{}", text);
|
|
|
|
|
predefinedAnswerAssistant.resetInitTime(text);
|
|
|
|
|
String voiceBaseId = MD5.create().digestHex(text);
|
|
|
|
|
map.put("audioId", voiceBaseId);
|
|
|
|
|
log.info("response前置回答id:{}", voiceBaseId);
|
|
|
|
|
audioCache.put(voiceBaseId, predefinedAnswerAssistant.getPredefinedAnswerAudio(text));
|
|
|
|
|
return ServerSentEvent.builder(map).build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}).filter(Objects::nonNull);
|
|
|
|
|
}
|
|
|
|
@ -186,7 +232,7 @@ public class ChatServiceImpl implements IChatService {
|
|
|
|
|
public void getAudio(HttpServletResponse response, String audioId) throws IOException {
|
|
|
|
|
|
|
|
|
|
log.info("audioId:{}", audioId);
|
|
|
|
|
if (StrUtil.isEmpty(audioId) && StrUtil.equals(audioId, "undefined")) {
|
|
|
|
|
if (StrUtil.isEmpty(audioId) || StrUtil.equals(audioId, "undefined")) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Base64.decodeToStream(audioCache.get(audioId), response.getOutputStream(), false);
|
|
|
|
@ -262,6 +308,8 @@ public class ChatServiceImpl implements IChatService {
|
|
|
|
|
return sb.toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 示例测试
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
String sampleText = "欢迎来到孟河小镇,体验独特的小镇风情;另外,还有梦和小镇等待你探访。";
|
|
|
|
|