pythonazureazure-cognitive-searchvector-search

AttributeError: 'str' object has no attribute 'get_token' in Python when using Azure Cognitive Search Vector Search


I am trying to do a Vector Search with Azure Cognitive Search in Python. This is my code:

query = "What are the advantages of an open-source ai model?"
search_client = SearchClient(AZURE_COGNITIVE_SEARCH_SERVICE_ENDPOINT, AZURE_COGNITIVE_SEARCH_INDEX_NAME, credential=AZURE_COGNITIVE_SEARCH_API_KEY)
vector_query = VectorizableTextQuery(text=query, k=3, fields="content_vector")

results = search_client.search(  
    search_text=None,  
    vector_queries= [vector_query],
    select=["title", "content_vector", "metadata"],
)
    
for result in results:  
  print(result)

But this throws me the following error:

AttributeError                            Traceback (most recent call last)
<ipython-input-32-a8dbad346de2> in <cell line: 16>()
     14 print(results)
     15 
---> 16 for result in results:
     17   print(result)

14 frames
/usr/local/lib/python3.10/dist-packages/azure/core/pipeline/policies/_authentication.py in on_request(self, request)
     97                 self._token = self._credential.get_token(*self._scopes, enable_cae=self._enable_cae)
     98             else:
---> 99                 self._token = self._credential.get_token(*self._scopes)
    100         self._update_headers(request.http_request.headers, self._token.token)
    101 

AttributeError: 'str' object has no attribute 'get_token'

When i do a simple print(results) i get

<iterator object azure.core.paging.ItemPaged at 0x7f17cf3af550>

I am using the latest Azure Cognitive Search API via:

!pip install azure-search-documents --pre --upgrade

> Requirement already satisfied: azure-search-documents in 
> /usr/local/lib/python3.10/dist-packages (11.4.0b11)

Solution

  • Thanks you Tim Roberts i have changed the following:

    I have added the line

    AZURE_COGNITIVE_SEARCH_CREDENTIAL = AzureKeyCredential(AZURE_COGNITIVE_SEARCH_API_KEY)
    

    and changed the following line

    search_client = SearchClient(AZURE_COGNITIVE_SEARCH_SERVICE_ENDPOINT, AZURE_COGNITIVE_SEARCH_INDEX_NAME, credential=AZURE_COGNITIVE_SEARCH_CREDENTIAL)