正在显示
1 个修改的文件
包含
52 行增加
和
40 行删除
| @@ -9,9 +9,11 @@ import dev.langchain4j.store.embedding.pgvector.PgVectorEmbeddingStore; | @@ -9,9 +9,11 @@ import dev.langchain4j.store.embedding.pgvector.PgVectorEmbeddingStore; | ||
| 9 | import io.swagger.v3.oas.annotations.Operation; | 9 | import io.swagger.v3.oas.annotations.Operation; |
| 10 | import lombok.extern.slf4j.Slf4j; | 10 | import lombok.extern.slf4j.Slf4j; |
| 11 | import org.apache.commons.lang3.StringUtils; | 11 | import org.apache.commons.lang3.StringUtils; |
| 12 | +import org.apache.shiro.SecurityUtils; | ||
| 12 | import org.jeecg.ai.handler.AIParams; | 13 | import org.jeecg.ai.handler.AIParams; |
| 13 | import org.jeecg.ai.handler.LLMHandler; | 14 | import org.jeecg.ai.handler.LLMHandler; |
| 14 | import org.jeecg.common.api.vo.Result; | 15 | import org.jeecg.common.api.vo.Result; |
| 16 | +import org.jeecg.common.system.vo.LoginUser; | ||
| 15 | import org.jeecg.modules.airag.app.entity.AiragLog; | 17 | import org.jeecg.modules.airag.app.entity.AiragLog; |
| 16 | import org.jeecg.modules.airag.app.entity.QuestionEmbedding; | 18 | import org.jeecg.modules.airag.app.entity.QuestionEmbedding; |
| 17 | import org.jeecg.modules.airag.app.service.IAiragLogService; | 19 | import org.jeecg.modules.airag.app.service.IAiragLogService; |
| @@ -52,6 +54,7 @@ public class ZdyRagController { | @@ -52,6 +54,7 @@ public class ZdyRagController { | ||
| 52 | String modelId = "1926875898187878401"; | 54 | String modelId = "1926875898187878401"; |
| 53 | Integer topNumber = 1; | 55 | Integer topNumber = 1; |
| 54 | Double similarity = 0.8; | 56 | Double similarity = 0.8; |
| 57 | + | ||
| 55 | // 创建日志对象 | 58 | // 创建日志对象 |
| 56 | AiragLog logRecord = new AiragLog() | 59 | AiragLog logRecord = new AiragLog() |
| 57 | .setQuestion(questionText) | 60 | .setQuestion(questionText) |
| @@ -59,43 +62,44 @@ public class ZdyRagController { | @@ -59,43 +62,44 @@ public class ZdyRagController { | ||
| 59 | .setCreateTime(new Date()); | 62 | .setCreateTime(new Date()); |
| 60 | 63 | ||
| 61 | 64 | ||
| 65 | + | ||
| 62 | HashMap<String, Object> resMap = new HashMap<>(); | 66 | HashMap<String, Object> resMap = new HashMap<>(); |
| 63 | //根据问题相似度进行查询 | 67 | //根据问题相似度进行查询 |
| 64 | -// List<QuestionEmbedding> questionEmbeddings = questionEmbeddingService.similaritySearchByQuestion(questionText, 1,0.8); | ||
| 65 | -// for (QuestionEmbedding questionEmbedding : questionEmbeddings) { | ||
| 66 | -// resMap.put("question", questionText); | ||
| 67 | -// resMap.put("answer", questionEmbedding.getAnswer()); | ||
| 68 | -// resMap.put("similarity", questionEmbedding.getSimilarity()); | ||
| 69 | -// | ||
| 70 | -// ObjectMapper objectMapper = new ObjectMapper(); | ||
| 71 | -// Map<String, String> metadata = objectMapper.readValue(questionEmbedding.getMetadata(), Map.class); | ||
| 72 | -// // 获取docName和docId | ||
| 73 | -// if (metadata != null) { | ||
| 74 | -// String docName = metadata.get("docName"); | ||
| 75 | -// resMap.put("fileName", docName); | ||
| 76 | -// String fileName = generateFilePath(questionEmbedding.getMetadata()); | ||
| 77 | -// | ||
| 78 | -// if (StringUtils.isNotBlank(fileName)) { | ||
| 79 | -// resMap.put("fileBase64", FileToBase64Util.fileToBase64(uploadPath + fileName)); | ||
| 80 | -// } | ||
| 81 | -// } | ||
| 82 | -// // 记录日志 - 从问题库匹配 | ||
| 83 | -// logRecord.setAnswer(questionEmbedding.getAnswer()); | ||
| 84 | -// logRecord.setAnswerType(1); | ||
| 85 | -// airagLogService.save(logRecord); | ||
| 86 | -// | ||
| 87 | -// log.info("questionEmbedding.getMetadata() = " + questionEmbedding.getMetadata()); | ||
| 88 | -// log.info("questionEmbedding.getQuestion() = " + questionEmbedding.getQuestion()); | ||
| 89 | -// log.info("questionEmbedding.getAnswer() = " + questionEmbedding.getAnswer()); | ||
| 90 | -// log.info("questionEmbedding.getSimilarity() = " + questionEmbedding.getSimilarity()); | ||
| 91 | -// log.info("-------------------------------------------------------------"); | ||
| 92 | -// } | ||
| 93 | -// //返回问题库命中的问题 | ||
| 94 | -// if (!questionEmbeddings.isEmpty()) { | ||
| 95 | -// return Result.OK(resMap); | ||
| 96 | -// } | ||
| 97 | - | ||
| 98 | - List<Map<String, Object>> maps = embeddingHandler.searchEmbedding(knowId, questionText, topNumber, similarity); | 68 | + List<QuestionEmbedding> questionEmbeddings = questionEmbeddingService.similaritySearchByQuestion(questionText, 1,0.8); |
| 69 | + for (QuestionEmbedding questionEmbedding : questionEmbeddings) { | ||
| 70 | + resMap.put("question", questionText); | ||
| 71 | + resMap.put("answer", questionEmbedding.getAnswer()); | ||
| 72 | + resMap.put("similarity", questionEmbedding.getSimilarity()); | ||
| 73 | + | ||
| 74 | + ObjectMapper objectMapper = new ObjectMapper(); | ||
| 75 | + Map<String, String> metadata = objectMapper.readValue(questionEmbedding.getMetadata(), Map.class); | ||
| 76 | + // 获取docName和docId | ||
| 77 | + if (metadata != null) { | ||
| 78 | + String docName = metadata.get("docName"); | ||
| 79 | + resMap.put("fileName", docName); | ||
| 80 | + String fileName = generateFilePath(questionEmbedding.getMetadata()); | ||
| 81 | + | ||
| 82 | + if (StringUtils.isNotBlank(fileName)) { | ||
| 83 | + resMap.put("fileBase64", FileToBase64Util.fileToBase64(uploadPath + fileName)); | ||
| 84 | + } | ||
| 85 | + } | ||
| 86 | + // 记录日志 - 从问题库匹配 | ||
| 87 | + logRecord.setAnswer(questionEmbedding.getAnswer()); | ||
| 88 | + logRecord.setAnswerType(1); | ||
| 89 | + airagLogService.save(logRecord); | ||
| 90 | + | ||
| 91 | + log.info("questionEmbedding.getMetadata() = " + questionEmbedding.getMetadata()); | ||
| 92 | + log.info("questionEmbedding.getQuestion() = " + questionEmbedding.getQuestion()); | ||
| 93 | + log.info("questionEmbedding.getAnswer() = " + questionEmbedding.getAnswer()); | ||
| 94 | + log.info("questionEmbedding.getSimilarity() = " + questionEmbedding.getSimilarity()); | ||
| 95 | + log.info("-------------------------------------------------------------"); | ||
| 96 | + } | ||
| 97 | + //返回问题库命中的问题 | ||
| 98 | + if (!questionEmbeddings.isEmpty()) { | ||
| 99 | + return Result.OK(resMap); | ||
| 100 | + } | ||
| 101 | + | ||
| 102 | + List<Map<String, Object>> maps = embeddingHandler.searchEmbedding(knowId, questionText, 3, 0.75); | ||
| 99 | if (CollectionUtil.isEmpty(maps)) { | 103 | if (CollectionUtil.isEmpty(maps)) { |
| 100 | resMap.put("answer", "该问题未记录在知识库中"); | 104 | resMap.put("answer", "该问题未记录在知识库中"); |
| 101 | // 记录日志 - 未命中任何知识库 | 105 | // 记录日志 - 未命中任何知识库 |
| @@ -109,7 +113,7 @@ public class ZdyRagController { | @@ -109,7 +113,7 @@ public class ZdyRagController { | ||
| 109 | 113 | ||
| 110 | StringBuilder content = new StringBuilder(); | 114 | StringBuilder content = new StringBuilder(); |
| 111 | for (Map<String, Object> map : maps) { | 115 | for (Map<String, Object> map : maps) { |
| 112 | - if (Double.parseDouble(map.get("score").toString()) > similarity){ | 116 | + if (Double.parseDouble(map.get("score").toString()) > 0.75){ |
| 113 | log.info("score = " + map.get("score").toString()); | 117 | log.info("score = " + map.get("score").toString()); |
| 114 | log.info("content = " + map.get("content").toString()); | 118 | log.info("content = " + map.get("content").toString()); |
| 115 | content.append(map.get("content").toString()).append("\n"); | 119 | content.append(map.get("content").toString()).append("\n"); |
| @@ -118,10 +122,18 @@ public class ZdyRagController { | @@ -118,10 +122,18 @@ public class ZdyRagController { | ||
| 118 | 122 | ||
| 119 | 123 | ||
| 120 | List<ChatMessage> messages = new ArrayList<>(); | 124 | List<ChatMessage> messages = new ArrayList<>(); |
| 121 | - String questin = "请整理出与用户所提出的问题相关的信息," + | ||
| 122 | - "参考内容中含有与问题无关信息,舍弃掉与问题无关的内容,进行整理,回答用户的问题" + | ||
| 123 | - "问题如下:" + questionText + | ||
| 124 | - "参考信息如下:" + content ; | 125 | + String questin = "你是一个严谨的信息处理助手,请严格按照以下要求处理用户问题:" + questionText + "\n\n" + |
| 126 | + "处理步骤和要求:\n" + | ||
| 127 | + "1. 严格基于参考内容回答,禁止任何超出参考内容的推断或想象\n" + | ||
| 128 | + "2. 回答结构:\n" + | ||
| 129 | + " - 首先用一句话直接回答问题核心(仅限参考内容中明确包含的信息)\n" + | ||
| 130 | + " - 然后列出支持该答案的具体证据(可直接引用参考内容)\n" + | ||
| 131 | + "3. 禁止以下行为:\n" + | ||
| 132 | + " - 添加参考内容中不存在的信息\n" + | ||
| 133 | + " - 进行任何推测性陈述\n" + | ||
| 134 | + " - 使用模糊或不确定的表达\n" + | ||
| 135 | + " - 参考内容为空时应该拒绝回答\n" + | ||
| 136 | + "参考内容(请严格限制回答范围于此):\n" + content; | ||
| 125 | 137 | ||
| 126 | 138 | ||
| 127 | messages.add(new UserMessage("user", questin)); | 139 | messages.add(new UserMessage("user", questin)); |
-
请 注册 或 登录 后发表评论