Embedding
A representation technique that maps unstructured data like text or images into dense numerical vectors where semantic similarity corresponds to geometric proximity
Table of Contents
Structure Diagram
What is Embedding?
A representation technique that maps unstructured data like text or images into dense numerical vectors where semantic similarity corresponds to geometric proximity. It is a core concept in Retrieval & RAG within AI / ML.
Core Principles
An Embedding transforms unstructured inputs—such as sentences, code snippets, images, or audio—into dense numerical vectors (e.g., [0.021, -0.418, 0.773, ...]) spanning hundreds or thousands of dimensions.
The magic is geometric: concepts with similar meaning land close together in vector space. Whereas keyword search fails to connect "dog food" with "puppy nutrition" because no words overlap, their embeddings yield a high Cosine Similarity, unlocking true semantic search, clustering, and recommendations.
Practical Takeaways
- Never mix embedding models across index and query: Vectors from two different models live in incompatible coordinate spaces. Upgrading your embedding model requires re-embedding and re-indexing your entire vector store.
- Control memory with Matryoshka dimensions and quantization: Millions of 1536–3072d float32 vectors consume huge RAM. Truncating Matryoshka-trained embeddings (e.g., to 512d) or applying
int8/binary quantization slashes storage and accelerates ANN search with minimal recall loss. - Pair with BM25 for exact identifiers: Dense embeddings excel at paraphrases and synonyms but struggle with exact error codes (
ERR-4092) or product SKUs. Combining vector similarity with lexical BM25 (Hybrid Search) covers both worlds.
Notable Examples
- RAG retrieval backends: Indexing document chunks in pgvector, Qdrant, or Pinecone to retrieve semantically relevant context in milliseconds.
- Recommendation engines: Spotify, Netflix, and YouTube embedding users and items into a shared latent space to surface personalized feeds.
- Deduplication and clustering: Grouping thousands of customer support tickets or product reviews by semantic intent automatically.
Practical Code Snippet
Copy and drop straight into your projectexport function cosineSimilarity(a: number[], b: number[]): number { let dot = 0; let normA = 0; let normB = 0; for (let i = 0; i < a.length; i++) { dot += a[i] * b[i]; normA += a[i] * a[i]; normB += b[i] * b[i]; } return dot / (Math.sqrt(normA) * Math.sqrt(normB));} // Closer to 1.0 = stronger semantic similarity