RAG Chat (Swan)
Swan is MealPal’s AI nutrition chatbot, built with RAG (Retrieval-Augmented Generation) for accurate, source-backed answers.
Architecture
Section titled “Architecture”- User asks a question via the chat interface
- Query classification — determines if the query is temporal (date-based) or semantic
- Retrieval:
- Temporal queries (e.g., “what did I eat yesterday”) → direct database queries
- Semantic queries (e.g., “how much protein do I need”) → pgvector similarity search
- Generation — retrieved context is injected into the LLM prompt
- Streaming response — answer streams back via SSE with citations
Knowledge Base
Section titled “Knowledge Base”Swan’s knowledge base includes:
- Health Canada dietary guidelines
- USDA food composition data
- WHO nutrition recommendations
PDFs are ingested, chunked, embedded (OpenAI text-embedding-3-small, 1536 dimensions), and stored in PostgreSQL with pgvector.
Key Design Decisions
Section titled “Key Design Decisions”- Embeddings for finding, not storing — we use embeddings to find relevant content, not to encode facts
- Temporal queries bypass embeddings — date-based questions go straight to SQL
- Prompt caching — system prompts are cached in Valkey to reduce token costs
- Citation tracking — every RAG response includes source citations