Cosine similarity
A score of how close two embeddings are in meaning. 1 means the same meaning, 0 means unrelated.
Last updated
Analogy
A compatibility score for sentences. "Revert a deploy" and "rollback steps" swipe right on each other.
Code example
function cosineSimilarity(a: number[], b: number[]): number { const dot = a.reduce((sum, value, i) => sum + value * b[i], 0); const lengthA = Math.hypot(...a); const lengthB = Math.hypot(...b); return dot / (lengthA * lengthB);}In an agent
Vector search sorts your chunks by this score and hands the top few to the agent.
Practice
Flashcards
- What is cosine similarity?A score of how close two embeddings are in meaning: 1 is the same, 0 is unrelated.
Interview questions
- What are embeddings?
- Vectors that represent meaning.
- Similar meanings give nearby vectors, measured with cosine similarity.
- They power semantic search, RAG, deduplication and clustering.
Questions people ask
What does cosine similarity measure?
How close two embeddings are in meaning, by comparing the angle between the vectors. A score near 1 means the texts mean nearly the same thing; near 0 means they are unrelated. Vector search sorts stored chunks by this score and hands the closest few to the model.
Do I need to calculate cosine similarity myself?
Usually not. Vector databases compute it for you; in pgvector, the <=> operator gives cosine distance, where smaller means closer. Writing the formula once in TypeScript is still useful for understanding it: the dot product of the two vectors divided by the product of their lengths.