twest

Prompt

Act as a Principal System Architect and Lead HealthTech Engineer. I am designing the backend architecture for an AI-powered nutrition and intermittent fasting tracking application. ### Core Requirements & Constraints: 1. Natural Language Input: Users can ask arbitrary, unstructured questions (e.g., "Will a bite of a rice cookie break my fast and how many calories is that?"). Different users will phrase the same intent in dozens of varied ways. 2. 100% Determinism: Nutrition values, unit conversions, and threshold checks must produce reproducible, mathematically identical results every time for the same inputs. The LLM must not guess or hallucinate calorie/macro counts. 3. Cost & Budget Optimization: I cannot rely on expensive third-party nutrition APIs (e.g., Nutritionix, FatSecret) on every request, nor can I afford massive LLM token overhead per query. 4. Fasting Rules Engine: Fasting definitions vary (e.g., strict water/autophagy fast: >0 kcal/insulin trigger vs. metabolic/dirty fast: <50 kcal threshold). The system must evaluate this predictably. 5. Legal & Regulatory Compliance: The system must avoid unauthorized medical advice, adhere to standard dietary labeling rules (e.g., FDA/EU food data regulations), and handle edge cases safely. ### Please provide a comprehensive technical blueprint covering: 1. End-to-End Pipeline Architecture: Detail the flow from user input to final response (parsing, entity normalization, lookup, calculation, and response generation). 2. Cost-Effective Semantic Extraction: How to parse informal units (e.g., "a bite", "half a handful", "a sip") into structured schemas using small/self-hosted LLMs or function calling. 3. Open-Source / Local Food Data Layer: How to structure, store, and query an offline database (such as USDA FoodData Central or Open Food Facts) with fast local semantic/vector search (e.g., pgvector/SQLite). 4. Deterministic Business Logic & Fasting Engine: How to separate LLM parsing from the mathematical calculation layer, and how to model differing fasting thresholds in application code. 5. Edge Cases & Compliance Guardrails: Strategies for ambiguous portion sizes, non-food queries, medical edge cases (e.g., diabetic emergencies), and mandatory disclaimer generation.