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.

81 lines
3.2 KiB
Java

package com.supervision.service.impl;
import cn.hutool.core.codec.Base64;
4 months ago
import com.supervision.dto.dify.ChatResDTO;
5 months ago
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;
4 months ago
import com.supervision.model.RobotTalkReq;
5 months ago
import com.supervision.model.dify.DIFYChatReqInputVO;
import com.supervision.model.dify.DifyChatReqVO;
import com.supervision.service.IChatService;
5 months ago
import com.supervision.util.AsrUtil;
import com.supervision.util.DifyApiUtil;
import com.supervision.util.TtsUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
5 months ago
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
5 months ago
import org.springframework.util.StopWatch;
5 months ago
import org.springframework.web.multipart.MultipartFile;
5 months ago
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
5 months ago
@Slf4j
@Service
public class ChatServiceImpl implements IChatService {
5 months ago
@Resource
private DifyApiUtil difyApiUtil;
5 months ago
Map<String, String> voiceCache = new HashMap<>();
5 months ago
@Override
4 months ago
public RobotTalkDTO talk(MultipartFile file, RobotTalkReq robotTalkReq) {
log.info("robotTalkReq:{}", robotTalkReq);
RobotTalkDTO.RobotTalkDTOBuilder builder = RobotTalkDTO.builder();
5 months ago
5 months ago
try {
5 months ago
byte[] bytes = file.getBytes();
StopWatch stopWatch = new StopWatch();
5 months ago
DifyChatReqVO difyChatReqVO = new DifyChatReqVO();
difyChatReqVO.setUser("admin");
DIFYChatReqInputVO inputs = new DIFYChatReqInputVO();
5 months ago
stopWatch.start("stt");
stopWatch.stop();
4 months ago
difyChatReqVO.setQuery(AsrUtil.asrTransformByBytes(bytes));
difyChatReqVO.setConversation_id(robotTalkReq.getSessionId());
5 months ago
stopWatch.start("dify");
4 months ago
ChatResDTO chatResDTO = difyApiUtil.chat(difyChatReqVO);
5 months ago
stopWatch.stop();
4 months ago
log.info("response:{}", chatResDTO.getAnswer());
builder.askInfo(AskInfo.builder().contentType(2).message(inputs.getQuery()).audioLength(100L).askId(chatResDTO.getMessage_id()).build());
voiceCache.put(chatResDTO.getMessage_id(), Base64.encode(bytes));
5 months ago
stopWatch.start("tts");
4 months ago
TtsResultDTO ttsResultDTO = TtsUtil.ttsTransform(chatResDTO.getAnswer());
5 months ago
stopWatch.stop();
String voiceBaseId = UUID.randomUUID().toString();
4 months ago
builder.answerInfo(AnswerInfo.builder().contentType(2).message(chatResDTO.getAnswer()).voiceBaseId(voiceBaseId).voiceBase64(ttsResultDTO.getAudio()).build());
builder.sessionId(chatResDTO.getConversation_id());
5 months ago
voiceCache.put(voiceBaseId, ttsResultDTO.getAudio());
log.info("耗时:{}", stopWatch.prettyPrint());
5 months ago
} catch (IOException e) {
throw new RuntimeException(e);
}
5 months ago
return builder.build();
}
@Override
public void getAudio(HttpServletResponse response, String audioId) throws IOException {
5 months ago
log.info("audioId:{}", audioId);
Base64.decodeToStream(voiceCache.get(audioId), response.getOutputStream(), false);
5 months ago
}
}