An embedding maps text into a dense vector so similar meaning sits nearby in space—powering semantic search and RAG retrieval.
Similarity search
import math
def cosine(a, b):
dot = sum(x * y for x, y in zip(a, b))
na = math.sqrt(sum(x * x for x in a))
nb = math.sqrt(sum(y * y for y in b))
return dot / (na * nb + 1e-9)
# Higher cosine ≈ more similar meaning (illustrative 3-D vectors)
Embedding models
Dedicated embedding APIs (e.g. small transformer encoders) produce vectors for passages and queries. Use the same model for indexing and querying.
Not magic
- Synonyms and paraphrases match well; exact SKU codes may need hybrid keyword + vector search
- Stale docs in the index produce confident wrong answers—operate the corpus
Important interview questions and answers
- Q: Cosine similarity vs Euclidean?
A: Cosine is standard for normalized embedding vectors; scale-invariant direction match.
Self-check
- What is an embedding used for in RAG?
- Why use the same model for index and query?
Tip: Re-embed the corpus when you change embedding models—mixed indexes break retrieval.
Interview prep
- Cosine similarity?
Measures angle between vectors—standard for normalized embeddings.
- Same model index/query?
Mixing embedding models breaks semantic search geometry.