Exercise 11 — Embedding Search¶
Timebox: 30-45 minutes
Goal¶
Build a tiny vector search: cosine similarity over an in-memory store, then add a quick approximate-NN trick. Useful as a RAG-eval primitive.
Work in¶
embedding_search.py
Tasks¶
cosine(u, v)— pure NumPy.Index.add(id, vec)andIndex.search(query, k)returning top-k by cosine.- Normalize vectors on insert so search is a single matmul.
- Add a simple ANN path: random projection LSH or IVF (assign vectors to centroids, search only nearest M centroids' shards).
- Measure recall@k of ANN against exact for a synthetic corpus.
Done when¶
- Exact and ANN paths both work
- Recall@10 and latency printed for both on a 10K-vector synthetic corpus
- You can explain why normalization makes cosine == dot product
Stretch¶
- Plug in real embeddings (
sentence-transformers) and search over a small text corpus - Add filtering (metadata predicate at search time)