From 868a71364c15b582435e3627153fb38cfe68f584 Mon Sep 17 00:00:00 2001 From: yaxin Date: Wed, 11 Dec 2024 10:40:48 +0800 Subject: [PATCH] =?UTF-8?q?=E9=97=AE=E9=A2=98=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../police/service/impl/ChatServiceImpl.java | 31 ++++++++++++++----- .../police/service/impl/ModelServiceImpl.java | 3 +- .../java/com/supervision/demo/DifyTest.java | 7 +---- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/supervision/police/service/impl/ChatServiceImpl.java b/src/main/java/com/supervision/police/service/impl/ChatServiceImpl.java index c9b6d95..13c63d0 100644 --- a/src/main/java/com/supervision/police/service/impl/ChatServiceImpl.java +++ b/src/main/java/com/supervision/police/service/impl/ChatServiceImpl.java @@ -7,6 +7,7 @@ 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.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.supervision.chat.client.LangChainChatService; @@ -53,7 +54,7 @@ public class ChatServiceImpl implements ChatService { private final ConversationQaService conversationQaService; private final ModelIndexService modelIndexService; private final DifyApiUtil difyApiUtil; - private final NoteRecordService noteRecordService; + private final ComDictionaryService comDictionaryService; @Override @@ -116,7 +117,8 @@ public class ChatServiceImpl implements ChatService { conversationService.updateById(conversation); log.info("会话已更新,conversationId:{}", conversation.getId()); } - List modelIndices = modelIndexService.list(); + List modelIndices = modelIndexService.list(new LambdaQueryWrapper().eq(ModelIndex::getDataStatus, "1")); + Map indexType = comDictionaryService.getDictionaryMap("index_type"); switch (type) { case QA_TYPE_DIFY: if (modelIndices.stream().map(ModelIndex::getName).anyMatch(query::equals)) { @@ -164,15 +166,30 @@ public class ChatServiceImpl implements ChatService { if (json != null) { intentType = json.getString("intentType"); if (json.getBoolean("isFunctionCalling")) { - answerMap = executeIntent(json.getString("intentType"), caseId, query, answerMap, modelIndices); + answerMap = executeIntent(json.getString("intentType"), caseId, json.getString("query"), answerMap, modelIndices); } else { 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")); + chatResVO.setDialogueCount(json.getInteger("dialogueCount") == null ? 0 : json.getInteger("dialogueCount")); + } + JSONArray adviceList = json.getJSONArray("adviceList"); + if (adviceList != null && !adviceList.isEmpty()) { + List> adviceMapList = new ArrayList<>(); + // 遍历adviceList里的指标名称,尝试在modelIndices集合中找到name匹配的指标,并根据indexType更新当前指标名称,在字符串前插入对应的指标类型 + for (int i = 0; i < adviceList.size(); i++) { + String advice = adviceList.getString(i); + for (ModelIndex modelIndex : modelIndices) { + if (advice.contains(modelIndex.getName())) { + Map map = new HashMap<>(); + map.put("indexType", indexType.get(modelIndex.getIndexType())); + map.put("indexName", modelIndex.getName()); + adviceMapList.add(map); + break; + } + } } + adviceMapList.add(Map.of("indexType", "", "indexName", "以上都不是")); + answerMap.put("adviceList", adviceMapList); } } else { log.error("返回结果格式异常,query:【{}】", query); diff --git a/src/main/java/com/supervision/police/service/impl/ModelServiceImpl.java b/src/main/java/com/supervision/police/service/impl/ModelServiceImpl.java index 5b994b5..d9fb4e7 100644 --- a/src/main/java/com/supervision/police/service/impl/ModelServiceImpl.java +++ b/src/main/java/com/supervision/police/service/impl/ModelServiceImpl.java @@ -115,8 +115,7 @@ public class ModelServiceImpl implements ModelService { CasePerson casePerson = casePersonMapper.selectOne(new LambdaQueryWrapper() .eq(CasePerson::getCaseId, caseId) .eq(CasePerson::getCaseActorFlag, 1) - .eq(CasePerson::getRoleCode, "1") - .eq(StrUtil.isNotEmpty(modelCase.getLawActor()), CasePerson::getName, modelCase.getLawActor())); + .eq(CasePerson::getRoleCode, "1")); if (ObjectUtil.isEmpty(casePerson)) { log.error("未找到的行为人:【{}】", modelCase.getLawActor()); return R.fail("未找到的行为人"); diff --git a/src/test/java/com/supervision/demo/DifyTest.java b/src/test/java/com/supervision/demo/DifyTest.java index 40c6448..fac2690 100644 --- a/src/test/java/com/supervision/demo/DifyTest.java +++ b/src/test/java/com/supervision/demo/DifyTest.java @@ -1,7 +1,5 @@ package com.supervision.demo; -import com.supervision.common.constant.DifyConstants; -import com.supervision.police.domain.ConversationQa; import com.supervision.police.vo.dify.DifyChatReqVO; import com.supervision.utils.DifyApiUtil; import com.supervision.utils.Document; @@ -10,7 +8,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.List; -import java.util.Map; @SpringBootTest public class DifyTest { @@ -43,9 +40,7 @@ public class DifyTest { System.out.println("Test"); DifyChatReqVO difyChatReqVO = new DifyChatReqVO(); difyChatReqVO.setUser("admin"); -// chatReqVO.setQuery("Who are you?"); difyChatReqVO.setQuery("裴金禄的户籍地址"); - difyChatReqVO.setInputs(Map.of("dataset_id", "13c60b8c-341f-43ea-b3cc-5289a518abd9")); System.out.println(difyApiUtil.chat(difyChatReqVO)); } @@ -57,7 +52,7 @@ public class DifyTest { } @Test - public void createDocumentByTextTest(){ + public void createDocumentByTextTest() { String documentId = difyApiUtil.createDocumentByText("d5da0a24-a8e9-4ceb-8004-6da4847fbb04", "test-text", "坚定不移中国特色社会主义道路"); System.out.println(documentId); }