AI engineering glossary · RAG vector search

What is vector database?

Vector database (also called Vector store): A database built to store embeddings together with their metadata and to quickly return the vectors closest to a query vector. It typically uses approximate indexes to stay fast at large scale, and adds filtering, updates and persistence.

Level: beginnerTopic: RAG vector search

Vector database: an example

A team stores 10 million chunk embeddings in a vector database and retrieves the top 5 matches for each question in under 50 ms.

Vector database in Python

A short example you can run (Python 3.11, NumPy). The output below is what it really prints.

import numpy as np

class TinyVectorStore:
    def __init__(self):
        self.vecs, self.docs = [], []
    def add(self, vec, doc):
        self.vecs.append(vec / np.linalg.norm(vec)); self.docs.append(doc)
    def search(self, q, k=2):
        scores = np.array(self.vecs) @ (q / np.linalg.norm(q))
        top = np.argsort(-scores)[:k]
        return [(self.docs[i], round(float(scores[i]), 3)) for i in top]

db = TinyVectorStore()
db.add(np.array([1.0, 0.0, 0.2]), "GST is charged at 18% on online courses")
db.add(np.array([0.1, 1.0, 0.0]), "PyTorch tensors live on the CPU or GPU")
db.add(np.array([0.9, 0.1, 0.4]), "Invoices must show the supplier's GSTIN")
print(db.search(np.array([1.0, 0.1, 0.3])))

Output

[("Invoices must show the supplier's GSTIN", 0.992), ('GST is charged at 18% on online courses', 0.991)]

Related terms

More RAG vector search terms

Where you learn it

Vector database is part of the AI Engineering Program, which teaches it with notes, a hands-on lab and an exam. The first lesson is free.

Before you go

Take the cheat sheet with you

One free PDF: the key AI-engineering terms, formulas and Python idioms. No spam; unsubscribe any time.