Closed courtneyjean closed 4 months ago
I had hoped that this update has fixed this issue, but it seems it hasn't: https://github.com/run-llama/llama_index/pull/13215
The error you're encountering when applying more than one metadata filter using FilterCondition.OR
in AzureAISearchVectorStore
is likely due to the way the filter conditions are being constructed and interpreted by Azure Cognitive Search. The HttpResponseError
suggests a syntax issue in the OData filter string, which is crucial for Azure Cognitive Search queries.
To address this, you'll need to ensure that your filter string correctly implements the OR
condition. Azure Cognitive Search expects the OData filter syntax to be precise. For combining filters with an OR
condition, you might need to manually construct a filter string that explicitly specifies this condition. Here's a simplified approach:
def construct_or_filter(field_name, values):
return f"({field_name} eq '{values[0]}' or {field_name} eq '{values[1]}')"
filter_string = construct_or_filter('country', ['United Kingdom', 'Ireland'])
This filter string should then be integrated into your query mechanism, ensuring that the fields you're filtering by are marked as filterable in your Azure AI Search index schema.
The pull request you mentioned does not seem to address the handling of FilterCondition.OR
directly. For your specific issue, focusing on the correct construction and application of the OData filter string is key. Ensure that your index schema in Azure AI Search is set up correctly to support filtering on the necessary fields.
To continue the conversation, mention @dosu.
How can I implement this filter_string into the retrieval?
This code fails:
def construct_or_filter(field_name, values): return f"({field_name} eq '{values[0]}' or {field_name} eq '{values[1]}')"
print("IRELAND AND UK FILTERS") filter_string = construct_or_filter('country', ['United Kingdom', 'Ireland']) print("RETRIEVE IRELAND & UK DOCs ONLY") azs_two_filters_retriever = azs_index.as_retriever(filters=filter_string) print(azs_two_filters_retriever.retrieve('What locations are celebrated for being birthplaces of famous writers?')) print("--------------------------------------------------------------")
AttributeError Traceback (most recent call last)
File
File /local_disk0/.ephemeral_nfs/cluster_libraries/python/lib/python3.9/site-packages/llama_index/core/instrumentation/dispatcher.py:274, in Dispatcher.span.
File /local_disk0/.ephemeral_nfs/cluster_libraries/python/lib/python3.9/site-packages/llama_index/core/base/base_retriever.py:244, in BaseRetriever.retrieve(self, str_or_query_bundle) 239 with self.callback_manager.as_trace("query"): 240 with self.callback_manager.event( 241 CBEventType.RETRIEVE, 242 payload={EventPayload.QUERY_STR: query_bundle.query_str}, 243 ) as retrieve_event: --> 244 nodes = self._retrieve(query_bundle) 245 nodes = self._handle_recursive_retrieval(query_bundle, nodes) 246 retrieve_event.on_end( 247 payload={EventPayload.NODES: nodes}, 248 )
File /local_disk0/.ephemeral_nfs/cluster_libraries/python/lib/python3.9/site-packages/llama_index/core/instrumentation/dispatcher.py:274, in Dispatcher.span.
File /local_disk0/.ephemeral_nfs/cluster_libraries/python/lib/python3.9/site-packages/llama_index/core/indices/vector_store/retrievers/retriever.py:101, in VectorIndexRetriever._retrieve(self, query_bundle) 95 if query_bundle.embedding is None and len(query_bundle.embedding_strs) > 0: 96 query_bundle.embedding = ( 97 self._embed_model.get_agg_embedding_from_queries( 98 query_bundle.embedding_strs 99 ) 100 ) --> 101 return self._get_nodes_with_embeddings(query_bundle)
File /local_disk0/.ephemeral_nfs/cluster_libraries/python/lib/python3.9/site-packages/llama_index/core/indices/vector_store/retrievers/retriever.py:177, in VectorIndexRetriever._get_nodes_with_embeddings(self, query_bundle_with_embeddings) 173 def _get_nodes_with_embeddings( 174 self, query_bundle_with_embeddings: QueryBundle 175 ) -> List[NodeWithScore]: 176 query = self._build_vector_store_query(query_bundle_with_embeddings) --> 177 query_result = self._vector_store.query(query, **self._kwargs) 178 return self._build_node_list_from_query_result(query_result)
File /local_disk0/.ephemeral_nfs/cluster_libraries/python/lib/python3.9/site-packages/llama_index/vector_stores/azureaisearch/base.py:616, in AzureAISearchVectorStore.query(self, query, **kwargs) 614 odata_filter = None 615 if query.filters is not None: --> 616 odata_filter = self._create_odata_filter(query.filters) 617 azure_query_result_search: AzureQueryResultSearchBase = ( 618 AzureQueryResultSearchDefault( 619 query, self._field_mapping, odata_filter, self._search_client 620 ) 621 ) 622 if query.mode == VectorStoreQueryMode.SPARSE:
File /local_disk0/.ephemeral_nfs/cluster_libraries/python/lib/python3.9/site-packages/llama_index/vector_stores/azureaisearch/base.py:580, in AzureAISearchVectorStore._create_odata_filter(self, metadata_filters)
578 """Generate an OData filter string using supplied metadata filters."""
579 odata_filter: List[str] = []
--> 580 for f in metadata_filters.legacy_filters():
581 if not isinstance(f, ExactMatchFilter):
582 raise NotImplementedError(
583 "Only ExactMatchFilter
filters are supported"
584 )
AttributeError: 'str' object has no attribute 'legacy_filters'
I had hoped that this update has fixed this issue, but it seems it hasn't: https://github.com/run-llama/llama_index/pull/13215
Hi @courtneyjean, I think the problem was just caused by that PR. You can try the latest version v0.10.37, which I guess has fixed the problem (by #13435).
I had hoped that this update has fixed this issue, but it seems it hasn't: #13215
Hi @courtneyjean, I think the problem was just caused by that PR. You can try the latest version v0.10.37, which I guess has fixed the problem (by #13435).
Hi @RussellLuo, I'm using v0.10.37 already :(
I cannot run your code as I have no available azure credential on hand. To see what happened, maybe you could add some debugging logs using print or set a breakpoint on this line:
@courtneyjean v0.10.37 is the version of the llama-index/llama-index-core package. But you'll want to make sure you have the latest azure search version
pip install -U llama-index-vector-stores-azureaisearch
@courtneyjean v0.10.37 is the version of the llama-index/llama-index-core package. But you'll want to make sure you have the latest azure search version
pip install -U llama-index-vector-stores-azureaisearch
Hi @logan-markewich :
Here is from my pip list. I believe I have the latest version:
llama-index 0.10.37 llama-index-agent-openai 0.2.5 llama-index-cli 0.1.12 llama-index-core 0.10.37 llama-index-embeddings-azure-openai 0.1.9 llama-index-embeddings-openai 0.1.9 llama-index-experimental 0.1.3 llama-index-indices-managed-llama-cloud 0.1.6 llama-index-legacy 0.9.48 llama-index-llms-azure-openai 0.1.8 llama-index-llms-openai 0.1.19 llama-index-multi-modal-llms-openai 0.1.6 llama-index-program-openai 0.1.6 llama-index-question-gen-openai 0.1.3 llama-index-readers-file 0.1.22 llama-index-readers-llama-parse 0.1.4 llama-index-vector-stores-azureaisearch 0.1.5 llama-index-vector-stores-postgres 0.1.7 llama-parse 0.4.3 llamaindex-py-client 0.1.19
llama-index-vector-stores-azureaisearch 0.1.6 is the correct version, but seems like this version has not been published on pypi.
Good catch @RussellLuo the automatic publishing must have failed. Just manually published.
@courtneyjean can you try updating one more time?
Thanks both. I've tried it, and this is an improvement as it no longer throws an error. But unfortunately I'm still not getting the behaviour I expected.
From the code above, here is the new output. When I apply 'NO METADATA FILTERS', the retriever returns two documents. A single filter on Ireland works well, but applying two filters:
filters=[MetadataFilter(key='country', value='United Kingdom', operator=<FilterOperator.EQ: '=='>), MetadataFilter(key='country', value='Ireland', operator=<FilterOperator.EQ: '=='>)] condition=<FilterCondition.OR: 'or'>
Returns only documents related to the second filter.
I also tried similarity_top_k=2 to try to achieve the desired result, but it had no impact.
Here is the output I am currently getting:
NO METADATA FILTERS [NodeWithScore(node=TextNode(id_='899d3cd4-0619-4b35-9644-197aa208d1dd', embedding=None, metadata={'country': 'Ireland'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={<NodeRelationship.SOURCE: '1'>: RelatedNodeInfo(node_id='1b11291f-e1e8-4c34-92f7-51c798a86649', node_type=<ObjectType.DOCUMENT: '4'>, metadata={'country': 'Ireland'}, hash='2156acad3ceff3ff92570304d6c7aed3d123661249e1eebde150da45a391af56')}, text='The Republic of Ireland occupies most of the island of Ireland, off the coast of England and Wales. Its capital, Dublin, is the birthplace of writers like Oscar Wilde, and home of Guinness beer.', start_char_idx=0, end_char_idx=194, text_template='{metadata_str}\n\n{content}', metadata_template='{key}: {value}', metadataseperator='\n'), score=0.7949773663245251), NodeWithScore(node=TextNode(id='0229d71c-4e04-4243-b938-fa7ee8727be5', embedding=None, metadata={'country': 'United Kingdom'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={<NodeRelationship.SOURCE: '1'>: RelatedNodeInfo(node_id='6740d811-49b7-49e1-9a8f-b39ebc42f455', node_type=<ObjectType.DOCUMENT: '4'>, metadata={'country': 'United Kingdom'}, hash='ffa0a601c09be1c86666805fac5f7bdd024235d2784bcd44e711b164e188cf8a')}, text='The United Kingdom, made up of England, Scotland, Wales and Northern Ireland, is an island nation in northwestern Europe. England â birthplace of Shakespeare and The Beatles â is home to the capital, London, a globally influential centre of finance and culture.', start_char_idx=0, end_char_idx=261, text_template='{metadata_str}\n\n{content}', metadata_template='{key}: {value}', metadata_seperator='\n'), score=0.7814661419624729)]
RETRIEVE IRELAND DOCs ONLY [NodeWithScore(node=TextNode(id_='f53c324f-4ea2-41c2-92ec-7d85832947b6', embedding=None, metadata={'country': 'Ireland'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={<NodeRelationship.SOURCE: '1'>: RelatedNodeInfo(node_id='e97d3939-da45-4f9c-a826-462d79128dd5', node_type=<ObjectType.DOCUMENT: '4'>, metadata={'country': 'Ireland'}, hash='2156acad3ceff3ff92570304d6c7aed3d123661249e1eebde150da45a391af56')}, text='The Republic of Ireland occupies most of the island of Ireland, off the coast of England and Wales. Its capital, Dublin, is the birthplace of writers like Oscar Wilde, and home of Guinness beer.', start_char_idx=0, end_char_idx=194, text_template='{metadata_str}\n\n{content}', metadata_template='{key}: {value}', metadata_seperator='\n'), score=0.8298598)]
IRELAND AND UK FILTERS filters=[MetadataFilter(key='country', value='United Kingdom', operator=<FilterOperator.EQ: '=='>), MetadataFilter(key='country', value='Ireland', operator=<FilterOperator.EQ: '=='>)] condition=<FilterCondition.OR: 'or'> RETRIEVE IRELAND & UK DOCs ONLY [NodeWithScore(node=TextNode(id_='f53c324f-4ea2-41c2-92ec-7d85832947b6', embedding=None, metadata={'country': 'Ireland'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={<NodeRelationship.SOURCE: '1'>: RelatedNodeInfo(node_id='e97d3939-da45-4f9c-a826-462d79128dd5', node_type=<ObjectType.DOCUMENT: '4'>, metadata={'country': 'Ireland'}, hash='2156acad3ceff3ff92570304d6c7aed3d123661249e1eebde150da45a391af56')}, text='The Republic of Ireland occupies most of the island of Ireland, off the coast of England and Wales. Its capital, Dublin, is the birthplace of writers like Oscar Wilde, and home of Guinness beer.', start_char_idx=0, end_char_idx=194, text_template='{metadata_str}\n\n{content}', metadata_template='{key}: {value}', metadata_seperator='\n'), score=0.8298598)]
Here is some code for the same process applied to a llama_index vector store, and the resulting output. In this code the metadata filter uses FilterCondition.OR and returns two documents (metadata tags country='United Kingdom' OR country='Ireland'.
This code demonstrates the anticipated behaviour and output of AzureAISearchVectorStore code above, and demonstrates that there remains an issue with the application of the metadata filter in this code.
from llama_index.core import Document
documents = [ Document( text="The United Kingdom, made up of England, Scotland, Wales and Northern Ireland, is an island nation in northwestern Europe. England â birthplace of Shakespeare and The Beatles â is home to the capital, London, a globally influential centre of finance and culture.", metadata={"country" : "United Kingdom"} ), Document( text="The Republic of Ireland occupies most of the island of Ireland, off the coast of England and Wales. Its capital, Dublin, is the birthplace of writers like Oscar Wilde, and home of Guinness beer.", metadata={"country" : "Ireland"} ), Document( text="Japan is an island country in East Asia. It is in the northwest Pacific Ocean and is bordered on the west by the Sea of Japan, extending from the Sea of Okhotsk in the north toward the East China Sea, Philippine Sea, and Taiwan in the south.", metadata={"country" : "Japan"} ) ]
from llama_index.core import VectorStoreIndex
index = VectorStoreIndex.from_documents(documents) index.ref_doc_info
basic_retriver = index.as_retriever() print("--------------------------------------------------------------") response = basic_retriver.retrieve('What locations are celebrated for being birthplaces of famous writers?') print("NO METADATA FILTERS") print(response) print("--------------------------------------------------------------")
UK_filter = MetadataFilter(key='country', operator=FilterOperator.EQ, value='United Kingdom') Ireland_filter = MetadataFilter(key='country', operator=FilterOperator.EQ, value='Ireland')
retriever_ireland = index.as_retriever(filters=MetadataFilters(filters=[Ireland_filter])) print("RETRIEVE IRELAND DOCs ONLY") print(retriever_ireland.retrieve('What locations are celebrated for being birthplaces of famous writers?')) print("--------------------------------------------------------------")
filter_names = [UK_filter, Ireland_filter] filters = MetadataFilters(filters=filter_names, condition=FilterCondition.OR) print("IRELAND AND UK FILTERS") print(filters) print("RETRIEVE IRELAND & UK DOCs ONLY") two_filters_retriever = index.as_retriever(filters=filters) print(two_filters_retriever.retrieve('What locations are celebrated for being birthplaces of famous writers?')) print("--------------------------------------------------------------")
Output is as expected: NO METADATA FILTERS [NodeWithScore(node=TextNode(id_='899d3cd4-0619-4b35-9644-197aa208d1dd', embedding=None, metadata={'country': 'Ireland'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={<NodeRelationship.SOURCE: '1'>: RelatedNodeInfo(node_id='1b11291f-e1e8-4c34-92f7-51c798a86649', node_type=<ObjectType.DOCUMENT: '4'>, metadata={'country': 'Ireland'}, hash='2156acad3ceff3ff92570304d6c7aed3d123661249e1eebde150da45a391af56')}, text='The Republic of Ireland occupies most of the island of Ireland, off the coast of England and Wales. Its capital, Dublin, is the birthplace of writers like Oscar Wilde, and home of Guinness beer.', start_char_idx=0, end_char_idx=194, text_template='{metadata_str}\n\n{content}', metadata_template='{key}: {value}', metadataseperator='\n'), score=0.7949773663245251), NodeWithScore(node=TextNode(id='0229d71c-4e04-4243-b938-fa7ee8727be5', embedding=None, metadata={'country': 'United Kingdom'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={<NodeRelationship.SOURCE: '1'>: RelatedNodeInfo(node_id='6740d811-49b7-49e1-9a8f-b39ebc42f455', node_type=<ObjectType.DOCUMENT: '4'>, metadata={'country': 'United Kingdom'}, hash='ffa0a601c09be1c86666805fac5f7bdd024235d2784bcd44e711b164e188cf8a')}, text='The United Kingdom, made up of England, Scotland, Wales and Northern Ireland, is an island nation in northwestern Europe. England â birthplace of Shakespeare and The Beatles â is home to the capital, London, a globally influential centre of finance and culture.', start_char_idx=0, end_char_idx=261, text_template='{metadata_str}\n\n{content}', metadata_template='{key}: {value}', metadata_seperator='\n'), score=0.7814661419624729)]
RETRIEVE IRELAND DOCs ONLY [NodeWithScore(node=TextNode(id_='899d3cd4-0619-4b35-9644-197aa208d1dd', embedding=None, metadata={'country': 'Ireland'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={<NodeRelationship.SOURCE: '1'>: RelatedNodeInfo(node_id='1b11291f-e1e8-4c34-92f7-51c798a86649', node_type=<ObjectType.DOCUMENT: '4'>, metadata={'country': 'Ireland'}, hash='2156acad3ceff3ff92570304d6c7aed3d123661249e1eebde150da45a391af56')}, text='The Republic of Ireland occupies most of the island of Ireland, off the coast of England and Wales. Its capital, Dublin, is the birthplace of writers like Oscar Wilde, and home of Guinness beer.', start_char_idx=0, end_char_idx=194, text_template='{metadata_str}\n\n{content}', metadata_template='{key}: {value}', metadata_seperator='\n'), score=0.7949773663245251)]
IRELAND AND UK FILTERS filters=[MetadataFilter(key='country', value='United Kingdom', operator=<FilterOperator.EQ: '=='>), MetadataFilter(key='country', value='Ireland', operator=<FilterOperator.EQ: '=='>)] condition=<FilterCondition.OR: 'or'> RETRIEVE IRELAND & UK DOCs ONLY [NodeWithScore(node=TextNode(id_='899d3cd4-0619-4b35-9644-197aa208d1dd', embedding=None, metadata={'country': 'Ireland'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={<NodeRelationship.SOURCE: '1'>: RelatedNodeInfo(node_id='1b11291f-e1e8-4c34-92f7-51c798a86649', node_type=<ObjectType.DOCUMENT: '4'>, metadata={'country': 'Ireland'}, hash='2156acad3ceff3ff92570304d6c7aed3d123661249e1eebde150da45a391af56')}, text='The Republic of Ireland occupies most of the island of Ireland, off the coast of England and Wales. Its capital, Dublin, is the birthplace of writers like Oscar Wilde, and home of Guinness beer.', start_char_idx=0, end_char_idx=194, text_template='{metadata_str}\n\n{content}', metadata_template='{key}: {value}', metadataseperator='\n'), score=0.7949773663245251), NodeWithScore(node=TextNode(id='0229d71c-4e04-4243-b938-fa7ee8727be5', embedding=None, metadata={'country': 'United Kingdom'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={<NodeRelationship.SOURCE: '1'>: RelatedNodeInfo(node_id='6740d811-49b7-49e1-9a8f-b39ebc42f455', node_type=<ObjectType.DOCUMENT: '4'>, metadata={'country': 'United Kingdom'}, hash='ffa0a601c09be1c86666805fac5f7bdd024235d2784bcd44e711b164e188cf8a')}, text='The United Kingdom, made up of England, Scotland, Wales and Northern Ireland, is an island nation in northwestern Europe. England â birthplace of Shakespeare and The Beatles â is home to the capital, London, a globally influential centre of finance and culture.', start_char_idx=0, end_char_idx=261, text_template='{metadata_str}\n\n{content}', metadata_template='{key}: {value}', metadata_seperator='\n'), score=0.7814661419624729)]
Hi All, This error I've shown above occurred when the index was created and then overridden a few times during testing. Once the index was re-created from scratch, the error no longer occurred. Thanks for your help on this, and I'm sorry the solution wasn't more satisfying.
Bug Description
I've created a set of documents in an AzureAISearchVectorStore, with a 'country' metadata key. I'm trying to create a filter on documents where 'country' equals 'United Kingdom' OR 'Ireland', but it's throwing an error.
Version
0.10.37
Steps to Reproduce
import os import tiktoken import llama_index from llama_index.core import PromptHelper from llama_index.llms.azure_openai import AzureOpenAI from llama_index.embeddings.azure_openai import AzureOpenAIEmbedding from llama_index.core.node_parser import SimpleNodeParser from llama_index.core import VectorStoreIndex, SimpleDirectoryReader from llama_index.core import set_global_service_context from llama_index.core import StorageContext,load_index_from_storage from llama_index.core import Settings from llama_index.vector_stores.azureaisearch import AzureAISearchVectorStore from llama_index.vector_stores.azureaisearch import ( IndexManagement, MetadataIndexFieldType, ) from azure.core.credentials import AzureKeyCredential from azure.search.documents import SearchClient from azure.search.documents.indexes import SearchIndexClient from llama_index.core.retrievers import VectorIndexRetriever from llama_index.core.retrievers import VectorIndexAutoRetriever from llama_index.core.vector_stores.types import MetadataInfo, VectorStoreInfo from llama_index.core.query_engine import RetrieverQueryEngine from llama_index.core import get_response_synthesizer
import pandas as pd
from llama_index.core.vector_stores import ( MetadataFilter, MetadataFilters, FilterOperator, FilterCondition )
azure_endpoint = "xx" api_version = "2024-02-15-preview" api_key="xxxx"
search_service_api_key = "xxxx" search_service_endpoint = "xx" search_service_api_version = "2023-11-01" credential = AzureKeyCredential(search_service_api_key)
model = "gpt-4" deployment_name = "GPT4-Turbo" embed_model = "text-embedding-ada-002" embed_deployment_name = "ada002embedding" temperature = 0 chunk_size = 1024 chunk_overlap = 20 maxWorkers = 5 sleepTimeBeforeRetry = 30
Settings.llm = AzureOpenAI( model=model, deployment_name=deployment_name, api_key=api_key, azure_endpoint=azure_endpoint, api_version=api_version, temperature = temperature )
Settings.embed_model = AzureOpenAIEmbedding( model=embed_model, deployment_name=embed_deployment_name, api_key=api_key, azure_endpoint=azure_endpoint, api_version=api_version)
Set up some example documents with some metadata
from llama_index.core import Document
documents = [ Document( text="The United Kingdom, made up of England, Scotland, Wales and Northern Ireland, is an island nation in northwestern Europe. England â birthplace of Shakespeare and The Beatles â is home to the capital, London, a globally influential centre of finance and culture.", metadata={"country" : "United Kingdom"} ), Document( text="The Republic of Ireland occupies most of the island of Ireland, off the coast of England and Wales. Its capital, Dublin, is the birthplace of writers like Oscar Wilde, and home of Guinness beer.", metadata={"country" : "Ireland"} ), Document( text="Japan is an island country in East Asia. It is in the northwest Pacific Ocean and is bordered on the west by the Sea of Japan, extending from the Sea of Okhotsk in the north toward the East China Sea, Philippine Sea, and Taiwan in the south.", metadata={"country" : "Japan"} ) ]
Create an AzureAISearch Vector Index
vector_index_name = 'testci3'
index_client = SearchIndexClient( endpoint=search_service_endpoint, index_name=vector_index_name, credential=credential)
metadata_fields = {'country' : 'country'}
AzureAISearch_vector_store = AzureAISearchVectorStore( search_or_index_client=index_client, filterable_metadata_field_keys= metadata_fields, index_name=vector_index_name, index_management=IndexManagement.CREATE_IF_NOT_EXISTS, id_field_key="id", chunk_field_key="chunk", embedding_field_key="embedding", embedding_dimensionality=1536, metadata_string_field_key="metadata", doc_id_field_key="doc_id", language_analyzer="en.lucene", vector_algorithm_type="exhaustiveKnn" )
storage_context = StorageContext.from_defaults(vector_store=AzureAISearch_vector_store) azs_index = VectorStoreIndex.from_documents(documents, storage_context=storage_context )
Demonstration of error when you apply more than one filter
azs_retriver = azs_index.as_retriever() print("--------------------------------------------------------------") response = basic_retriver.retrieve('What locations are celebrated for being birthplaces of famous writers?') print("NO METADATA FILTERS") print(response) print("--------------------------------------------------------------")
Create some metadata filters
UK_filter = MetadataFilter(key='country', operator=FilterOperator.EQ, value='United Kingdom') Ireland_filter = MetadataFilter(key='country', operator=FilterOperator.EQ, value='Ireland')
Ask this question and filter just for Ireland
azs_retriever_ireland = azs_index.as_retriever(filters=MetadataFilters(filters=[Ireland_filter])) print("RETRIEVE IRELAND DOCs ONLY") print(azs_retriever_ireland.retrieve('What locations are celebrated for being birthplaces of famous writers?')) print("--------------------------------------------------------------")
Ask this question and filter the UK and Ireland
filter_names = [UK_filter, Ireland_filter] filters = MetadataFilters(filters=filter_names, condition=FilterCondition.OR) print("IRELAND AND UK FILTERS") print(filters) print("RETRIEVE IRELAND & UK DOCs ONLY") azs_two_filters_retriever = azs_index.as_retriever(filters=filters) print(azs_two_filters_retriever.retrieve('What locations are celebrated for being birthplaces of famous writers?')) print("--------------------------------------------------------------")
Relevant Logs/Tracbacks