|
...
|
...
|
@@ -9,9 +9,11 @@ import dev.langchain4j.store.embedding.pgvector.PgVectorEmbeddingStore; |
|
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import org.apache.shiro.SecurityUtils;
|
|
|
|
import org.jeecg.ai.handler.AIParams;
|
|
|
|
import org.jeecg.ai.handler.LLMHandler;
|
|
|
|
import org.jeecg.common.api.vo.Result;
|
|
|
|
import org.jeecg.common.system.vo.LoginUser;
|
|
|
|
import org.jeecg.modules.airag.app.entity.AiragLog;
|
|
|
|
import org.jeecg.modules.airag.app.entity.QuestionEmbedding;
|
|
|
|
import org.jeecg.modules.airag.app.service.IAiragLogService;
|
|
...
|
...
|
@@ -52,6 +54,7 @@ public class ZdyRagController { |
|
|
|
String modelId = "1926875898187878401";
|
|
|
|
Integer topNumber = 1;
|
|
|
|
Double similarity = 0.8;
|
|
|
|
|
|
|
|
// 创建日志对象
|
|
|
|
AiragLog logRecord = new AiragLog()
|
|
|
|
.setQuestion(questionText)
|
|
...
|
...
|
@@ -59,43 +62,44 @@ public class ZdyRagController { |
|
|
|
.setCreateTime(new Date());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HashMap<String, Object> resMap = new HashMap<>();
|
|
|
|
//根据问题相似度进行查询
|
|
|
|
// List<QuestionEmbedding> questionEmbeddings = questionEmbeddingService.similaritySearchByQuestion(questionText, 1,0.8);
|
|
|
|
// for (QuestionEmbedding questionEmbedding : questionEmbeddings) {
|
|
|
|
// resMap.put("question", questionText);
|
|
|
|
// resMap.put("answer", questionEmbedding.getAnswer());
|
|
|
|
// resMap.put("similarity", questionEmbedding.getSimilarity());
|
|
|
|
//
|
|
|
|
// ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
// Map<String, String> metadata = objectMapper.readValue(questionEmbedding.getMetadata(), Map.class);
|
|
|
|
// // 获取docName和docId
|
|
|
|
// if (metadata != null) {
|
|
|
|
// String docName = metadata.get("docName");
|
|
|
|
// resMap.put("fileName", docName);
|
|
|
|
// String fileName = generateFilePath(questionEmbedding.getMetadata());
|
|
|
|
//
|
|
|
|
// if (StringUtils.isNotBlank(fileName)) {
|
|
|
|
// resMap.put("fileBase64", FileToBase64Util.fileToBase64(uploadPath + fileName));
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// // 记录日志 - 从问题库匹配
|
|
|
|
// logRecord.setAnswer(questionEmbedding.getAnswer());
|
|
|
|
// logRecord.setAnswerType(1);
|
|
|
|
// airagLogService.save(logRecord);
|
|
|
|
//
|
|
|
|
// log.info("questionEmbedding.getMetadata() = " + questionEmbedding.getMetadata());
|
|
|
|
// log.info("questionEmbedding.getQuestion() = " + questionEmbedding.getQuestion());
|
|
|
|
// log.info("questionEmbedding.getAnswer() = " + questionEmbedding.getAnswer());
|
|
|
|
// log.info("questionEmbedding.getSimilarity() = " + questionEmbedding.getSimilarity());
|
|
|
|
// log.info("-------------------------------------------------------------");
|
|
|
|
// }
|
|
|
|
// //返回问题库命中的问题
|
|
|
|
// if (!questionEmbeddings.isEmpty()) {
|
|
|
|
// return Result.OK(resMap);
|
|
|
|
// }
|
|
|
|
|
|
|
|
List<Map<String, Object>> maps = embeddingHandler.searchEmbedding(knowId, questionText, topNumber, similarity);
|
|
|
|
List<QuestionEmbedding> questionEmbeddings = questionEmbeddingService.similaritySearchByQuestion(questionText, 1,0.8);
|
|
|
|
for (QuestionEmbedding questionEmbedding : questionEmbeddings) {
|
|
|
|
resMap.put("question", questionText);
|
|
|
|
resMap.put("answer", questionEmbedding.getAnswer());
|
|
|
|
resMap.put("similarity", questionEmbedding.getSimilarity());
|
|
|
|
|
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
Map<String, String> metadata = objectMapper.readValue(questionEmbedding.getMetadata(), Map.class);
|
|
|
|
// 获取docName和docId
|
|
|
|
if (metadata != null) {
|
|
|
|
String docName = metadata.get("docName");
|
|
|
|
resMap.put("fileName", docName);
|
|
|
|
String fileName = generateFilePath(questionEmbedding.getMetadata());
|
|
|
|
|
|
|
|
if (StringUtils.isNotBlank(fileName)) {
|
|
|
|
resMap.put("fileBase64", FileToBase64Util.fileToBase64(uploadPath + fileName));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 记录日志 - 从问题库匹配
|
|
|
|
logRecord.setAnswer(questionEmbedding.getAnswer());
|
|
|
|
logRecord.setAnswerType(1);
|
|
|
|
airagLogService.save(logRecord);
|
|
|
|
|
|
|
|
log.info("questionEmbedding.getMetadata() = " + questionEmbedding.getMetadata());
|
|
|
|
log.info("questionEmbedding.getQuestion() = " + questionEmbedding.getQuestion());
|
|
|
|
log.info("questionEmbedding.getAnswer() = " + questionEmbedding.getAnswer());
|
|
|
|
log.info("questionEmbedding.getSimilarity() = " + questionEmbedding.getSimilarity());
|
|
|
|
log.info("-------------------------------------------------------------");
|
|
|
|
}
|
|
|
|
//返回问题库命中的问题
|
|
|
|
if (!questionEmbeddings.isEmpty()) {
|
|
|
|
return Result.OK(resMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
List<Map<String, Object>> maps = embeddingHandler.searchEmbedding(knowId, questionText, 3, 0.75);
|
|
|
|
if (CollectionUtil.isEmpty(maps)) {
|
|
|
|
resMap.put("answer", "该问题未记录在知识库中");
|
|
|
|
// 记录日志 - 未命中任何知识库
|
|
...
|
...
|
@@ -109,7 +113,7 @@ public class ZdyRagController { |
|
|
|
|
|
|
|
StringBuilder content = new StringBuilder();
|
|
|
|
for (Map<String, Object> map : maps) {
|
|
|
|
if (Double.parseDouble(map.get("score").toString()) > similarity){
|
|
|
|
if (Double.parseDouble(map.get("score").toString()) > 0.75){
|
|
|
|
log.info("score = " + map.get("score").toString());
|
|
|
|
log.info("content = " + map.get("content").toString());
|
|
|
|
content.append(map.get("content").toString()).append("\n");
|
|
...
|
...
|
@@ -118,10 +122,18 @@ public class ZdyRagController { |
|
|
|
|
|
|
|
|
|
|
|
List<ChatMessage> messages = new ArrayList<>();
|
|
|
|
String questin = "请整理出与用户所提出的问题相关的信息," +
|
|
|
|
"参考内容中含有与问题无关信息,舍弃掉与问题无关的内容,进行整理,回答用户的问题" +
|
|
|
|
"问题如下:" + questionText +
|
|
|
|
"参考信息如下:" + content ;
|
|
|
|
String questin = "你是一个严谨的信息处理助手,请严格按照以下要求处理用户问题:" + questionText + "\n\n" +
|
|
|
|
"处理步骤和要求:\n" +
|
|
|
|
"1. 严格基于参考内容回答,禁止任何超出参考内容的推断或想象\n" +
|
|
|
|
"2. 回答结构:\n" +
|
|
|
|
" - 首先用一句话直接回答问题核心(仅限参考内容中明确包含的信息)\n" +
|
|
|
|
" - 然后列出支持该答案的具体证据(可直接引用参考内容)\n" +
|
|
|
|
"3. 禁止以下行为:\n" +
|
|
|
|
" - 添加参考内容中不存在的信息\n" +
|
|
|
|
" - 进行任何推测性陈述\n" +
|
|
|
|
" - 使用模糊或不确定的表达\n" +
|
|
|
|
" - 参考内容为空时应该拒绝回答\n" +
|
|
|
|
"参考内容(请严格限制回答范围于此):\n" + content;
|
|
|
|
|
|
|
|
|
|
|
|
messages.add(new UserMessage("user", questin));
|
...
|
...
|
|