yaxin 4 months ago
parent e56db6285e
commit 5201605876

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

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

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

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

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

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

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

Loading…
Cancel
Save