Giter VIP home page Giter VIP logo

Comments (12)

dosubot avatar dosubot commented on June 8, 2024 2

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.

Details

Sources


About Dosu
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

To continue the conversation, mention @dosu.

from llama_index.

courtneyjean avatar courtneyjean commented on June 8, 2024 1

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]}')"

Ask this question and filter the UK and Ireland

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("--------------------------------------------------------------")

Traceback:
AttributeError: 'str' object has no attribute 'legacy_filters'

AttributeError Traceback (most recent call last)
File :10
8 print("RETRIEVE IRELAND & UK DOCs ONLY")
9 azs_two_filters_retriever = azs_index.as_retriever(filters=filter_string)
---> 10 print(azs_two_filters_retriever.retrieve('What locations are celebrated for being birthplaces of famous writers?'))
11 print("--------------------------------------------------------------")

File /local_disk0/.ephemeral_nfs/cluster_libraries/python/lib/python3.9/site-packages/llama_index/core/instrumentation/dispatcher.py:274, in Dispatcher.span..wrapper(func, instance, args, kwargs)
270 self.span_enter(
271 id_=id_, bound_args=bound_args, instance=instance, parent_id=parent_id
272 )
273 try:
--> 274 result = func(*args, **kwargs)
275 except BaseException as e:
276 self.event(SpanDropEvent(span_id=id_, err_str=str(e)))

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..wrapper(func, instance, args, kwargs)
270 self.span_enter(
271 id_=id_, bound_args=bound_args, instance=instance, parent_id=parent_id
272 )
273 try:
--> 274 result = func(*args, **kwargs)
275 except BaseException as e:
276 self.event(SpanDropEvent(span_id=id_, err_str=str(e)))

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'

from llama_index.

logan-markewich avatar logan-markewich commented on June 8, 2024 1

@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

from llama_index.

courtneyjean avatar courtneyjean commented on June 8, 2024

I had hoped that this update has fixed this issue, but it seems it hasn't: #13215

from llama_index.

RussellLuo avatar RussellLuo commented on June 8, 2024

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).

from llama_index.

courtneyjean avatar courtneyjean commented on June 8, 2024

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 :(

from llama_index.

RussellLuo avatar RussellLuo commented on June 8, 2024

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:

from llama_index.

courtneyjean avatar courtneyjean commented on June 8, 2024

@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

from llama_index.

RussellLuo avatar RussellLuo commented on June 8, 2024

llama-index-vector-stores-azureaisearch 0.1.6 is the correct version, but seems like this version has not been published on pypi.

from llama_index.

logan-markewich avatar logan-markewich commented on June 8, 2024

Good catch @RussellLuo the automatic publishing must have failed. Just manually published.

@courtneyjean can you try updating one more time?

from llama_index.

courtneyjean avatar courtneyjean commented on June 8, 2024

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}', metadata_seperator='\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)]

from llama_index.

courtneyjean avatar courtneyjean commented on June 8, 2024

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.

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"}
)
]

Setp up a normal vector index

from llama_index.core import VectorStoreIndex

build index

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("--------------------------------------------------------------")

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

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("--------------------------------------------------------------")

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")
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}', metadata_seperator='\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}', metadata_seperator='\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)]

from llama_index.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.