Cosine Similarity

Cosine similarity measures the angle between two vectors in high-dimensional space. In AI, it quantifies how semantically similar two embeddings are — a score of 1.0 means identical direction (highly similar), 0 means orthogonal (unrelated), -1 means opposite meaning.

What is Cosine Similarity?

Cosine similarity measures the cosine of the angle between two vectors. In embedding space, it's the standard way to measure how semantically similar two pieces of text are, and is the core distance metric used by most vector databases and RAG systems.

Formula

cosine_similarity(A, B) = (A · B) / (|A| × |B|)

Where A · B is the dot product and |A|, |B| are the magnitudes. The result ranges from -1 to 1.

Interpreting Scores

Score Interpretation
1.0 Vectors are identical in direction (maximally similar)
0.9+ Very high similarity
0.7–0.9 Good semantic match
0.5–0.7 Some relation
< 0.5 Likely not related
~0 Orthogonal — unrelated
-1 Opposite meaning (rare in text embeddings)

Cosine vs Dot Product vs Euclidean Distance

For normalized vectors (most embedding models output unit vectors), cosine similarity equals the dot product. Euclidean distance is also valid but less commonly used for high-dimensional embeddings due to the curse of dimensionality.

Practical Implementation

import numpy as np

def cosine_similarity(a, b):
    return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))

Most vector databases handle similarity computation internally — you provide the query embedding and they return the top-k most similar stored vectors.

Example

Embedding 'I love machine learning' and 'I adore artificial intelligence' produces vectors with cosine similarity ~0.92 — very close. 'I love machine learning' and 'What should I have for dinner?' produces cosine similarity ~0.15 — very different.

Related terms

  • Embedding An embedding is a numerical vector representation of data (text, image, audio) that captures semantic meaning. Similar concepts have embeddings that are close together in vector space, enabling AI systems to measure similarity, cluster data, and retrieve relevant information.
  • Vector Database A vector database stores and retrieves high-dimensional vector embeddings efficiently. It enables semantic similarity search — finding documents, images, or records that are semantically 'nearest' to a query vector. Examples include Pinecone, Weaviate, Qdrant, and ChromaDB.
  • Semantic Search Semantic search retrieves results based on the meaning of a query rather than exact keyword matches. Using vector embeddings, it finds content that is conceptually related to the query, even when no words overlap — solving the vocabulary mismatch problem in traditional search.
  • RAG (Retrieval-Augmented Generation) RAG is a technique that combines an LLM with an external knowledge retrieval system. Instead of relying solely on its training data, the model retrieves relevant documents at query time and uses them as context to generate more accurate, up-to-date responses.