Cheat sheet · Module 3
Cheat sheet: Embeddings and vector search
Generative AI · Cheat sheet · Embeddings and vector search
Embeddings: a dedicated embedding model returns one dense vector per chunk or query. Close vectors mean similar meaning (contrastive training). Cosine similarity is the usual text metric: cos = a·b / (|a||b|), which is just a·b on unit-length vectors. Use the metric the model was trained for, and configure the index to match. Index and query must use the same model and version.
Where it fails: exact identifiers and numbers, rare terms, domain shift, and “on topic but not an answer”. Fixes: hybrid search (BM25 plus vectors), reranking, a better-suited model, and measuring on your own queries.
ANN indexes:
- Exact search costs vectors x dimensions per query. ANN is approximate: you trade recall for latency and memory, and tune the trade.
- HNSW = layered graph. High recall, memory-heavy, incremental inserts. Knobs:
M,ef_construction(build time),ef_search(query time). - IVF = clusters. Lighter on memory, needs training on representative data. Knobs:
nlistorlists, andnprobeorprobes(query time). - Quantization (PQ and others) shrinks memory at some recall cost.
- Measure recall@k against brute force on real queries, one knob at a time.
Choosing a store: start with what you already run (often pgvector), measure, and move when a measured limit forces it. FAISS is a library, not a database. Qdrant and Weaviate are open source and self-hostable, with managed clouds too. Check current docs for hosting and limits.
Production: combine vector search with metadata filters (permissions, department, date). Post-filtering can return fewer than k results. Use deterministic chunk IDs and delete removed chunks so edits do not leave stale copies.
Quick answers
- ANN knobs:
ef_search(andM,ef_constructionat build) for HNSW;nprobe(andnlistat build) for IVF. Verify recall against brute force. - Normalized vectors: cosine equals dot product, and Euclidean gives the same ranking.
- Exact code missing from results: add keyword search (hybrid).