DEV Community

Pratik
Pratik

Posted on

Redis AI Challenge: Real-Time AI Product Recommender

Redis AI Challenge: Real-Time AI Innovators

Overview
For the Redis AI Challenge, I built a Real-Time AI Product Recommender, a single-page web application that delivers personalized e-commerce product recommendations using Redis 8’s advanced features. The app leverages Redis vector search for AI-driven recommendations, Redis JSON for rich product storage, Redis Streams for real-time interaction logging, and full-text search for dynamic product discovery. A semantic caching layer optimizes performance, and a modern UI with real-time analytics enhances the user experience.
This project addresses the Real-Time AI Innovators prompt by providing low-latency recommendations, dynamic search, and live analytics (e.g., cache hit rate and popular products). It overcomes a critical rating error in the Redis schema, ensuring robust functionality, and includes a debug interface for troubleshooting.
Features

AI-Powered Recommendations: Uses Redis HNSW vector search to find similar products based on embeddings of product names and descriptions.
Real-Time Analytics: Displays cache hit rate and top products via a dynamic progress bar and list, updated using Redis Streams.
Full-Text Search: Allows users to search products by name or description with Redis full-text search.
Semantic Caching: Stores recommendation results in Redis with a one-hour TTL, improving performance with cache hit/miss tracking.
Dynamic UI: Single-page app with Tailwind CSS, Font Awesome icons, search bar, category filter, demo mode, and debug toggle.
Error Handling: Resolved an Unknown argument 'rating' error by fixing the Redis schema and added a debug interface for schema inspection.

Redis Features Utilized

Redis Vector Search (HNSW): Performs KNN similarity searches on 128-dimensional product embeddings for accurate recommendations.
Redis JSON: Stores product data (name, category, brand, description, price, rating) for efficient retrieval.
Redis Streams: Logs user interactions (e.g., recommendation requests) for real-time analytics.
Redis Full-Text Search: Enables dynamic product search by name or description.
Redis Key-Value: Manages semantic caching and tracks cache hits/misses.

Real-Time Aspects

Low-Latency Recommendations: Fetch API delivers vector search results in milliseconds, with semantic caching reducing latency for repeated queries.
Dynamic Analytics: Updates cache hit rate and top products list in real-time using Redis Streams data.
Interactive UI: Search and category filters update the product dropdown instantly without page reloads, with toast notifications for user feedback.

Challenges Faced and Solutions
Challenge: Encountered an error where the Redis schema rejected the rating field during index creation.
Solution:

Identified the issue in the schema definition for the search index.
Fixed by restructuring the schema to explicitly define rating as a numeric, sortable field.
Added a debug interface (accessible via a “Toggle Debug” button) to inspect the Redis schema, ensuring correct configuration with fields like name (TEXT), category (TAG), brand (TEXT), description (TEXT), price (NUMERIC), rating (NUMERIC), and embedding (VECTOR).

Challenge: Form reset unexpectedly when searching or filtering categories, showing a “Form cleared” alert.
Solution:

Modified the JavaScript logic to prevent the form from resetting during search or category filter actions.
Ensured form inputs (search, category) persist and only clear the product selection if it’s invalid after filtering.
Added informational toast notifications to confirm successful search or filter actions, improving user experience.

How It Works

Initialization:

The app loads a dataset of 20 e-commerce products (e.g., iPhone 14 Pro, Bose QC45 Headphones).
Stores products in Redis JSON with embeddings for vector search and enables full-text search capabilities.

User Interaction:

Search: Users enter a query (e.g., “wireless headphones”) to filter products dynamically.
Category Filter: Narrows the product dropdown by category (e.g., “Audio”).
Recommendations: Selecting a product triggers a vector search for similar products, with results cached for one hour.
Demo Mode: Randomly selects a product and fetches recommendations.
Debug Toggle: Displays the Redis index schema for troubleshooting.

Analytics:

Logs interactions to a Redis Stream for real-time tracking.
Displays cache hit rate and top products in a dynamic analytics card.

Access the Application
Production URL: https://ai-redis-innovation.onrender.com
Note: The app is deployed on render. Visit the URL to interact with the app. If the URL is not accessible, ensure you have a stable internet connection or contact me for assistance. For local testing, the app can be run at http://localhost:5000 using the setup instructions provided in the project documentation.
Future Improvements

Real Embeddings: Integrate a production-grade embedding model for enhanced recommendation accuracy.
Analytics Visualization: Add a graph to visualize cache hit rate trends over time.
Security: Store Redis credentials securely using environment variables.
Scalability: Optimize for high user loads with performance testing.

Conclusion
This Real-Time AI Product Recommender showcases Redis 8’s capabilities for AI-driven e-commerce applications. By leveraging vector search, JSON storage, Streams, and full-text search, it delivers fast, personalized recommendations with real-time analytics. The resolution of the rating error and the debug interface demonstrate robust development practices, making this a compelling entry for the Redis AI Challenge.

redischallenge #devchallenge #ai #database #ecommerce #realtimedata

Top comments (0)