Finding the Best Match: Cosine Similarity

cosine similarity retrieval

Part of: Chat With Your PDF

You have a vector for every chunk and, in a moment, a vector for the user's question. Now you need one number that answers "how relevant is this chunk to this question." That number is cosine similarity . It's what turns a pile of vectors into a ranked search result. Why not just compare the raw numbers? You might reach for the dot product: multiply matching entries, sum the results. The catch is that the dot product grows with vector length , not just direction. A long, verbose chunk can score high because its numbers are bigger, not because it answers the question. You want to compare direction and throw away size. Cosine similarity Cosine similarity divides the dot product by the product of both vectors' magnitudes, which cancels out length and leaves only direction: The result always falls between -1 and 1. 1 means the vectors point the same way (near-identical meaning). 0 means unrelated. -1 means opposite. In practice, relevant chunks for a real question land somewhere around 0.2 to 0.6 and rarely near 1. Retrieval is just ranking Retrieval means: embed the question, compute cosine similarity against every stored chunk vector, and take the highest-scoring ones. That's the ent

Challenge: Rank Chunks by Similarity