Module 85: Embeddings and Vector Search

Lesson 2 of 5 · 15–25 minutes

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

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));
}

🖱️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.

← PreviousEmbeddings
ProLearners Assist