Overview
The KNN (K-Nearest Neighbors) recommendation system is the core of SmartEat AI’s recipe matching engine. It finds nutritionally similar recipes based on calorie and macronutrient profiles, enabling personalized meal plans and smart recipe swaps.How It Works
The KNN model operates in the feature space of nutritional values, using Euclidean distance to measure recipe similarity.Normalization
Features are standardized using scikit-learn’s StandardScaler:This ensures all features contribute equally to distance calculations.
Model Architecture
Model Files
The KNN system consists of three joblib files stored inbackend/app/files/:
df_recetas.joblib
Cleaned recipe dataframe with features and metadata
scaler.joblib
StandardScaler fitted on training data
knn.joblib
Trained NearestNeighbors model
Recommendation Process
Finding Similar Recipes
When a user wants to swap a meal, the system:Apply Filters
Filter candidates by:
- Meal type (breakfast, lunch, dinner, snack)
- Dietary restrictions (vegan, vegetarian, etc.)
- Recipes not already in the current plan
Performance Characteristics
- Speed
- Accuracy
- Trade-offs
- Query time: ~50-100ms for 550 neighbors
- Memory: ~200MB for model files
- Scalability: O(log n) with ball tree indexing
Integration Points
Backend Services
The KNN model is used by:-
Agent Tools (
backend/app/services/agent/tools/)suggest_recipe_alternatives: Finds 3 similar recipesreplace_meal_in_plan: Swaps a meal with a similar one
-
Recommender Service (
backend/app/core/recommender.py)swap_for_similar(): Core similarity search function
-
Plan Generation (
generate_weekly_plan.py)- Ensures nutritionally balanced weekly plans
The KNN model focuses on nutritional similarity only. Additional filters for ingredients, cuisine, and dietary restrictions are applied separately using SQL queries and LLM validation.
Example: Recipe Swap
Here’s how a user swaps a breakfast recipe:Model Parameters
| Parameter | Value | Rationale |
|---|---|---|
n_neighbors | 550 | Large pool for filtering |
metric | euclidean | Intuitive for nutritional space |
algorithm | auto | Scikit-learn optimizes based on data |
Future Enhancements
Collaborative Filtering
Learn from user ratings and swaps to improve recommendations
Deep Learning
Use embeddings to capture ingredient relationships and flavor profiles
A/B Testing
Compare KNN vs. other algorithms for user satisfaction
Real-time Training
Update model as new recipes are added to the database
Related Documentation
- ML Pipeline - How the model is trained
- AI Agent - How recommendations are presented to users
