Introduction to Vector Databases
Why vector DBs exist, FAISS for local similarity search, storing embeddings for AI retrieval.
Introduction to Vector Databases is a free Learn AI with Python lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Beyond Exact Matches
Traditional databases find rows by exact values (WHERE name = "x"). But AI works with embeddings — vectors that capture meaning — and you often want the items most similar to a query, not exact matches.
Vector databases exist to make this similarity search fast.
What Is an Embedding?
An embedding is a list of numbers (a vector) representing text, an image, or audio so that similar items sit close together in the vector space.
Semantic search, recommendations, and retrieval-augmented generation (RAG) all rest on embeddings.
import numpy as np
# A toy 4-dim embedding for a sentence
vec = np.array([0.12, -0.43, 0.88, 0.05], dtype="float32")
print(vec.shape) # (4,)Why Not a Normal Database?
Comparing a query against millions of vectors with a brute-force loop is slow. Vector DBs use specialized indexes and distance math to return the nearest neighbors in milliseconds.
They also scale, persist, and handle metadata alongside vectors.
Measuring Similarity
Closeness is measured with a distance metric:
- L2 (Euclidean) — straight-line distance; smaller is closer
- Cosine — angle between vectors; good for text
FAISS supports several; we will use L2.
Introducing FAISS
FAISS (Facebook AI Similarity Search) is a fast, free library for vector search. It runs locally with no server — ideal for learning and prototyping.
import faiss
import numpy as np
# pip install faiss-cpuCreating an Index with IndexFlatL2
IndexFlatL2(dim) builds a flat (brute-force, exact) index using L2 distance. You must tell it the dimension of your vectors.
"Flat" means exact results — perfect for small to medium collections.
import faiss
dim = 4
index = faiss.IndexFlatL2(dim)
print(index.is_trained) # True (flat index needs no training)Adding Vectors with index.add
Feed your embeddings in as a 2D float32 NumPy array of shape (n, dim). index.add stores them; index.ntotal reports the count.
import numpy as np
embeddings = np.random.random((100, dim)).astype("float32")
index.add(embeddings)
print(index.ntotal) # 100Searching with index.search
index.search(query, k) returns the k nearest vectors. It gives back two arrays: distances D and indices I (positions in the order you added).
query = np.random.random((1, dim)).astype("float32")
D, I = index.search(query, k=5)
print("nearest ids:", I[0])
print("distances:", D[0])From Indices Back to Items
FAISS returns positions, not your original data. Keep a parallel list (or DB) mapping index position to the real item so you can look results up.
docs = ["doc about cats", "doc about dogs", "doc about cars"]
# after search:
for pos in I[0]:
print(docs[pos]) # map id -> original documentA Tiny Semantic Search Pipeline
The full pattern: embed your documents, add them to an index, embed the query, search, and return the matching documents. (Embedding models like sentence-transformers produce the vectors.)
import faiss, numpy as np
# doc_vectors: (n, dim) from an embedding model
index = faiss.IndexFlatL2(doc_vectors.shape[1])
index.add(doc_vectors)
# query_vec: (1, dim) embedding of the user query
D, I = index.search(query_vec, k=3)
results = [docs[i] for i in I[0]]
print(results)When to Reach for a Vector DB
Use a vector database when you need:
- Semantic search over text or images
- Recommendations by similarity
- RAG: retrieving relevant context for an LLM
FAISS is great locally; managed options (Pinecone, Weaviate, pgvector) add persistence and scale.
Quick Check: FAISS Search
You call index.search(query, k=5) on a FAISS index.
Recap: Vector Databases
You learned the foundation of similarity search:
- Embeddings place similar items close together in vector space
- Vector DBs make nearest-neighbor search fast at scale
- FAISS
IndexFlatL2(dim)builds an exact L2 index index.add(embeddings)stores vectors;index.search(query, k)returns distances and indices- Map returned indices back to your original items
That completes databases. Next: structuring AI projects with Git.
Frequently asked questions
Is the “Introduction to Vector Databases” lesson free?
Yes — the full text of “Introduction to Vector Databases” is free to read here on the web, and the Learn AI with Python course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.
What will I learn in “Introduction to Vector Databases”?
Why vector DBs exist, FAISS for local similarity search, storing embeddings for AI retrieval. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Learn AI with Python?
No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Introduction to Vector Databases” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Learn AI with Python lesson?
Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- SQLite with Python's sqlite3 Module
- Pandas and SQL Integration
- Storing and Querying ML Results
- Introduction to Vector Databases