Find answers from the community

Updated 10 months ago

how to implement auto-retrieval with qdrant? The current retriever is using a sentence_win

At a glance
@kapa.ai how to implement auto-retrieval with qdrant? The current retriever is using a sentence_window_query_engine
k
t
7 comments
Please stand by @tatanfort as I search the knowledge base. I normally take about 20 seconds or so. :wave:
----------------------
To implement auto-retrieval with Qdrant in LlamaIndex, you would need to follow a few steps. Here's a general guide based on the information provided in the knowledge sources:

  1. Define the Schema: Define the vector db schema (e.g., the metadata fields). This will be put into the LLM input prompt when it's deciding what metadata filters to infer.
  1. Instantiate the VectorIndexAutoRetriever class: This creates a retriever on top of your summarized metadata index, and takes in the defined schema as input. Here's an example of how to instantiate the VectorIndexAutoRetriever class:
Plain Text
from llama_index.core.retrievers import VectorIndexAutoRetriever

retriever = VectorIndexAutoRetriever(
    index,
    vector_store_info=vector_store_info,
    similarity_top_k=2,
    empty_query_top_k=10,  # if only metadata filters are specified, this is the limit
    verbose=True,
)

(Source: GitHub ())

  1. Define a wrapper retriever: This allows us to postprocess each node into an IndexNode, with an index id linking back source document. This will allow us to do recursive retrieval in the next section (which depends on IndexNode objects linking to downstream retrievers/query engines/other Nodes).
  1. Use the Qdrant filters: You can use Qdrant filters to refine your retrieval. Here's an example of how to use Qdrant filters:
Plain Text
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})

(Source: GitHub ())

Please note that you would need to replace the index and vector_store_info with your own Qdrant index and vector store information. Also, the filters should be adjusted according to your specific needs.

As for the sentence_window_query_engine, it seems to be a different type of query engine. If you want to switch from sentence_window_query_engine to VectorIndexAutoRetriever, you would need to instantiate the VectorIndexAutoRetriever as shown above and use it for your retrieval tasks.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
@kapa.ai ok but some of the query will not require any filters (no auto retrieval) and some of them will. How to implement a hybrid method to apply qdrant filters only when necessary ?
Also how to integrate that with a sentence window retriever?
Let me check the knowledge base for you. One moment! :books:
----------------------
To implement a hybrid method that applies Qdrant filters only when necessary and integrates with a sentence window retriever, you can create a custom retriever that checks the query and decides whether to use the Qdrant filters or not.

Here's a general idea of how you can implement this:

  1. Create a Custom Retriever: This retriever will take in the query and decide whether to use the Qdrant filters or not. If the query requires filters, it will use the VectorIndexAutoRetriever with the appropriate filters. If the query does not require filters, it will use the sentence_window_query_engine.
Plain Text
class CustomRetriever:
    def __init__(self, vector_index_auto_retriever, sentence_window_query_engine):
        self.vector_index_auto_retriever = vector_index_auto_retriever
        self.sentence_window_query_engine = sentence_window_query_engine

    def retrieve(self, query):
        if self._requires_filters(query):
            return self.vector_index_auto_retriever.retrieve(query)
        else:
            return self.sentence_window_query_engine.retrieve(query)

    def _requires_filters(self, query):
        # Implement your logic here to decide whether the query requires filters or not
        pass


Please note that you would need to implement the _requires_filters method to decide whether the query requires filters or not. This could be based on the presence of certain keywords, the structure of the query, or any other criteria that you decide.

  1. Instantiate the Custom Retriever: Once you have defined the CustomRetriever, you can instantiate it with your VectorIndexAutoRetriever and sentence_window_query_engine.
Plain Text
custom_retriever = CustomRetriever(vector_index_auto_retriever, sentence_window_query_engine)
  1. Use the Custom Retriever: Now you can use the custom_retriever to retrieve documents based on the query. The custom_retriever will decide whether to use the Qdrant filters or not based on the query.
Plain Text
response = custom_retriever.retrieve("Your query here")


Please note that this is a general idea and you would need to adjust the code according to your specific needs and the actual classes and methods you are using.
----------------------
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