|
|
|
@ -12,6 +12,8 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
|
|
import com.supervision.chat.client.LangChainChatService;
|
|
|
|
|
import com.supervision.chat.client.dto.chat.ChatReqDTO;
|
|
|
|
|
import com.supervision.chat.client.dto.chat.ChatResDTO;
|
|
|
|
|
import com.supervision.common.domain.R;
|
|
|
|
|
import com.supervision.constant.CaseAnalysisStatusEnum;
|
|
|
|
|
import com.supervision.police.domain.Conversation;
|
|
|
|
|
import com.supervision.police.domain.ConversationQa;
|
|
|
|
|
import com.supervision.police.domain.ModelCase;
|
|
|
|
@ -90,7 +92,7 @@ public class ChatServiceImpl implements ChatService {
|
|
|
|
|
|
|
|
|
|
@Transactional(transactionManager = "dataSourceTransactionManager", rollbackFor = Exception.class)
|
|
|
|
|
@Override
|
|
|
|
|
public ChatResVO chatNew(ChatReqVO chatReqVO) {
|
|
|
|
|
public R<ChatResVO> chatNew(ChatReqVO chatReqVO) {
|
|
|
|
|
ChatResVO chatResVO = new ChatResVO();
|
|
|
|
|
Map<String, Object> answerMap = new HashMap<>();
|
|
|
|
|
String query = chatReqVO.getQuery();
|
|
|
|
@ -145,11 +147,11 @@ public class ChatServiceImpl implements ChatService {
|
|
|
|
|
difyChatReqVO.setUser(userId);
|
|
|
|
|
difyChatReqVO.setConversationId(chatReqVO.getConversationId());
|
|
|
|
|
difyChatReqVO.setQuery(query);
|
|
|
|
|
difyChatReqVO.setInputs(Map.of("dataset_id", modelCase.getKnowledgeBaseId()));
|
|
|
|
|
difyChatReqVO.setInputs(Map.of("dataset_id", modelCase.getKnowledgeBaseId(), "participator", modelCase.getLawActor()));
|
|
|
|
|
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));
|
|
|
|
|
chatResVO.setSegmentList(JSONArray.parseArray(json.getString("quote"), KnowledgeBaseSegmentVO.class));
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case QA_TYPE_NX_LLM:
|
|
|
|
@ -184,6 +186,10 @@ public class ChatServiceImpl implements ChatService {
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (StringUtils.isEmpty(chatResVO.getAnswer()) && "{}".equals(JSON.toJSONString(answerMap))) {
|
|
|
|
|
log.error("chatNew: 未找到答案,query:【{}】", query);
|
|
|
|
|
return R.fail("未找到答案");
|
|
|
|
|
}
|
|
|
|
|
long end = System.currentTimeMillis();
|
|
|
|
|
ConversationQa qa = new ConversationQa();
|
|
|
|
|
qa.setId(UUID.randomUUID().toString());
|
|
|
|
@ -191,6 +197,7 @@ public class ChatServiceImpl implements ChatService {
|
|
|
|
|
qa.setQuestionTime(new Date(startTime));
|
|
|
|
|
qa.setAnswer(chatResVO.getAnswer());
|
|
|
|
|
qa.setAnswerMap(JSON.toJSONString(answerMap));
|
|
|
|
|
qa.setSegmentList(chatResVO.getSegmentList());
|
|
|
|
|
qa.setAnswerTime(new Date(end));
|
|
|
|
|
qa.setType(type);
|
|
|
|
|
qa.setIntentType(intentType);
|
|
|
|
@ -204,7 +211,7 @@ public class ChatServiceImpl implements ChatService {
|
|
|
|
|
chatResVO.setType(type);
|
|
|
|
|
chatResVO.setIntentType(intentType);
|
|
|
|
|
chatResVO.setConversationId(chatReqVO.getConversationId());
|
|
|
|
|
return chatResVO;
|
|
|
|
|
return R.ok(chatResVO);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@ -247,12 +254,21 @@ public class ChatServiceImpl implements ChatService {
|
|
|
|
|
* @param answerMap 答案map
|
|
|
|
|
*/
|
|
|
|
|
private void handleIndexResultQA(List<ModelIndex> modelIndices, String query, String caseId, Map<String, Object> answerMap) {
|
|
|
|
|
//如果案件分析状态不是分析成功,则先执行案件分析
|
|
|
|
|
ModelCase modelCase = modelCaseService.getById(caseId);
|
|
|
|
|
ModelIndex modelIndex = modelIndices.stream().filter(index -> query.contains(index.getName())).findFirst().get();
|
|
|
|
|
IndexResultQuery indexResultQuery = new IndexResultQuery();
|
|
|
|
|
indexResultQuery.setCaseId(caseId);
|
|
|
|
|
indexResultQuery.setIndexType(modelIndex.getIndexType());
|
|
|
|
|
indexResultQuery.setIndexName(modelIndex.getName());
|
|
|
|
|
IPage<IndexDetail> indexDetailPage = modelCaseService.getIndexDetail(indexResultQuery, 1, 1);
|
|
|
|
|
if (indexDetailPage.getRecords().isEmpty() || (modelCase.getTotalScore() == null && modelCase.getCaseAnalysisStatus() != CaseAnalysisStatusEnum.ANALYZED.getCode())) {
|
|
|
|
|
log.info("案件【{}】尚未执行模型分析,现在开始执行", modelCase.getCaseName());
|
|
|
|
|
AnalyseCaseDTO analyseCaseDTO = new AnalyseCaseDTO();
|
|
|
|
|
analyseCaseDTO.setCaseId(caseId);
|
|
|
|
|
modelService.analyseCaseNew(analyseCaseDTO);
|
|
|
|
|
}
|
|
|
|
|
indexDetailPage = modelCaseService.getIndexDetail(indexResultQuery, 1, 1);
|
|
|
|
|
if (!indexDetailPage.getRecords().isEmpty()) {
|
|
|
|
|
IndexDetail indexDetail = indexDetailPage.getRecords().get(0);
|
|
|
|
|
answerMap.put("indexName", modelIndex.getName());
|
|
|
|
@ -267,8 +283,9 @@ public class ChatServiceImpl implements ChatService {
|
|
|
|
|
qaMap.put("question", split.getQuestion());
|
|
|
|
|
qaMap.put("answer", split.getAnswer());
|
|
|
|
|
qaMap.put("noteRecordId", split.getNoteRecordId());
|
|
|
|
|
qaMap.put("fileIds", noteRecordService.getById(split.getNoteRecordId()).getFileIds());
|
|
|
|
|
qaMap.put("noteRecordName", split.getNoteName());
|
|
|
|
|
qaMap.put("splitId", split.getId());
|
|
|
|
|
qaMap.put("noteName", split.getNoteName());
|
|
|
|
|
qaMap.put("noteFileId", split.getNoteFileId());
|
|
|
|
|
qaSplitList.add(qaMap);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|