How sultanoid/ai-companion works
Project Type and Purpose
• Type: This is a backend‐centric, AI‐driven messaging agent (“Ava”) that exposes a WhatsApp interface and a developer UI (via Chainlit) and orchestrates multiple AI modules.
• Purpose: To receive user inputs (text, voice, images) over WhatsApp (and Chainlit), process them through a LangGraph‐based “brain” with memory, vision, speech and scheduling modules, and return rich, context‐aware responses (text, TTS voice notes, generated images).
File Structure Analysis
• Top‐level
• .chainlit/ – Chainlit dev UI config and translations
• Dockerfile, docker-compose.yml, cloudbuild.yaml – container build & deploy scripts
• notebooks/ – exploratory Jupyter notebooks
• Source code (src/ai_companion/)
• core/ – central orchestrator: exceptions, prompt templates, scheduling logic
• graph/ – LangGraph workflow definitions: nodes, edges, state management
• interfaces/ – external entry points:
– chainlit/app.py – dev UI server
– whatsapp/webhook_endpoint.py – receives WhatsApp callbacks; whatsapp_response.py – formats replies
• modules/ – plugin modules for each capability:
– speech/ – speech‑to‑text & text‑to‑speech wrappers (Whisper, ElevenLabs)
– image/ – image recognition & generation (Vision models, Together AI)
– memory/long_term/ – vector‐store memory manager (Qdrant) & SQLite for short‐term
– schedules/ – periodic “daily activity” context generation
• settings.py – environment & API key configurations
Key Architecture Insights from README
• AI Providers: Groq Llama 3.x (text & vision), Whisper (speech recognition), ElevenLabs TTS, Together AI image gen
• Memory: SQLite for session/short‑term; Qdrant for long‑term recall
• Workflow Engine: LangGraph for defining and executing multi‐step AI workflows
• Deployment: Docker, Cloud Run on GCP, Cloud Build for CI/CD
Diagramming the System
a. Main Components
• User Interfaces:
• WhatsApp Chat (via webhook)
• Chainlit Dev UI
• API Gateway / Web Server: Single Python service exposing /webhook and Chainlit endpoints
• Core Orchestrator: LangGraph controller that routes inputs through nodes & edges
• Modules (Plugin Layer): Speech, Image, Memory, Schedule
• Data Stores:
• SQLite (short‑term memory)
• Qdrant (long‑term vector memory)
• External AI Services:
• Groq Llama 3.x (text/vision inference)
• Whisper (speech‐to‐text)
• ElevenLabs (text‐to‐speech)
• Together AI (image generation)
• Deployment Infrastructure: Docker containers, Cloud Run, Cloud Build, Docker Compose (local dev)
b. Relationships & Data Flow
1. Incoming Message
User → WhatsApp API → Webhook Endpoint
2. Pre‑processing
If voice → Speech‑to‑Text module; if image → Vision node
3. Core Graph Execution
Orchestrator uses LangGraph to run a workflow:
• Retrieves context from SQLite/Qdrant
• Invokes AI inference (Llama) for response generation
• Uses modules (image/text/speech) as needed
• Updates memory stores
4. Post‑processing & Response
If response is text → return as WhatsApp text
If response is voice → ElevenLabs TTS → send voice note
If image → send image media
5. Developer UI
Chainlit UI taps into the same orchestrator for real‑time testing
c. Architectural Patterns & Principles
• Hexagonal/Ports & Adapters: interfaces/ directory isolates transport from core logic
• Plugin Architecture: each capability lives in its own module, easily extended
• Workflow Engine: LangGraph graph of nodes & edges models complex multistep reasoning
• Separation of Concerns: clear division between I/O (interfaces), orchestration (core/graph), and capability modules
d. Technologies & Frameworks
• Python 3.x (pyproject.toml)
• Chainlit (dev UI)
• FastAPI/Flask (implied by webhook, but abstracted behind Chainlit)
• LangGraph (workflow definitions in graph/)
• Qdrant (vector DB), SQLite
• Docker, Docker Compose, Cloud Build, Cloud Run