Embeddings and cosine similarity
EasyEmbeddingsSimilarityVectors
What are embeddings, and how does cosine similarity find related content?
TL;DR
An embedding is a vector of numbers (often 384 to 3,072 dimensions) that represents the meaning of a piece of text, an image or other data. An embedding model is trained so that similar meanings land close together in that space: "How do I reset my password?" ends up near "I forgot my login", even with no words in common. Cosine similarity measures the angle between two vectors: 1 means the same direction (very similar), 0 means unrelated. It's the foundation of semantic search, RAG, recommendations, clustering and deduplication.
Key points
- Cosine similarity =
(a · b) / (|a| × |b|). With normalized vectors (length 1), it equals the plain dot product, which is faster. - Embedding models aren't chat LLMs; they're separate, smaller models. Choose by quality on your data and language (check multilingual support for Hebrew), dimension size and cost.
- Query and documents must use the same embedding model. Switching models means re-embedding everything.
- Some models use different modes for queries and documents (asymmetric search); follow the model's instructions.
- Embeddings capture topic similarity, not exact facts: "the meeting is on Monday" and "the meeting is on Tuesday" are very close vectors.
- Dense embeddings miss exact keywords (IDs, product codes), which is why hybrid search adds keyword matching.
Example
python
q = embed("how do I reset my password")
docs = {"Forgot your login? Use the reset link": embed(...),
"Our office hours are 9-5": embed(...)}
def cosine(a, b):
return dot(a, b) / (norm(a) * norm(b))
# cosine(q, doc1) ≈ 0.82 → relevant
# cosine(q, doc2) ≈ 0.11 → not relevantCommon follow-ups
- Cosine vs Euclidean distance? On normalized vectors they rank results the same way. Cosine ignores vector length, which mostly reflects text length rather than meaning.
- How do you evaluate an embedding model? Build a small labeled set of queries with known relevant documents and measure recall@k on your own data, not only public leaderboards.
What interviewers look for
Tip
Meaning as geometry, cosine as an angle, the same model for queries and documents, and the limits (exact terms, negation, facts).
- Red flag: assuming high similarity means the content answers the question correctly.