GENERATIVE AI • LESSON 7

Vector Databases

You already learned: Embeddings → Vectors → Cosine Similarity → Semantic Search

CORE IDEA

A vector database is a database designed to store vectors and quickly find vectors that are similar to a given vector.

01

First, Why Do We Need a Vector Database?

Suppose you have only 5 documents:

Document 1 → Vector
Document 2 → Vector
Document 3 → Vector
Document 4 → Vector
Document 5 → Vector

No big problem.

But imagine your application has:

10,000 documents
1 million documents
100 million documents

Each document can be converted into an embedding:

Document
   ↓
Embedding Model
   ↓
Vector

You need somewhere to store those vectors and efficiently search them.

That's where a vector database comes in.

02

Normal Database vs Vector Database

A traditional database is very good at questions like:

"Find the user whose email is john@example.com."

or:

"Find products where price is less than ₹1,000."

For example:

Users

id | name  | email
---|-------|----------------
1  | John  | john@email.com
2  | David | david@email.com

That's structured data.

A vector database is optimized for a different kind of question:

"Find information that is semantically similar to this question."
03

Traditional Database

Think:

SQL Database
     ↓
Exact / structured filtering
     ↓
Rows

Example:

</>   SQL
SELECT *
FROM products
WHERE price < 1000;

You're explicitly telling the database what condition to match.

04

What Does a Vector Database Store?

It doesn't usually store only the vector.

A record might look conceptually like:

ID: 101

Text:
"Python is a programming language."

Vector:
[0.21, -0.45, 0.78, ...]

And another:

ID: 102

Text:
"Machine learning uses data to learn patterns."

Vector:
[0.15, -0.22, 0.81, ...]

You can also store metadata:

ID: 101

Text:
"Python is a programming language."

Vector:
[...]

Metadata:
{
    "category": "programming",
    "source": "python-guide.pdf",
    "page": 10
}

That metadata becomes very useful when building real applications.

05

How Does It Work?

Suppose you have these documents:

Document 1:
"Python is a programming language."

Document 2:
"Football is a popular sport."

Document 3:
"Pizza is an Italian food."

Document 4:
"Machine learning uses data."

First, create embeddings:

Document 1 → Vector 1
Document 2 → Vector 2
Document 3 → Vector 3
Document 4 → Vector 4

Store them:

Vector Database

Vector 1 → Python document
Vector 2 → Football document
Vector 3 → Pizza document
Vector 4 → ML document

Now user asks:

"What programming language should I learn?"

Create an embedding:

Question
   ↓
Embedding Model
   ↓
Query Vector

Then search:

Query Vector
     ↓
Vector Database
     ↓
Compare/search vectors
     ↓
Most similar vectors

Result:

Document 1
"Python is a programming language."
06

What Is "Nearest"?

This is another important concept.

Query Vector
     ↓
     ●

And your database has:

     ● Document A

                    ● Document B


   ● Query


                         ● Document C

The system wants to find vectors that are nearest/similar to the query according to the chosen distance or similarity metric.

Conceptually:

Query
  ↓
Nearest vectors
  ↓
Most relevant content

This is why you will hear terms such as:

Nearest Neighbor Search

and:

Approximate Nearest Neighbor (ANN)
07

Cosine Similarity Connection

You learned cosine similarity earlier.

Now connect it:

Query
 ↓
Embedding
 ↓
Query Vector
 ↓
Vector Database
 ↓
Similarity Search
 ↓
Cosine / distance metric
 ↓
Similar Vectors

For example:

Query ↔ Document A = 0.92
Query ↔ Document B = 0.14
Query ↔ Document C = 0.76

The database can return:

Document A
Document C

because they are the most similar.

Important: vector databases can use different similarity/distance metrics. Cosine similarity is common, but it isn't the only option.

08

Popular Vector Databases

You will encounter several technologies:

• Qdrant
• Pinecone
• Weaviate
• Milvus
• Chroma
• FAISS

There is an important distinction here:

IMPORTANT

FAISS is primarily a similarity-search library, not a full traditional database.

For learning, FAISS is excellent because you can understand vector search without dealing with a large database infrastructure.

09

Practical Python Example Using FAISS

For learning, let's build a tiny vector search system.

First install:

</>   Python
pip install faiss-cpu

Then:

</>   Python
import faiss
import numpy as np

vectors = np.array([
    [1, 2, 3],
    [2, 3, 4],
    [10, 20, 30]
], dtype="float32")

dimension = 3

index = faiss.IndexFlatL2(dimension)

index.add(vectors)

print("Number of vectors:", index.ntotal)

Now we have:

Vector 1 → [1, 2, 3]
Vector 2 → [2, 3, 4]
Vector 3 → [10, 20, 30]

inside the FAISS index.

10

Search the Vectors

Create a query:

</>   Python
query = np.array([
    [1, 2, 2]
], dtype="float32")

Search:

</>   Python
distances, indexes = index.search(query, k=2)

print(indexes)
print(distances)

Here:

k = 2

means:

Find the 2 nearest vectors.

You might get something conceptually like:

Nearest vectors:
Vector 1
Vector 2

because they are closer to:

[1, 2, 2]

than Vector 3.

11

But Real Applications Need Text Too

The vector alone isn't useful to the user.

Suppose:

</>   Python
documents = [
    "Python is a programming language.",
    "Machine learning learns patterns from data.",
    "Football is played between two teams."
]

You want to maintain the relationship:

Vector 0 → Document 0
Vector 1 → Document 1
Vector 2 → Document 2

Then when the vector search returns:

index = 0

your application knows:

Document 0:
"Python is a programming language."

This is called retrieval.