6100: Vector Embeddings¶
Module Overview¶
This module covers vector embeddings and similarity search, the foundation of modern retrieval-augmented generation (RAG) systems. You'll learn to embed documents, find similar content, and build efficient vector indexes.
Why This Matters:
- Embeddings transform text into semantic meaning representations
- Vector search powers ChatGPT's browsing, code assistants, and recommendation systems
- Efficient similarity search is critical for RAG performance
- Understanding embeddings enables better semantic understanding
Learning Objectives¶
After completing this module, you will be able to:
- Embedding Fundamentals: Understand how text becomes vectors
- Semantic Similarity: Measure meaning-based similarity between documents
- HNSW Indexing: Build fast approximate nearest neighbor indexes
- Embedding Models: Choose and use appropriate embedding models
- Vector Operations: Perform efficient similarity search at scale
- Embedding Sciences: Pooling rules, matryoshka truncation, and reading MTEB by task menu
- Contextual and Multilingual Embeddings: Static-vs-contextual geometry, anisotropy, and cross-lingual shared spaces
Module Contents¶
6101: HNSW Indexing¶
Hierarchical Navigable Small World Graphs
- The problem of vector search at scale
- HNSW algorithm and architecture
- Indexing with FAISS
- Key parameters: M, ef_construction, ef_search
- Integration and evaluation
Experiments:
- Implement HNSW from scratch
- Compare HNSW vs brute force search
- Tune HNSW parameters
- Benchmark query performance
6102: Semantic Similarity¶
Measuring Document Similarity
- Distance vs similarity: what each measures
- Cosine similarity and dot product
- Euclidean and Manhattan distance
- Advanced metrics and when to use each
- Hands-on practice with similarity computation
Experiments:
- Generate embeddings for documents
- Compare similarity metrics
- Test different embedding models
- Build semantic search engine
6103: HNSW Tuning Guide¶
Production Vector Index Optimization (Guide)
- HNSW architecture and parameter deep dive
- Tuning strategy: build time, memory, recall
- Production configurations and optimal settings by use case
- Dynamic ef adjustment
- Performance benchmarks and tuning checklist
Guide: guides/6103-HNSW-Tuning-Guide.md
6104: Embedding Sciences¶
Pooling, Matryoshka, and MTEB
- Pooling rules: CLS readout vs masked mean, the padded-batch trap
- Matryoshka Representation Learning: trained dimension order, truncation ladders
- Two-stage adaptive retrieval on truncated prefixes
- The storage ledger: dimensionality and precision per million documents
- Reading MTEB: the benchmark mean as a function of the task menu
Experiments:
- Compare CLS vs mean pooling on your own model
- Truncate and measure recall at 1/4 and 1/16 of the dimensions
- Price the storage ledger before shipping a fleet
6105: Contextual and Multilingual Embeddings¶
Anisotropy, Alignment, and the Shared Space
- One vector per type vs one vector per use: the 0.707-vs-0.505/0.910 split
- Anisotropy: the mean-dominated cone, the collapsed margin, and mean-centering
- Orthogonal Procrustes alignment of two independently trained spaces
- The language-identity probe: score bands and the linear language probe
- Three retrieval stacks priced: mono index, translate-then-retrieve, shared space
Experiments:
- Center your raw states and re-measure every threshold policy
- Fit a Procrustes map on translation pairs and verify orthogonality
- Probe your multilingual embedder's bands before trusting it
Prerequisites¶
Before starting this module, ensure you have:
- Module 2100: Calculus for Deep Learning (vectors, dot products)
- Module 3200: Embedding Latent Spaces (embedding fundamentals)
- Python and NumPy proficiency
- Basic understanding of neural networks
- Familiarity with REST APIs
Review: PREREQUISITES.md for detailed requirements.
Assessment¶
Quiz¶
- Format: 20 multiple-choice questions
- Passing Score: 80% (16/20)
- Topics: Embeddings, similarity, HNSW
- Location: 6100: Vector Embeddings - Quiz
Practice Exercises¶
- Format: Hands-on vector search projects
- Duration: 2 hours
- Topics:
- Generate and compare embeddings
- Build HNSW index
- Implement semantic search
- Optimize query performance
- Location: 6100: Vector Embeddings - Practice
Related Modules¶
This module connects to:
- 3200: Embedding Latent Spaces (embedding model architectures)
- 6200: Retrieval Strategies (advanced retrieval techniques)
- 6400: Vector Databases (production vector stores)
- 6300: Context Management (knowledge graph integration)
Time Commitment¶
| Activity | Time |
|---|---|
| 6101: HNSW Indexing | 3 hours |
| 6102: Semantic Similarity | 3 hours |
| Guide (6103) | 3 hours |
| 6104: Embedding Sciences | 3 hours |
| 6105: Contextual and Multilingual Embeddings | 3 hours |
| Quiz | 30 minutes |
| Practice | 2 hours |
| Total | 17.5 hours |
Resources¶
Essential Libraries:
- sentence-transformers
- hnswlib / faiss
- numpy / scipy
- langchain embeddings
Essential Models:
- all-MiniLM-L6-v2 (fast, English)
- bge-base-en-v1.5 (quality, English)
- e5-large-v2 (multilingual)
- text-embedding-3-small (OpenAI)
Embedding Model Comparison¶
| Model | Dimensions | Speed | Quality | Best For |
|---|---|---|---|---|
| all-MiniLM-L6-v2 | 384 | ⭐⭐⭐⭐⭐ | ⭐⭐⭐ | Fast English search |
| bge-base-en-v1.5 | 768 | ⭐⭐⭐⭐ | ⭐⭐⭐⭐⭐ | Quality English RAG |
| e5-large-v2 | 1024 | ⭐⭐⭐ | ⭐⭐⭐⭐⭐ | Multilingual |
| text-embedding-3-small | 1536 | ⭐⭐⭐ | ⭐⭐⭐⭐⭐ | Production (API) |
| jina-embeddings-v2 | 768 | ⭐⭐⭐⭐ | ⭐⭐⭐⭐ | Long documents |
Distance Metrics¶
| Metric | Range | Use Case |
|---|---|---|
| Cosine | 0-2 | Most embeddings (default) |
| Dot Product | -∞ to ∞ | Normalized vectors |
| Euclidean | 0-∞ | Physical distance |
| Manhattan | 0-∞ | Sparse vectors |
HNSW Parameters¶
| Parameter | Effect | Tradeoff |
|---|---|---|
| ef_construction | Index quality | Build time |
| M | Connectivity | Memory vs recall |
| ef_search | Query accuracy | Query speed |
Tips for Success¶
- Start with cosine similarity: It's the standard for embeddings
- Choose model by use case: Speed vs quality vs multilingual
- Tune HNSW carefully: Small changes, big impact
- Normalize embeddings: Required for cosine similarity
- Batch embeddings: Much faster than one-by-one
Common Pitfalls¶
- Wrong embedding model: Domain mismatch kills quality
- Not normalizing: Breaks cosine similarity
- HNSW overkill: Small datasets don't need it
- Ignoring context: Sentence vs document embeddings matter
- Forgetting updates: HNSW is expensive to update
When to Use Vector Search¶
| Scenario | Recommended Approach |
|---|---|
| Semantic search | Vector embeddings |
| Keyword search | BM25 / lexical |
| Hybrid queries | Vector + keyword fusion |
| Exact matching | Lexical search |
| Fuzzy matching | Vector search |
Embedding Workflow¶
1. Chunk Documents
↓
2. Generate Embeddings (batch)
↓
3. Build HNSW Index
↓
4. Query Embedding
↓
5. Approximate Nearest Neighbor Search
↓
6. Return Top-K Results
Next Module: 6200: Retrieval Strategies
Previous Module: 5300: Synthetic Data
Questions? Review the PREREQUISITES.md or check Phase 6 documentation.