Find answers from the community

Updated 8 months ago

How do I add filters to retrievers?

At a glance
The post asks how to add filters to retrievers. The comments provide several examples of how to create filter objects using the MetadataFilters, MetadataFilter, Filter, FieldCondition, and MatchValue classes from the llama_index.core.vector_stores module. The community members demonstrate how to pass these filter objects to the as_retriever method of an index to apply the filters. There is no explicitly marked answer, but the comments provide detailed code examples to address the original question.
Useful resources
@kapa.ai How do I add filters to retrievers?
k
3 comments
Please stand by @Callam as I search the knowledge base. I normally take about 20 seconds or so. :wave:
----------------------
To add filters to retrievers in LlamaIndex, you need to create a filter object and pass it to the as_retriever method of the index. The filter object can be created using the Filter, MetadataFilters, or MetadataFilter classes from the llama_index.core.vector_stores module, depending on the type of filter you want to apply.

Here are some examples:

  1. Using MetadataFilters and MetadataFilter:
Plain Text
from llama_index.core.vector_stores import MetadataFilters, MetadataFilter

filters = MetadataFilters(
    filters=[
        MetadataFilter(key="theme", value="Mafia"),
        MetadataFilter(key="year", value=1972),
    ]
)

retriever = index.as_retriever(filters=filters)
retriever.retrieve("What is inception?")

Source ()

  1. Using Filter, FieldCondition, and MatchValue:
```python
filters = Filter(
should=[
Filter(
must=[
FieldCondition(
key="fruit",
match=MatchValue(value="apple"),
),
FieldCondition(
key="city",
match=MatchValue(value="Tokyo"),
),
]
),
Filter(
must=[
FieldCondition(
key="fruit",
match=MatchValue(value="grape"),
),
FieldCondition(
key="city",
match=MatchValue(value="Toronto"),
),
]
),
]
)
retriever = index.as_retriever(vector_store_kwargs={"qdrant_filters": filters})
response = retriever.retrieve("Who makes grapes?")
Plain Text
**Source** (<https://docs.llamaindex.ai/en/latest/examples/vector_stores/Qdrant_using_qdrant_filters/>)

3. Using `MetadataFilters` with `FilterOperator` and `FilterCondition`:
python
from llama_index.core.vector_stores import FilterOperator, FilterCondition

filters = MetadataFilters(
filters=[
MetadataFilter(
key="theme",
value=["Fiction", "Horror"],
operator=FilterOperator.IN,
),
MetadataFilter(key="year", value=1997, operator=FilterOperator.GT),
],
condition=FilterCondition.AND,
)

retriever = index.as_retriever(filters=filters)
retriever.retrieve("Harry Potter?")
```
Source ()

Remember to replace the keys, values, and operators with the ones that suit your needs.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Add a reply
Sign up and join the conversation on Discord