DEV Community

丁久
丁久

Posted on • Originally published at dingjiu1989-hue.github.io

AI Recommendation Systems: From Embeddings to Production

This article was originally published on AI Study Room. For the full version with working code examples and related articles, visit the original post.

AI Recommendation Systems: From Embeddings to Production

Recommendation systems power personalization across e-commerce, media, and SaaS. Modern AI approaches combine embedding-based similarity, collaborative filtering, and LLM-driven reasoning to deliver relevant suggestions at scale.

The Evolution of Recommendations

Traditional recommender systems fell into two camps: collaborative filtering (user-item interactions) and content-based filtering (item attributes). Both have known limitations — cold start for new users, sparse interaction data, and inability to understand semantic meaning.

Embedding-based recommendations solve these problems by representing users and items as dense vectors in a shared semantic space.

How Embedding-Based Recommendations Work

The core idea is simple:

  1. Convert every item into a vector embedding using a model like text-embedding-3-small or BERT
  2. Convert user preferences into the same vector space
  3. Find items whose vectors are closest to the user vector using cosine similarity or dot product
from openai import OpenAI
import numpy as np

client = OpenAI()

def get_embedding(text, model='text-embedding-3-small'):
    resp = client.embeddings.create(input=[text], model=model)
    return np.array(resp.data[0].embedding)

items = ['Python tutorial', 'Advanced ML', 'Web dev with React']
item_embeddings = {item: get_embedding(item) for item in items}

user_query = 'I want to learn programming'
user_emb = get_embedding(user_query)

similarities = {
    item: np.dot(user_emb, emb) / (np.linalg.norm(user_emb) * np.linalg.norm(emb))
    for item, emb in item_embeddings.items()
}
ranked = sorted(similarities.items(), key=lambda x: x[1], reverse=True)
Enter fullscreen mode Exit fullscreen mode

Production Vector Search

For production, never compute cosine similarity in application code. Use a vector database for approximate nearest neighbor (ANN) search: Pinecone, Weaviate, Qdrant, or pgvector for PostgreSQL. These databases index embeddings using HNSW or IVF algorithms, returning top-K results in milliseconds even with millions of vectors.

Hybrid Filtering

Pure embedding similarity misses collaborative signals. Hybrid approaches combine vector search with collaborative filtering using a weighted ensemble:

final_score = 0.6 * embedding_similarity + 0.3 * collaborative_score + 0.1 * popularity_bonus
Enter fullscreen mode Exit fullscreen mode

The weights are tuned via A/B testing. Most production systems use this blended approach.

LLM-Powered Personalization

LLMs add a reasoning layer on top of vector search. Instead of returning raw results, the LLM re-ranks and explains recommendations. This generates personalized descriptions for each recommendation, improving click-through rates by 15-30% in A/B tests.

Cold Start Solutions

New items or users with no history are a classic problem. Embedding-based approaches solve cold start naturally: a new item's embedding is derived from its metadata or content, not from user interactions. For new users, ask 3-5 preference questions during onboarding and convert answers to an embedding vector.

Handling Real-Time Updates

Recommendation systems need to reflect user behavior in real time. Architecture patterns include streaming updates via Kafka to the vector database, session-based embeddings that capture current browsing context, and periodic re-indexing for model updates. The Lambda Architecture pattern — batch layer for offline computation + speed layer for real-time — remains the gold standard.

Evaluation Metrics

Offline metrics help iterate quickly: Precision@K measures relevance fraction in top-K, Recall@K measures coverage, NDCG accounts for ranking quality, and MAP averages precision across users. Always complement offline metrics with online A/B testing.

Business Impact

A media platform using embedding-based recommendations saw a 40% increase in engagement time. An e-commerce site using hybrid filtering reported 25% higher conversion rates. The key insight: embedding-based systems capture semantic relationships that collaborative filtering alone misses, while hybrid approaches maintain the serendipity of collaborative signals.

Summary

Modern recommendation systems combine embeddings for semantic understanding, vector databases for scale, hybrid filtering for collaborative signals, and LLMs for personalization and explanation. Start with simple embedding similarity, add hybrid signals as your data grows, and layer LLM reasoning for the final polish.


Read the full article on AI Study Room for complete code examples, comparison tables, and related resources.

Found this useful? Check out more developer guides and tool comparisons on AI Study Room.

Top comments (0)