ereztobias/CatalogueSearch
CatalogueSearch with ChromaDB + RAG
AI-powered semantic search for catalogues using ChromaDB, vector embeddings, and RAG (Retrieval-Augmented Generation) with Claude AI.
Features
- Semantic Search: ChromaDB vector database with 26,818+ clothing items
- Smart Filtering: Automatic extraction of gender, category, color, and stock filters
- RAG Mode: AI-powered recommendations using Claude API
- Web UI: Flask-based visual search interface
- Image Cache: Separate JSON cache for product images
- Persistent Storage: Local vector database with metadata filtering
- 50% Match Threshold: Only shows highly relevant results
Installation
- Install dependencies:
pip install -r requirements.txt- Set up environment variables (for RAG mode):
# Copy the example file
cp .env.example .env
# Edit .env and add your Anthropic API key
# Get your key from: https://console.anthropic.com/
ANTHROPIC_API_KEY=your-key-hereQuick Start
Load the Catalogue
First, load the clothing catalogue into ChromaDB:
python load_by_category.pyThis will fetch and embed 26,818 products across 5 categories:
- Shirts (16,625 items)
- Pants (5,518 items)
- Dresses (2,606 items)
- Jeans (1,400 items)
- Skirts (669 items)
Build Image Cache (Optional)
Build the image cache for the web UI:
python build_image_cache.pyRun the Web UI
Launch the Flask web interface:
python browse_ui.pyThen open http://localhost:5000 in your browser.
Features:
- Standard semantic search
- RAG mode with AI recommendations (toggle checkbox)
- Product images and details
- Filter detection (gender, category, color, stock)
- Match percentage display
Search Modes
1. Semantic Search (Default)
Pure vector similarity search with smart filtering:
python smart_search.pyExample queries:
- "blue jeans for women"
- "casual shirts for men"
- "summer dress available in stock"
2. RAG Mode (AI-Powered)
Retrieval + AI-generated recommendations:
python rag_search.pyHow it works:
- Retrieves relevant products using semantic search
- Sends top matches to Claude API
- Generates personalized, conversational recommendations
- Returns both AI recommendation AND product results
Example query → AI response:
"pants for a conference for women" "For a professional conference setting, I'd recommend these tailored options... The gray suit trousers from Brand X offer excellent quality at £89, currently on sale from £125..."
Basic Usage
from chroma_search import CatalogueSearch
# Initialize search
search = CatalogueSearch()
# Add items to catalogue
items = [
{
"id": "prod_001",
"text": "Premium wireless Bluetooth headphones with noise cancellation",
"category": "electronics",
"price": "199.99"
},
{
"id": "prod_002",
"text": "Organic cotton t-shirt, eco-friendly",
"category": "clothing",
"price": "29.99"
}
]
search.add_items(items)
# Search the catalogue
results = search.search("headphones for music", n_results=5)
for doc, distance in zip(results["documents"], results["distances"]):
print(f"{doc} (similarity: {1 - distance:.3f})")Run the Example
python chroma_search.pyThis will:
- Create a ChromaDB collection
- Add sample catalogue items
- Demonstrate semantic search with different queries
- Show how to use metadata filters
API Reference
CatalogueSearch
__init__(collection_name, persist_directory)
Initialize the ChromaDB client and collection.
collection_name(str): Name for the collection (default: "catalogue_items")persist_directory(str): Directory to persist data (default: "./chroma_db")
add_items(items)
Add items to the catalogue.
items(List[Dict]): List of items with 'id', 'text', and optional metadata
search(query, n_results, filter_metadata)
Search the catalogue using semantic similarity.
query(str): Search query textn_results(int): Number of results to return (default: 5)filter_metadata(Dict): Optional metadata filter
Returns a dictionary with:
ids: List of matching item IDsdocuments: List of matching documentsdistances: List of cosine distances (lower = more similar)metadatas: List of metadata dictionaries
delete_items(ids)
Delete items by ID.
ids(List[str]): List of item IDs to delete
clear_collection()
Clear all items from the collection.
get_collection_stats()
Get collection statistics.
Advanced Usage
Metadata Filtering
# Search only within a specific category
results = search.search(
"gadgets",
n_results=5,
filter_metadata={"category": "electronics"}
)Custom Collection
# Use a custom collection name and storage location
search = CatalogueSearch(
collection_name="my_products",
persist_directory="./my_db"
)How It Works
- Vector Embeddings: Text descriptions are automatically converted to vector embeddings using sentence transformers
- Similarity Search: Queries are embedded and compared using cosine similarity
- Persistence: All data is stored locally in the ChromaDB directory
- Semantic Understanding: The system understands meaning, not just keywords (e.g., "headphones" matches "audio equipment")
Data Structure
Each catalogue item should have:
id(required): Unique identifiertext(required): Description or content to search- Additional fields: Any other metadata (category, price, etc.)
Performance Tips
- Add items in batches for better performance
- Use metadata filters to narrow search scope
- The first query may be slower as models load
- Subsequent queries are much faster
Architecture
Semantic Search Flow
- Query → Extract filters (gender, category, color, stock)
- Clean query (remove filter keywords)
- Embed query with
all-MiniLM-L6-v2(384 dimensions) - Search ChromaDB with cosine similarity
- Apply metadata filters
- Filter results (≥50% match)
- Return products
RAG Flow
- Retrieval: Semantic search (above)
- Augmentation: Format top 5 products as context
- Generation: Send to Claude API with system prompt
- Output: Natural language recommendation + product results
Matching Score
Match % = (1 - cosine_distance) × 100
- 90-100%: Very strong semantic match
- 70-90%: Good semantic match
- 50-70%: Moderate match
- <50%: Filtered out (not shown)
Project Structure
CatalogueSearch/
├── chroma_search.py # Core ChromaDB wrapper
├── smart_search.py # Semantic search + auto-filtering
├── rag_search.py # RAG with Claude API
├── load_by_category.py # Load 26K+ products from API
├── build_image_cache.py # Build image cache
├── browse_ui.py # Flask web UI
├── api_helper.py # API utilities
├── search_demo.py # CLI demo
├── chroma_db/ # Vector database (persistent)
├── image_cache.json # Product images cache
├── .env # API keys (not in git)
└── requirements.txt # DependenciesDependencies
chromadb: Vector databasesentence-transformers: Text embedding models (all-MiniLM-L6-v2)anthropic: Claude API client for RAGflask: Web UI frameworkrequests: API callspython-dotenv: Environment configuration
Persistence
Data persists in the ./chroma_db directory by default. This directory contains:
- Vector embeddings
- Documents
- Metadata
- Index structures
License
MIT
