Back to glossary

Term

Cosine Similarity

Cosine similarity measures how similar two vectors are by the angle between them — the default metric in RAG retrieval for comparing query embeddings against the embeddings stored in a vector index.

Cosine Similarity — explained in more detail

Mathematically, cosine similarity is the dot product of two vectors divided by the product of their magnitudes — which is the cosine of the angle between them. Values range from -1 (opposite direction) to 1 (identical direction); 0 means orthogonal, i.e. no similarity. The key point: only the direction of the vectors matters, not their length. That makes the metric robust against scale effects — two texts of different lengths but similar meaning produce embeddings with a high cosine score even though their vector magnitudes differ.

Example / Practical context

In a RAG pipeline, the user query is converted into a vector (e.g. 1536-dimensional for OpenAI text-embedding-3-small) and compared against every chunk vector in the index via cosine similarity. The top-k with the highest scores land in the LLM context. When all vectors are normalised to length 1 (the norm in modern embedding models), cosine similarity is computationally identical to the dot product — vector databases like Pinecone or Qdrant exploit that equivalence for speed.

Euclidean distance measures geometric distance and depends on vector magnitudes — for normalised vectors it produces the same ranking as cosine similarity. Dot product is a different metric for non-normalised vectors and favours longer ones. Jaccard similarity operates on sets rather than vectors and is more typical of token-based search.

Entdecke mehr

Themenuebersicht