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.

72 lines
2.7 KiB
Java

package com.supervision.service.impl;
import cn.hutool.core.codec.Base64;
5 months ago
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;
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;
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;
Map<String,String> voiceCache = new HashMap<>();
5 months ago
@Override
public RobotTalkDTO talk(MultipartFile file) {
5 months ago
String talkResult = "";
RobotTalkDTO.RobotTalkDTOBuilder builder = RobotTalkDTO.builder().sessionId("111");
5 months ago
try {
byte[] bytes = file.getBytes();
5 months ago
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));
5 months ago
difyChatReqVO.setInputs(inputs);
String response = difyApiUtil.chat(difyChatReqVO);
log.info("response:{}", response);
5 months ago
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());
5 months ago
} 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);
5 months ago
}
}