Merge remote-tracking branch 'origin/dev' into dev

# Conflicts:
#	src/main/java/com/supervision/police/vo/ChatResVO.java
jinan_dev
xueqingkun 5 months ago
commit 9f7c9a4277

@ -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.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.supervision.chat.client.LangChainChatService;
@ -20,11 +22,15 @@ import com.supervision.police.vo.ChatReqVO;
import com.supervision.police.vo.ChatResVO;
import com.supervision.police.vo.ConversationResVo;
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.*;
@ -79,17 +85,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());
@ -97,35 +103,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)) {
@ -166,6 +177,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);

@ -1,10 +1,10 @@
package com.supervision.police.vo;
import com.supervision.chat.client.dto.chat.ChatResDTO;
import com.supervision.police.domain.ConversationQa;
import com.supervision.police.vo.dify.KnowledgeBaseSegmentVO;
import lombok.Data;
import java.util.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@ -29,6 +29,7 @@ public class ChatResVO {
private String type;
private String intentType;
private List<String> docs;
private List<KnowledgeBaseSegmentVO> segmentVOList = new ArrayList<>();
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