MMOOZZGG.com

Glossary

What is cosine similarity

In short

Cosine similarity is a measure of how close two vectors are by the angle between them: the smaller the angle, the higher the value, and the more alike the texts these vectors describe.

Search and RAG · cosine similarity

MMOOZZGG.com editorsChecked: 1 min read

For a child

Imagine that you and a friend stand in the middle of a playground and point in a direction. If you both point at the slide, your arms look almost the same way. If you point at the slide and he points at the swings, there is a wide angle between your arms. It does not matter whose arm is longer. Cosine similarity measures how much two directions match.

In plain words

When a text is turned into an embedding, you get an arrow in a space with many axes. Texts with similar meaning give arrows that point almost the same way. This measure compares the direction, not the length of the arrows. A value of 1 means the directions match, 0 means the arrows are perpendicular and the texts are unrelated, and a negative value means they point in different directions. For example, the phrases “the printer broke” and “the multifunction printer will not print” get a high value, while “the printer broke” and “the vacation schedule” get a low one.

For business

Almost every search by meaning uses this formula, and a manager needs to know one thing: the similarity number is not a percentage of correctness. A value of 0.8 means different things in different models, and the threshold for “count this passage as relevant” is tuned on your own data. A wrong threshold is costly: too low pulls junk into the answer, too high leaves the assistant with no material, and it answers “I don’t know”. The calculations themselves are cheap; costs depend on the number of vectors and the way you search them. When a team says “similarity 0.9”, ask on which set of questions the threshold was tested. Such questions are easier to ask if you understand how AI works in general.

For a specialist

The cosine similarity of vectors a and b equals their dot product divided by the product of their norms: cos θ = a·b / (‖a‖ ‖b‖). The value ranges from -1 to 1 and does not depend on the length of the vectors. For unit-length vectors, it matches the dot product, and ranking by it matches ranking by Euclidean distance. The OpenAI embeddings documentation recommends the cosine by default, noting that the choice of distance function usually matters little. Sentence-BERT (Reimers, Gurevych, 2019) compares sentence embeddings with the cosine. Limits: Steck, Ekanadham and Kallus (2024) showed analytically on regularized linear models that the cosine similarity of learned embeddings can give arbitrary values that depend on the choice of regularization, and they urged not to use it without checks. In vector databases, the cosine is often implemented by normalizing vectors at write time and using the dot product at search time, which simplifies approximate search indexes.

Example

An assistant gets the question “how do I file sick leave” and compares its vector with the vectors of all passages in the base. The passage from the policy on temporary disability gets a higher similarity than the rest and goes into the answer.

Related terms

Questions and answers

What does a cosine similarity of 0.8 mean? That the vectors of two texts point almost the same way. But you can compare this number only within one model: in different models, the same values mean different degrees of likeness.
How does cosine similarity differ from the dot product? The dot product depends on both the angle and the length of the vectors, while the cosine depends only on the angle. If the vectors are normalized to unit length, the two measures match.

Sources

Not sure whether your company needs this?

AI solutions map
Contact us