|
|
@@ -0,0 +1,99 @@
|
|
|
+package com.storlead.knowledge.service.impl;
|
|
|
+
|
|
|
+import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
+import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
+import com.fasterxml.jackson.databind.JsonNode;
|
|
|
+import com.storlead.framework.common.result.Result;
|
|
|
+import com.storlead.knowledge.config.DifyProperties;
|
|
|
+import com.storlead.knowledge.entity.AiMessageEntity;
|
|
|
+import com.storlead.knowledge.pojo.dto.ChatDTO;
|
|
|
+import com.storlead.knowledge.service.AiMessageService;
|
|
|
+import com.storlead.knowledge.service.ChatService;
|
|
|
+import com.storlead.knowledge.utils.HttpService;
|
|
|
+import com.storlead.knowledge.utils.JacksonHolder;
|
|
|
+import lombok.extern.flogger.Flogger;
|
|
|
+import lombok.extern.log4j.Log4j2;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 对话服务实现
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@Log4j2
|
|
|
+public class ChatServiceImpl implements ChatService {
|
|
|
+
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(ChatServiceImpl.class);
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private HttpService httpService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private DifyProperties difyProperties;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private AiMessageService aiMessageService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result<Object> requestAnalysisToAi(ChatDTO chatDTO) {
|
|
|
+ try {
|
|
|
+ String url = difyProperties.getBaseUrl() + "chat-messages";
|
|
|
+ String body = JacksonHolder.OBJECT_MAPPER.writeValueAsString(chatDTO);
|
|
|
+ Result<Object> result = httpService.post(url, null, "Bearer " + chatDTO.getAppId(), body, new TypeReference<>() {});
|
|
|
+
|
|
|
+ if (result.isSuccess() && result.getResult() != null) {
|
|
|
+ saveBlockingMessage(chatDTO, result.getResult());
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }catch (Exception e){
|
|
|
+ return Result.error("requestAnalysis -- error",e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void asynRequestAnalysisToAi(ChatDTO chatDTO) {
|
|
|
+ try {
|
|
|
+ String url = difyProperties.getBaseUrl() + "chat-messages";
|
|
|
+ String body = JacksonHolder.OBJECT_MAPPER.writeValueAsString(chatDTO);
|
|
|
+ Result<Object> result = httpService.post(url, null, "Bearer " + chatDTO.getAppId(), body, new TypeReference<>() {
|
|
|
+ });
|
|
|
+ if (result.isSuccess() && result.getResult() != null) {
|
|
|
+ saveBlockingMessage(chatDTO, result.getResult());
|
|
|
+ }
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error("requestAsynAnalysisToAi -- error",e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void saveBlockingMessage(ChatDTO chatDTO, Object responseData) {
|
|
|
+ try {
|
|
|
+ String json = JacksonHolder.OBJECT_MAPPER.writeValueAsString(responseData);
|
|
|
+ JsonNode root = JacksonHolder.OBJECT_MAPPER.readTree(json);
|
|
|
+ AiMessageEntity entity = new AiMessageEntity();
|
|
|
+ entity.setTaskId(getJsonText(root, "task_id"));
|
|
|
+ entity.setMessageId(getJsonText(root, "message_id"));
|
|
|
+ entity.setConversationId(getJsonText(root, "conversation_id"));
|
|
|
+ entity.setAppId(chatDTO.getAppId());
|
|
|
+ entity.setInputs(chatDTO.getInputs() != null ? JacksonHolder.OBJECT_MAPPER.writeValueAsString(chatDTO.getInputs()) : null);
|
|
|
+ entity.setQuery(chatDTO.getQuery());
|
|
|
+ entity.setResponseMode(chatDTO.getResponse_mode());
|
|
|
+ entity.setUserId(chatDTO.getUser());
|
|
|
+ entity.setAnswer(getJsonText(root, "answer"));
|
|
|
+ String createdAt = getJsonText(root, "created_at");
|
|
|
+ if (createdAt != null) {
|
|
|
+ entity.setCreatedAt(Integer.parseInt(createdAt));
|
|
|
+ }
|
|
|
+ aiMessageService.save(entity);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("解析并保存blocking聊天记录失败", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getJsonText(JsonNode node, String field) {
|
|
|
+ JsonNode value = node.get(field);
|
|
|
+ return value != null && !value.isNull() ? value.asText() : null;
|
|
|
+ }
|
|
|
+}
|