|
...
|
...
|
@@ -12,7 +12,9 @@ import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.jeecg.ai.handler.AIParams;
|
|
|
|
import org.jeecg.ai.handler.LLMHandler;
|
|
|
|
import org.jeecg.common.api.vo.Result;
|
|
|
|
import org.jeecg.modules.airag.app.entity.AiragLog;
|
|
|
|
import org.jeecg.modules.airag.app.entity.QuestionEmbedding;
|
|
|
|
import org.jeecg.modules.airag.app.service.IAiragLogService;
|
|
|
|
import org.jeecg.modules.airag.app.service.IQuestionEmbeddingService;
|
|
|
|
import org.jeecg.modules.airag.app.utils.FileToBase64Util;
|
|
|
|
import org.jeecg.modules.airag.common.handler.IAIChatHandler;
|
|
...
|
...
|
@@ -25,10 +27,7 @@ import org.springframework.web.bind.annotation.GetMapping; |
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
@RequestMapping("/airag/zdyRag")
|
|
...
|
...
|
@@ -42,14 +41,24 @@ public class ZdyRagController { |
|
|
|
private IQuestionEmbeddingService questionEmbeddingService;
|
|
|
|
@Value("${jeecg.upload.path}")
|
|
|
|
private String uploadPath;
|
|
|
|
@Autowired
|
|
|
|
private IAiragLogService airagLogService;
|
|
|
|
|
|
|
|
|
|
|
|
@Operation(summary = "send")
|
|
|
|
@GetMapping("send")
|
|
|
|
public Result<Map<String, Object>> send(String questionText) throws Exception {
|
|
|
|
String knowId = "1926872137990148098";
|
|
|
|
String modelId = "1926875898187878401";
|
|
|
|
Integer topNumber = 1;
|
|
|
|
Double similarity = 0.8;
|
|
|
|
// 创建日志对象
|
|
|
|
AiragLog logRecord = new AiragLog()
|
|
|
|
.setQuestion(questionText)
|
|
|
|
.setModelId(modelId)
|
|
|
|
.setCreateTime(new Date());
|
|
|
|
|
|
|
|
|
|
|
|
HashMap<String, Object> resMap = new HashMap<>();
|
|
|
|
//根据问题相似度进行查询
|
|
|
|
List<QuestionEmbedding> questionEmbeddings = questionEmbeddingService.similaritySearchByQuestion(questionText, 1,0.8);
|
|
...
|
...
|
@@ -70,6 +79,10 @@ public class ZdyRagController { |
|
|
|
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());
|
|
...
|
...
|
@@ -85,8 +98,10 @@ public class ZdyRagController { |
|
|
|
List<Map<String, Object>> maps = embeddingHandler.searchEmbedding(knowId, questionText, topNumber, similarity);
|
|
|
|
if (CollectionUtil.isEmpty(maps)) {
|
|
|
|
resMap.put("answer", "该问题未记录在知识库中");
|
|
|
|
//未记录在知识库中的问题插入未记录问题表
|
|
|
|
|
|
|
|
// 记录日志 - 未命中任何知识库
|
|
|
|
logRecord.setAnswer("该问题未记录在知识库中");
|
|
|
|
logRecord.setAnswerType(3);
|
|
|
|
airagLogService.save(logRecord);
|
|
|
|
|
|
|
|
return Result.OK(resMap);
|
|
|
|
}
|
|
...
|
...
|
@@ -102,19 +117,27 @@ public class ZdyRagController { |
|
|
|
|
|
|
|
|
|
|
|
List<ChatMessage> messages = new ArrayList<>();
|
|
|
|
String questin = "请整理出与用户所提出的问题相关的信息,参考内容中含有与问题无关信息,舍弃掉与问题无关的内容,进行整理,回答用户的问题" +
|
|
|
|
String questin = "请整理出与用户所提出的问题相关的信息," +
|
|
|
|
"参考内容中含有与问题无关信息,舍弃掉与问题无关的内容,进行整理,回答用户的问题" +
|
|
|
|
"问题如下:" + questionText +
|
|
|
|
"参考信息如下:" + content ;
|
|
|
|
|
|
|
|
|
|
|
|
messages.add(new UserMessage("user", questin));
|
|
|
|
String chat = aiChatHandler.completions("1926875898187878401", messages, null);
|
|
|
|
String chat = aiChatHandler.completions(modelId, messages, null);
|
|
|
|
resMap.put("question", questionText);
|
|
|
|
resMap.put("answer", chat);
|
|
|
|
resMap.put("similarity", maps.get(0).get("score").toString());
|
|
|
|
String fileName = generateFilePath(maps.get(0).get("metadata").toString());
|
|
|
|
resMap.put("fileName", fileName);
|
|
|
|
resMap.put("fileBase64",FileToBase64Util.fileToBase64(uploadPath + fileName));
|
|
|
|
|
|
|
|
|
|
|
|
// 记录日志 - 从知识库生成回答
|
|
|
|
logRecord.setAnswer(chat);
|
|
|
|
logRecord.setAnswerType(2);
|
|
|
|
airagLogService.save(logRecord);
|
|
|
|
|
|
|
|
return Result.OK(resMap);
|
|
|
|
}
|
|
|
|
|
...
|
...
|
|