DIFY知识库问答

jinan_dev
DESKTOP-DDTUS3E\yaxin 5 months ago
parent 6baed0d345
commit dac315f28c

@ -3,6 +3,8 @@ package com.supervision.police.service.impl;
import cn.hutool.core.lang.Assert; import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.supervision.chat.client.LangChainChatService; import com.supervision.chat.client.LangChainChatService;
import com.supervision.chat.client.dto.chat.ChatReqDTO; 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.ChatReqVO;
import com.supervision.police.vo.ChatResVO; import com.supervision.police.vo.ChatResVO;
import com.supervision.police.vo.dify.DifyChatReqVO; 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.DifyApiUtil;
import com.supervision.utils.UserUtil;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*; import java.util.*;
@ -77,17 +83,17 @@ public class ChatServiceImpl implements ChatService {
return new ChatResVO(chat); return new ChatResVO(chat);
} }
@Transactional(transactionManager = "dataSourceTransactionManager", rollbackFor = Exception.class)
@Override @Override
public ChatResVO chatNew(ChatReqVO chatReqVO) { public ChatResVO chatNew(ChatReqVO chatReqVO) {
ChatResVO chatResVO = new ChatResVO(); ChatResVO chatResVO = new ChatResVO();
Map<String, Object> answerMap = new HashMap<>(); Map<String, Object> answerMap = new HashMap<>();
String query = chatReqVO.getQuery(); String query = chatReqVO.getQuery();
String caseId = chatReqVO.getCaseId(); String caseId = chatReqVO.getCaseId();
String userId = chatReqVO.getUserId(); String userId = UserUtil.getUser().getId();
String type = chatReqVO.getType(); String type = chatReqVO.getType();
String intentType = chatReqVO.getIntentType(); String intentType = chatReqVO.getIntentType();
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
// 会话创建或更新
if (StringUtils.isEmpty(chatReqVO.getConversationId())) { if (StringUtils.isEmpty(chatReqVO.getConversationId())) {
Conversation conversation = new Conversation(); Conversation conversation = new Conversation();
conversation.setId(UUID.randomUUID().toString()); conversation.setId(UUID.randomUUID().toString());
@ -95,35 +101,40 @@ public class ChatServiceImpl implements ChatService {
conversation.setUserId(userId); conversation.setUserId(userId);
conversationService.save(conversation); conversationService.save(conversation);
chatReqVO.setConversationId(conversation.getId()); chatReqVO.setConversationId(conversation.getId());
log.info("会话已创建conversationId:{}", conversation.getId());
} else { } 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<ModelIndex> modelIndices = modelIndexService.list(); List<ModelIndex> modelIndices = modelIndexService.list();
// 问答类型判断
switch (type) { switch (type) {
case QA_TYPE_DIFY: case QA_TYPE_DIFY:
if (modelIndices.stream().map(ModelIndex::getName).anyMatch(query::contains)) { if (modelIndices.stream().map(ModelIndex::getName).anyMatch(query::contains)) {
handleIndexResultQA(modelIndices, query, caseId, answerMap); 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); 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); ModelCase modelCase = modelCaseService.getById(caseId);
answerMap.put("answerText", modelCase.getCaseDetail()); 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); CaseScoreDetailDTO caseScoreDetailDTO = modelService.caseScoreDetail(caseId);
answerMap.put("guideDesc", caseScoreDetailDTO.getGuideDesc()); answerMap.put("guideDesc", caseScoreDetailDTO.getGuideDesc());
} else { } else {
ModelCase modelCase = modelCaseService.getById(caseId); ModelCase modelCase = modelCaseService.getById(caseId);
DifyChatReqVO difyChatReqVO = new DifyChatReqVO(); DifyChatReqVO difyChatReqVO = new DifyChatReqVO();
difyChatReqVO.setUser(chatReqVO.getUserId()); difyChatReqVO.setUser(userId);
difyChatReqVO.setConversationId(chatReqVO.getConversationId()); difyChatReqVO.setConversationId(chatReqVO.getConversationId());
difyChatReqVO.setQuery(chatReqVO.getQuery()); difyChatReqVO.setQuery(query);
difyChatReqVO.setInputs(Map.of("dataset_id", "13c60b8c-341f-43ea-b3cc-5289a518abd9")); difyChatReqVO.setInputs(Map.of("dataset_id", modelCase.getKnowledgeBaseId()));
System.out.println(difyApiUtil.chat(difyChatReqVO)); 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; break;
case QA_TYPE_NX_LLM: case QA_TYPE_NX_LLM:
// 意图判断
switch (intentType) { switch (intentType) {
case INTENT_TYPE_INDEX_RESULT: case INTENT_TYPE_INDEX_RESULT:
if (modelIndices.stream().map(ModelIndex::getName).anyMatch(query::contains)) { if (modelIndices.stream().map(ModelIndex::getName).anyMatch(query::contains)) {
@ -164,6 +175,7 @@ public class ChatServiceImpl implements ChatService {
qa.setUserId(userId); qa.setUserId(userId);
qa.setConversationId(chatReqVO.getConversationId()); qa.setConversationId(chatReqVO.getConversationId());
conversationQaService.save(qa); conversationQaService.save(qa);
log.info("QA已保存qaId:{}", qa.getId());
chatResVO.setAnswwerMap(answerMap); chatResVO.setAnswwerMap(answerMap);
chatResVO.setType(type); chatResVO.setType(type);
chatResVO.setIntentType(intentType); chatResVO.setIntentType(intentType);

@ -1,8 +1,10 @@
package com.supervision.police.vo; package com.supervision.police.vo;
import com.supervision.chat.client.dto.chat.ChatResDTO; import com.supervision.chat.client.dto.chat.ChatResDTO;
import com.supervision.police.vo.dify.KnowledgeBaseSegmentVO;
import lombok.Data; import lombok.Data;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
@ -15,6 +17,7 @@ public class ChatResVO {
private String type; private String type;
private String intentType; private String intentType;
private List<String> docs; private List<String> docs;
private List<KnowledgeBaseSegmentVO> segmentVOList = new ArrayList<>();
public ChatResVO() { public ChatResVO() {
} }

@ -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;
}
Loading…
Cancel
Save