Overview
Smarty is SmartEat AI’s intelligent nutritional assistant, powered by LangChain and LangGraph. Smarty helps users manage their meal plans, swap recipes, and get personalized dietary advice through natural conversation.Smarty uses Ollama with the Llama 3.1 model for local, private AI inference without external API dependencies.
Architecture
The agent is built with LangGraph, a framework for stateful, multi-step AI workflows.Core Components
- Workflow
- Executor
- Memory
- Prompts
File:
backend/app/services/agent/workflow.pyDefines the agent’s state graph:Agent Tools
Smarty has access to 7 specialized tools for meal planning:search_recipes_by_criteria
Search recipes by name, ingredients, or nutritional requirements
generate_weekly_plan
Create a complete 7-day meal plan based on user profile
get_current_plan_summary
Display the user’s active meal plan with all meals
suggest_recipe_alternatives
Find 3 similar recipes for a specific meal
replace_meal_in_plan
Swap a meal with a chosen alternative
update_user_preference
Update dietary preferences or restrictions
get_user_profile_summary
View user’s dietary profile and goals
Tool Workflow Example
When a user says: “Change my Wednesday lunch to something lighter”1
Agent Understands Intent
Smarty parses the request and identifies:
- Action: Replace meal
- Day: Wednesday (day 3)
- Meal: Lunch
- Criteria: Lower calories
2
Call suggest_recipe_alternatives
3
Present Options
Smarty shows:
4
User Chooses
User replies: “Option 2”
5
Call replace_meal_in_plan
State Management
DietGraphState Schema
messages: Conversation history with automatic deduplicationprofile: User’s dietary profile (calories, restrictions, preferences)
Memory Optimization
For environments with limited GPU (8GB VRAM):- Context Limits
- Compression
- Monitoring
Conversation Flow
First Interaction
Meal Swap Flow
LangChain Integration
API Integration
The agent is exposed via REST API:Performance Optimization
Lazy Loading
Model and tools are loaded once at startup
Message Trimming
Automatic context window management
Tool Result Caching
Database queries are cached per session
Async Processing
Non-blocking API calls with FastAPI
Debugging and Monitoring
Future Enhancements
1
Persistent Memory
Replace MemorySaver with PostgreSQL or Redis for conversation persistence across sessions
2
Multi-Language Support
Currently English-only; add i18n for Spanish, French, etc.
3
Voice Interface
Integrate with speech-to-text for voice-based meal planning
4
Proactive Suggestions
Agent initiates conversations: “It’s Sunday, want to generate next week’s plan?”
Related Documentation
- Ollama Setup - Configuring the LLM backend
- KNN Recommender - How recipe suggestions work
