Embeddings & Vector Similarity
The building block underneath RAG, semantic search, deduplication, and recommendation.
An embedding model maps a piece of text to a fixed-length vector of floats (commonly 384-3072 dimensions) such that texts with similar meaning end up close together in that vector space, and unrelated texts end up far apart. It's a separate, usually much smaller, model than the LLM you'd chat with — trained specifically so that distance in vector space tracks semantic similarity, not to generate text at all.
from openai import OpenAI
client = OpenAI()
vec = client.embeddings.create(
model="text-embedding-3-small",
input="How do I reset my password?"
).data[0].embedding
print(len(vec)) # 1536 floats
import numpy as np
def cosine_similarity(a, b):
return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
# 1.0 = identical direction, 0.0 = unrelated, -1.0 = opposite
| Consideration | Trade-off |
|---|---|
| Higher dimensionality | Generally better recall, but more storage and slower similarity search at scale |
| General-purpose vs. domain-specific model | Domain-tuned embeddings (e.g. code, legal, medical) usually outperform general models within their domain |
| Matryoshka/truncatable embeddings | Some newer models let you truncate the vector to fewer dimensions with graceful quality degradation, trading a small accuracy hit for less storage |
Brute-force cosine similarity against every stored vector is fine up to roughly tens of thousands of vectors; beyond that, an approximate nearest-neighbor index (HNSW is the common default) trades a small amount of recall for orders-of-magnitude faster lookup. PostgreSQL's pgvector extension implements exactly this — see PostgreSQL Extensions (pgvector, PostGIS) if you'd rather not stand up a dedicated vector database.