E-commerce Products
Tutorial 2: E-commerce Product Catalog¶
Build a product search system that combines semantic similarity with exact category matching.
Goal¶
Create a product catalog where: - Name + Description form composite semantic embedding - Category must match exactly (high weight) - Brand is considered but less important
Step 1: Create Configuration¶
Create config/products.yaml:
schema:
name:
type: text
required: true
description:
type: text
required: true
category:
type: text
required: true
brand: text
price:
type: number
required: true
embeddings:
# Name embedding
name_vec:
field: name
model: tfidf
# Description embedding
desc_vec:
field: description
model: tfidf
# Combined product text embedding
product_vec:
combine:
- ref: name_vec
weight: 0.4
- ref: desc_vec
weight: 0.6
similarities:
# Semantic text similarity
text_sim:
embedding: product_vec
# Category must match exactly
category_sim:
field: category
metric: exact
# Brand similarity helps
brand_sim:
field: brand
metric: exact
# Combined similarity
overall:
combine:
- ref: text_sim
weight: 0.6
- ref: category_sim
weight: 0.3
- ref: brand_sim
weight: 0.1
network:
edges:
similarity: overall
min: 0.5
Step 2: Add Products¶
from src.network_rag import NetworkRAG
rag = (NetworkRAG.builder()
.with_storage('products.db')
.with_tfidf_embeddings()
.from_config('config/products.yaml')
.build())
products = [
{
'id': 'mbp14',
'name': 'MacBook Pro 14"',
'description': 'Powerful laptop with M3 Pro chip, perfect for development and creative work',
'category': 'laptops',
'brand': 'Apple',
'price': 1999.00
},
{
'id': 'mbp16',
'name': 'MacBook Pro 16"',
'description': 'Premium laptop with M3 Max chip, exceptional performance for professionals',
'category': 'laptops',
'brand': 'Apple',
'price': 2499.00
},
{
'id': 'xps15',
'name': 'Dell XPS 15',
'description': 'High-performance laptop for developers, great keyboard and screen',
'category': 'laptops',
'brand': 'Dell',
'price': 1799.00
},
{
'id': 'airpods',
'name': 'AirPods Pro',
'description': 'Wireless earbuds with active noise cancellation',
'category': 'audio',
'brand': 'Apple',
'price': 249.00
},
{
'id': 'ipad',
'name': 'iPad Pro 12.9"',
'description': 'Powerful tablet with M2 chip, excellent for creative work and entertainment',
'category': 'tablets',
'brand': 'Apple',
'price': 1099.00
}
]
for product in products:
rag.add(product['id'], document=product)
rag.build_network()
Step 3: Search Products¶
# Search for laptops
print("=== Search: 'powerful laptop for development' ===")
results = rag.search('powerful laptop for development').top(5)
for i, result in enumerate(results, 1):
doc = result.metadata
print(f"\n{i}. {doc['name']} (score: {result.score:.3f})")
print(f" Price: ${doc['price']}")
print(f" Category: {doc['category']}")
print(f" Description: {doc['description'][:60]}...")
Expected output:
1. MacBook Pro 14" (score: 0.782)
Price: $1999.0
Category: laptops
Description: Powerful laptop with M3 Pro chip, perfect for development...
2. Dell XPS 15 (score: 0.765)
Price: $1799.0
Category: laptops
Description: High-performance laptop for developers, great keyboard...
3. MacBook Pro 16" (score: 0.743)
Price: $2499.0
Category: laptops
Description: Premium laptop with M3 Max chip, exceptional performance...
Step 4: Category-Filtered Search¶
# Search only within laptops category
results = (rag.search('Apple products')
.filter(category='laptops')
.top(3))
for result in results:
doc = result.metadata
print(f"{doc['name']}: ${doc['price']}")
Step 5: Find Similar Products¶
# Find products similar to a specific product
similar = rag.get_neighbors('mbp14', k_hops=1)
print(f"\nProducts similar to MacBook Pro 14\":")
for node_id in similar:
node = rag.storage.get_node(node_id)
doc = node['metadata']
print(f" - {doc['name']} ({doc['category']})")