What is Cosine Similarity?
Cosine similarity is a mathematical formula used to measure the angle between two vectors in a multi-dimensional space. In AI, it is commonly used to estimate how similar two pieces of text are based on their embeddings.
How does it work?
When text is converted into an embedding (a list of numbers), it becomes a point in space. Cosine similarity calculates the angle between a line drawn to the first point and a line drawn to the second.
- A cosine similarity of 1 means the vectors point in the exact same direction (highly similar).
- A value of 0 means they are perpendicular (unrelated).
- A value of -1 means they point in opposite directions.
What is a common misconception?
Do not claim that a high cosine similarity directly proves that two pieces of text have the exact same factual meaning. It only means they use similar language in similar contexts. "The dog chased the cat" and "The cat chased the dog" might have very high cosine similarity despite meaning different things.
Why does it matter?
Cosine similarity is the mathematical engine behind most semantic search and RAG applications. It provides a fast, computationally efficient way to rank thousands of documents based on their relevance to a user's prompt.