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
| Word | What it means |
|---|---|
| Vector database | a store that finds the nearest vectors fast |
| Point | one stored vector with its id and payload |
| Payload | the data kept beside a vector |
| Collection | a set of points with one size and one distance |
| Tenant | one customer or group whose data is kept apart |
| Namespace | the 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 questionsWhat 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
docker exec tools python3 steps/stores.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
The same data in two stores
| Store | Where | Points or rows | Numbers each |
|---|---|---|---|
| Qdrant | collection shop | 12 | 4 |
| Qdrant | collection docs | 20000 | 48 |
| Qdrant | collection notes | 8 | 4, plus a sparse vector |
| PostgreSQL | table items | 12 | vector(4) |
| PostgreSQL | table docs | 20000 | vector(48) |
The cells students fill in are left blank in this preview.
Check
Have a go
Check
3 questionsA 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
docker exec tools python3 steps/wrongsize.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Give pgvector a vector of the wrong size
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 questionsA 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
docker exec tools python3 steps/shop.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Search the north shop only
docker exec tools python3 steps/shop.py northShown here, not run. Press Start lab to type it into your own lab’s terminal.
The same search in PostgreSQL
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
| Search | First | Second | Third |
|---|---|---|---|
| Every shop | hot chocolate (north) | porridge (south) | coffee (south) |
| North shop only | hot chocolate | flapjack | tea |
The cells students fill in are left blank in this preview.
Try it: search one tenant only
# 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.
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