Measuring similarity

Now that we can turn text into vectors, how do we measure if two pieces of text are similar? The answer is cosine similarity, which measures the angle between two vectors. A smaller angle means more similar meaning.

The cosine similarity scale: 1.0 means identical meaning, 0.0 means completely unrelated, and -1.0 means opposite meaning. In practice, most text pairs fall between 0.2 and 0.8.

Cosine similarity is calculated as the dot product of two vectors divided by the product of their magnitudes (lengths). numpy makes this easy: np.dot() computes the dot product, and np.linalg.norm() computes the magnitude. The result is always between -1 and 1.

Yes, cosine similarity ranges from -1 to 1. A negative score means the vectors point in roughly opposite directions, which can indicate contrasting meanings. In practice with modern embedding models, negative scores are rare for natural text and most pairs fall between 0.2 and 0.8.

02_embeddings.ipynb
python
import numpy as np

def calculate_similarity(text1, text2):
    """Calculate cosine similarity between two texts."""
    resp1 = litellm.embedding(model=EMBEDDING_MODEL, input=[text1])
    resp2 = litellm.embedding(model=EMBEDDING_MODEL, input=[text2])
    vec1 = np.array(resp1.data[0]["embedding"])
    vec2 = np.array(resp2.data[0]["embedding"])
    return np.dot(vec1, vec2) / (np.linalg.norm(vec1) * np.linalg.norm(vec2))

# Compare: "high protein meal" vs menu items
score1 = calculate_similarity("high protein meal", "Quinoa lentil power bowl")
print(f"Protein vs Lentil Bowl: {score1:.4f}")  # ~0.57

score2 = calculate_similarity("high protein meal", "Plastic chair")
print(f"Protein vs Plastic Chair: {score2:.4f}")  # ~0.37

Calculate cosine similarity between food items

Similarity score comparison

Higher scores indicate closer semantic meaning.

Great question! Even unrelated English text shares some features since both use English words, both are noun phrases, etc. The model encodes these shared features too. That's why truly unrelated items still score 0.2-0.4. In practice, you set a threshold (like 0.5) and only consider results above it as "similar."

AI prompt: Try it with AI

Loading practice…

Quiz: Quiz

Loading practice…

Matching exercise: Match similarity concepts

Loading practice…