Generative AI Course
Vector Databases
Up to now, you have learned how text becomes embeddings and how similarity search works using vectors.
That approach works well for small examples.
But real GenAI systems deal with tens of thousands, millions, or even billions of vectors.
This is where vector databases become essential.
The Core Problem
Let’s clearly define the problem first.
You have:
- Large volumes of text data
- Each piece converted into embeddings
- User queries that must find relevant information fast
A traditional database cannot do this efficiently.
Why Traditional Databases Fail
Relational databases are built for exact matches.
They excel at queries like:
- Find rows where ID = 42
- Filter records by date
They are not designed to:
- Compare high-dimensional vectors
- Compute similarity at scale
- Rank results by semantic meaning
Trying to use SQL for vector similarity quickly becomes slow and impractical.
What a Vector Database Is
A vector database is a system designed to:
- Store embeddings efficiently
- Index vectors for fast retrieval
- Perform similarity search at scale
It is optimized specifically for vector operations.
Thinking Before Coding
Before writing any code, ask yourself:
What will my system need to retrieve later?
That question determines:
- What data you embed
- How you chunk documents
- What metadata you store
Vector databases are not just storage — they shape system behavior.
Basic Vector Database Workflow
Every vector DB pipeline follows the same flow:
- Split data into chunks
- Convert chunks into embeddings
- Store embeddings with metadata
- Embed user query
- Retrieve top-k similar vectors
Conceptual Example (In-Memory)
Let’s simulate a vector database using Python to understand the idea clearly.
import numpy as np
class SimpleVectorDB:
def __init__(self):
self.vectors = []
self.metadata = []
def add(self, vector, meta):
self.vectors.append(vector)
self.metadata.append(meta)
def search(self, query, top_k=2):
scores = []
for v, m in zip(self.vectors, self.metadata):
score = np.dot(query, v) / (np.linalg.norm(query) * np.linalg.norm(v))
scores.append((score, m))
scores.sort(reverse=True)
return scores[:top_k]
Before running this, understand the design:
- Vectors store meaning
- Metadata stores context
- Search ranks by similarity
Using the Simple Vector DB
db = SimpleVectorDB()
db.add(np.array([0.8, 0.2]), "Document about AI")
db.add(np.array([0.1, 0.9]), "Document about Cars")
db.add(np.array([0.75, 0.25]), "Article on Machine Learning")
query = np.array([0.78, 0.22])
results = db.search(query)
for score, doc in results:
print(doc, score)
What this code demonstrates:
- Data is stored once
- Queries are compared numerically
- Results are ranked by meaning
Why Real Vector DBs Are More Powerful
Real vector databases go far beyond this example.
They support:
- Approximate nearest neighbor search
- Persistent storage
- Filtering using metadata
- Horizontal scaling
This is why they are used in production GenAI systems.
Where Vector Databases Are Used
Vector databases power:
- RAG systems
- Semantic search engines
- Chatbots with memory
- Recommendation systems
Any job involving GenAI backend work will involve vector databases.
Practice
What type of data do vector databases primarily store?
What operation do vector databases optimize?
Why are vector databases needed instead of normal DBs?
Quick Quiz
Vector databases store:
Main purpose of a vector database?
Vector databases are critical for:
Recap: Vector databases store and retrieve embeddings efficiently, enabling large-scale semantic search.
Next up: We explore ChromaDB — a popular open-source vector database and how developers use it in practice.