Embeddings: The Geometry of Meaning

An "Embedding" is a dense, high-dimensional vector representation (\mathbb{R}^d) that captures semantic relationships in a continuous space. For Retrieval-Augmented Generation (RAG), the choice of embedding model and distance metric is as critical as the LLM itself.

1. Distance Metrics: Cosine vs. Inner Product

The performance of your vector search depends on matching the search metric to the model's training objective.

\text{sim}(A, B) = \frac{A \cdot B}{\|A\| \|B\|}

2. Vector Quantization (Compression)

Storing raw float32 vectors is memory-intensive (e.g., 1 million 1536-dim vectors take ~6GB). Databases use quantization to reduce this footprint:

3. Matryoshka Embeddings (Truncation)

Recent models (like OpenAI text-embedding-3-large or nomic-embed-text) are trained with Matryoshka Representation Learning. This allows you to truncate the vector dimensions (e.g., from 3072 down to 256) without a catastrophic loss in performance.

Concrete Impact: Accuracy vs. Dimensions

DimensionsMTEB Score (Retrieval)Storage (1M vectors)
307254.912.0 GB
102454.14.0 GB
25652.01.0 GB

Practical Rule: Use 256 or 512 dimensions for initial retrieval to save memory, then use the full vector (if needed) only for the top 50 results.

4. The Embedding Pipeline

  1. Chunking: Split text into semantic blocks (e.g., 512 tokens with 50-token overlap).
  2. Normalization: Most models require input to be normalized (unit length) for dot product search.
  3. Storage: Save in a vector database using HNSW or IVF indexing.

Concrete Tip: Always use a dedicated embedding model (e.g., BAAI/bge-m3 or nomic-embed-text) rather than trying to extract hidden states from a generative model like Llama. Generative models are optimized for prediction, not vector space alignment.