Harnessing the Power of Azure Cosmos DB as a Vector Database
In the realm of artificial intelligence and machine learning, vector databases have emerged as a vital component for managing and querying high-dimensional data efficiently. Azure Cosmos DB for NoSQL, known for its globally distributed, multi-model database capabilities, has now positioned itself as a formidable vector database (VectorDB) through its support for vector embeddings and similarity searches.
This article explores how Azure Cosmos DB can be harnessed as a powerful VectorDB, enabling scalable and efficient storage and querying of vector data. We’ll delve into its capabilities, use cases, and the steps to set up a robust vector-based retrieval-augmented generation (RAG) application.
Understanding Vector Databases
Vector databases are designed to handle data represented as high-dimensional vectors. These vectors are typically embeddings derived from various sources such as images, text, or any numerical data that can be converted into vector space. VectorDBs excel in performing similarity searches using these embeddings, a critical function in AI applications such as recommendation systems, search engines, and natural language processing.
Why Choose Azure Cosmos DB as a VectorDB?
Azure Cosmos DB offers several compelling features that make it an ideal choice as a VectorDB:
Steps to Leverage Azure Cosmos DB as a VectorDB
1. Provision Azure Cosmos DB for NoSQL
Start by creating an Azure Cosmos DB account with NoSQL capabilities. Opt for the "Serverless" Capacity Mode for cost efficiency during initial development phases. Follow the QuickStart guide to set up your Cosmos DB account.
2. Set Up Your Environment
Ensure you have the necessary Python libraries to interact with Cosmos DB and OpenAI:
pip install numpy openai python-dotenv azure-core azure-cosmos pandas
These libraries will facilitate data operations and AI model integrations.
3. Load and Prepare Data
Prepare your data by generating vector embeddings. For this example, we use text data about Azure services. Use an embedding model from Azure OpenAI to convert text into vectors.
Here’s a function to generate embeddings:
def generate_embeddings(text, AOAI_client, EMBEDDING_MODEL_DEPLOYMENT_NAME):
response = AOAI_client.embeddings.create(input=text, model=EMBEDDING_MODEL_DEPLOYMENT_NAME)
return response['data'][0]['embedding']
Recommended by LinkedIn
4. Create a Vector-Enabled Container
Connect to your Cosmos DB instance and create a container with vector embeddings and indexing policies. This setup is crucial for enabling vector search capabilities.
from azure.cosmos import CosmosClient, PartitionKey, exceptions
cosmos_client = CosmosClient(url=COSMOSDB_NOSQL_ACCOUNT_ENDPOINT, credential=COSMOSDB_NOSQL_ACCOUNT_KEY)
db = cosmos_client.create_database_if_not_exists(id="vector-nosql-db")
vector_embedding_policy = {
"vectorEmbeddings": [
{"path":"/titleVector", "dataType":"float32", "distanceFunction":"dotproduct", "dimensions":1536},
{"path":"/contentVector", "dataType":"float32", "distanceFunction":"cosine", "dimensions":1536}
]
}
indexing_policy = {
"includedPaths": [{"path": "/*"}],
"vectorIndexes": [
{"path": "/titleVector", "type": "quantizedFlat"},
{"path": "/contentVector", "type": "quantizedFlat"}
]
}
container = db.create_container_if_not_exists(
id="vector-nosql-cont",
partition_key=PartitionKey(path='/id', kind='Hash'),
indexing_policy=indexing_policy,
vector_embedding_policy=vector_embedding_policy
)
Upload your data with embeddings to this container to enable efficient vector-based querying.
5. Implement Vector Search Functionality
Use vector search to find the most relevant data points based on the user’s query. This involves generating embeddings for the query and matching them against the stored data vectors.
def vector_search(query, AOAI_client, container_client, num_results=5):
query_embedding = generate_embeddings(query, AOAI_client, EMBEDDING_MODEL_DEPLOYMENT_NAME)
query_string = '''
SELECT TOP @num_results c.content, c.title, c.category,
VectorDistance(c.contentVector, @embedding) AS SimilarityScore
FROM c
'''
parameters = [
{"name": "@num_results", "value": num_results},
{"name": "@embedding", "value": query_embedding}
]
results = []
for item in container_client.query_items(query=query_string, parameters=parameters, enable_cross_partition_query=True):
results.append(item)
return results
6. Use GPT-4 for Response Generation
Leverage the power of GPT-4 to generate responses based on the most relevant retrieved data:
def generate_completion(vector_search_results, user_prompt, AOAI_client):
system_prompt = '''
You are an intelligent assistant for Microsoft Azure services...
'''
messages = [{"role": "system", "content": system_prompt}]
for item in vector_search_results:
messages.append({"role": "system", "content": item['content']})
messages.append({"role": "user", "content": user_prompt})
response = AOAI_client.chat.completions.create(
model=COMPLETIONS_MODEL_DEPLOYMENT_NAME,
messages=messages,
temperature=0
)
return response['choices'][0]['message']['content']
7. Create an Interactive Q&A Loop
Implement a loop to accept user queries and provide responses generated by GPT-4 based on the retrieved data:
print("*** Please ask your model questions about Azure services. Type 'end' to end the session.\n")
user_input = input("Prompt: ")
while user_input.lower() != "end":
results_for_prompt = vector_search(user_input, AOAI_client, container_client)
completions_results = generate_completion(results_for_prompt, user_input, AOAI_client)
print("\nModel Output:\n")
print(completions_results)
user_input = input("Prompt: ")
print("Session ended.")
Real-World Applications
Azure Cosmos DB as a VectorDB can be utilized in various scenarios, including:
Conclusion
Azure Cosmos DB for NoSQL, with its new vector capabilities, is a game-changer for AI applications requiring efficient management and querying of high-dimensional data. By leveraging its robust features, you can build scalable, low-latency AI solutions that harness the power of vector embeddings.
Oracle Certified Master (OCM) / Oracle ACE Pro / Udemy Instructor Partner/ Coursera Instructor / Exadata Cloud@Customer/Oracle Cloud Security Architect/ Generative AI / Azure OpenAI /
11moGreat article Ajay Kumar Barun , very well articulated 👏👏