Filters as a variable construct?

Description

I’ve got a RAG application and I am trying to improve retrieval accuracy.

After the user enters their query, I have an LLM question pre-processor which expands and classifies the question type. For now it breaks the question type down into either “tech” (general technology question) or “spec” (specific product question), but eventually I’d like to extract more targeting data from the pre-processor… I’d like to use a different filter for each case.

        #set filters, etc to match search_type
        my_filters=wvc.query.Filter.by_property("file_name").like("ER*")
        if search_type == "tech":
            my_filters=(wvc.query.Filter.by_property("file_name").like("EP*") |
            wvc.query.Filter.by_property("file_name").like("ES*") ),

        response = collection.query.hybrid(
            filters=my_filters,
            query=instring,
            query_properties=["page_content","title","semantic_concepts"],
            alpha=0.6, #proportion of vector search 1.0 = 100% vector
            return_metadata=wvc.query.MetadataQuery(
                score=True,
                explain_score=True,
                ),
            limit=100,
            fusion_type=HybridFusion.RELATIVE_SCORE,
            rerank=Rerank(
                prop="page_content",
                query=instring
            ),
        )

The above construct fails with:
Argument of type "tuple[_Filters] | _Filters" cannot be assigned to parameter "filters" of type "_Filters | None" in function "hybrid"

Yes, I could absolutely set up different queries for each search type, but that seems like it will get long and messy as I add refinements from the pre-processor.

Is there a programatic way to define filters outside of the collection.query() function call?

Server Setup Information

Server Setup Information

Weaviate Server Version: 1.24.1
Deployment Method: docker
Multi Node? Number of Running Nodes: 1
Client Language and Version: python 4.6.1

Any additional Information

above

hi @Gene_Mc !!

the filter argument will only receive a _Filters parameters or None.

your my_filters is a tuple, so that’s the reason of your issue.

That’s because you have a , and the end of your my_filters variable definition:

my_filters=(wvc.query.Filter.by_property("file_name").like("EP*") |
            wvc.query.Filter.by_property("file_name").like("ES*") )--->,<-------

If you remove that ,, it should work as expected.

Let me know if this helps :slight_smile:

hi @DudaNogueira,

Thank you for catching my TYPO! :slight_smile:

Apologies for raising that…

Yes, it works properly now…sigh…

–gene

1 Like

hi @Gene_Mc !!

Glad we were able to get it working!

Don’t worry! We are here to help you succeed! :wink:

Thanks for using Weaviate!