|
@@ -4,10 +4,7 @@ import cn.dev33.satoken.SaManager;
|
|
|
import cn.hutool.core.lang.Tuple;
|
|
|
import cn.hutool.crypto.digest.MD5;
|
|
|
import cn.jlsxwkj.common.config.UserConfig;
|
|
|
-import cn.jlsxwkj.common.exception.CustomException;
|
|
|
-import cn.jlsxwkj.common.exception.FileTypeDoesNotSupportException;
|
|
|
-import cn.jlsxwkj.common.exception.InsertFailException;
|
|
|
-import cn.jlsxwkj.common.exception.UnknownException;
|
|
|
+import cn.jlsxwkj.common.exception.*;
|
|
|
import cn.jlsxwkj.common.utils.FileType;
|
|
|
import cn.jlsxwkj.common.utils.Log;
|
|
|
import cn.jlsxwkj.common.utils.MergeDocuments;
|
|
@@ -89,7 +86,9 @@ public class ChatService {
|
|
|
* @return 文本内容
|
|
|
*/
|
|
|
public String search(String keyword) {
|
|
|
- return mergeDocuments(vectorStore.similaritySearch(SearchRequest.query(keyword).withSimilarityThreshold(0.5)));
|
|
|
+ SearchRequest searchRequest = SearchRequest.query(keyword).withSimilarityThreshold(0.5);
|
|
|
+ List<Document> documents = vectorStore.similaritySearch(searchRequest);
|
|
|
+ return mergeDocuments(documents);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -121,7 +120,11 @@ public class ChatService {
|
|
|
String fileName = supportFile.get(1);
|
|
|
String fileTypeCn = supportFile.get(2);
|
|
|
DocumentReader reader = supportFile.get(3);
|
|
|
- String context = fileTypeCn + " :: " + fileName + " ====> " + mergeDocuments(reader.get()) + "\n" + "↑ " + message;
|
|
|
+ String format = """
|
|
|
+ %s :: %s ====> %s
|
|
|
+ ↑ %s
|
|
|
+ """;
|
|
|
+ String context = String.format(format, fileTypeCn, fileName, mergeDocuments(reader.get()), message);
|
|
|
UserMessage userMessage = new UserMessage(context);
|
|
|
Log.info(this.getClass(), context);
|
|
|
return stream(message, userMessage);
|
|
@@ -135,7 +138,8 @@ public class ChatService {
|
|
|
*/
|
|
|
private String mergeDocuments(List<Document> documents) {
|
|
|
return MergeDocuments.mergeDocuments(documents).stream()
|
|
|
- .map(Document::getContent).collect(Collectors.joining("\n"));
|
|
|
+ .map(Document::getContent)
|
|
|
+ .collect(Collectors.joining("\n"));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -157,14 +161,15 @@ public class ChatService {
|
|
|
listMessage.add(new SystemMessage(userConfig.getSysMessage()));
|
|
|
}
|
|
|
if (listMessage.size() > userConfig.getMaxListMessageLength()) {
|
|
|
- listMessage = listMessage.subList(listMessage.size() - userConfig.getMaxListMessageLength(), listMessage.size())
|
|
|
- .stream()
|
|
|
+ int start = listMessage.size() - userConfig.getMaxListMessageLength();
|
|
|
+ listMessage = listMessage.subList(start, listMessage.size()).stream()
|
|
|
.filter(msg -> !msg.getContent().equals(userConfig.getSysMessage()))
|
|
|
.map(msg -> {
|
|
|
if (msg.getRole().equals(UserRole.ASSISTANT)) {
|
|
|
String content = msg.getContent();
|
|
|
if (content.length() > userConfig.getMaxMessageLength()) {
|
|
|
- return new AssistantMessage(content.substring(0, userConfig.getMaxMessageLength()));
|
|
|
+ String assistantMessage = content.substring(0, userConfig.getMaxMessageLength());
|
|
|
+ return new AssistantMessage(assistantMessage);
|
|
|
}
|
|
|
}
|
|
|
return msg;
|
|
@@ -223,11 +228,11 @@ public class ChatService {
|
|
|
* @return 元组
|
|
|
*/
|
|
|
private Tuple isSupportFile(MultipartFile file) throws CustomException {
|
|
|
- byte[] bytes = new byte[0];
|
|
|
+ byte[] bytes;
|
|
|
try {
|
|
|
bytes = file.getBytes();
|
|
|
} catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
+ throw new OpenFileFailException("读取文件失败");
|
|
|
}
|
|
|
String[] split = Objects.requireNonNull(file.getOriginalFilename()).split("\\.");
|
|
|
String fileType = split[split.length - 1].toLowerCase(Locale.ROOT);
|