>_ IzzyLab

Hands-on task

This task is step 18 of the course “Redis in production”

Similar documents: Vector Sets

medium~10 min

/root/vectors.txt holds embeddings of knowledge base articles: each line has an article name and 4 vector coordinates separated by spaces.

  1. Load all lines into the vector set docs with VADD (the article name is the element, the vector is its coordinates).
  2. Using VSIM, find the 3 articles closest to the query vector 0.7 0.4 0.1 0.0.
  3. Write their names to /root/nearest.txt — one per line, from the most similar to the least similar.

Similarity here is cosine, as with embeddings: what matters is the direction of the vector, not the distance between points.

What you'll practise:
  • redis
  • Vector Sets
  • VADD
  • VSIM
  • VCARD
  • vector search
  • embeddings

Topic breakdown

A Vector Set is a Redis 8 data type similar to a sorted set, but instead of a numeric score each element has a vector. Inside is an HNSW graph that finds nearest neighbors approximately but fast, even across millions of vectors; similarity is computed by cosine, so the direction of a vector matters, not its length. By default coordinates are quantized to int8 — 4 times less memory at the cost of a small error. This is the foundation of semantic search and RAG: texts are turned into embeddings, and for a user's query you find the documents closest in meaning.

What's checked

  1. docs has all articles from the file, dimension 4
  2. vectors loaded from the file
  3. /root/nearest.txt has the three nearest articles in order

What it looks like