A smart chatbot that helps users find products based on natural language queries. Built with Node.js, Express, and Svelte, powered by OpenRouter AI and featuring STEM (Semantic Token Embedding Matching) v2.0.
The current version implements a hierarchical pattern matching system that processes natural language queries through multiple layers of analysis.
STEM (Semantic Token Embedding Matching) v2.0 is our current implementation that combines natural language understanding with semantic search for more intelligent product matching.
Intent Extraction
{
"category": "sandals", // Product category
"priceRange": { // Price constraints
"min": 30,
"max": 60
},
"filters": { // Additional filters
"purpose": null, // e.g., casual, sport, formal
"age_group": null, // e.g., kids, adult
"style": null // specific attributes only
}
}
Semantic Search
// Using OpenRouter's text-embedding-ada-002 model
const embeddings = await getEmbeddings(productDescription);
const similarities = computeCosineSimilarity(queryEmbedding, embeddings);
Smart Filtering
Intent Analysis
Semantic Matching
Intent
{
action: "find",
category: "sneakers",
constraints: {
price: { max: 50 },
purpose: "sport"
}
}
Semantic Understanding
Entity Recognition
Context Awareness
Filter Types
The SmartMatchβ’ algorithm is an intelligent product matching system that understands natural language queries without requiring complex database schemas. It works through:
{
action: "find", // Search intent
category: "sneakers", // Product category
constraints: { // Limiting factors
price: { max: 50 },
attributes: ["comfortable", "sport"]
}
}
{
"sport": ["sport", "training", "running", "athletic", "gym"],
"casual": ["casual", "daily", "walking"],
"formal": ["formal", "dress", "business"],
"comfort": ["comfort", "comfortable", "soft"]
}
svelte-product-finder/
βββ backend/ # Node.js + Express backend
β βββ src/
β β βββ routes/ # API route handlers
β β βββ controllers/ # Business logic
β β βββ services/ # External services (AI)
β β βββ data/ # Mock product database
β β βββ app.js # Express app entry point
β βββ package.json
β βββ .env
β
βββ frontend/ # Svelte frontend
β βββ src/
β β βββ lib/ # Reusable components
β β βββ routes/ # Page components
β βββ package.json
Navigate to the backend directory:
cd backend
Install dependencies:
npm install
Create .env file:
OPENROUTER_API_KEY=your_openrouter_api_key_here
PORT=5000
Start the server:
node src/app.js
Navigate to the frontend directory:
cd frontend
Install dependencies:
npm install
Start the development server:
npm run dev
SmartMatchβ’ understands complex natural language queries. Here are some examples:
Example Query: "comfortable sport shoes under $50"
Intent Parsing (HKMS v1.0):
{
"action": "find",
"category": "sneakers",
"constraints": {
"price": {
"max": 50,
"currency": "USD"
},
"attributes": ["comfortable", "sport"]
}
}
Future Processing (STEM v2.0): ```javascript // 1. Convert query to embedding const queryEmbedding = await encoder.encode( "comfortable sport shoes under $50" );
// 2. Compare with product embeddings const productEmbeddings = products.map(p => ({ id: p.id, embedding: p.precomputedEmbedding }));
// 3. Find similar products using cosine similarity const results = findSimilarProducts( queryEmbedding, productEmbeddings, 0.7 // similarity threshold );
2. Smart Filtering:
- Matches "sport" against ["sport", "training", "athletic", "gym"]
- Matches "comfortable" against ["comfort", "comfortable", "soft"]
- Applies price constraint ($50)
- Ranks results by relevance
## Product Categories
The chatbot understands these main categories:
1. Sneakers
- Running shoes
- Sport sneakers
- Training shoes
- Casual sneakers
2. Formal Shoes
- Oxford shoes
- Dress shoes
- Loafers
- Derby shoes
3. Sandals
- Comfort sandals
- Sport sandals
- Beach sandals
- Casual sandals
## Technical Details
### Backend
- **Express.js**: Web server framework
- **OpenRouter AI**: For natural language processing
- **Axios**: HTTP client for API requests
- **Dotenv**: Environment configuration
### Frontend
- **Svelte**: UI framework
- **Vite**: Build tool and dev server
- **Axios**: HTTP client for API requests
### API Endpoints
- `POST /api/chatbot`
- Request body: `{ "message": "your search query" }`
- Response: `{ "products": [...], "intent": { "category": "...", "maxPrice": ... } }`
## Error Handling
The application includes comprehensive error handling:
- AI service failures
- Invalid user inputs
- Network issues
- JSON parsing errors
## Development
### Adding New Products
Add new products to `backend/src/data/products.json`:
```json
{
"id": "unique_id",
"name": "Product Name",
"category": "sneakers|formal|sandals",
"price": 99.99,
"url": "https://shop.com/buy/product-id"
}
Adjust the prompt in backend/src/services/chatgptService.js
to modify how the AI interprets user queries.
git checkout -b feature/AmazingFeature
)git commit -m 'Add some AmazingFeature'
)git push origin feature/AmazingFeature
)This project is licensed under the MIT License - see the LICENSE file for details.