From dac315f28c473129361a24d4e6688188281c7b74 Mon Sep 17 00:00:00 2001 From: "DESKTOP-DDTUS3E\\yaxin" Date: Wed, 27 Nov 2024 14:12:09 +0800 Subject: [PATCH] =?UTF-8?q?DIFY=E7=9F=A5=E8=AF=86=E5=BA=93=E9=97=AE?= =?UTF-8?q?=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../police/service/impl/ChatServiceImpl.java | 36 ++++++++++++------- .../com/supervision/police/vo/ChatResVO.java | 3 ++ .../vo/dify/KnowledgeBaseSegmentVO.java | 11 ++++++ 3 files changed, 38 insertions(+), 12 deletions(-) create mode 100644 src/main/java/com/supervision/police/vo/dify/KnowledgeBaseSegmentVO.java diff --git a/src/main/java/com/supervision/police/service/impl/ChatServiceImpl.java b/src/main/java/com/supervision/police/service/impl/ChatServiceImpl.java index b5e32e5..01f24ab 100644 --- a/src/main/java/com/supervision/police/service/impl/ChatServiceImpl.java +++ b/src/main/java/com/supervision/police/service/impl/ChatServiceImpl.java @@ -3,6 +3,8 @@ package com.supervision.police.service.impl; import cn.hutool.core.lang.Assert; import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.metadata.IPage; import com.supervision.chat.client.LangChainChatService; import com.supervision.chat.client.dto.chat.ChatReqDTO; @@ -18,11 +20,15 @@ import com.supervision.police.service.*; import com.supervision.police.vo.ChatReqVO; import com.supervision.police.vo.ChatResVO; import com.supervision.police.vo.dify.DifyChatReqVO; +import com.supervision.police.vo.dify.DifyChatResVO; +import com.supervision.police.vo.dify.KnowledgeBaseSegmentVO; import com.supervision.utils.DifyApiUtil; +import com.supervision.utils.UserUtil; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.util.*; @@ -77,17 +83,17 @@ public class ChatServiceImpl implements ChatService { return new ChatResVO(chat); } + @Transactional(transactionManager = "dataSourceTransactionManager", rollbackFor = Exception.class) @Override public ChatResVO chatNew(ChatReqVO chatReqVO) { ChatResVO chatResVO = new ChatResVO(); Map answerMap = new HashMap<>(); String query = chatReqVO.getQuery(); String caseId = chatReqVO.getCaseId(); - String userId = chatReqVO.getUserId(); + String userId = UserUtil.getUser().getId(); String type = chatReqVO.getType(); String intentType = chatReqVO.getIntentType(); long startTime = System.currentTimeMillis(); - // 会话创建或更新 if (StringUtils.isEmpty(chatReqVO.getConversationId())) { Conversation conversation = new Conversation(); conversation.setId(UUID.randomUUID().toString()); @@ -95,35 +101,40 @@ public class ChatServiceImpl implements ChatService { conversation.setUserId(userId); conversationService.save(conversation); chatReqVO.setConversationId(conversation.getId()); + log.info("会话已创建,conversationId:{}", conversation.getId()); } else { - conversationService.updateById(conversationService.getById(chatReqVO.getConversationId())); + Conversation conversation = conversationService.getById(chatReqVO.getConversationId()); + conversation.setUpdateTime(new Date()); + conversationService.updateById(conversation); + log.info("会话已更新,conversationId:{}", conversation.getId()); } List modelIndices = modelIndexService.list(); - // 问答类型判断 switch (type) { case QA_TYPE_DIFY: if (modelIndices.stream().map(ModelIndex::getName).anyMatch(query::contains)) { handleIndexResultQA(modelIndices, query, caseId, answerMap); - } else if (INTENT_TYPE_TEXT_CASE_RESULT.equals(query)) { + } else if (INTENT_TYPE_TEXT_CASE_RESULT.equals(query) || INTENT_TYPE_CASE_RESULT.equals(intentType)) { answerMap = JSON.parseObject(JSON.toJSONString(modelService.caseScoreDetail(caseId)), Map.class); - } else if (INTENT_TYPE_CASE_OVERVIEW.equals(query)) { + } else if (INTENT_TYPE_CASE_OVERVIEW.equals(query) || INTENT_TYPE_CASE_OVERVIEW.equals(intentType)) { ModelCase modelCase = modelCaseService.getById(caseId); answerMap.put("answerText", modelCase.getCaseDetail()); - } else if (INTENT_TYPE_CASE_EVIDENCE_GUIDE.equals(query)) { + } else if (INTENT_TYPE_CASE_EVIDENCE_GUIDE.equals(query) || INTENT_TYPE_CASE_EVIDENCE_GUIDE.equals(intentType)) { CaseScoreDetailDTO caseScoreDetailDTO = modelService.caseScoreDetail(caseId); answerMap.put("guideDesc", caseScoreDetailDTO.getGuideDesc()); } else { ModelCase modelCase = modelCaseService.getById(caseId); DifyChatReqVO difyChatReqVO = new DifyChatReqVO(); - difyChatReqVO.setUser(chatReqVO.getUserId()); + difyChatReqVO.setUser(userId); difyChatReqVO.setConversationId(chatReqVO.getConversationId()); - difyChatReqVO.setQuery(chatReqVO.getQuery()); - difyChatReqVO.setInputs(Map.of("dataset_id", "13c60b8c-341f-43ea-b3cc-5289a518abd9")); - System.out.println(difyApiUtil.chat(difyChatReqVO)); + difyChatReqVO.setQuery(query); + difyChatReqVO.setInputs(Map.of("dataset_id", modelCase.getKnowledgeBaseId())); + DifyChatResVO chat = difyApiUtil.chat(difyChatReqVO); + JSONObject json = JSON.parseObject(chat.getAnswer()); + chatResVO.setAnswer(json.getString("answerText")); + chatResVO.setSegmentVOList(JSONArray.parseArray(json.getString("quote"), KnowledgeBaseSegmentVO.class)); } break; case QA_TYPE_NX_LLM: - // 意图判断 switch (intentType) { case INTENT_TYPE_INDEX_RESULT: if (modelIndices.stream().map(ModelIndex::getName).anyMatch(query::contains)) { @@ -164,6 +175,7 @@ public class ChatServiceImpl implements ChatService { qa.setUserId(userId); qa.setConversationId(chatReqVO.getConversationId()); conversationQaService.save(qa); + log.info("QA已保存,qaId:{}", qa.getId()); chatResVO.setAnswwerMap(answerMap); chatResVO.setType(type); chatResVO.setIntentType(intentType); diff --git a/src/main/java/com/supervision/police/vo/ChatResVO.java b/src/main/java/com/supervision/police/vo/ChatResVO.java index 607a467..48f3fbd 100644 --- a/src/main/java/com/supervision/police/vo/ChatResVO.java +++ b/src/main/java/com/supervision/police/vo/ChatResVO.java @@ -1,8 +1,10 @@ package com.supervision.police.vo; import com.supervision.chat.client.dto.chat.ChatResDTO; +import com.supervision.police.vo.dify.KnowledgeBaseSegmentVO; import lombok.Data; +import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Objects; @@ -15,6 +17,7 @@ public class ChatResVO { private String type; private String intentType; private List docs; + private List segmentVOList = new ArrayList<>(); public ChatResVO() { } diff --git a/src/main/java/com/supervision/police/vo/dify/KnowledgeBaseSegmentVO.java b/src/main/java/com/supervision/police/vo/dify/KnowledgeBaseSegmentVO.java new file mode 100644 index 0000000..5c49e08 --- /dev/null +++ b/src/main/java/com/supervision/police/vo/dify/KnowledgeBaseSegmentVO.java @@ -0,0 +1,11 @@ +package com.supervision.police.vo.dify; + +import lombok.Data; + +@Data +public class KnowledgeBaseSegmentVO { + private String id; + private String name; + private String snippet; + private String score; +}