What is cosine similarity?

Cosine similarity measures how similar two vectors are by the angle between them, producing a score from -1 to 1 where 1 means they point in exactly the same direction.

It is the standard way to compare embeddings, and its defining property is that it ignores magnitude entirely. Two vectors pointing the same way score identically whether one is twice as long as the other. In practice, scores for text embeddings tend to fall in a narrow band — often 0.6 to 0.9 for anything vaguely related — which is why a raw score is meaningless without knowing the distribution for a given model.

Why it matters

Ignoring length is precisely what retrieval wants. Embedding magnitude tends to track how much text was embedded, so a distance measure that counted it would systematically prefer long passages over short ones. Cosine does not, so a one-sentence answer about delivery times competes fairly with a five-paragraph section covering the same ground — and the short, direct one is usually the better answer to return.

Examples

  • A short FAQ answer beating a long page section on the same topic, correctly.
  • Scores of 0.82 and 0.79 being meaningfully different where 0.30 and 0.28 are not.
  • Two passages saying the same thing at different lengths scoring nearly alike.

Not the same as

Euclidean distance

Euclidean distance measures straight-line separation and does count magnitude, which makes it a poorer default for text.

Related

term

What is an embedding?

Embeddings are why an AI representative can answer "do you deliver to Berlin?" from a page that only says "we ship across the EU". Text is converted to vectors, and similar meaning lands close together.

term

What is semantic search?

Keyword search asks whether two pieces of text share words. Semantic search asks whether they mean the same thing. The difference shows up immediately in customer questions, because customers do not use a company's vocabulary: someone asks 'can I send it back?' about a page headed 'Returns and exchanges', and the two share not one significant word.

term

What is a vector database?

Comparing a question to every stored passage is exact and gets slower in proportion to how many passages there are. A vector database trades a little accuracy for a large speed gain by building an index that skips most of the comparisons. That trade is worth making at millions of vectors and is close to pointless at thousands.

term

What is a confidence score in AI answers?

Two very different things get called confidence. Retrieval confidence measures how close the best-matching passages were to the question — a property of the search step, computed before any answer is written. Model confidence is the model's own assessment of its answer, which it will happily report as high while inventing a price.

term

What is semantic search?

Keyword search asks whether two pieces of text share words. Semantic search asks whether they mean the same thing. The difference shows up immediately in customer questions, because customers do not use a company's vocabulary: someone asks 'can I send it back?' about a page headed 'Returns and exchanges', and the two share not one significant word.

term

What is a vector database?

Comparing a question to every stored passage is exact and gets slower in proportion to how many passages there are. A vector database trades a little accuracy for a large speed gain by building an index that skips most of the comparisons. That trade is worth making at millions of vectors and is close to pointless at thousands.

RubyRep is this, applied to your website.

Give it your URL and it reads your pages, then answers your customers from them.

Train My RubyRep