Generative AIby Dhruv Narang

Three Ways to Score Similarity

To find the best chunk we need a score: one number that says how similar two vectors are. There are three common ways. Look at two vectors as arrows from the same point.

a = (3, 1)b = (2, 2)anglestraight line between the tips
Cosine similarity
The angle
How much the arrows point the same way. Length is ignored. Bigger is closer.
Dot product
Angle and length
Pointing the same way and being long both raise the score. Bigger is closer.
Euclidean distance (L2)
The straight line
How far apart the two tips are, like a ruler. Smaller is closer.

Cosine Similarity

Cosine looks only at the angle between two arrows. The length of the arrows does not matter.

a = (3, 1)b = (2, 2)angle
The angle between the two arrows. The length does not matter. Cosine = 0.894.
The formula
cos(θ) = (a · b) / (|a| × |b|)
Our arrows
a = (3, 1) and b = (2, 2)
Step 1: dot product
3×2 + 1×2 = 8
Step 2: lengths
|a| = √10 = 3.162   |b| = √8 = 2.828
Step 3: divide
8 / (3.162 × 2.828) = 0.894
What it means
The angle is about 26.6 degrees. The score runs from -1 (opposite) through 0 (unrelated) to 1 (same direction).
Length does not matter. Make b twice as long, b = (4, 4), and the cosine is still 0.894. Only the direction counts. That is why cosine is the favourite for text.
ChunkVectorPoints, compared with the question (0.6, 0.8)Cosine
Chunk 1[0.8, 0.6]the same way0.96
Chunk 2[-0.6, 0.8]almost sideways0.28
Chunk 3[0.8, -0.6]exactly sideways0
Chunk 4[-0.8, -0.6]the opposite way-0.96
Chunk 1, twice as long[1.6, 1.2]the same way, but longer0.96
Same direction gives 1, sideways gives 0, the opposite way gives -1. The last row is Chunk 1 made twice as long: the cosine does not change.

Dot Product

The dot product multiplies the two vectors number by number and adds it up. It looks at the angle and the length.

a = (3, 1)b = (2, 2)reach
Gold: how far b reaches along a. A longer reach, or a longer a, gives a bigger dot product.
The formula
a · b = a1×b1 + a2×b2 + …
Our arrows
3×2 + 1×2 = 8
Longer b = (4, 4)
3×4 + 1×4 = 16. The score doubled, although the direction did not change.
Bigger is closer
A higher dot product means more similar. Longer vectors score higher.
The link to cosine: if both vectors have length 1, the lengths in the cosine formula are 1 and 1, so dot product = cosine. Normalized embeddings make the dot product a fast way to compute cosine.
ChunkVectorPoints, compared with the question (0.6, 0.8)Dot product
Chunk 1[0.8, 0.6]the same way0.96
Chunk 2[-0.6, 0.8]almost sideways0.28
Chunk 3[0.8, -0.6]exactly sideways0
Chunk 4[-0.8, -0.6]the opposite way-0.96
Chunk 1, twice as long[1.6, 1.2]the same way, but longer1.92
Same direction gives a big positive number, sideways gives 0, the opposite way gives a negative number. The last row is Chunk 1 made twice as long: the score doubles.

Euclidean Distance

The straight-line distance between the tips of the two arrows. Smaller means closer.

a = (3, 1)b = (2, 2)1.41
Gold: the straight line between the two tips. Smaller means closer.
The formula
distance = √((a1−b1)² + (a2−b2)² + …)
Our arrows
√((3−2)² + (1−2)²) = √2 = 1.414
Longer b = (4, 4)
√((3−4)² + (1−4)²) = √10 = 3.162. The distance grew, although the direction is the same.
Smaller is closer
A distance of 0 means the same point. This is the opposite of cosine and dot product, where bigger is closer.
The link to cosine: for vectors of length 1, distance² = 2 − 2 × cosine. So the smaller the distance, the bigger the cosine. Same order.
ChunkVectorPoints, compared with the question (0.6, 0.8)Distance
Chunk 1[0.8, 0.6]the same way0.28
Chunk 2[-0.6, 0.8]almost sideways1.20
Chunk 3[0.8, -0.6]exactly sideways1.41
Chunk 4[-0.8, -0.6]the opposite way1.98
Chunk 1, twice as long[1.6, 1.2]the same way, but longer1.08
Smaller is closer. The last row is Chunk 1 made twice as long: it points the same way, but its tip is farther from the question, so the distance is bigger.

Which One Should You Use?

For normalized embeddings, all three give the same ranking.

ChunkCosine (bigger is closer)Dot product (bigger is closer)Distance (smaller is closer)
Chunk A0.9950.9950.098
Chunk B0.7450.7450.714
Chunk C-0.167-0.1671.528
Same order: Chunk A, then Chunk B, then Chunk C. All three rank the chunks the same way, because the vectors are normalized. Only the numbers look different.
Toy vectors of length 1.
Which one for you? Use the one your embedding model's docs recommend. Many embedding APIs return vectors of length 1 (OpenAI says so in its Embeddings FAQ), and for those, all three give the same ranking.

Where Do You Set It?

So far we only compared the three ways. In a real project you choose one when you create the collection in your vector database. Every vector database has this setting, but each one gives it a different name.

Vector databaseName of the settingHow you write itChoices
Chromaspaceconfiguration={"hnsw": {"space": "cosine"}} when the collection is createdcosine, ip (dot product), l2 (the default)
Pineconemetriccreate_index(…, metric="cosine", …)cosine (the default), dotproduct, euclidean
QdrantdistanceVectorParams(size=1536, distance=Distance.COSINE)COSINE, DOT, EUCLID, MANHATTAN
Same idea, three names. space is only the name Chroma gives to this setting. It is not a Python or LangChain word. Pinecone calls it metric and Qdrant calls it distance. It tells the database which of the three ways to use when it scores and ranks.
Set it once, when you create the collection. In Chroma you cannot change space later. To change it, you create a new collection. With LangChain you pass the same thing: Chroma(…, collection_configuration={"hnsw": {"space": "cosine"}}).