Skip to content

RAG Chat (Swan)

Swan is MealPal’s AI nutrition chatbot, built with RAG (Retrieval-Augmented Generation) for accurate, source-backed answers.

  1. User asks a question via the chat interface
  2. Query classification — determines if the query is temporal (date-based) or semantic
  3. 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
  4. Generation — retrieved context is injected into the LLM prompt
  5. Streaming response — answer streams back via SSE with citations

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.

  • 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