Azure-Samples / azure-search-openai-demo

A sample app for the Retrieval-Augmented Generation pattern running in Azure, using Azure AI Search for retrieval and Azure OpenAI large language models to power ChatGPT-style and Q&A experiences.
https://azure.microsoft.com/products/search
MIT License
5.85k stars 3.99k forks source link

hybrid search with two vector fields #1461

Open agnosus opened 5 months ago

agnosus commented 5 months ago

Hi I would like to add additional vector fields to my documents for hybrid search. Is updating the index in azure AI search service enough, or do I need to change something in the code as well? Thanks in advance. Regards agnosus

pamelafox commented 5 months ago

Yes, you'd need to change some code. Currently the approach files like chatreadretrieveread.py call approach.py:compute_text_embedding to return a VectorizedQuery for the embedding field, and they pass that to the approach.py:search method. You would want to pass in a VectorizedQuery for each of the fields, though I think you could reuse the same vector for each, assuming you're searching with user query vector.

agnosus commented 5 months ago

Thanks a lot Pamela. 😊 This did the trick: VectorizedQuery(vector=query_vector, k_nearest_neighbors=50, fields="embedding1, embedding2")

I have a related question on search algorithm: I am trying to use exhaustive KNN search instead of the default HNSW. In the searchmanager.py, I modifiyed the code to :

vector_search=VectorSearch( algorithms=[ ExhaustiveKnnAlgorithmConfiguration( name="vector-config-1711876367278", parameters=ExhaustiveKnnParameters(metric="cosine"), ) ], profiles=[ VectorSearchProfile( name="fullknn", algorithm_configuration_name="vector-config-1711876367278", vectorizer=( f"{self.search_info.index_name}-vectorizer" if self.use_int_vectorization else None ), ), ], vectorizers=vectorizers, )

I also manually added 'fullknn' to my index but CAN NOT delete the existing HNSW profile. So I ended up with two vector search profiles. Now I am not sure which profile is being used for the vector search??

Appreciate your help. Thanks.

pamelafox commented 5 months ago

If you're trying to use exhaustive KNN, I don't think you need to add a new profile - you can just send exhaustive=True in VectorizedQuery constructor.

As to what profile is being used, if you don't specify exhaustive, it should be whatever profile is associated with the field (embedding1, embedding2). More details in https://learn.microsoft.com/en-us/azure/search/vector-search-how-to-create-index?tabs=config-2023-11-01%2Crest-2023-11-01%2Cpush%2Cportal-check-index