Vector Search in Azure Cognitive Search

Sannidhi Siva
5 min readJul 20, 2023

--

Photo by Shubham Dhage on Unsplash

As per Microsoft Learn

“Vector search is a new capability for indexing, storing, and retrieving vector embeddings from a search index”

Vector search allows encoding various data types like text, images, audio, and video using embedding models and conducting similarity searches across them, including multi-lingual search and hybrid search with semantic capabilities for enhanced accuracy, alongside filtered vector search with filter expressions. Additionally, it enables Cognitive Search as a vector database to serve as a long-term memory or external knowledge base for Large Language Models (LLMs) and other applications.

Embeddings

“Embeddings” refers to a technique used in natural language processing and machine learning to represent words, phrases, or documents as numerical vectors in a high-dimensional space. These vectors are designed to capture the semantic meaning and relationships between different elements, allowing for more efficient and effective information retrieval and search.

Vector Representation for relationships-(Gender Type)

Prerequisites for Vector Search

  1. Create Azure Cognitive Search

Introduction to Azure Cognitive Search — Azure Cognitive Search | Microsoft Learn

2.Add Vector Embeddings in your documents as cognitive search doesn’t generate vectors. We can use any model to create vectors and can use OpenAI-Embedding model to create vectors.

Sample Python code:

# Install the OpenAI Python library if not already installed
!pip install openai

# Import the required OpenAI library
import openai

# Set the API type, key, base URL, and version for OpenAI
openai.api_type = "azure"

# Choose the appropriate API type, e.g., 'rest' or 'davinci'
openai.api_key = "API-KEY"

.# Replace "YOUR-API-KEY" with your actual API key
openai.api_base = "https://{NAME}.openai.azure.com"

# Replace with your OpenAI resource base URL
openai.api_version = "2023-05-15"
# Set the desired API version

# Create an embedding for the given input text using the specified engine
response = openai.Embedding.create(
input="How do I use Vector Search in Azure Cognitive Search",
# Replace with your desired input text
engine="text-embedding-ada-002"
)

# Extract the embeddings from the response
embeddingText= response['data'][0]['embedding']

# Print the embeddings
print(embeddingText)

Sample C# Code

  var embeddingRespone= 
await openAICleit.GetEmbeddingsAsync("text-embedding-ada-002",
new EmbeddingsOptions(text));

Azure OpenAI embeddings depend on cosine similarity to compute similarity between documents and questions.

Embeddings for Image

For images we can use Image Retrieval-Vectorize Image to generate vectors

Image Retrieval — Vectorize Image — REST API (Azure Cognitive Services — Computer Vision) | Microsoft Learn

Image Retrieval concepts — Image Analysis 4.0 — Azure AI services | Microsoft Learn

Text/Image Embeddings for Search

Documents structure for Indexing

In General, an index in search is like a list of information that helps a search engine find what you’re looking for quickly. Think of it like the index at the back of a book, where you can look up a word and find the page number where it appears. In the same way, a search engine uses an index to quickly find the most relevant information when you type in a search query

To facilitate efficient document retrieval and search, a unique identifier should be assigned to each document using a field or metadata property.

The vector data, which consists of embeddings generated by embedding models, should be provided in separate source fields, with each field containing one embedding.

Additionally, to enable comprehensive query responses and support hybrid query scenarios that involve both full-text search and semantic ranking in a single request, other fields containing alphanumeric content should be included.

Vector Search Section index.json file

"vectorSearch": {
"algorithmConfigurations": [
{
"name": "vectorSearchConfiguration",
"kind": "hnsw",
"hnswParameters": {
"m": 2,
"efConstruction": 500,
"efSearch": 400,
"metric": "euclidean"
}
}
]
}

Fields section in vector search

"fields": [
{
"name": "content",
"type": "Edm.String",
"key": true,
"vectorSearchConfiguration": "vectorSearchConfiguration"
},

We can create Search index in two ways

  1. Azure Portal
Steps to Create Index from Azure Portal

2.With API

PUT: https://{{search-service-name}}.search.windows.net/indexes/{{index-name}}?api-version={{search-api-version}}

GET: https://{{search-service-name}}.search.windows.net/indexes/{{index-name}}?api-version={{search-api-version}}

Upload Documents with Embeddings:

https://{{search-service-name}}.search.windows.net/indexes/{{index-name}}/docs/index?api-version={{search-api-version}}

Benefits

Vector search is a ML technique that converts unstructured data like text and images into numeric representations to capture their meaning and context.
It utilizes approximate nearest neighbor algorithms to perform semantic search, leading to more relevant and faster search results compared to traditional keyword-based search.
Vector search enables searching based on the intended meaning, as it incorporates synonyms and associations within the data.
Vector search is not limited to text and images; it can be applied to any type of data where each item can be represented by a vector, making it versatile for various business applications.

Advantages of Vector Search in Retrieval-Augmented Generation (RAG)

Vector search plays a important role in combining retrieval and generation methods to improve NL tasks.

Semantic Search for Retrieval: Vector search converts unstructured data (text, images) into numeric representations (vectors) capturing meaning. Retrieval finds similar data quickly using approximate nearest neighbor algorithms based on vector representations.

Improved Context and Relevance: RAG understands synonyms and context through vector embeddings. This leads to more relevant responses, enhancing conversational experiences for users.

Combining Retrieval and Generation: RAG combines retrieval and generation methods, leveraging vector search for relevant data retrieval. Generation models add context or details to the responses as needed.

Hybrid Search and Scoring: Vector search enables hybrid search, combining retrieved results with traditional scoring for optimized, accurate responses that align with user queries and utilize retrieved information.

Example Workflow:

Links:

Vision Studio (azure.com)

Quickstart vector search — Azure Cognitive Search | Microsoft Learn

Quickstart: Create a search index in the Azure portal — Azure Cognitive Search | Microsoft Learn

Announcing a renaissance in computer vision AI with Microsoft | Azure Blog | Microsoft Azure

--

--

No responses yet