Computing Cosine Similarity
What you'll learn: How to implement cosine similarity from scratch to measure how aligned two embedding vectors are, powering semantic search comparisons.
What Is Cosine Similarity?
Cosine similarity measures the angle between two vectors in embedding space, not their magnitude. It tells you how similar their directions are, which in semantic search means how similar their meanings are.
Think of two arrows pointing in space: if they point in nearly the same direction, they're semantically similar (cosine similarity close to 1). If they point in opposite directions, they're dissimilar (close to -1). If they're perpendicular, they're unrelated (close to 0).
The Formula
Cosine similarity is the dot product of two vectors divided by the product of their magnitudes:
cosine_similarity = (A · B) / (||A|| × ||B||)
Where:
A · Bis the dot product (sum of element-wise multiplications)||A||is the magnitude (square root of sum of squared elements)
Why It Works for Embeddings
Since embedding APIs often return normalized vectors (magnitude = 1), the formula simplifies to just the dot product. This makes comparison extremely fast—just multiply corresponding dimensions and sum them up.
When vectors are normalized, cosine similarity ranges from -1 to 1, where higher values mean greater semantic similarity.
Implementation Insight
You'll compute this for every query-document pair in your search system. For 1 query against 10,000 documents, that's 10,000 cosine similarity calculations—which is why vector databases optimize this operation heavily.
Key Takeaway: Cosine similarity measures the directional alignment between embedding vectors using dot products and magnitudes, giving you a numeric score of semantic similarity.