Jelajahi Sumber

代码提交

SGTY 4 bulan lalu
induk
melakukan
f0135a131f

+ 39 - 23
src/main/java/com/qizhen/healsphere/common/ai/QizhenAssistant.java

@@ -17,24 +17,30 @@ import java.util.regex.Pattern;
 public class QizhenAssistant {
 
     public static String noAnswer = "未找到相关知识";
-    public static Map<String,String> getChatResponse(String userInput, String conversationId,String appId) {
+
+    public static Map<String, String> getChatResponse(String userInput, String conversationId, String appId, boolean tuili) {
         int retry = 0;
         try {
-           return getAnswer(userInput, conversationId,appId);
-        }catch(Exception e){
+            return getAnswer(userInput, conversationId, appId, tuili);
+        } catch (Exception e) {
             e.printStackTrace();
             retry++;
-            if(retry>=2){
-                Map<String,String> map = new HashMap<>();
-                map.put("answer","failed");
+            if (retry >= 2) {
+                Map<String, String> map = new HashMap<>();
+                map.put("answer", "failed");
                 return map;
             }
-            return getAnswer(userInput, conversationId,appId);
+            return getAnswer(userInput, conversationId, appId, tuili);
         }
     }
+
+    public static Map<String, String> getChatResponse(String userInput, String conversationId, String appId) {
+        return getChatResponse(userInput, conversationId, appId, false);
+    }
+
     public static final String domain = "https://console.bce.baidu.com/ai_apaas/personalSpace/knowledgeBase/document/detail/";
 
-    private static boolean contants(String content, String regex){
+    private static boolean contants(String content, String regex) {
         // 编译正则表达式
         Pattern pattern = Pattern.compile(regex);
 
@@ -43,8 +49,8 @@ public class QizhenAssistant {
         return matcher.find();
     }
 
-    private static Map<String,String> getAnswer(String userInput, String conversationId,String appId) {
-        Map<String,String> map = new HashMap<>();
+    private static Map<String, String> getAnswer(String userInput, String conversationId, String appId, boolean tuili) {
+        Map<String, String> map = new HashMap<>();
         JSONObject json = new JSONObject();
         json.put("app_id", appId);
         json.put("conversation_id", conversationId);
@@ -54,30 +60,30 @@ public class QizhenAssistant {
         HttpRequest request = HttpUtil.createRequest(Method.POST, "https://qianfan.baidubce.com/v2/app/conversation/runs")
                 .header("X-Appbuilder-Authorization", "Bearer bce-v3/ALTAK-MyGbNEA18oT3boS2nOga1/d8b5057f7842f59b2c64971d8d077fe724d0aed5")
                 .header("Content-Type", "application/json")
-                .body(json.toJSONString()).timeout(60*1000);
+                .body(json.toJSONString()).timeout(60 * 1000);
         ;
         long l = System.currentTimeMillis();
         String resposne = request.execute().body();
         String answer = JSONObject.parseObject(resposne).getString("answer");
-        if(answer.contains(noAnswer)){
-            map.put("answer",noAnswer);
+        if (answer.contains(noAnswer)) {
+            map.put("answer", noAnswer);
             return map;
         }
-        map.put("answer",answer == null ? noAnswer : answer);
+        map.put("answer", answer == null ? noAnswer : answer);
         JSONArray contents = JSONObject.parseObject(resposne).getJSONArray("content");
         JSONArray docs = new JSONArray();
         List<String> defaultReferences = new ArrayList<>();
-        for(int i=0;i<contents.size();i++){
+        for (int i = 0; i < contents.size(); i++) {
             JSONObject content = contents.getJSONObject(i);
-            if("rag".equals(content.getString("content_type"))){
+            if ("rag".equals(content.getString("content_type"))) {
                 JSONObject outputs = content.getJSONObject("outputs");
-                if(outputs!=null) {
+                if (outputs != null) {
                     JSONArray references = outputs.getJSONArray("references");
                     if (references != null && references.size() > 0) {
                         for (int j = 0; j < references.size(); j++) {
                             JSONObject reference = new JSONObject();
                             String id = references.getJSONObject(j).getString("id");
-                            if(!contants(answer,"\\^(\\[\\d+\\])*\\["+id+"\\](\\[\\d+\\])*\\^")){
+                            if (!contants(answer, "\\^(\\[\\d+\\])*\\[" + id + "\\](\\[\\d+\\])*\\^")) {
                                 continue;
                             }
                             reference.put("index", id);
@@ -93,15 +99,25 @@ public class QizhenAssistant {
                 }
             }
         }
-        if(docs==null || docs.size()==0){
-            map.put("answer",noAnswer);
-            return map;
+
+        boolean notMatch = (docs == null || docs.size() == 0);
+        if (!tuili) {//不允许推理
+            if(notMatch) {
+                map.put("answer", noAnswer);
+                return map;
+            }
+            map.put("references", docs.toJSONString());
+            map.put("defaultReferences", JSON.toJSONString(defaultReferences));
+        }else  if(!notMatch) {
+            map.put("references", docs.toJSONString());
+        }else{
+            map.put("references", "[]");
         }
-        map.put("references", docs.toJSONString());
-        map.put("defaultReferences", JSON.toJSONString(defaultReferences));
+
         System.out.println((System.currentTimeMillis() - l) / 1000 + userInput);
         return map;
     }
+
     public static String getConversationId(String appId) {
         JSONObject json = new JSONObject();
         json.put("app_id", appId);

+ 0 - 1
src/test/java/com/qizhen/healsphere/EnglishTest.java

@@ -108,7 +108,6 @@ public class EnglishTest {
         String accessToken = BaidubceUtil.getAccessToken();
         HSSFSheet sheet = workbook.createSheet(label + "英文名称");
         saveExeclFromExecl(label, question, accessToken, sheet);
-        //saveExecl(label,question,accessToken, sheet);
     }
 
     private static List<Knowlege> saveExeclFromExecl(String label, String orgQuestion, String accessToken, HSSFSheet sheet) throws Exception {

+ 86 - 82
src/test/java/com/qizhen/healsphere/JianchaKfTest.java

@@ -38,19 +38,100 @@ public class JianchaKfTest {
     RelationshipService relationshipService;
     @Autowired
     EntityService entityService;
-    private static int maxCount= 100;
+    private static int maxCount= 30;
     private static String directoryPath  = "C:\\Users\\17664\\Desktop\\检查";
     private static String urlExcelPath = "C:\\Users\\17664\\Desktop\\检查列表.xlsx";
-    static HSSFWorkbook workbook;
-    static String startLabel = "检验";
+    static HSSFWorkbook workbook = new HSSFWorkbook();//这里也可以设置sheet的Name;
+    static String startLabel = "辅助检查";
 
     @Test
     public void writeTypeNeo4j() throws Exception {
-        String property = "检查类别,检查类别";
+        String property = "检查类别,科室类别";
         String[] split = property.split(",");
+
         for(String temp:split) {
-            typeHandle(temp);
+            List<Knowlege> knowleges = saveExeclFromExecl(temp);
+            saveNeo4j(temp,knowleges);
+        }
+    }
+
+    private static List<Knowlege> saveExeclFromExecl(String property) throws Exception {
+        int curCount = 0;
+
+        HSSFSheet sheet = workbook.createSheet(property);
+        List<Knowlege> result = new ArrayList<>();
+        InputStream fis = new FileInputStream(urlExcelPath);
+        Workbook urlWorkbook = new XSSFWorkbook(fis);
+        Sheet urlSheet = urlWorkbook.getSheetAt(0);
+
+        for (int rowNum = 0; rowNum <= urlSheet.getLastRowNum(); rowNum++) {
+            try {
+                Row row = urlSheet.getRow(rowNum);
+                String name = row.getCell(0).getStringCellValue();
+                if (StringUtils.isEmpty(name)) {
+                    continue;
+                }
+                HSSFRow writeRow = sheet.createRow(curCount);
+                name = (name == null ? "" : name);
+                String answer ="";
+                if("检查类别".equals(property)) {
+                    answer = jianchaMap.get(name);
+                    if (StringUtils.isBlank(answer)) {
+                        if (name.length() < 3) {
+                            answer = "[]";
+                        } else {
+                            for (String temp : jianchaSplit) {
+                                if (temp.contains(name)) {
+                                    answer = temp.split("\t")[0];
+                                    break;
+                                }
+                            }
+                        }
+                    }
+                }else if("科室类别".equals(property)){
+                    answer = keshiMap.get(name);
+                    if (StringUtils.isBlank(answer)) {
+                        if (name.length() < 3) {
+                            answer = "[]";
+                        } else {
+                            for (String temp : keshiSplit) {
+                                if (temp.contains(name)) {
+                                    answer = temp.split("\t")[0];
+                                    break;
+                                }
+                            }
+                        }
+                    }
+                }else{
+                    System.out.println("property值不正确");
+                    return null;
+                }
+
+                if(StringUtils.isBlank(answer)){
+                    answer = "[]";
+                }else{
+                    answer = "[\""+answer+"\"]";
+                }
+                writeRow.createCell(0).setCellValue(name);
+                writeRow.createCell(1).setCellValue(answer);
+                Knowlege knowlege = new Knowlege();
+                knowlege.setEntity(name);
+
+                knowlege.setProperty(property);
+                knowlege.setValue(answer);
+
+                result.add(knowlege);
+                curCount++;
+                if (maxCount > 0) {
+                    if (curCount >= maxCount) {
+                        break;
+                    }
+                }
+            } catch (Exception e) {
+            }
         }
+        save(property);
+        return result;
     }
 
     public static void main(String[] args) throws Exception {
@@ -129,83 +210,6 @@ public class JianchaKfTest {
         return content.toString();
     }
 
-    private static List<Knowlege> saveExeclFromExecl(String label, String orgQuestion, String accessToken, HSSFSheet sheet) throws Exception {
-        int curCount = 0;
-        List<Knowlege> result = new ArrayList<>();
-        InputStream fis = new FileInputStream(urlExcelPath);
-        Workbook urlWorkbook = new XSSFWorkbook(fis);
-        Sheet urlSheet = urlWorkbook.getSheetAt(0);
-
-       /* for (int rowNum = 0; rowNum <= urlSheet.getLastRowNum(); rowNum++) {
-            try {
-                Row row = urlSheet.getRow(rowNum);
-                String name = row.getCell(0).getStringCellValue();
-                if (StringUtils.isEmpty(name)) {
-                    continue;
-                }
-                HSSFRow writeRow = sheet.createRow(curCount);
-                name = (name == null ? "" : name);
-                String answer ="";
-                if("检查类别".equals(property)) {
-                    answer = jianyanMap.get(name);
-                    if (StringUtils.isBlank(answer)) {
-                        if (name.length() < 3) {
-                            answer = "[]";
-                        } else {
-                            for (String temp : jianyanSplit) {
-                                if (temp.contains(name)) {
-                                    answer = temp.split("\t")[0];
-                                    break;
-                                }
-                            }
-                        }
-                    }
-                }else if("科室类别".equals(property)){
-                    answer = huayanMap.get(name);
-                    if (StringUtils.isBlank(answer)) {
-                        if (name.length() < 3) {
-                            answer = "[]";
-                        } else {
-                            for (String temp : huayanSplit) {
-                                if (temp.contains(name)) {
-                                    answer = temp.split("\t")[0];
-                                    break;
-                                }
-                            }
-                        }
-                    }
-                }else{
-                    System.out.println("property值不正确");
-                    return;
-                }
-
-                if(StringUtils.isBlank(answer)){
-                    answer = "[]";
-                }else{
-                    answer = "[\""+answer+"\"]";
-                }
-                writeRow.createCell(0).setCellValue(name);
-                writeRow.createCell(1).setCellValue(answer);
-                Knowlege knowlege = new Knowlege();
-                knowlege.setEntity(name);
-
-                knowlege.setProperty(property);
-                knowlege.setValue(answer);
-
-                result.add(knowlege);
-                curCount++;
-                if (maxCount > 0) {
-                    if (curCount >= maxCount) {
-                        break;
-                    }
-                }
-            } catch (Exception e) {
-            }
-        }*/
-        save(label);
-        return result;
-    }
-
     private static List<Knowlege> saveExecl(String property, String accessToken, HSSFSheet sheet) throws Exception {
         int curCount = 0;
         List<Knowlege> result = new ArrayList<>();

+ 88 - 3
src/test/java/com/qizhen/healsphere/MigrateDataTest.java

@@ -40,7 +40,8 @@ public class MigrateDataTest {
 
     @Test
     public void migrateData() {
-        String endLabels = "药品不良反应,药品商品名,药品性状,药品成份,药品英文名称";
+
+        String endLabels = "剂型,规格";
         String[] split = endLabels.split(",");
         String startLabel = "药品";
         addIfNotExist(startLabel);
@@ -49,7 +50,7 @@ public class MigrateDataTest {
             String name = "";
 
 
-            List<Edge> edges = baseRelationshipRepository.triples(startLabel, relationship, "实验室检查"+endLabel);
+            List<Edge> edges = baseRelationshipRepository.triples(startLabel, relationship, startLabel+endLabel);
             if (CollectionUtil.isEmpty(edges)) {
                 continue;
             }
@@ -62,7 +63,7 @@ public class MigrateDataTest {
             } else {
                 name = relationship;
             }
-            addIfNotExist("实验室检查"+endLabel);
+            addIfNotExist(startLabel+endLabel);
 
             for (Edge temp : edges) {
                 Integer startNodeId = addNode(temp.getStartNode());
@@ -88,6 +89,90 @@ public class MigrateDataTest {
         }
     }
 
+    @Test
+    public void migrateDataByExist() {
+
+        String relationships = "常见疾病";
+        String[] split = relationships.split(",");
+        String startLabel = "症状";
+        String endLabel = "疾病";
+        addIfNotExist(startLabel);
+        for(String relationship:split) {
+            List<Edge> edges = baseRelationshipRepository.triples(startLabel, startLabel+"相关"+relationship, endLabel);
+            if (CollectionUtil.isEmpty(edges)) {
+                continue;
+            }
+            addIfNotExist(startLabel+relationship);
+
+            for (Edge temp : edges) {
+                Integer startNodeId = addNode(temp.getStartNode());
+                Integer endNodeId = addNode(temp.getEndNode());
+                if (startNodeId <= 0 || endNodeId <= 0) {
+                    continue;
+                }
+                HashMap<String, Object> columnMap = new HashMap<>();
+                columnMap.put("src_id", startNodeId);
+                columnMap.put("dest_id", endNodeId);
+                columnMap.put("name", relationship);
+                List<KgEdges> kgEdgesList = kgEdgesMapper.selectByMap(columnMap);
+                if (CollectionUtil.isEmpty(kgEdgesList)) {
+                    KgEdges kgEdges = new KgEdges();
+                    kgEdges.setSrc_id(startNodeId);
+                    kgEdges.setDest_id(endNodeId);
+                    kgEdges.setName(relationship);
+                    kgEdges.setCategory(relationship);
+                    kgEdges.setVersion(version);
+                    kgEdgesMapper.insert(kgEdges);
+                }
+            }
+        }
+    }
+
+
+
+    @Test
+    public void migrateDataBySelf() {
+/*        String propertyStr = "别名##症状\t“#症状#”此处作为一种“症状”,“#症状#”的别称或别名有哪些(请更加严格的遵照文献原文,不要把子类名称当做别称给我)" +
+                ",常伴随的症状##症状\t“#症状#”此处作为一种“症状”,“#症状#”常伴随的症状有哪些症状" +
+                ",常见疾病##疾病\t“#症状#”此处作为一种“症状”,“#症状#”常见于哪些疾病" +
+                ",子类或分类##症状\t“#症状#”此处作为一种“症状”,“#症状#”的症状子类或症状分类有哪些症状(答案必须包含“#症状#”几个字,否则不提取)";*/
+
+        String relationships = "别名,常伴随的症状,子类或分类";
+        String[] split = relationships.split(",");
+        String startLabel = "症状";
+        String endLabel = startLabel;
+        addIfNotExist(startLabel);
+        for(String relationship:split) {
+            List<Edge> edges = baseRelationshipRepository.triples(startLabel, startLabel+"相关"+relationship, endLabel);
+            if (CollectionUtil.isEmpty(edges)) {
+                continue;
+            }
+            addIfNotExist(startLabel+relationship);
+
+            for (Edge temp : edges) {
+                Integer startNodeId = addNode(temp.getStartNode());
+                Integer endNodeId = addNode(temp.getEndNode());
+                if (startNodeId <= 0 || endNodeId <= 0) {
+                    continue;
+                }
+                HashMap<String, Object> columnMap = new HashMap<>();
+                columnMap.put("src_id", startNodeId);
+                columnMap.put("dest_id", endNodeId);
+                columnMap.put("name", relationship);
+                List<KgEdges> kgEdgesList = kgEdgesMapper.selectByMap(columnMap);
+                if (CollectionUtil.isEmpty(kgEdgesList)) {
+                    KgEdges kgEdges = new KgEdges();
+                    kgEdges.setSrc_id(startNodeId);
+                    kgEdges.setDest_id(endNodeId);
+                    kgEdges.setName(relationship);
+                    kgEdges.setCategory(relationship);
+                    kgEdges.setVersion(version);
+                    kgEdgesMapper.insert(kgEdges);
+                }
+            }
+        }
+    }
+
     private void addIfNotExist(String label){
         if("就诊科室".equals(label)) {
             label = "科室";

+ 8 - 15
src/test/java/com/qizhen/healsphere/ZhengzhuangDataWriteTest31.java

@@ -40,7 +40,7 @@ public class ZhengzhuangDataWriteTest31 {
     RelationshipService relationshipService;
     @Autowired
     EntityService entityService;
-    private static int maxCount= 30;
+    private static int maxCount= 50;
     private static boolean selfCheck = false;
     private static String urlExcelPath = "C:\\Users\\17664\\Desktop\\检查列表.xlsx";
     private static String  placeholder= "#检查#";
@@ -71,7 +71,7 @@ public class ZhengzhuangDataWriteTest31 {
                 endLabel = startLabel+endLabel;
             }
             HSSFSheet sheet = workbook.createSheet(relationShip);
-            List<Knowlege> data = getData(BaidubceUtil.getAccessToken(), property);
+            List<Knowlege> data = getData(BaidubceUtil.getAccessToken(), property,false);
             if(!CollectionUtils.isEmpty(data)) {
 
                 for (Knowlege temp:data) {
@@ -146,9 +146,8 @@ public class ZhengzhuangDataWriteTest31 {
                 ",患者提醒\t“"+placeholder+"”此处作为一种“辅助检查项目”,“"+placeholder+"”检查前和检查后,作为“患者”或“被检查者”都需要注意些什么(不要给我医生需要注意的事项,只要患者需要注意的事项)" +
                 ",禁忌人群\t“"+placeholder+"”此处作为一种“辅助检查项目”,“"+placeholder+"”的“禁忌人群”归纳后有哪些“人群”(只要“人群”,不要“疾病”;所有文献资料的语义匹配度不高时,建议回答“无”;所有文献资料语义都不太明确时,建议回答“无”;若该检查项目“没有绝对禁忌”成立时,必须回答“无”;不要“相对禁忌”内容)" +
                 ",检查目的\t“"+placeholder+"”此处作为一种“辅助检查项目”,“"+placeholder+"”检查的“主要目的”或“主要目标”归纳后有哪些";*/
-        String propertyStr = "不适用性别\t“"+placeholder+"”此处作为一种“辅助检查项目”,“"+placeholder+"”检查不适用于哪个性别(你只能回答:“男性”、“女性”、“无”)";
-        saveExel(propertyStr, accessToken,"辅助检查");
-
+        String propertyStr = "仅适用的性别\t“"+placeholder+"”此处作为一种“辅助检查项目”,“"+placeholder+"”检查“肯定”适用于哪个性别(你只能回答:“男性”或“女性”,不能附带其它任何信息;如果“肯定”适用于“男性”,则答案中必须要有“男性”;如果“肯定”适用于“女性”,且答案中必须要有“女性”;如果你实在不确定,则必须回答“未找到相关知识”)";
+        saveExel(propertyStr, accessToken,"不适用性别",true);
     }
 
     static HSSFWorkbook workbook;
@@ -156,13 +155,13 @@ public class ZhengzhuangDataWriteTest31 {
     static Long successCount = 0l;
     static Long failCount = 0l;
     static Long unkonwCount = 0l;
-    private static void saveExel(String propertyStr, String accessToken,String fileName) {
+    private static void saveExel(String propertyStr, String accessToken,String fileName,boolean tuili) {
         String[] properties = propertyStr.split(",");
         workbook = new HSSFWorkbook();//这里也可以设置sheet的Name
         HSSFSheet sheet = workbook.createSheet(fileName);
         int rows = 0;
         for(String property:properties){
-            List<Knowlege> data = getData(accessToken, property);
+            List<Knowlege> data = getData(accessToken, property,tuili);
             if(!CollectionUtils.isEmpty(data)) {
                 for (Knowlege temp:data) {
                     HSSFRow row = sheet.createRow(rows++);
@@ -201,7 +200,7 @@ public class ZhengzhuangDataWriteTest31 {
         }
     }
 
-    private static List<Knowlege> getData(String accessToken,String property) {
+    private static List<Knowlege> getData(String accessToken,String property,boolean tuili) {
         List<Knowlege> list = new ArrayList<>();
         try {
             String appId= "3b615957-f9b1-4811-9dfc-13dcee6e0a37";
@@ -229,7 +228,7 @@ public class ZhengzhuangDataWriteTest31 {
                     String quetionParty = split[1];
                     String question = quetionParty.replaceAll(placeholder,disease)+"?";
 
-                    Map<String, String> result = QizhenAssistant.getChatResponse(question, QizhenAssistant.getConversationId(appId),appId);
+                    Map<String, String> result = QizhenAssistant.getChatResponse(question, QizhenAssistant.getConversationId(appId),appId,tuili);
                     String answer = result.get("answer");
                     String references = result.get("references");
                     String defaultReferences = result.get("defaultReferences");
@@ -255,12 +254,6 @@ public class ZhengzhuangDataWriteTest31 {
                                 "**是**^[2][4][6]^。\n" +
                                 "\n" +
                                 "输出:[\"是\"]\n\n"+
-                                "#示例3\n" +
-                                "以抽取“不适用性别”为例\n" +
-                                "文本:\n" +
-                                "**女性(此处指未婚无性生活的女性)**^[3]^。\n" +
-                                "\n" +
-                                "输出:[\"未婚无性生活的女性\"]\n\n"+
                                 "2、没有可抽取的“"+relation+"”,则返回空json数组。\n" +
                                 "\n" +
                                 "本次抽取的文本如下:\n\n";