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

jinan_dev
xueqingkun 5 months ago
commit aae0920d6a

@ -33,6 +33,11 @@ public class ConversationQa implements Serializable {
*/
private String intentType;
/**
*
*/
private int dialogCount;
/**
*
*/

@ -25,6 +25,7 @@ import com.supervision.police.service.*;
import com.supervision.police.vo.ChatReqVO;
import com.supervision.police.vo.ChatResVO;
import com.supervision.police.vo.ConversationResVo;
import com.supervision.police.vo.dify.DIFYChatReqInputVO;
import com.supervision.police.vo.dify.DifyChatReqVO;
import com.supervision.police.vo.dify.DifyChatResVO;
import com.supervision.police.vo.dify.KnowledgeBaseSegmentVO;
@ -122,7 +123,7 @@ public class ChatServiceImpl implements ChatService {
type = QA_TYPE_NX_LLM;
intentType = INTENT_TYPE_INDEX_RESULT;
handleIndexResultQA(modelIndices, query, caseId, answerMap);
} else if (INTENT_TYPE_TEXT_CASE_RESULT.equals(query) || INTENT_TYPE_CASE_RESULT.equals(intentType)) {
} else if (INTENT_TYPE_TEXT_CASE_RESULT.equals(query)) {
type = QA_TYPE_NX_LLM;
intentType = INTENT_TYPE_CASE_RESULT;
ModelCase modelCase = modelCaseService.getById(caseId);
@ -133,11 +134,11 @@ public class ChatServiceImpl implements ChatService {
modelService.analyseCase(analyseCaseDTO);
}
answerMap = JSON.parseObject(JSON.toJSONString(modelService.caseScoreDetail(caseId)), Map.class);
} else if (INTENT_TYPE_TEXT_CASE_OVERVIEW.equals(query) || INTENT_TYPE_CASE_OVERVIEW.equals(intentType)) {
} else if (INTENT_TYPE_TEXT_CASE_OVERVIEW.equals(query)) {
type = QA_TYPE_NX_LLM;
intentType = INTENT_TYPE_CASE_OVERVIEW;
answerMap.put("answerText", modelCaseService.getById(caseId).getCaseDetail());
} else if (INTENT_TYPE_TEXT_CASE_EVIDENCE_GUIDE.equals(query) || INTENT_TYPE_CASE_EVIDENCE_GUIDE.equals(intentType)) {
} else if (INTENT_TYPE_TEXT_CASE_EVIDENCE_GUIDE.equals(query)) {
type = QA_TYPE_NX_LLM;
intentType = INTENT_TYPE_CASE_EVIDENCE_GUIDE;
answerMap.put("guideDesc", modelService.caseScoreDetail(caseId).getGuideDesc());
@ -147,42 +148,40 @@ public class ChatServiceImpl implements ChatService {
difyChatReqVO.setUser(userId);
difyChatReqVO.setConversationId(chatReqVO.getConversationId());
difyChatReqVO.setQuery(query);
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.setSegmentList(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::equals)) {
handleIndexResultQA(modelIndices, query, caseId, answerMap);
DIFYChatReqInputVO inputs = new DIFYChatReqInputVO();
inputs.setCase_id(caseId);
inputs.setUser_id(userId);
inputs.setConversation_id(chatReqVO.getConversationId());
inputs.setDataset_id(modelCase.getKnowledgeBaseId());
inputs.setParticipator(modelCase.getLawActor());
inputs.setDialogue_count(chatReqVO.getDialogueCount());
inputs.setIntent_type(intentType);
difyChatReqVO.setInputs(inputs);
DifyChatResVO response = difyApiUtil.chat(difyChatReqVO);
log.info("response:{}", JSON.toJSONString(response));
// 拼装返回对象
JSONObject json = JSON.parseObject(response.getAnswer());
if (json != null) {
intentType = json.getString("intentType");
if (json.getBoolean("isFunctionCalling")) {
answerMap = executeIntent(json.getString("intentType"), caseId, query, answerMap, modelIndices);
} else {
answerMap.put("answerText", "暂无相关指标信息");
chatResVO.setAnswer(json.getString("answerText"));
chatResVO.setSegmentList(JSONArray.parseArray(json.getString("quote"), KnowledgeBaseSegmentVO.class));
int dialogueCount = json.getInteger("dialogueCount") != null ? json.getInteger("dialogueCount") : 0;
chatResVO.setDialogueCount(dialogueCount);
if (json.getInteger("dialogueCount") != null) {
answerMap.put("adviceList", json.getJSONArray("adviceList"));
}
}
break;
case INTENT_TYPE_CASE_RESULT:
ModelCase modelCase = modelCaseService.getById(caseId);
if (modelCase.getTotalScore() == null) {
log.info("案件【{}】尚未执行模型分析,现在开始执行", modelCase.getCaseName());
AnalyseCaseDTO analyseCaseDTO = new AnalyseCaseDTO();
analyseCaseDTO.setCaseId(caseId);
modelService.analyseCase(analyseCaseDTO);
}
answerMap = JSON.parseObject(JSON.toJSONString(modelService.caseScoreDetail(caseId)), Map.class);
break;
case INTENT_TYPE_CASE_OVERVIEW:
answerMap.put("answerText", modelCaseService.getById(caseId).getCaseDetail());
break;
case INTENT_TYPE_CASE_EVIDENCE_GUIDE:
answerMap.put("guideDesc", modelService.caseScoreDetail(caseId).getGuideDesc());
break;
default:
break;
} else {
log.error("返回结果格式异常query:【{}】", query);
}
}
break;
case QA_TYPE_NX_LLM:
answerMap = executeIntent(intentType, caseId, query, answerMap, modelIndices);
break;
default:
break;
}
@ -193,6 +192,7 @@ public class ChatServiceImpl implements ChatService {
long end = System.currentTimeMillis();
ConversationQa qa = new ConversationQa();
qa.setId(UUID.randomUUID().toString());
qa.setDialogCount(chatResVO.getDialogueCount());
qa.setQuestion(query);
qa.setQuestionTime(new Date(startTime));
qa.setAnswer(chatResVO.getAnswer());
@ -308,4 +308,35 @@ public class ChatServiceImpl implements ChatService {
answerMap.put("evidenceNames", evidenceList);
}
}
}
private Map<String, Object> executeIntent(String intentType, String caseId, String query, Map<String, Object> answerMap, List<ModelIndex> modelIndices) {
switch (intentType) {
case INTENT_TYPE_INDEX_RESULT:
if (modelIndices.stream().map(ModelIndex::getName).anyMatch(query::equals)) {
handleIndexResultQA(modelIndices, query, caseId, answerMap);
} else {
answerMap.put("answerText", "暂无相关指标信息");
}
break;
case INTENT_TYPE_CASE_RESULT:
ModelCase modelCase = modelCaseService.getById(caseId);
if (modelCase.getTotalScore() == null) {
log.info("案件【{}】尚未执行模型分析,现在开始执行", modelCase.getCaseName());
AnalyseCaseDTO analyseCaseDTO = new AnalyseCaseDTO();
analyseCaseDTO.setCaseId(caseId);
modelService.analyseCase(analyseCaseDTO);
}
answerMap = JSON.parseObject(JSON.toJSONString(modelService.caseScoreDetail(caseId)), Map.class);
break;
case INTENT_TYPE_CASE_OVERVIEW:
answerMap.put("answerText", modelCaseService.getById(caseId).getCaseDetail());
break;
case INTENT_TYPE_CASE_EVIDENCE_GUIDE:
answerMap.put("guideDesc", modelService.caseScoreDetail(caseId).getGuideDesc());
break;
default:
break;
}
return answerMap;
}
}

@ -27,6 +27,9 @@ public class ChatReqVO {
@Schema(description = "用户输入的文本")
private String query;
@Schema(description = "多轮对话轮数")
private int dialogueCount = 0;
@Schema(description = "历史会话")
private List<History> history;
}

@ -28,6 +28,7 @@ public class ChatResVO {
private Map<String, Object> answwerMap;
private String type;
private String intentType;
private int dialogueCount;
private List<String> docs;
private List<KnowledgeBaseSegmentVO> segmentList = new ArrayList<>();
@ -47,6 +48,7 @@ public class ChatResVO {
return;
}
this.setId(conversationQa.getId());
this.setDialogueCount(conversationQa.getDialogCount());
this.setQuestion(conversationQa.getQuestion());
this.setAnswer(conversationQa.getAnswer());
if (StrUtil.isNotEmpty(conversationQa.getAnswerMap())){

@ -0,0 +1,17 @@
package com.supervision.police.vo.dify;
import lombok.Data;
@Data
public class DIFYChatReqInputVO {
private String type;
private String intent_type;
private String case_id;
private String user_id;
private String conversation_id;
private String dataset_id;
private String participator;
private int dialogue_count;
private String previous_query;
private String previous_answer;
}

@ -13,5 +13,5 @@ public class DifyChatReqVO {
private String user;
private String query;
private String response_mode = CHAT_RESPONSE_MODE_BLOCKING;
private Map<String, Object> inputs = new HashMap<>();
private DIFYChatReqInputVO inputs = new DIFYChatReqInputVO();
}

@ -6,6 +6,7 @@ spring:
base-url: http://192.168.10.70:11434
chat:
enabled: true
options:
model: qwen2.5:32b
# 控制模型在请求后加载到内存中的时间(稍微长一点的时间,避免重复加载浪费性能,加快处理速度)
@ -93,4 +94,4 @@ xxl:
dify:
url: http://192.168.10.137/v1
dataset-auth: Bearer dataset-PLOwR22cFObxN1AlGM0QdBXT
app-auth: Bearer app-pMR3NUdDtTAcCiyGcohIMjVi
app-auth: Bearer app-BhLyNUCJpTZ4OHkgpgUHXwGl

Loading…
Cancel
Save