GuidesAI and e-commerce

Tutorial: product recommendations with embeddings

By the Banzai Labs teamUpdated on 2 min read

Viewed product0.920.880.810.77The closest in meaning, suggested alongside
Around the viewed product, the closest products in meaning, with their similarity score.

1. The text to embed

const text = [p.title, p.category, p.material, p.description].filter(Boolean).join('. ');

No price or stock in this text: those are filters, not meaning.

2. Storage

CREATE EXTENSION IF NOT EXISTS vector;
ALTER TABLE products ADD COLUMN embedding vector(1024); -- the chosen model's dimension
const v = await embed(text); // call to the embedding model
await db.query('UPDATE products SET embedding = $1 WHERE id = $2', [JSON.stringify(v), p.id]);

3. The closest ones

SELECT id, title
FROM products
WHERE id <> $1 AND in_stock
ORDER BY embedding <=> (SELECT embedding FROM products WHERE id = $1)
LIMIT 6;

pgvector’s <=> operator measures cosine distance: the smaller it is, the more alike the products.

4. Useful settings

  • Same category or notSimilar (another dress) or complementary (shoes).
  • StockNever recommend a sold-out product.
  • MeasurementClicks and add-to-carts from the block.

Vectors: embeddings explained. On the same base: semantic search.

Frequently asked questions

Which embedding model should I use?
A text embedding model, hosted (Voyage AI, OpenAI, Mistral…) or open source. It must handle the language of your product pages well. Use the same model for every product.
What about customer purchases?
“Bought together” recommendations come from order history. Embeddings help when that history is missing: new products, small catalogue.
How often do I recompute?
Only when a product's text changes. A new product is embedded when it is created.

Read next

All guides