Hybrid search through python code in Azure AI search.

Smit Tiwary 5 Reputation points
2023-12-12T05:38:03.01+00:00
# Pure Vector Search
query = "how many rooms are there."
# generate_embeddings(query, model="model_nme")

search_client = SearchClient(search_service_endpoint, index_name, credential=credential)
vector_query = VectorizedQuery(text=query, k=1, fields="vector", exhaustive=True)

results = search_client.search(
    search_text=None,
    vector_queries= [vector_query],
    select=["title", "content", "category"],

)

# Display search results
for result in results:
    print(f"Title: {result['title']}")
    print(f"Score: {result['@search.score']}")
    print(f"Content: {result['content']}")
    print(f"Category: {result['category']}\n")

My Embedding are working fine. However, when I am running the above peice of code, its not showing me any results and neither showing any result. Please help me.
Azure OpenAI Service
Azure OpenAI Service
An Azure service that provides access to OpenAI’s GPT-3 models with enterprise capabilities.
4,081 questions
Azure AI services
Azure AI services
A group of Azure services, SDKs, and APIs designed to make apps more intelligent, engaging, and discoverable.
3,602 questions
{count} votes

2 answers

Sort by: Most helpful
  1. dupammi 8,615 Reputation points Microsoft External Staff
    2023-12-15T05:15:16.8733333+00:00

    Hi @Smit Tiwary ,

    I was able to successfully execute the VectorizedQuery. For the purpose of debugging the code, I had programatically generated the embeddings by setting the dimensionality to 1536 i.e. same as the dimensionality of the vector I created in the Azure portal. Please find below:

    import random
    
    # Set the dimensionality
    dimensionality = 1536
    
    # Generate a random query vector with values between -1 and 1
    query_vector = [random.uniform(-1, 1) for _ in range(dimensionality)]
    
    # Print or use the query vector as needed
    print(query_vector)
    
    

    Then I used this query_vector further in the piece of code that you were facing issue with and it is working fine for me now. Please find below:

    import json
    from azure.search.documents import SearchClient
    from azure.search.documents.models import VectorizedQuery
    from azure.core.credentials import AzureKeyCredential
    
    # Set the values of these variables to your Azure Cognitive Search service, index, and credentials
    search_service_endpoint = "****"
    index_name = "langchain-vector-demo"
    api_key = "****"
    
    
    # Create an AzureKeyCredential object with the specified API key
    credential = AzureKeyCredential(api_key)
    
    # Create a SearchClient object with the specified endpoint, index name, and credentials
    search_client = SearchClient(search_service_endpoint, index_name, credential)
    
    # Create a VectorizedQuery object with the specified query vector and field name
    vector_query = VectorizedQuery(vector=query_vector, fields="content_vector", exhaustive=True)
    
    # Perform the vector search and retrieve the search results
    results = search_client.search(
        search_text=None,
        vector_queries=[vector_query],
        select=["*"],
    )
    
    # Display search results
    for result in results:
        print(result)
    
    

    I found that the code is working as expected.

    Below are the dimensions of the vector I created in the portal, just for testing, without depending on the GIT embeddings:

    User's image

    Note : Try to add more PRINT / debug statements for knowing the dimensionality, SELECT query columns etc need to match exactly in however way defined in the Azure portal.

    1. Match the dimensionality Azure Portal Vs Python code.
    2. Match column names Azure Portal Vs Python code.

    Repro Output:
    User's image

    Hope this helps.


    If this answers your query, do click Accept Answer and Yes for was this answer helpful.

    1 person found this answer helpful.
    0 comments No comments

  2. JianHrris 0 Reputation points
    2024-01-30T17:09:07.2733333+00:00

    onoaolnosmponoaonon;k=noa;paolakpolnoao ooa


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.