Introduction
Modern AI systems need to understand similarity and relationships between data. Whether it's recommending products, finding similar documents, or powering semantic search, the foundation lies in embeddings and vector databases.
This post explores:
- What embeddings are and why they matter
- How cosine similarity measures semantic closeness
- Vector databases and pgvector for PostgreSQL
- Building recommendation systems with embeddings
- Practical implementation examples
What Are Embeddings?
The Core Concept
An embedding is a dense vector representation of data (text, images, audio) in a continuous vector space. Instead of treating words or items as discrete symbols, embeddings map them to points in high-dimensional space where semantic similarity is preserved.
Why Embeddings Matter
Traditional databases treat "Paris" and "France" as completely different strings. Embeddings capture their relationship:
- "Paris" ≈ [0.9, 0.1, 0.5, ...]
- "France" ≈ [0.8, 0.2, 0.4, ...] (Mathematically close)
- "Sushi" ≈ [-0.5, 0.9, -0.2, ...] (Far away)
Understanding Semantic Space
The power of embeddings lies in the semantic space they create. This is a high-dimensional vector space where the geometric relationships between points reflect semantic relationships between concepts.
Key Properties of Semantic Space:
- Distance = Similarity: Words with similar meanings are close together in space
- Direction = Relationship: Analogies become vector arithmetic:
CEO - Company + School ≈ Principal - Clusters = Categories: Related concepts naturally group together
Practical Example:
In the visualization above, notice how concepts naturally separate:
- Nature Cluster: Trees, flowers, and forests group together.
- Tech Cluster: Robots, code, and cybernetics form their own region.
- Royalty Cluster: Kings and Queens sit in their own semantic neighborhood.
Cosine Similarity: Measuring Semantic Closeness
The Mathematics
Cosine similarity measures the cosine of the angle between two vectors. It tells us if two concepts are pointing in the "same direction" semantically.
Why It Works
If we compare the vector for "Machine Learning" with others:
- vs. "Deep Learning": 0.85 (Very High Similarity)
- vs. "Cooking Pasta": 0.12 (Low Similarity)
We don't need to manually tag these topics. The math reveals the relationship automatically.
Vector Databases & pgvector
The Challenge
Traditional databases are optimized for exact matches (WHERE id = 5). But with
embeddings, we need to find the "nearest neighbors" in a space with hundreds of dimensions.
Checking every single row is too slow.
Enter pgvector
pgvector is an extension that turns PostgreSQL into a vector database. It allows us to store embeddings and perform similarity searches using standard SQL.
Instead of writing complex Python code to calculate similarity, we can just ask the database:
-- Find the 5 items most similar to our query vector
SELECT name, description
FROM items
ORDER BY embedding <=> '[0.1, 0.5, ...]'::vector
LIMIT 5;
The <=> operator calculates the cosine distance efficiently, using specialized
indexes (IVFFlat or HNSW) to avoid scanning the entire table.
Building Recommendation Systems
How It Works
Once we have embeddings in our database, building a recommendation engine becomes a geometry problem.
1. Content-Based Filtering
"If you liked this article, you'll like these."
We simply take the embedding of the current article and query the database for its nearest neighbors. If the user is reading about "Neural Networks", the nearest neighbors will naturally be other AI articles.
2. User-Based Recommendations
"Based on your reading history..."
We can represent a User as the average of all the content they've interacted with. If a user likes 5 articles about cooking and 1 about tech, their "User Vector" will point strongly towards the "Cooking" cluster.
We then search for items closest to this User Vector that they haven't seen yet.
Advanced Considerations
Index Optimization
pgvector uses IVFFlat (Inverted File with Flat compression) for approximate nearest neighbor search:
- lists parameter: Number of clusters (√(rows) is a good starting point)
- probes: Number of clusters to search (trade-off: speed vs accuracy)
-- Set number of probes for queries
SET ivfflat.probes = 10;
-- More probes = better recall, slower search
-- Fewer probes = faster search, lower recall
Handling Cold Start
For new users/items without interaction history:
- Content features: Use item metadata embeddings
- Popularity: Recommend trending items
- Demographic: Use similar users' preferences
Evaluation Metrics
Measure recommendation quality:
- Precision@K: Fraction of top-K recommendations that are relevant
- Recall@K: Fraction of relevant items in top-K
- NDCG: Normalized Discounted Cumulative Gain (ranking quality)
- MRR: Mean Reciprocal Rank (position of first relevant item)
Conclusion
Embeddings and vector databases form the backbone of modern recommendation systems and semantic search. By representing data as dense vectors and using cosine similarity, we can capture semantic relationships that traditional methods miss.
Key Takeaways:
- Embeddings map data to continuous vector spaces preserving semantic similarity
- Cosine similarity measures the angle between vectors, ideal for high-dimensional data
- pgvector brings vector operations to PostgreSQL with familiar SQL syntax
- Recommendation systems leverage embeddings for content-based and collaborative filtering
- Proper indexing and evaluation are crucial for production systems
The combination of embeddings, vector databases, and similarity search enables intelligent systems that understand context and relationships, powering everything from product recommendations to semantic search engines.