yaxin 2 months ago
parent e56db6285e
commit 5201605876

@ -47,7 +47,7 @@
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83_noneautotype</version>
<version>1.2.83</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>

@ -1,18 +1,21 @@
package com.supervision.contoller;
import com.supervision.service.IChatService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
@Slf4j
@RestController
@RequestMapping("/chat")
@RequiredArgsConstructor
public class ChatController {
private final IChatService chatService;
@PostMapping("/queryStatisticsData")
public String queryStatisticsData() {
return "success";
@PostMapping("/talk")
public String talk(@RequestParam("file") MultipartFile multipartFile) {
return chatService.talk(multipartFile);
}
}

@ -0,0 +1,45 @@
package com.supervision.vo.talk;
import cn.hutool.core.util.StrUtil;
import lombok.Data;
@Data
public class RobotTalkReq {
private String sessionId;
/**
* id
*/
private String askId;
private String message;
/**
* 1-2-3-4-5-
*/
private Integer answerType;
/**
*
*/
private boolean confirmFlag;
// ========方便前端好传参=======>>>>>>
/**
*
*/
private String name;
/**
*
*/
private String cardNumber;
/**
*
*/
private String idNumber;
// <<<<<========方便前端好传参=======
}

@ -4,14 +4,5 @@ import lombok.Data;
@Data
public class DIFYChatReqInputVO {
private String type;
private String intent_type;
private String case_id;
private String user_id;
private String conversation_id;
private String dataset_id;
private String participator;
private int dialogue_count;
private String previous_query;
private String previous_answer;
private String query;
}

@ -6,9 +6,7 @@ import static com.supervision.common.constant.DifyConstants.CHAT_RESPONSE_MODE_B
@Data
public class DifyChatReqVO {
private String conversationId;
private String user;
private String query;
private String response_mode = CHAT_RESPONSE_MODE_BLOCKING;
private DIFYChatReqInputVO inputs = new DIFYChatReqInputVO();
}

@ -2,15 +2,11 @@ package com.supervision.model.dify;
import lombok.Data;
import java.util.Map;
@Data
public class DifyChatResVO {
private String event;
private String task_id;
private String id;
private String message_id;
private String conversation_id;
private String mode;
private String answer;
private Metadata metadata;
private String created_at;
private String workflow_run_id;
private Map<String, Object> data;
}

@ -1,4 +1,8 @@
package com.supervision.service;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
public interface IChatService {
String talk(MultipartFile file);
}

@ -1,6 +1,48 @@
package com.supervision.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.supervision.dto.paddlespeech.res.TtsResultDTO;
import com.supervision.model.dify.DIFYChatReqInputVO;
import com.supervision.model.dify.DifyChatReqVO;
import com.supervision.model.dify.DifyChatResVO;
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 lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
@Slf4j
@Service
public class ChatServiceImpl implements IChatService {
@Resource
private DifyApiUtil difyApiUtil;
@Override
public String talk(MultipartFile file) {
String talkResult = "";
try {
DifyChatReqVO difyChatReqVO = new DifyChatReqVO();
difyChatReqVO.setUser("admin");
DIFYChatReqInputVO inputs = new DIFYChatReqInputVO();
inputs.setQuery(AsrUtil.asrTransformByBytes(file.getBytes()));
difyChatReqVO.setInputs(inputs);
String response = difyApiUtil.chat(difyChatReqVO);
log.info("response:{}", response);
TtsResultDTO ttsResultDTO = TtsUtil.ttsTransform(response);
talkResult = JSON.toJSONString(ttsResultDTO);
log.info("ttsResultDTO:{}", JSON.toJSONString(ttsResultDTO));
} catch (IOException e) {
throw new RuntimeException(e);
}
return talkResult;
}
}

@ -1,8 +1,8 @@
package com.supervision.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.supervision.model.dify.DifyChatReqVO;
import com.supervision.model.dify.DifyChatResVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.hc.client5.http.ClientProtocolException;
import org.apache.hc.client5.http.classic.methods.HttpPost;
@ -18,35 +18,32 @@ import org.springframework.stereotype.Component;
import java.nio.charset.StandardCharsets;
import static com.supervision.common.constant.DifyConstants.METHOD_CHAT_MESSAGES;
@Component
@Slf4j
public class DifyApiUtil {
@Value("${dify.url}")
private String difyUrl;
@Value("${dify.dataset-auth}")
private String difyDatasetAuth;
@Value("${dify.app-auth}")
private String difyAppAuth;
public DifyChatResVO chat(DifyChatReqVO difyChatReqVO) {
DifyChatResVO difyChatResVO = null;
public String chat(DifyChatReqVO difyChatReqVO) {
String result = null;
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpPost httpPost = new HttpPost(difyUrl + METHOD_CHAT_MESSAGES);
HttpPost httpPost = new HttpPost(difyUrl);
httpPost.setHeader(HttpHeaders.AUTHORIZATION, difyAppAuth);
httpPost.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
log.info("发起对话:{}", difyChatReqVO);
StringEntity entity = new StringEntity(com.alibaba.fastjson.JSONObject.toJSON(difyChatReqVO).toString(), StandardCharsets.UTF_8);
httpPost.setEntity(entity);
difyChatResVO = httpClient.execute(httpPost, response -> {
result = httpClient.execute(httpPost, response -> {
final int status = response.getCode();
if (status >= HttpStatus.SC_SUCCESS && status < HttpStatus.SC_REDIRECTION) {
final HttpEntity responseEntity = response.getEntity();
String responseStr = EntityUtils.toString(responseEntity);
log.info("发起对话成功!");
return JSON.parseObject(responseStr, DifyChatResVO.class);
log.info("DIFY返回{}", responseStr);
JSONObject jsonObject = JSON.parseObject(responseStr);
return jsonObject.getJSONObject("data").getJSONObject("outputs").get("answerText").toString();
} else {
log.info("responseEntity {}", EntityUtils.toString(response.getEntity()));
throw new ClientProtocolException("Unexpected response status: " + status);
@ -55,7 +52,7 @@ public class DifyApiUtil {
} catch (Exception e) {
log.error("发起对话失败", e);
}
return difyChatResVO;
return result;
}
}

@ -6,9 +6,8 @@ server:
servlet:
context-path: /speech-demo-service
dify:
url: http://192.168.10.137/v1
dataset-auth: Bearer dataset-g3RctoaHdvXAAQjUVNMuUWCY
app-auth: Bearer app-YrNuU7Puxr3ko8ftUdWtDKhz
url: http://192.168.10.137/v1/workflows/run
app-auth: Bearer app-cSxbjvdTZQhq2p2cKinChIhj
paddle-speech:
tts: http://192.168.10.138:8090/paddlespeech/tts
asr: http://192.168.10.138:8090/paddlespeech/asr

Loading…
Cancel
Save