|
@@ -12,7 +12,9 @@ import org.apache.commons.lang3.StringUtils; |
|
@@ -12,7 +12,9 @@ import org.apache.commons.lang3.StringUtils; |
|
12
|
import org.jeecg.ai.handler.AIParams;
|
12
|
import org.jeecg.ai.handler.AIParams;
|
|
13
|
import org.jeecg.ai.handler.LLMHandler;
|
13
|
import org.jeecg.ai.handler.LLMHandler;
|
|
14
|
import org.jeecg.common.api.vo.Result;
|
14
|
import org.jeecg.common.api.vo.Result;
|
|
|
|
15
|
+import org.jeecg.modules.airag.app.entity.AiragLog;
|
|
15
|
import org.jeecg.modules.airag.app.entity.QuestionEmbedding;
|
16
|
import org.jeecg.modules.airag.app.entity.QuestionEmbedding;
|
|
|
|
17
|
+import org.jeecg.modules.airag.app.service.IAiragLogService;
|
|
16
|
import org.jeecg.modules.airag.app.service.IQuestionEmbeddingService;
|
18
|
import org.jeecg.modules.airag.app.service.IQuestionEmbeddingService;
|
|
17
|
import org.jeecg.modules.airag.app.utils.FileToBase64Util;
|
19
|
import org.jeecg.modules.airag.app.utils.FileToBase64Util;
|
|
18
|
import org.jeecg.modules.airag.common.handler.IAIChatHandler;
|
20
|
import org.jeecg.modules.airag.common.handler.IAIChatHandler;
|
|
@@ -25,10 +27,7 @@ import org.springframework.web.bind.annotation.GetMapping; |
|
@@ -25,10 +27,7 @@ import org.springframework.web.bind.annotation.GetMapping; |
|
25
|
import org.springframework.web.bind.annotation.RequestMapping;
|
27
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
26
|
import org.springframework.web.bind.annotation.RestController;
|
28
|
import org.springframework.web.bind.annotation.RestController;
|
|
27
|
|
29
|
|
|
28
|
-import java.util.ArrayList;
|
|
|
|
29
|
-import java.util.HashMap;
|
|
|
|
30
|
-import java.util.List;
|
|
|
|
31
|
-import java.util.Map;
|
30
|
+import java.util.*;
|
|
32
|
|
31
|
|
|
33
|
@RestController
|
32
|
@RestController
|
|
34
|
@RequestMapping("/airag/zdyRag")
|
33
|
@RequestMapping("/airag/zdyRag")
|
|
@@ -42,14 +41,24 @@ public class ZdyRagController { |
|
@@ -42,14 +41,24 @@ public class ZdyRagController { |
|
42
|
private IQuestionEmbeddingService questionEmbeddingService;
|
41
|
private IQuestionEmbeddingService questionEmbeddingService;
|
|
43
|
@Value("${jeecg.upload.path}")
|
42
|
@Value("${jeecg.upload.path}")
|
|
44
|
private String uploadPath;
|
43
|
private String uploadPath;
|
|
|
|
44
|
+ @Autowired
|
|
|
|
45
|
+ private IAiragLogService airagLogService;
|
|
45
|
|
46
|
|
|
46
|
|
47
|
|
|
47
|
@Operation(summary = "send")
|
48
|
@Operation(summary = "send")
|
|
48
|
@GetMapping("send")
|
49
|
@GetMapping("send")
|
|
49
|
public Result<Map<String, Object>> send(String questionText) throws Exception {
|
50
|
public Result<Map<String, Object>> send(String questionText) throws Exception {
|
|
50
|
String knowId = "1926872137990148098";
|
51
|
String knowId = "1926872137990148098";
|
|
|
|
52
|
+ String modelId = "1926875898187878401";
|
|
51
|
Integer topNumber = 1;
|
53
|
Integer topNumber = 1;
|
|
52
|
Double similarity = 0.8;
|
54
|
Double similarity = 0.8;
|
|
|
|
55
|
+ // 创建日志对象
|
|
|
|
56
|
+ AiragLog logRecord = new AiragLog()
|
|
|
|
57
|
+ .setQuestion(questionText)
|
|
|
|
58
|
+ .setModelId(modelId)
|
|
|
|
59
|
+ .setCreateTime(new Date());
|
|
|
|
60
|
+
|
|
|
|
61
|
+
|
|
53
|
HashMap<String, Object> resMap = new HashMap<>();
|
62
|
HashMap<String, Object> resMap = new HashMap<>();
|
|
54
|
//根据问题相似度进行查询
|
63
|
//根据问题相似度进行查询
|
|
55
|
List<QuestionEmbedding> questionEmbeddings = questionEmbeddingService.similaritySearchByQuestion(questionText, 1,0.8);
|
64
|
List<QuestionEmbedding> questionEmbeddings = questionEmbeddingService.similaritySearchByQuestion(questionText, 1,0.8);
|
|
@@ -70,6 +79,10 @@ public class ZdyRagController { |
|
@@ -70,6 +79,10 @@ public class ZdyRagController { |
|
70
|
resMap.put("fileBase64", FileToBase64Util.fileToBase64(uploadPath + fileName));
|
79
|
resMap.put("fileBase64", FileToBase64Util.fileToBase64(uploadPath + fileName));
|
|
71
|
}
|
80
|
}
|
|
72
|
}
|
81
|
}
|
|
|
|
82
|
+ // 记录日志 - 从问题库匹配
|
|
|
|
83
|
+ logRecord.setAnswer(questionEmbedding.getAnswer());
|
|
|
|
84
|
+ logRecord.setAnswerType(1);
|
|
|
|
85
|
+ airagLogService.save(logRecord);
|
|
73
|
|
86
|
|
|
74
|
log.info("questionEmbedding.getMetadata() = " + questionEmbedding.getMetadata());
|
87
|
log.info("questionEmbedding.getMetadata() = " + questionEmbedding.getMetadata());
|
|
75
|
log.info("questionEmbedding.getQuestion() = " + questionEmbedding.getQuestion());
|
88
|
log.info("questionEmbedding.getQuestion() = " + questionEmbedding.getQuestion());
|
|
@@ -85,8 +98,10 @@ public class ZdyRagController { |
|
@@ -85,8 +98,10 @@ public class ZdyRagController { |
|
85
|
List<Map<String, Object>> maps = embeddingHandler.searchEmbedding(knowId, questionText, topNumber, similarity);
|
98
|
List<Map<String, Object>> maps = embeddingHandler.searchEmbedding(knowId, questionText, topNumber, similarity);
|
|
86
|
if (CollectionUtil.isEmpty(maps)) {
|
99
|
if (CollectionUtil.isEmpty(maps)) {
|
|
87
|
resMap.put("answer", "该问题未记录在知识库中");
|
100
|
resMap.put("answer", "该问题未记录在知识库中");
|
|
88
|
- //未记录在知识库中的问题插入未记录问题表
|
|
|
|
89
|
-
|
101
|
+ // 记录日志 - 未命中任何知识库
|
|
|
|
102
|
+ logRecord.setAnswer("该问题未记录在知识库中");
|
|
|
|
103
|
+ logRecord.setAnswerType(3);
|
|
|
|
104
|
+ airagLogService.save(logRecord);
|
|
90
|
|
105
|
|
|
91
|
return Result.OK(resMap);
|
106
|
return Result.OK(resMap);
|
|
92
|
}
|
107
|
}
|
|
@@ -102,19 +117,27 @@ public class ZdyRagController { |
|
@@ -102,19 +117,27 @@ public class ZdyRagController { |
|
102
|
|
117
|
|
|
103
|
|
118
|
|
|
104
|
List<ChatMessage> messages = new ArrayList<>();
|
119
|
List<ChatMessage> messages = new ArrayList<>();
|
|
105
|
- String questin = "请整理出与用户所提出的问题相关的信息,参考内容中含有与问题无关信息,舍弃掉与问题无关的内容,进行整理,回答用户的问题" +
|
120
|
+ String questin = "请整理出与用户所提出的问题相关的信息," +
|
|
|
|
121
|
+ "参考内容中含有与问题无关信息,舍弃掉与问题无关的内容,进行整理,回答用户的问题" +
|
|
106
|
"问题如下:" + questionText +
|
122
|
"问题如下:" + questionText +
|
|
107
|
"参考信息如下:" + content ;
|
123
|
"参考信息如下:" + content ;
|
|
108
|
|
124
|
|
|
109
|
|
125
|
|
|
110
|
messages.add(new UserMessage("user", questin));
|
126
|
messages.add(new UserMessage("user", questin));
|
|
111
|
- String chat = aiChatHandler.completions("1926875898187878401", messages, null);
|
127
|
+ String chat = aiChatHandler.completions(modelId, messages, null);
|
|
112
|
resMap.put("question", questionText);
|
128
|
resMap.put("question", questionText);
|
|
113
|
resMap.put("answer", chat);
|
129
|
resMap.put("answer", chat);
|
|
114
|
resMap.put("similarity", maps.get(0).get("score").toString());
|
130
|
resMap.put("similarity", maps.get(0).get("score").toString());
|
|
115
|
String fileName = generateFilePath(maps.get(0).get("metadata").toString());
|
131
|
String fileName = generateFilePath(maps.get(0).get("metadata").toString());
|
|
116
|
resMap.put("fileName", fileName);
|
132
|
resMap.put("fileName", fileName);
|
|
117
|
resMap.put("fileBase64",FileToBase64Util.fileToBase64(uploadPath + fileName));
|
133
|
resMap.put("fileBase64",FileToBase64Util.fileToBase64(uploadPath + fileName));
|
|
|
|
134
|
+
|
|
|
|
135
|
+
|
|
|
|
136
|
+ // 记录日志 - 从知识库生成回答
|
|
|
|
137
|
+ logRecord.setAnswer(chat);
|
|
|
|
138
|
+ logRecord.setAnswerType(2);
|
|
|
|
139
|
+ airagLogService.save(logRecord);
|
|
|
|
140
|
+
|
|
118
|
return Result.OK(resMap);
|
141
|
return Result.OK(resMap);
|
|
119
|
}
|
142
|
}
|
|
120
|
|
143
|
|