|
|
|
package com.supervision.service.impl;
|
|
|
|
|
|
|
|
import cn.hutool.core.codec.Base64;
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
import com.supervision.dto.paddlespeech.res.TtsResultDTO;
|
|
|
|
import com.supervision.dto.robot.AnswerInfo;
|
|
|
|
import com.supervision.dto.robot.AskInfo;
|
|
|
|
import com.supervision.dto.robot.RobotTalkDTO;
|
|
|
|
import com.supervision.model.dify.DIFYChatReqInputVO;
|
|
|
|
import com.supervision.model.dify.DifyChatReqVO;
|
|
|
|
import com.supervision.service.IChatService;
|
|
|
|
import com.supervision.util.AsrUtil;
|
|
|
|
import com.supervision.util.DifyApiUtil;
|
|
|
|
import com.supervision.util.TtsUtil;
|
|
|
|
import jakarta.annotation.Resource;
|
|
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
@Service
|
|
|
|
public class ChatServiceImpl implements IChatService {
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
private DifyApiUtil difyApiUtil;
|
|
|
|
|
|
|
|
Map<String,String> voiceCache = new HashMap<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public RobotTalkDTO talk(MultipartFile file) {
|
|
|
|
String talkResult = "";
|
|
|
|
RobotTalkDTO.RobotTalkDTOBuilder builder = RobotTalkDTO.builder().sessionId("111");
|
|
|
|
try {
|
|
|
|
byte[] bytes = file.getBytes();
|
|
|
|
DifyChatReqVO difyChatReqVO = new DifyChatReqVO();
|
|
|
|
difyChatReqVO.setUser("admin");
|
|
|
|
DIFYChatReqInputVO inputs = new DIFYChatReqInputVO();
|
|
|
|
inputs.setQuery(AsrUtil.asrTransformByBytes(bytes));
|
|
|
|
String askUUid = UUID.randomUUID().toString();
|
|
|
|
builder.askInfo(AskInfo.builder().contentType(2).message(inputs.getQuery()).audioLength(100L).askId(askUUid).build());
|
|
|
|
voiceCache.put(askUUid,Base64.encode(bytes));
|
|
|
|
difyChatReqVO.setInputs(inputs);
|
|
|
|
String response = difyApiUtil.chat(difyChatReqVO);
|
|
|
|
log.info("response:{}", response);
|
|
|
|
|
|
|
|
TtsResultDTO ttsResultDTO = TtsUtil.ttsTransform(response);
|
|
|
|
log.info("ttsResultDTO:{}", JSON.toJSONString(ttsResultDTO));
|
|
|
|
String voiceBaseId = UUID.randomUUID().toString();
|
|
|
|
builder.answerInfo(AnswerInfo.builder().contentType(2).message(response).voiceBaseId(voiceBaseId).voiceBase64(ttsResultDTO.getAudio()).build());
|
|
|
|
voiceCache.put(voiceBaseId,ttsResultDTO.getAudio());
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
return builder.build();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void getAudio(HttpServletResponse response, String audioId) throws IOException {
|
|
|
|
|
|
|
|
log.info("audioId:{}",audioId);
|
|
|
|
Base64.decodeToStream(voiceCache.get(audioId), response.getOutputStream(),false);
|
|
|
|
}
|
|
|
|
}
|