Cosine Similarity
A metric used to measure how similar two vectors are, commonly used in NLP and vector databases.
How it works
Cosine similarity measures the cosine of the angle between two vectors in a multi-dimensional space. It is computed as the dot product of the two vectors divided by the product of their magnitudes. A result of 1 means the vectors point in the same direction (identical meaning), 0 means they are orthogonal (unrelated), and -1 means they are opposite. Crucially, it ignores the magnitude of the vectors and focuses only on their direction, making it robust to differences in text length or embedding scale.
Why it matters
Cosine similarity is the backbone of semantic search. When a user searches a vector database with a query embedding, cosine similarity is used to rank which stored documents are most semantically relevant. It enables RAG systems to retrieve the most appropriate context, powers recommendation engines that find similar items, and allows clustering algorithms to group semantically related embeddings — all fundamental operations in modern AI applications.