Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,14 @@ public ChatClientResponse after(ChatClientResponse chatClientResponse, AdvisorCh

@Nullable
protected Filter.Expression doGetFilterExpression(Map<String, Object> context) {
if (!context.containsKey(FILTER_EXPRESSION)
|| !StringUtils.hasText(context.get(FILTER_EXPRESSION).toString())) {
var filterExpression = context.get(FILTER_EXPRESSION);
if (filterExpression instanceof Filter.Expression) {
return (Filter.Expression) filterExpression;
}
if (!context.containsKey(FILTER_EXPRESSION) || !StringUtils.hasText(filterExpression.toString())) {
return this.searchRequest.getFilterExpression();
}
return new FilterExpressionTextParser().parse(context.get(FILTER_EXPRESSION).toString());
return new FilterExpressionTextParser().parse(filterExpression.toString());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.springframework.ai.rag.retrieval.search.DocumentRetriever;
import org.springframework.ai.rag.retrieval.search.VectorStoreDocumentRetriever;
import org.springframework.ai.vectorstore.filter.Filter;
import org.springframework.ai.vectorstore.filter.FilterExpressionBuilder;
import org.springframework.ai.vectorstore.pgvector.PgVectorStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
Expand Down Expand Up @@ -134,4 +135,29 @@ void withRequestFilter() {
assertThat(retrievedDocuments).anyMatch(document -> document.getId().equals(documents.get("4").getId()));
}

@Test
void withRequestFilterExpression() {
FilterExpressionBuilder b = new FilterExpressionBuilder();
DocumentRetriever documentRetriever = VectorStoreDocumentRetriever.builder()
.vectorStore(this.pgVectorStore)
.similarityThreshold(0.50)
.topK(3)
.build();

Query query = Query.builder()
.text("Who is Anacletus?")
.context(Map.of(VectorStoreDocumentRetriever.FILTER_EXPRESSION,
b.eq("location", "Whispering Woods").build()))
.build();
List<Document> retrievedDocuments = documentRetriever.retrieve(query);

assertThat(retrievedDocuments).hasSize(2);
assertThat(retrievedDocuments).anyMatch(document -> document.getId().equals(documents.get("1").getId()));
assertThat(retrievedDocuments).anyMatch(document -> document.getId().equals(documents.get("2").getId()));

// No request filter expression applied, so full access to all documents.
retrievedDocuments = documentRetriever.retrieve(new Query("Who is Birba?"));
assertThat(retrievedDocuments).anyMatch(document -> document.getId().equals(documents.get("4").getId()));
}

}