run-llama / llama_index

LlamaIndex is a data framework for your LLM applications
https://docs.llamaindex.ai
MIT License
36.73k stars 5.27k forks source link

[Bug]: ImportError - cannot import name 'Vector' from 'azure.search.documents.models' #10242

Closed MattiaSanna-M closed 9 months ago

MattiaSanna-M commented 9 months ago

Bug Description

Hello! I had to update my azure-search-documents to the latest stable version, and unfortunately, the cogsearch.py has compatibility issues.

The problem is caused because in azure-search-documents v11.4.0b10 (2023-10-10) they renamed Vector as VectorizedQuery.

I needed to do two things to make it compatible with the latest version. Firstly, I substituted each Vector call within the cogsearch.py with the following:

# -> line 627
class AzureQueryResultSearchDefault(AzureQueryResultSearchBase):
    def _create_query_vector(self) -> Optional[List[Any]]:
        """Query vector store."""
        from azure.search.documents.models import VectorizedQuery

        if not self._query.query_embedding:
            raise ValueError("Query missing embedding")

        vector = VectorizedQuery(
            vector=self._query.query_embedding,
            k_nearest_neighbors=self._query.similarity_top_k,
            fields=self._field_mapping["embedding"],
        )
        vectors = [vector]
        logger.info("Vector search with supplied embedding")
        return vectors

# -> line 666
class AzureQueryResultSearchSemanticHybrid(AzureQueryResultSearchHybrid):
    def _create_query_vector(self) -> Optional[List[Any]]:
        """Query vector store."""
        from azure.search.documents.models import VectorizedQuery

        if not self._query.query_embedding:
            raise ValueError("Query missing embedding")
        # k is set to 50 to align with the number of accept document in azure semantic reranking model.
        # https://learn.microsoft.com/en-us/azure/search/semantic-search-overview
        vector = VectorizedQuery(
            vector=self._query.query_embedding,
            k_nearest_neighbors=50,
            fields=self._field_mapping["embedding"],
        )
        vectors = [vector]
        logger.info("Vector search with supplied embedding")
        return vectors

Moreover, accordingly to azure-search-documents changelog you need to modify the parameter vector within the _search_client.search into vector_queries (There are two calls within the cogsearch.py code).

I was able to then run it without problems :)

I hope it helps!

Version

0.9.35

Steps to Reproduce

Relevant Logs/Tracbacks

No response

dosubot[bot] commented 9 months ago

🤖

Thank you for sharing the details of the issue and the solution you found! It's great to see that you were able to make the necessary modifications to make it compatible with the latest version of azure-search-documents. Your contribution is valuable and will likely help others facing a similar compatibility issue. Keep up the good work!


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.

If you want to continue the conversation, start your reply with @dosu-bot.