MORAIDICTIONNAIRE IA
Inférence & Prompting

Cosine Similarity

Measure how close two texts are by the angle between their vectors, not their length.

Cosine similarity measures how much two vectors "point in the same direction." Picture two arrows starting from the origin: their length doesn't matter — only the angle between them does. The smaller the angle, the more semantically alike the two objects are. It is the workhorse for comparing embeddings in AI.

The formula

We compare two vectors $\vec{A}$ and $\vec{B}$ via the cosine of their angle:

$$\text{sim}(\vec{A}, \vec{B}) = \cos(\theta) = \frac{\vec{A} \cdot \vec{B}}{|\vec{A}|\, |\vec{B}|}$$

The dot product in the numerator is normalized by the product of the norms. The result is bounded between -1 and 1:

Value Angle Interpretation
1 Same direction (very similar)
0 90° Orthogonal vectors (unrelated)
-1 180° Opposite directions

With text embeddings, values usually fall between 0 and 1.

Why not Euclidean distance?

Cosine's key strength is that it ignores magnitude. A long text and a short text on the same topic produce vectors of different lengths but similar orientation. Cosine captures meaning, not size — which is why it dominates in:

Comparing two ideas means measuring the angle between their vectors: meaning wins over magnitude.

Explore the full AI dictionary →