Commit e6c91d4a authored by 林洋洋's avatar 林洋洋

优化知识库召回提示词

parent a52c0d1b
...@@ -17,10 +17,9 @@ import org.springframework.beans.factory.annotation.Value; ...@@ -17,10 +17,9 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import java.util.Arrays; import java.util.*;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.regex.Matcher; import java.util.regex.Matcher;
...@@ -88,9 +87,25 @@ public class ChatService { ...@@ -88,9 +87,25 @@ public class ChatService {
* @return 拼接后的字符串 * @return 拼接后的字符串
*/ */
public String convertDocumentsToString(List<Document> documents) { public String convertDocumentsToString(List<Document> documents) {
if (documents == null || documents.isEmpty()) {
return "";
}
AtomicInteger index = new AtomicInteger(1);
return documents.stream() return documents.stream()
.map(Document::getText) // 提取每个Document的内容 .filter(Objects::nonNull) // 1. 过滤 null Document
.collect(Collectors.joining("\n")); // 用换行符拼接 .map(doc -> {
String title = Optional.ofNullable(doc.getMetadata())
.map(m -> String.valueOf(m.get("fileName")))
.orElse(""); // 2. 标题 null-safe
String text = Optional.ofNullable(doc.getText())
.orElse(""); // 3. 正文 null-safe
return String.format("[%d] %s\n%s",
index.getAndIncrement(),
title,
text);
})
.collect(Collectors.joining("\n\n"));
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment