Request-time filter expressions for RAG

When using the RetrievalAugmentationAdvisor with the VectorStoreDocumentRetriever, it’s now possible to provide a filter expression at request-time as an advisor context variable with key VectorStoreDocumentRetriever.FILTER_EXPRESSION.

Fixes gh-1776

Signed-off-by: Thomas Vitale <ThomasVitale@users.noreply.github.com>
This commit is contained in:
Thomas Vitale
2025-03-09 11:29:34 +01:00
committed by Ilayaperumal Gopinathan
parent 82b46d2182
commit 5a4e9f5108
6 changed files with 132 additions and 16 deletions

View File

@@ -39,15 +39,12 @@ This filter expression can be configured when creating the `QuestionAnswerAdviso
Here is how to create an instance of `QuestionAnswerAdvisor` where the threshold is `0.8` and to return the top `6` reulsts.
[source,java]
----
var qaAdvisor = new QuestionAnswerAdvisor(this.vectorStore,
SearchRequest.builder().similarityThreshold(0.8d).topK(6).build());
----
==== Dynamic Filter Expressions
Update the `SearchRequest` filter expression at runtime using the `FILTER_EXPRESSION` advisor context parameter:
@@ -118,6 +115,29 @@ String answer = chatClient.prompt()
.content();
----
The `VectorStoreDocumentRetriever` accepts a `FilterExpression` to filter the search results based on metadata.
You can provide one when instantiating the `VectorStoreDocumentRetriever` or at runtime per request,
using the `FILTER_EXPRESSION` advisor context parameter.
[source,java]
----
Advisor retrievalAugmentationAdvisor = RetrievalAugmentationAdvisor.builder()
.documentRetriever(VectorStoreDocumentRetriever.builder()
.similarityThreshold(0.50)
.vectorStore(vectorStore)
.build())
.build();
String answer = chatClient.prompt()
.advisors(retrievalAugmentationAdvisor)
.advisors(a -> a.param(VectorStoreDocumentRetriever.FILTER_EXPRESSION, "type == 'Spring'"))
.user(question)
.call()
.content();
----
See xref:api/retrieval-augmented-generation.adoc#_vectorstoredocumentretriever for more information.
===== Advanced RAG
[source,java]
@@ -298,6 +318,18 @@ DocumentRetriever retriever = VectorStoreDocumentRetriever.builder()
List<Document> documents = retriever.retrieve(new Query("What are the KPIs for the next semester?"));
----
You can also provide a request-specific filter expression via the `Query` API, using the `FILTER_EXPRESSION` parameter.
If both the request-specific and the retriever-specific filter expressions are provided, the request-specific filter expression takes precedence.
[source,java]
----
Query query = Query.builder()
.text("Who is Anacletus?")
.context(Map.of(VectorStoreDocumentRetriever.FILTER_EXPRESSION, "location == 'Whispering Woods'"))
.build();
List<Document> retrievedDocuments = documentRetriever.retrieve(query);
----
==== Document Join
A component for combining documents retrieved based on multiple queries and from multiple data sources into