|
|
|
@ -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<String, Object> 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<ModelIndex> 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);
|
|
|
|
|