Open MatinKhajavi opened 1 month ago
Hey @MatinKhajavi! I'm a bot here to help you with bug fixes, questions, and becoming a contributor. I'm here to assist you while waiting for a human maintainer.
Your current approach of using a set_filters
function to handle query-time metadata filtering in a Pinecone Retriever is indeed a standard and efficient method. This allows you to set different filters for each query dynamically.
The documentation does not mention a filters
attribute for QueryBundle
, and there is no indication that such an attribute is necessary. The current method using MetadataFilters
and set_filters
appears to be the best approach.
Here is an example of setting up and using metadata filters:
from llama_index.core.vector_stores import (
MetadataFilter,
MetadataFilters,
FilterOperator,
FilterCondition,
)
# Example of setting up a single filter
filters = MetadataFilters(
filters=[
MetadataFilter(
key="theme", operator=FilterOperator.EQ, value="Fiction"
),
]
)
# Example of setting up multiple filters with AND condition
filters = MetadataFilters(
filters=[
MetadataFilter(key="theme", value="Fiction"),
MetadataFilter(key="year", value=1997, operator=FilterOperator.GT),
],
condition=FilterCondition.AND,
)
# Example of setting up multiple filters with OR condition
filters = MetadataFilters(
filters=[
MetadataFilter(key="theme", value="Fiction"),
MetadataFilter(key="year", value=1997, operator=FilterOperator.GT),
],
condition=FilterCondition.OR,
)
# Using the filters with the retriever
retriever = index.as_retriever(filters=filters)
retriever.retrieve("Harry Potter?")
Additionally, you can use keyword arguments specific to Pinecone:
retriever = index.as_retriever(
vector_store_kwargs={"filter": {"theme": "Mafia"}}
)
retriever.retrieve("What is inception about?")
The PineconeVectorStore
class supports converting standard metadata filters to Pinecone-specific filter dictionaries using the _to_pinecone_filter
function, ensuring compatibility and efficiency during query time [1][2][3][4][5].
Your implementation is correct, and there is no need to add a filters
attribute to QueryBundle
.
To continue talking to Dosu, mention @dosu.
Question Validation
Question
Hi,
I am trying to implement a low-level Pinecone Retriever, and I want the ability to have a different set of filters for each query (since each query might have different metadata) during retrieval. When inheriting from the
BaseRetriever
class, we override the_retrieve
function, but there is no way to pass these filters toretrieve
.From the documentation and discussions on Discord (As answered by @logan-markewich ), the only solution I've found is to recreate the retrieval process for each query. I have managed to do this by adding a
set_filters
function and calling it each time before calling the retrieve method. (I guess it is either that or having a function to recreate theVectoreStoreQuery
and passing filters through that)Am I missing something, or is this the only way to achieve this functionality?
Additionally, shouldn't
QueryBundle
have afilters
attribute for cases like this?I can implement this myself and submit a pull request if you think it is a good idea.
Here is my current code: