StackPractices
intermediate By Mathias Paulenko

Compare Text Semantic Similarity with OpenAI Embeddings

Generate text embeddings with OpenAI and compute cosine similarity to measure semantic similarity between texts for search, dedup, and clustering

Topics: ai

An embedding model turns a piece of text into a long numeric vector that encodes its meaning, and cosine similarity compares the angle between two of those vectors — the smaller the angle, the more alike the texts. This recipe covers three things: generating embeddings with OpenAI, scoring pairs with cosine similarity, and wiring both into a small similarity search index.

When to Use This

  • Semantic search over a corpus of documents
  • Deduplication of semantically similar content
  • Clustering texts by meaning
  • Recommendation systems based on content similarity

Skip this approach when you only need exact-match or keyword filtering — a LIKE query or a full-text index is cheaper and faster. For corpora above roughly 100k documents, a dedicated vector database fits better than a brute-force in-memory index.

Prerequisites

  • Python 3.10+
  • openai package (pip install openai)
  • numpy package (pip install numpy)
  • An OpenAI API key

Solution

1. Install Dependencies

pip install openai numpy

2. Generate Embeddings

from openai import OpenAI
import numpy as np

client = OpenAI()

def get_embedding(text: str, model: str = "text-embedding-3-small") -> list[float]:
    """Generate an embedding for a single text.

    Args:
        text: Input text.
        model: OpenAI embedding model.

    Returns:
        Embedding vector as a list of floats.
    """
    response = client.embeddings.create(model=model, input=text)
    return response.data[0].embedding

def get_embeddings_batch(texts: list[str], model: str = "text-embedding-3-small") -> list[list[float]]:
    """Generate embeddings for multiple texts in one API call.

    Args:
        texts: List of input texts.
        model: OpenAI embedding model.

    Returns:
        List of embedding vectors.
    """
    response = client.embeddings.create(model=model, input=texts)
    return [item.embedding for item in response.data]

3. Compute Cosine Similarity

def cosine_similarity(vec_a: list[float], vec_b: list[float]) -> float:
    """Compute cosine similarity between two vectors.

    Args:
        vec_a: First vector.
        vec_b: Second vector.

    Returns:
        Similarity score between -1 and 1 (1 = identical).
    """
    a = np.array(vec_a)
    b = np.array(vec_b)
    dot = np.dot(a, b)
    norm_a = np.linalg.norm(a)
    norm_b = np.linalg.norm(b)
    if norm_a == 0 or norm_b == 0:
        return 0.0
    return float(dot / (norm_a * norm_b))

def cosine_similarity_matrix(vectors: list[list[float]]) -> np.ndarray:
    """Compute pairwise cosine similarity for a list of vectors.

    Args:
        vectors: List of embedding vectors.

    Returns:
        NxN similarity matrix.
    """
    matrix = np.array(vectors)
    norms = np.linalg.norm(matrix, axis=1, keepdims=True)
    normalized = np.divide(matrix, norms, out=np.zeros_like(matrix), where=norms > 0)
    return normalized @ normalized.T

4. Compare Two Texts

text_a = "Redis is an in-memory data structure store used for caching."
text_b = "Redis works as a fast cache by keeping data in memory."
text_c = "PostgreSQL is a relational database with ACID compliance."

emb_a = get_embedding(text_a)
emb_b = get_embedding(text_b)
emb_c = get_embedding(text_c)

sim_ab = cosine_similarity(emb_a, emb_b)
sim_ac = cosine_similarity(emb_a, emb_c)

print(f"Similarity (a, b): {sim_ab:.4f}")  # High — same topic
print(f"Similarity (a, c): {sim_ac:.4f}")  # Lower — different topics

5. Build a Similarity Search Index

from dataclasses import dataclass

@dataclass
class Document:
    id: str
    text: str
    embedding: list[float]

class SimilaritySearchIndex:
    def __init__(self):
        self.documents: list[Document] = []

    def add(self, doc_id: str, text: str) -> None:
        """Add a document to the index."""
        embedding = get_embedding(text)
        self.documents.append(Document(id=doc_id, text=text, embedding=embedding))

    def add_batch(self, items: list[tuple[str, str]]) -> None:
        """Add multiple documents efficiently with batch embedding."""
        texts = [text for _, text in items]
        embeddings = get_embeddings_batch(texts)
        for (doc_id, text), emb in zip(items, embeddings):
            self.documents.append(Document(id=doc_id, text=text, embedding=emb))

    def search(self, query: str, top_k: int = 5) -> list[dict]:
        """Search for the most similar documents.

        Args:
            query: Query text.
            top_k: Number of results to return.

        Returns:
            List of {id, text, score} dicts sorted by similarity.
        """
        query_emb = get_embedding(query)
        scores = [
            (doc.id, doc.text, cosine_similarity(query_emb, doc.embedding))
            for doc in self.documents
        ]
        scores.sort(key=lambda x: x[2], reverse=True)
        return [
            {"id": doc_id, "text": text, "score": score}
            for doc_id, text, score in scores[:top_k]
        ]

# Usage
index = SimilaritySearchIndex()
index.add_batch([
    ("doc1", "Redis is an in-memory data structure store for caching."),
    ("doc2", "PostgreSQL is a capable open-source relational database."),
    ("doc3", "Docker containers package applications with dependencies."),
    ("doc4", "Memcached is a distributed memory caching system."),
    ("doc5", "Kubernetes orchestrates containerized applications."),
])

results = index.search("caching solution for fast lookups", top_k=3)
for r in results:
    print(f"Score: {r['score']:.4f} | {r['text'][:60]}")

6. Pairwise Similarity Matrix

def print_similarity_matrix(texts: list[str]) -> None:
    """Print a pairwise similarity matrix for a list of texts."""
    embeddings = get_embeddings_batch(texts)
    matrix = cosine_similarity_matrix(embeddings)

    print(" " * 8, end="")
    for i in range(len(texts)):
        print(f" doc{i:>3d}", end="")
    print()

    for i, row in enumerate(matrix):
        print(f"doc{i:<4d}", end="")
        for val in row:
            print(f" {val:6.2f}", end="")
        print()

texts = [
    "Redis caching strategies",
    "In-memory cache with Redis",
    "PostgreSQL database indexing",
    "Docker container deployment",
]
print_similarity_matrix(texts)

How It Works

flowchart diagram: Input texts
  1. Embeddings convert text into a high-dimensional vector (1536 dimensions for text-embedding-3-small) where semantically similar texts are close together in vector space.
  2. Cosine similarity is the cosine of the angle between two vectors. Scores run from -1 (opposite meanings) to 1 (identical), though related text usually lands between 0.5 and 1.0.
  3. Batch embedding packs up to 2,048 texts into one API call, which cuts both latency and cost versus calling once per text.
  4. Similarity matrix L2-normalizes every vector and then takes the dot product — equivalent to cosine similarity on unit vectors — producing an NxN matrix whose cell matrix[i][j] holds the score for texts i and j.

Choosing a Distance Metric

MetricFormulaWhen it fitsWatch out
Cosine similaritydot(a, b) / (‖a‖ · ‖b‖)Default for text embeddingsRange -1 to 1; ignores vector magnitude
Dot productdot(a, b)Vectors already L2-normalizedEquals cosine only on unit vectors
Euclidean distance‖a - b‖Clustering in a normalized spaceSensitive to magnitude; lower is better

Cosine is the safe default for semantic comparison because embedding magnitude carries little meaning — two texts about the same topic can produce vectors of different lengths. Euclidean distance only makes sense after normalizing, at which point it ranks identically to cosine.

Handling API Errors

embeddings.create raises openai.RateLimitError when you exceed your quota and openai.APIError on server failures. For large corpora, chunk the input into batches of a few hundred texts and retry with exponential backoff instead of failing the whole job:

import time
from openai import RateLimitError

def embed_with_retry(texts: list[str], retries: int = 3) -> list[list[float]]:
    """Embed texts, retrying on rate limits with exponential backoff."""
    for attempt in range(retries):
        try:
            return get_embeddings_batch(texts)
        except RateLimitError:
            if attempt == retries - 1:
                raise
            time.sleep(2 ** attempt)
    return []

Variants

Euclidean Distance

def euclidean_distance(vec_a: list[float], vec_b: list[float]) -> float:
    """Compute Euclidean distance between two vectors."""
    a = np.array(vec_a)
    b = np.array(vec_b)
    return float(np.linalg.norm(a - b))

Threshold-Based Deduplication

def find_duplicates(texts: list[str], threshold: float = 0.92) -> list[tuple[int, int, float]]:
    """Find pairs of texts that are semantically duplicates.

    Args:
        texts: List of texts to check.
        threshold: Similarity threshold for duplicate detection.

    Returns:
        List of (index_a, index_b, similarity) tuples.
    """
    embeddings = get_embeddings_batch(texts)
    matrix = cosine_similarity_matrix(embeddings)

    duplicates = []
    for i in range(len(texts)):
        for j in range(i + 1, len(texts)):
            if matrix[i][j] >= threshold:
                duplicates.append((i, j, float(matrix[i][j])))

    return duplicates

dups = find_duplicates([
    "Redis is a fast in-memory cache.",
    "Redis works as an in-memory caching store.",
    "PostgreSQL is a relational database.",
])
# [(0, 1, 0.94)] — texts 0 and 1 are duplicates

Using HuggingFace Embeddings (No API Key)

from sentence_transformers import SentenceTransformer

class LocalSimilarityIndex:
    def __init__(self, model_name: str = "all-MiniLM-L6-v2"):
        self.model = SentenceTransformer(model_name)
        self.documents = []

    def add(self, doc_id: str, text: str) -> None:
        embedding = self.model.encode(text).tolist()
        self.documents.append(Document(id=doc_id, text=text, embedding=embedding))

    def search(self, query: str, top_k: int = 5) -> list[dict]:
        query_emb = self.model.encode(query).tolist()
        scores = [
            (doc.id, doc.text, cosine_similarity(query_emb, doc.embedding))
            for doc in self.documents
        ]
        scores.sort(key=lambda x: x[2], reverse=True)
        return [{"id": d, "text": t, "score": s} for d, t, s in scores[:top_k]]

Best Practices

  • For a deeper guide, see Store and Query Embeddings in Pinecone Vector Database.

  • Use the batch embedding API — up to 2,048 inputs per call cuts cost and latency by roughly 10x for large corpora

  • Normalize embeddings before storage — cosine similarity on normalized vectors is a simple dot product

  • Use text-embedding-3-small for most tasks — good balance of quality and cost ($0.02 per 1M tokens)

  • Set a similarity threshold for dedup — 0.90-0.95 is typical for semantic duplicates

  • Cache embeddings by content hash — re-embedding identical texts wastes tokens; see the Embedding Cache pattern for a storage-friendly design

Common Mistakes

  • Comparing embeddings from different models — each model has a different vector space; never mix embeddings from different models
  • Using Euclidean distance without normalization — cosine similarity is more reliable for text because it’s magnitude-invariant
  • Not batching API calls — one-by-one embedding calls are slow and expensive
  • Ignoring token limits — text-embedding-3-small has an 8191 token limit; truncate long texts

Frequently Asked Questions

What is a good cosine similarity threshold for "similar" texts?

0.80+ is typically similar, 0.90+ is near-duplicate, 0.70-0.80 is topically related — treat those numbers as a starting point and calibrate against your own corpus.

Should I use text-embedding-3-small or text-embedding-3-large?

Start with small (1536 dims, cheaper) and switch to large (3072 dims) only when small misses fine-grained distinctions you care about. Check current dimensions and pricing in the OpenAI embeddings guide.

Can I cache embeddings to avoid re-computing?

Yes — store them in a database or file keyed by a hash of the input text, and only re-embed when the source text changes.

How do I handle very long documents?

Split the document into segments of roughly 500-1000 tokens, embed each chunk separately, and average the chunk embeddings into a single document-level vector.

Is cosine similarity different from the dot product?

Cosine similarity is just the dot product computed on L2-normalized vectors. OpenAI embedding models already return unit-norm vectors, so both metrics rank identically with this provider — computing cosine explicitly keeps the code portable to providers that don't normalize.

Does the similarity threshold transfer across models?

No — scores aren't comparable across models, so a 0.90 threshold tuned on text-embedding-3-small means something different on all-MiniLM-L6-v2. Calibrate thresholds on your own data for each model.

What happens if I embed an empty or whitespace-only string?

The API still returns a vector for it, but it carries no real meaning — filter empty inputs before calling the API so they don't end up as junk entries in the index.

Companion code: similarity search project: the index, distance metrics, and dedup helpers packaged as importable modules, plus a runnable main.py.