Here is the reference document I follow with: https://esteininger.medium.com/building-a-vector-search-engine-using-hnsw-and-cosine-similarity-753fb5268839 & https://github.com/Azure/cognitive-search-vector-pr/blob/main/demo-python/code/azure-search-vector-python-sample.ipynb

from azure.search.documents.models import Vector
from azure.search.documents.indexes.models import (  
    SearchIndex,  
    SearchField,  
    SearchFieldDataType,  
    SimpleField,  
    SearchableField,  
    SearchIndex,  
    SemanticConfiguration,  
    PrioritizedFields,  
    SemanticField,  
    SearchField,  
    SemanticSettings,  
    VectorSearch, 
    HnswVectorSearchAlgorithmConfiguration 
)

enter image description here

At first, I faced import error which is about import Vector. I saw its solution on stackoverflow by updating azure.search.documents to the version ==11.4.0b6; While HnswVectorSearchAlgorithmConfiguration would be alwasy error no matter which version I used. I've tried azure.search.documents 11.4.0b6 and 11.4.0b4

If the import error remain unsolved, the folling part would be error too.

vector_search = VectorSearch(

    algorithm_configurations=[

        HnswVectorSearchAlgorithmConfiguration(

            name="my-vector-config",

            kind="hnsw",

            parameters={

                "m": 4,

                "efConstruction": 400,
                "efSearch": 500,
                "metric": "cosine"
            }
        )
    ]
)

I also tried to walkaround by import hnswlib, but it didn't work... enter image description here

If someone has conquer this issue, please let me know. Thank you!

4

There are 4 answers

2
Farzzy - MSFT On BEST ANSWER

Using Azure Cognitive Search’s Vector feature you don’t have to install hnswlib separately.

Can you ensure you’re using the latest azure-search-documents pip package?

Try pip install azure-search-documents --pre --upgrade.

The latest Python SDK pre-release version that includes Vector search capabilities can be found here: https://pypi.org/project/azure-search-documents/11.4.0b8/

0
Craig831 On

Neither of the suggested answers worked for me but I ended up finding another solution in Azure Samples. Hope this helps...

repo: https://github.com/Azure-Samples/azure-search-comparison-tool

I imported HnswParameters and VectorSearchAlgorithmConfiguration from azure.search.documents.indexes.models instead of HnswVectorSearchAlgorithmConfiguration:

from azure.search.documents.indexes.models import (
    HnswParameters,
    PrioritizedFields,
    SearchableField,
    SearchField,
    SearchFieldDataType,
    SearchIndex,
    SemanticConfiguration,
    SemanticField,
    SemanticSettings,
    SimpleField,
    VectorSearch,
    VectorSearchAlgorithmConfiguration,
)

Then I changed my vector search configuration to:

vector_search = VectorSearch(
    algorithm_configurations=[
        VectorSearchAlgorithmConfiguration(
            name="natp-vector-config",
            kind="hnsw",
            hnsw_parameters=HnswParameters(m=4, ef_construction=400, ef_search=500, metric="cosine")
        )
    ]
)
1
Tupis On

I recommend installing 11.4.0b8 pip install azure-search-documents==11.4.0b8.

1
Noam A On

Continuing Farzzy's helpful answer:

I had the same issue, For me just running pip install azure-search-documents --pre --upgrade didn't fix it, so I also ran the following commands:

pip install azure-search --pre --upgrade
pip install azure-core --pre --upgrade

And one of them fixed that.