Vector Spaces & Distance Metrics

📌 Searching the Space

We know that an embedding translates text into a vector (a coordinate in a high-dimensional space), and that words with similar meanings are clustered together.

But when a user types a search query (e.g., "Cute puppies"), how does the database actually find the closest match? It doesn't read the words. Instead, it performs heavy mathematical calculations to find the shortest Distance between the user's query vector and the document vectors.

📌 Metric 1: Cosine Similarity (The Industry Standard)

If you are building an AI text application, Cosine Similarity is almost certainly the metric you will use. It is the absolute industry standard for comparing LLM text embeddings.

Diagram showing two vectors on a graph, highlighting the angle between them
Diagram showing two vectors on a graph, highlighting the angle between them

Cosine Similarity measures the angle between two vectors, completely ignoring their magnitude (length). Why is this important?

  • Imagine Document A is a 500-word Wikipedia article about dogs.
  • Imagine Document B is a 5-word tweet: "I love my cute dog."

Because Document A is much longer, its vector might be "longer" (higher magnitude) than Document B. But they are both pointing in the exact same semantic direction (Dogs). Cosine Similarity realizes the angle between them is nearly 0 degrees, and correctly flags them as highly similar, regardless of document length.

📌 Metric 2: Euclidean Distance (L2)

Euclidean Distance is the most intuitive metric for humans. It is literally just measuring the straight-line physical distance between two points with a ruler (think of the Pythagorean theorem from high school geometry).

Diagram showing the direct straight line path between two points on a graph
Diagram showing the direct straight line path between two points on a graph

While Cosine Similarity looks at the direction, Euclidean looks at the absolute position. Because of this, it is highly sensitive to the length (magnitude) of the vectors. If you compare a long document to a short document, Euclidean distance might say they are far apart, even if they are about the exact same topic.

Use Case: Euclidean distance is often used for image embeddings, facial recognition, or when you explicitly want the length/frequency of the data to matter.

📌 Metric 3: Dot Product

The Dot Product is a hybrid calculation. It measures both the angle (direction) AND the magnitude (length) of the vectors.

If your embedding model is "normalized" (meaning all vectors are mathematically forced to be the exact same length of 1.0), then the Dot Product and Cosine Similarity will actually return the exact same result! OpenAI's embeddings are normalized by default, so you can often use the Dot Product because it is computationally faster for computers to calculate than Cosine Similarity.