Skip to content

Commit 0308d2a

Browse files
fix(admin): guard null topK/threshold in KnowledgeBaseDocumentRetriever (#4736)
The public retrieve(Query) auto-unboxes searchOptions.getSimilarityThreshold() in the filter and searchOptions.getTopK() in the limit, so it throws an NPE on the first RAG-backed chat turn whenever the agent-level FileSearchOptions leaves topK/similarityThreshold unset (the default). The private retrieve(KnowledgeBase, Query) in the same class already null-checks both; mirror that null-safety here. When topK is null the merged results are returned uncapped (Long.MAX_VALUE) and a null similarityThreshold skips the threshold filter — per-KB retrieval already applied its own. Resolves the NPE reported in #4734. Optional companion (not included): adding @Builder.Default to FileSearchOptions.topK/similarityThreshold would make the documented defaults of 3 / 0.2 actually apply — left as a maintainer decision on the intended semantics.
1 parent 03e18e8 commit 0308d2a

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

spring-ai-alibaba-admin/spring-ai-alibaba-admin-server-core/src/main/java/com/alibaba/cloud/ai/studio/core/rag/retriever/KnowledgeBaseDocumentRetriever.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,13 @@ public List<Document> retrieve(@NotNull Query query) {
9696
documents.addAll(future.get(SEARCH_TIMEOUT, TimeUnit.SECONDS));
9797
}
9898

99+
Integer topK = searchOptions.getTopK();
100+
Float similarityThreshold = searchOptions.getSimilarityThreshold();
99101
List<Document> results = documents.stream()
100102
.sorted(Comparator.comparing(Document::getScore, Comparator.nullsLast(Comparator.reverseOrder())))
101-
.filter(x -> x.getScore() != null && x.getScore() > searchOptions.getSimilarityThreshold())
102-
.limit(searchOptions.getTopK())
103+
.filter(x -> x.getScore() != null
104+
&& (similarityThreshold == null || x.getScore() > similarityThreshold))
105+
.limit(topK != null ? topK.longValue() : Long.MAX_VALUE)
103106
.toList();
104107

105108
LogUtils.monitor("DocumentRetriever", "retrieve", start, SUCCESS, query.text(), results.size());

0 commit comments

Comments
 (0)