0Pricing
LangChain / RAG / Vector DBs · Lesson

Measuring Embedding Similarity

Understand the distance and similarity metrics that power vector search and how to choose the right one.

From Vectors to Meaning

An embedding maps text to a list of numbers in high-dimensional space. Texts with similar meaning land close together. To rank results we need a way to measure that closeness.

Cosine Similarity

Cosine similarity measures the angle between two vectors, ignoring their length. It ranges from -1 (opposite) to 1 (identical direction).

import numpy as np

def cosine(a, b):
    a, b = np.array(a), np.array(b)
    return a.dot(b) / (np.linalg.norm(a) * np.linalg.norm(b))

print(cosine([1, 0], [1, 1]))  # ~0.707

All lessons in this course

  1. Understanding Text Embeddings
  2. Introduction to Vector Databases
  3. Storing and Retrieving Embeddings
  4. Measuring Embedding Similarity
← Back to LangChain / RAG / Vector DBs