Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Vector databases: collections and namespaces

After this lesson: I can organise vectors into collections and namespaces in a vector database.

60 min lesson6 commandsVector database lab: Qdrant and PostgreSQL with pgvector

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

Where do a million vectors live?

A vector database is a store that keeps vectors with their data and finds the ones nearest a question, fast. A list in a script does that for a thousand vectors.

A college with a million documents, three campuses and changes every hour needs more. It needs a store that keeps each campus apart, survives a restart and still answers in milliseconds.

By the end of this lesson

Note

I can organise vectors into collections and namespaces in a vector database.

Key words

WordWhat it means
Vector databasea store that finds the nearest vectors fast
Pointone stored vector with its id and payload
Payloadthe data kept beside a vector
Collectiona set of points with one size and one distance
Tenantone customer or group whose data is kept apart
Namespacethe part of a collection kept for one tenant

The cells students fill in are left blank in this preview.

Starter

Have a go

Starter

4 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

What a vector database adds to a list

Modules 66 and 77 kept vectors in a list and compared a question with each one. That is fine for a few thousand.

A vector database adds what a real service needs. It has an index, so a search does not read every vector. It keeps data on disk, takes updates while it answers, filters on the payload and speaks an API. Qdrant is one built for this job. pgvector adds the same to PostgreSQL.

See what the lab holds

Command
docker exec tools python3 steps/stores.py

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

The same data in two stores

StoreWherePoints or rowsNumbers each
Qdrantcollection shop124
Qdrantcollection docs2000048
Qdrantcollection notes84, plus a sparse vector
PostgreSQLtable items12vector(4)
PostgreSQLtable docs20000vector(48)

The cells students fill in are left blank in this preview.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

A collection holds one size and one distance

A collection is a set of points. Every vector in it has the same length and is compared with the same distance: cosine, dot product or Euclidean.

In PostgreSQL the column type does the same job: vector(4) takes only four numbers. One embedding model makes vectors of one length, so a collection usually holds one model's vectors.

Give Qdrant a vector of the wrong size

Command
docker exec tools python3 steps/wrongsize.py

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Give pgvector a vector of the wrong size

Command
docker exec pg psql -U lab -d shop -c "SELECT '[1,2,3]'::vector(4)"

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

A common mistake

Warning

Many people think vectors from two embedding models can share a collection if they have the same length. Each model places meanings differently, so the distances between them mean nothing. Keep one model per collection, or one named vector per model.

Namespaces keep tenants apart

A tenant is a customer whose data must stay apart: a school, a shop, a company. Pinecone calls each tenant's part of an index a namespace.

Qdrant keeps tenants in one collection with a payload field marked as the tenant, and every search filters on it. PostgreSQL does it with a column and a WHERE clause. With few large tenants, one collection each is simpler to delete and to move.

Search every shop for hot and sweet

Command
docker exec tools python3 steps/shop.py

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Search the north shop only

Command
docker exec tools python3 steps/shop.py north

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

The same search in PostgreSQL

Command
docker exec pg psql -U lab -d shop -c "SELECT id, name, shop FROM items WHERE shop = 'north' ORDER BY embedding <=> '[1,0,1,0]' LIMIT 3"

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Worked example: hot and sweet

SearchFirstSecondThird
Every shophot chocolate (north)porridge (south)coffee (south)
North shop onlyhot chocolateflapjacktea

The cells students fill in are left blank in this preview.

Try it: search one tenant only

Code: python
# Each point has an id, a tenant and a vector of 2 numbers.
points = [
    {"id": 1, "tenant": "north", "vector": [0.9, 0.1]},
    {"id": 2, "tenant": "south", "vector": [0.8, 0.3]},
    {"id": 3, "tenant": "north", "vector": [0.1, 0.9]},
]
question = [1.0, 0.0]
tenant = "north"
best = None
best_score = -1
for p in points:
    if p["tenant"] != tenant:
        continue
    score = p["vector"][0] * question[0] + p["vector"][1] * question[1]
    if score > best_score:
        best = p["id"]
        best_score = score
print("best for", tenant, "is id", best)
# Change tenant to "south". Which id wins now, and why not id 1?

Make a free account to save your work and track your progress.

Match each word to its meaning

Have a go

Loading…

Your answers are marked here and are not kept. An account keeps your marks.

Assignment: collections and tenants

A store keeps points as dictionaries with an "id", a "tenant" and a "vector". Write three functions.

fits(size, vector) is True when the vector has exactly size numbers. tenant_ids(points, tenant) is the list of ids of that tenant's points, in the order given. nearest(points, tenant, question, k) is the list of ids of the k points of that tenant with the largest dot product with the question, best first. A point of another tenant is never returned, even when it is closer. Ties keep the earlier point first.

Code: python
def fits(size, vector):
    return True


def tenant_ids(points, tenant):
    return []


def nearest(points, tenant, question, k):
    return []

Make a free account to save your work and track your progress.

Exit quiz

Have a go

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.