Spaces:
Running on Zero
Running on Zero
|
Download docs/Architecture.md from Catalna/ginchiee-bot: direct link, hf CLI and curl.
- Browser
- Download file 8.4 kB
-
https://huggingface.co/spaces/Catalna/ginchiee-bot/resolve/main/docs/Architecture.md
- Command line
-
hf download hf://spaces/Catalna/ginchiee-bot/docs/Architecture.md
-
curl -L -o Architecture.md https://huggingface.co/spaces/Catalna/ginchiee-bot/resolve/main/docs/Architecture.md
8.4 kB
| # Architecture β AI Diet Companion | |
| **Version:** 0.1.0 | |
| **Architecture Stage:** MVP | |
| --- | |
| # 1. Architecture Philosophy | |
| MVP menggunakan arsitektur sederhana tetapi modular. | |
| Prinsip utama: | |
| ```text | |
| Simple now | |
| Scalable later | |
| ``` | |
| Jangan memasukkan infrastructure kompleks sebelum dibutuhkan. | |
| MVP tidak membutuhkan: | |
| * Kubernetes, | |
| * microservices, | |
| * PostgreSQL, | |
| * Redis, | |
| * LangGraph, | |
| * Docker. | |
| Semua dapat berjalan sebagai satu Python application. | |
| --- | |
| # 2. High-Level Architecture | |
| ```text | |
| USER | |
| β | |
| βΌ | |
| βββββββββββββββββ | |
| β Telegram Bot β | |
| βββββββββ¬ββββββββ | |
| β | |
| βΌ | |
| βββββββββββββββββ | |
| β Bot Handler β | |
| βββββββββ¬ββββββββ | |
| β | |
| ββββββββββββββΌβββββββββββββ | |
| βΌ βΌ βΌ | |
| Commands Messages Callback | |
| β β β | |
| ββββββββββββββΌβββββββββββββ | |
| βΌ | |
| βββββββββββββββββ | |
| β Application β | |
| β Service Layer β | |
| βββββββββ¬ββββββββ | |
| β | |
| ββββββββββββββββββΌβββββββββββββββββ | |
| βΌ βΌ βΌ | |
| User Service Nutrition Service AI Service | |
| β β β | |
| βΌ βΌ βΌ | |
| SQLite Nutrition Data LLM API | |
| β | |
| βΌ | |
| Scheduler | |
| ``` | |
| --- | |
| # 3. Application Layers | |
| ## 3.1 Presentation Layer | |
| Responsible for Telegram interaction. | |
| ```text | |
| Telegram Update | |
| β | |
| Handler | |
| β | |
| Application Service | |
| ``` | |
| Tidak boleh mengandung business logic kompleks. | |
| --- | |
| # 4. Service Layer | |
| Service layer menangani business logic. | |
| Contoh: | |
| ```text | |
| UserService | |
| DietService | |
| MealService | |
| NutritionService | |
| ReminderService | |
| AIService | |
| ``` | |
| Contoh: | |
| ```python | |
| nutrition_service.calculate_daily_progress(user_id) | |
| ``` | |
| --- | |
| # 5. Data Layer | |
| MVP menggunakan SQLite. | |
| ```text | |
| SQLite | |
| β | |
| βββ users | |
| βββ diet_profiles | |
| βββ meal_schedules | |
| βββ food_logs | |
| βββ daily_nutrition | |
| βββ conversations | |
| ``` | |
| SQLite dipilih karena: | |
| * zero configuration, | |
| * gratis, | |
| * local, | |
| * mudah backup, | |
| * cukup untuk MVP. | |
| --- | |
| # 6. AI Layer | |
| AI Service menjadi abstraction layer untuk LLM. | |
| ```text | |
| Application | |
| β | |
| AIService | |
| β | |
| LLM Provider | |
| ``` | |
| Application tidak boleh langsung memanggil SDK LLM di seluruh codebase. | |
| Contoh: | |
| ```python | |
| response = ai_service.chat( | |
| message=user_message, | |
| context=user_context | |
| ) | |
| ``` | |
| Dengan abstraction ini provider dapat diganti tanpa mengubah seluruh application. | |
| --- | |
| # 7. Nutrition Layer | |
| Nutrition engine bertanggung jawab terhadap kalkulasi. | |
| ```text | |
| Food Input | |
| β | |
| Food Parser | |
| β | |
| Nutrition Database | |
| β | |
| Nutrition Calculator | |
| β | |
| Nutrition Result | |
| ``` | |
| Contoh: | |
| ```text | |
| Rice 200g | |
| Chicken 150g | |
| Egg 1 | |
| ``` | |
| menjadi: | |
| ```text | |
| Calories | |
| Protein | |
| Carbs | |
| Fat | |
| ``` | |
| LLM tidak menjadi sumber kebenaran utama untuk kalkulasi matematika. | |
| --- | |
| # 8. Scheduler | |
| Scheduler bertanggung jawab mengirim reminder. | |
| ```text | |
| Scheduler | |
| β | |
| Check Meal Schedule | |
| β | |
| Is reminder due? | |
| β | |
| YES | |
| β | |
| Get User Context | |
| β | |
| AI Service | |
| β | |
| Generate Message | |
| β | |
| Telegram | |
| ``` | |
| Untuk MVP: | |
| ```text | |
| APScheduler | |
| ``` | |
| dapat digunakan. | |
| --- | |
| # 9. Reminder Flow | |
| ```text | |
| Every minute | |
| β | |
| Scheduler | |
| β | |
| Query active schedules | |
| β | |
| Check current timezone | |
| β | |
| Find due reminders | |
| β | |
| Create reminder context | |
| β | |
| LLM | |
| β | |
| Telegram Bot | |
| β | |
| User | |
| ``` | |
| --- | |
| # 10. Food Logging Flow | |
| ```text | |
| User: | |
| "Aku makan nasi 200g dan ayam 150g" | |
| β | |
| βΌ | |
| Telegram | |
| β | |
| βΌ | |
| AI / Parser | |
| β | |
| βΌ | |
| Structured Food | |
| β | |
| βΌ | |
| Nutrition Service | |
| β | |
| βΌ | |
| Calculate macros | |
| β | |
| βΌ | |
| SQLite | |
| β | |
| βΌ | |
| AI Response | |
| β | |
| βΌ | |
| Telegram | |
| ``` | |
| --- | |
| # 11. AI Context Flow | |
| LLM tidak diberikan seluruh database. | |
| Context builder memilih informasi yang relevan. | |
| ```text | |
| User Message | |
| β | |
| βΌ | |
| Context Builder | |
| β | |
| βββ User Profile | |
| βββ Diet Goal | |
| βββ Daily Target | |
| βββ Today's Food | |
| βββ Today's Progress | |
| βββ Meal Schedule | |
| βββ Recent Conversation | |
| β | |
| βΌ | |
| LLM | |
| β | |
| βΌ | |
| Response | |
| ``` | |
| --- | |
| # 12. Google Integration β Future | |
| Google integration menggunakan OAuth 2.0. | |
| ```text | |
| User | |
| β | |
| βΌ | |
| Telegram | |
| β | |
| βΌ | |
| "Connect Google" | |
| β | |
| βΌ | |
| OAuth Authorization | |
| β | |
| βΌ | |
| β | |
| βΌ | |
| Access Token | |
| β | |
| βΌ | |
| Backend | |
| ``` | |
| Token disimpan secara aman. | |
| --- | |
| # 13. Google Calendar | |
| Future architecture: | |
| ```text | |
| Diet Schedule | |
| β | |
| βΌ | |
| Calendar Service | |
| β | |
| βΌ | |
| Google Calendar API | |
| β | |
| βΌ | |
| Calendar Event | |
| ``` | |
| Contoh: | |
| ```text | |
| Breakfast | |
| 07:00 | |
| Recurring Daily | |
| ``` | |
| Calendar menjadi external scheduling ecosystem. | |
| --- | |
| # 14. Google Tasks | |
| Tasks digunakan untuk habit/checklist. | |
| ```text | |
| Drink Water | |
| Take Snack | |
| Exercise | |
| Meal Preparation | |
| ``` | |
| Architecture: | |
| ```text | |
| Diet Service | |
| β | |
| Google Tasks Service | |
| β | |
| Google Tasks API | |
| ``` | |
| --- | |
| # 15. WhatsApp Integration β Future | |
| Channel abstraction: | |
| ```text | |
| Application | |
| β | |
| Message Service | |
| β | |
| ββββββββββββ΄βββββββββββ | |
| βΌ βΌ | |
| Telegram Adapter WhatsApp Adapter | |
| β β | |
| βΌ βΌ | |
| Telegram API WhatsApp API | |
| ``` | |
| Business logic tidak boleh bergantung langsung pada Telegram. | |
| --- | |
| # 16. LangGraph β Future | |
| LangGraph belum diperlukan untuk MVP. | |
| Ketika workflow mulai kompleks: | |
| ```text | |
| User Message | |
| β | |
| Intent Agent | |
| β | |
| Context Agent | |
| β | |
| Diet Agent | |
| β | |
| Nutrition Tool | |
| β | |
| Google Calendar Tool | |
| β | |
| Response Agent | |
| ``` | |
| LangGraph menjadi orchestration layer. | |
| --- | |
| # 17. Production Architecture | |
| Ketika sudah membutuhkan 24/7 deployment: | |
| ```text | |
| Internet | |
| β | |
| βΌ | |
| Nginx | |
| β | |
| βΌ | |
| FastAPI App | |
| β | |
| ββββββββββββββΌβββββββββββββ | |
| βΌ βΌ βΌ | |
| Worker PostgreSQL Redis | |
| β | |
| βΌ | |
| Scheduler | |
| β | |
| βββββββ΄ββββββ | |
| βΌ βΌ | |
| Telegram WhatsApp | |
| ``` | |
| Deployment: | |
| ```text | |
| GitHub | |
| β | |
| CI/CD | |
| β | |
| Docker | |
| β | |
| VPS | |
| ``` | |
| --- | |
| # 18. Design Principles | |
| ### Separation of concerns | |
| Telegram handler tidak menghitung nutrisi. | |
| Nutrition service tidak mengirim Telegram. | |
| AI service tidak mengakses database secara sembarangan. | |
| ### Provider abstraction | |
| LLM provider dapat diganti. | |
| ### Channel abstraction | |
| Telegram dan WhatsApp dapat menggunakan business logic yang sama. | |
| ### Deterministic calculation | |
| Nutrition calculation dilakukan oleh code/database. | |
| ### AI for language | |
| LLM digunakan untuk: | |
| * natural language, | |
| * suggestions, | |
| * conversational response, | |
| * personality, | |
| * summarization. | |
| --- | |
| # 19. MVP Dependency Flow | |
| ```text | |
| Telegram | |
| β | |
| Handler | |
| β | |
| Service | |
| β | |
| Database | |
| ``` | |
| AI: | |
| ```text | |
| Service | |
| β | |
| Context Builder | |
| β | |
| AI Service | |
| β | |
| LLM | |
| ``` | |
| Reminder: | |
| ```text | |
| Scheduler | |
| β | |
| Reminder Service | |
| β | |
| AI Service | |
| β | |
| Telegram | |
| ``` | |
| Nutrition: | |
| ```text | |
| Food Input | |
| β | |
| Parser | |
| β | |
| Nutrition Engine | |
| β | |
| Database | |
| ``` | |