Module 85: Embeddings and Vector Search
Similarity
🏠The everyday picture
Two people pointing in nearly the same direction are "agreeing"; pointing in opposite directions, they "disagree". How closely their arms line up is the measure.
📘What it really is
- To compare two embeddings, measure how close the vectors are. The most common measure is cosine similarity: how closely two vectors point in the same direction.
- near 1: very similar meaning;
- near 0: unrelated;
-
(negative values are possible but less common with text embeddings).
-
In TypeScript:
function cosineSimilarity(a: number[], b: number[]): number {
let dot = 0, normA = 0, normB = 0;
for (let i = 0; i < a.length; i++) {
dot += a[i] * b[i];
normA += a[i] * a[i];
normB += b[i] * b[i];
}
return dot / (Math.sqrt(normA) * Math.sqrt(normB));
}
-
Many embedding models return normalised vectors (length 1), in which case cosine similarity equals the simple dot product, which is faster.
-
Similarity isn't truth: close vectors mean "about the same topic", not "answers the question correctly". A passage saying "returns are not accepted on tailored items" is close to a question about returning tailored items, which is good (it's relevant), but the model must read it carefully.
-
Scores depend on the model; there's no universal "good" threshold. Find a sensible cut-off by testing (Lesson 5).
🖱️Try it
Compute the cosine similarity between every pair of your five sentences from Lesson 1, and print a small table. Check that the returns pair and the delivery pair score highest. Then embed a new question, "Can I send back a kurta I bought?", and rank the five sentences by similarity to it.
✅Quick check
What does a cosine similarity near 1 mean?
Show the answer
The two texts have very similar meanings.
Does high similarity mean a passage answers the question?
Show the answer
No. It means it's about the same topic; the answer still has to be read and checked.
