|
@@ -3,7 +3,9 @@ package com.ruoyi.web.llm.service.impl;
|
3
|
3
|
import com.alibaba.fastjson2.JSONObject;
|
4
|
4
|
import com.ruoyi.common.config.RuoYiConfig;
|
5
|
5
|
import com.ruoyi.llm.domain.CmcChat;
|
|
6
|
+import com.ruoyi.llm.domain.CmcDocument;
|
6
|
7
|
import com.ruoyi.llm.service.ICmcChatService;
|
|
8
|
+import com.ruoyi.llm.service.ICmcDocumentService;
|
7
|
9
|
import com.ruoyi.web.llm.service.ILangChainMilvusService;
|
8
|
10
|
import dev.langchain4j.data.document.Document;
|
9
|
11
|
import dev.langchain4j.data.document.parser.apache.pdfbox.ApachePdfBoxDocumentParser;
|
|
@@ -48,6 +50,9 @@ public class LangChainMilvusServiceImpl implements ILangChainMilvusService
|
48
|
50
|
@Autowired
|
49
|
51
|
private ICmcChatService cmcChatService;
|
50
|
52
|
|
|
53
|
+ @Autowired
|
|
54
|
+ private ICmcDocumentService cmcDocumentService;
|
|
55
|
+
|
51
|
56
|
/**
|
52
|
57
|
* 导入知识库文件
|
53
|
58
|
*/
|
|
@@ -146,12 +151,14 @@ public class LangChainMilvusServiceImpl implements ILangChainMilvusService
|
146
|
151
|
.apiKey("1")
|
147
|
152
|
.build();
|
148
|
153
|
|
149
|
|
- CmcChat cmcChat = new CmcChat();
|
150
|
|
- cmcChat.setTopicId(topicId);
|
151
|
|
- List<CmcChat> cmcChatList = cmcChatService.selectCmcChatList(cmcChat);
|
152
|
|
- for (CmcChat chat : cmcChatList) {
|
153
|
|
- chatSession.addMessage(ChatMessage.ofUser(chat.getInput()));
|
154
|
|
- chatSession.addMessage(ChatMessage.ofAssistant(chat.getOutput()));
|
|
154
|
+ if (topicId != null) {
|
|
155
|
+ CmcChat cmcChat = new CmcChat();
|
|
156
|
+ cmcChat.setTopicId(topicId);
|
|
157
|
+ List<CmcChat> cmcChatList = cmcChatService.selectCmcChatList(cmcChat);
|
|
158
|
+ for (CmcChat chat : cmcChatList) {
|
|
159
|
+ chatSession.addMessage(ChatMessage.ofUser(chat.getInput()));
|
|
160
|
+ chatSession.addMessage(ChatMessage.ofAssistant(chat.getOutput()));
|
|
161
|
+ }
|
155
|
162
|
}
|
156
|
163
|
chatSession.addMessage(ChatMessage.ofUser(prompt));
|
157
|
164
|
|
|
@@ -184,26 +191,30 @@ public class LangChainMilvusServiceImpl implements ILangChainMilvusService
|
184
|
191
|
* 调用LLM生成回答
|
185
|
192
|
*/
|
186
|
193
|
@Override
|
187
|
|
- public Flux<AssistantMessage> generateAnswerWithDocument(EmbeddingModel embeddingModel, String filename, String topicId, String question, String llmServiceUrl) throws IOException {
|
188
|
|
-
|
189
|
|
- File profilePath = new File( RuoYiConfig.getProfile() + "/upload/rag/document/" + filename);
|
190
|
|
- List<TextSegment> segments = splitDocument(filename, profilePath);
|
191
|
|
- List<Embedding> embeddings = embeddingModel.embedAll(segments).content();
|
192
|
|
- InMemoryEmbeddingStore<TextSegment> embeddingStore = new InMemoryEmbeddingStore<>();
|
193
|
|
- embeddingStore.addAll(embeddings, segments);
|
194
|
|
- Embedding queryEmbedding = embeddingModel.embed(question).content();
|
195
|
|
- EmbeddingSearchRequest embeddingSearchRequest = EmbeddingSearchRequest.builder()
|
196
|
|
- .queryEmbedding(queryEmbedding)
|
197
|
|
- .maxResults(1)
|
198
|
|
- .build();
|
199
|
|
- String contexts = embeddingStore.search(embeddingSearchRequest).matches().get(0).embedded().text();
|
200
|
|
- String sb = "问题: " + question + "\n\n" +
|
201
|
|
- "根据以下上下文回答问题:\n\n" +
|
202
|
|
- "文件" + ": " +
|
203
|
|
- filename + "\n\n" +
|
204
|
|
- "上下文" + ": " +
|
205
|
|
- contexts + "\n\n";
|
206
|
|
- return generateAnswer(topicId, sb, llmServiceUrl);
|
|
194
|
+ public Flux<AssistantMessage> generateAnswerWithDocument(EmbeddingModel embeddingModel, String chatId, String question, String llmServiceUrl) throws IOException {
|
|
195
|
+ String topicId = cmcChatService.selectCmcChatByChatId(chatId).getTopicId();
|
|
196
|
+ CmcDocument cmcDocument = new CmcDocument();
|
|
197
|
+ cmcDocument.setChatId(chatId);
|
|
198
|
+ List<CmcDocument> documentList = cmcDocumentService.selectCmcDocumentList(cmcDocument);
|
|
199
|
+ StringBuilder sb = new StringBuilder("问题: " + question + "\n\n").append("根据以下上下文回答问题:\n\n");
|
|
200
|
+ for (CmcDocument document : documentList) {
|
|
201
|
+ File profilePath = new File(RuoYiConfig.getProfile() + "/upload/rag/document/" + document.getPath());
|
|
202
|
+ List<TextSegment> segments = splitDocument(document.getPath(), profilePath);
|
|
203
|
+ List<Embedding> embeddings = embeddingModel.embedAll(segments).content();
|
|
204
|
+ InMemoryEmbeddingStore<TextSegment> embeddingStore = new InMemoryEmbeddingStore<>();
|
|
205
|
+ embeddingStore.addAll(embeddings, segments);
|
|
206
|
+ Embedding queryEmbedding = embeddingModel.embed(question).content();
|
|
207
|
+ EmbeddingSearchRequest embeddingSearchRequest = EmbeddingSearchRequest.builder()
|
|
208
|
+ .queryEmbedding(queryEmbedding)
|
|
209
|
+ .maxResults(1)
|
|
210
|
+ .build();
|
|
211
|
+ String contexts = embeddingStore.search(embeddingSearchRequest).matches().get(0).embedded().text();
|
|
212
|
+ sb.append("文件").append(": ")
|
|
213
|
+ .append(document.getPath()).append("\n\n")
|
|
214
|
+ .append("上下文").append(": ")
|
|
215
|
+ .append(contexts).append("\n\n");
|
|
216
|
+ }
|
|
217
|
+ return generateAnswer(topicId, sb.toString(), llmServiceUrl);
|
207
|
218
|
}
|
208
|
219
|
|
209
|
220
|
/**
|
|
@@ -270,8 +281,4 @@ public class LangChainMilvusServiceImpl implements ILangChainMilvusService
|
270
|
281
|
DocumentByParagraphSplitter splitter = new DocumentByParagraphSplitter(1000,200);
|
271
|
282
|
return splitter.split(document);
|
272
|
283
|
}
|
273
|
|
- interface Assistant {
|
274
|
|
-
|
275
|
|
- String chat(String message);
|
276
|
|
- }
|
277
|
284
|
}
|