How al3x2023/chatbotaiollama works
1. What this repo is
- A Django-based chatbot for UAEMEX that combines a web chat UI, a local Ollama model, and an automated knowledge-ingestion pipeline.
- The system is designed for continuous operation on a VPS, with deployment and scheduling scripts included in the repo.
2. Top-level runtime boundary
- chatbot_project/ is the Django project container: settings, root URLs, ASGI/WSGI entrypoints.
- chat/ is the main app and contains almost all product logic: UI, database models, service layer, and operational commands.
- manage.py is the operational entrypoint for migrations, scraping, PDF processing, and the full update pipeline.
3. User-facing interface
- The web experience is rendered from Django templates in chat/templates/chat/ and styled with static assets in chat/static/.
- chat/static/js/chat.js and chat/static/js/chat-widget.js suggest both a page-based chat and an embeddable widget-style client.
- chat/views.py and chat/urls.py expose the HTTP routes that serve the chat UI and the operational endpoints.
4. Operational endpoints
- The README highlights two non-chat endpoints: /api/estado/ for model status and /api/health/ for infrastructure health.
- These are used by deploy scripts and by health checks after updates, so they form the boundary between app runtime and ops automation.
5. Core data model
- The database layer is Django ORM-backed, with migrations showing entities such as Conversacion, ConocimientoUAEMEX, DocumentoPDF, and Fuente.
- The presence of a hash_contenido field on DocumentoPDF indicates deduplication or change detection for downloaded documents.
- Fuente uniqueness migrations suggest source normalization and controlled ingestion from external pages/links.
6. Knowledge ingestion pipeline
- The ingestion flow is split into discrete management commands under chat/management/commands/:
- scrape_uaemex.py for crawling UAEMEX sources
- procesar_pdfs.py for PDF extraction/processing
- actualizar_modelo.py and actualizar_auto.py for model refresh
- ejecutar_pipeline.py for end-to-end orchestration
- This pipeline is the main backend data flow: external UAEMEX content → local files/records → knowledge base → model update.
7. Scraping and source selection
- chat/services/scraper_service.py and chat/utils/url_loader.py indicate a service layer that collects URLs and normalizes sources before processing.
- The repo includes clasificadas/*.txt files, which act as curated queues or filters for HTML/PDF URLs.
- The pipeline can consume these lists directly, so the system supports both automated discovery and manual curation.
8. PDF processing and local document storage
- chat/services/pdf_service.py handles PDF downloading/extraction/processing.
- Downloaded artifacts are stored under media/pdfs/, making local file storage part of the architecture rather than transient in-memory handling.
- The pipeline is document-centric: ingest PDF, extract knowledge, persist structured output, and optionally trigger model refresh.
9. Ollama integration
- chat/services/ollama_service.py is the boundary to the local LLM runtime.
- The README configures OLLAMA_URL, MODEL_NAME, and BASE_MODEL, which means model access is local-network based rather than a hosted SaaS dependency.
- chat/services/model_updater.py and the update commands imply a controlled workflow for updating or switching the active model after new knowledge is ingested.
10. Orchestration layer
- chat/services/orchestrator.py is the main coordination point between scraping, PDF processing, knowledge updates, and model refresh.
- chat/management/commands/ejecutar_pipeline.py is the executable wrapper for that orchestration layer.
- This is the central “pipeline controller” boundary in the repo: it decides what runs, in what order, and whether updates are necessary.
11. Deployment and operations
- scripts/auto_deploy_vps.sh automates fetch/pull, dependency install, migrations, static collection, service restart, pipeline execution, and final health check.
- scripts/instalar_systemd_vps.sh creates systemd units for the Django/Gunicorn service and a timer-driven pipeline service.
- scripts/actualizar_todo.py appears to be an additional top-level automation script for full updates.
12. Environment and infrastructure
- Configuration is environment-driven via .env, with production settings for database, hostnames, timezone, and Ollama.
- The README supports MySQL in production, while the repo also contains db.sqlite3, suggesting SQLite is used in local/dev or as a fallback.
- The app is intended to run behind Gunicorn/systemd on a VPS, with static files collected and served through the deployment stack.
13. Observability and artifacts
- Runtime logs are kept in logs/chatbot.log and autonomo.log.
- conocimiento.csv suggests an exported or intermediate knowledge artifact used by the ingestion/update pipeline.
- These outputs are useful when drawing the architecture because they show which parts are persistent operational state versus ephemeral request handling.
14. How the main data flow fits together
- External UAEMEX sources and PDFs are discovered or curated.
- The pipeline downloads/processes them into local storage and database records.
- Structured knowledge is incorporated into the chatbot’s knowledge base.
- Ollama is updated or queried through the service layer.
- The web UI then serves user chat requests against the current local model and stored knowledge.
15. Main architectural boundaries to show in a diagram
- Browser/UI ↔ Django web app
- Django app ↔ Django ORM/database
- Django services ↔ local Ollama runtime
- Pipeline/orchestrator ↔ external UAEMEX websites
- Pipeline/orchestrator ↔ local PDF storage under media/pdfs
- Deploy scripts/systemd ↔ long-running app and scheduled ingestion jobs
Main entry points: views.py Views · orchestrator.py Orchestrator · auto_deploy_vps.sh Deploy script · settings.py Project core
Components 26
Django runtime
Main web application container and HTTP entrypoints.
- manage.py · Operational entrypoint for migrations and pipeline commands.
manage.py - Project core · Settings, root URLs, ASGI, and WSGI for the site.
chatbot_project/settings.py - Root URLs · Top-level URL routing for the Django project.
chatbot_project/urls.py - ASGI/WSGI · Async and WSGI application entrypoints.
chatbot_project/asgi.py
Chat app
User-facing chat, models, services, and management commands.
- Views · Serves the chat UI and operational endpoints.
chat/views.py - Chat URLs · Routes for chat UI and API endpoints.
chat/urls.py - Templates · HTML templates for the chat interface.
chat/templates/chat/index.html - Static UI · Styles and client scripts for page chat and widget mode.
chat/static/js/chat.js - Models · Core conversation, source, document, and knowledge entities.
chat/models.py
Ingestion pipeline
Scraping, PDF processing, orchestration, and model refresh.
- Scraper · Discovers and normalizes UAEMEX sources.
chat/services/scraper_service.py - URL loader · Loads curated URL lists and filters source queues.
chat/utils/url_loader.py - PDF service · Downloads, extracts, and processes PDF content.
chat/services/pdf_service.py - Ollama service · Boundary to the local Ollama runtime and active model.
chat/services/ollama_service.py - Model updater · Controls model refresh after new knowledge ingestion.
chat/services/model_updater.py - Orchestrator · Coordinates scraping, PDF processing, knowledge updates, and model refresh.
chat/services/orchestrator.py - Pipeline cmd · Executable wrapper for the end-to-end pipeline.
chat/management/commands/ejecutar_pipeline.py - Scrape cmd · Runs UAEMEX site crawling and source collection.
chat/management/commands/scrape_uaemex.py - PDF cmd · Runs PDF extraction and processing.
chat/management/commands/procesar_pdfs.py - Update cmd · Triggers model update and refresh workflows.
chat/management/commands/actualizar_modelo.py - Auto update · Automated update command for continuous operation.
chat/management/commands/actualizar_auto.py
Operations
Deployment, scheduling, logs, and runtime artifacts.
- Deploy script · Pulls code, installs dependencies, migrates, restarts, and checks health.
scripts/auto_deploy_vps.sh - systemd setup · Creates systemd units and timers for the app and pipeline.
scripts/instalar_systemd_vps.sh - Update script · Additional top-level automation for full updates.
scripts/actualizar_todo.py - PDF store · Local storage for downloaded PDFs and processed documents.
media/pdfs - Logs · Persistent application and automation logs.
logs/chatbot.log - Knowledge CSV · Exported or intermediate knowledge artifact for pipeline use.
conocimiento.csv
Connections 29
- manage.py → Project core: boots · Runs the Django project using shared settings.
- Project core → Root URLs: routes · Project settings connect to the root URL configuration.
- Project core → ASGI/WSGI: serves · Configures async and WSGI entrypoints.
- Root URLs → Chat URLs: includes · Delegates app routes to the chat app router.
- Chat URLs → Views: dispatches · Maps HTTP requests to view handlers.
- Views → Templates: renders · Serves the chat interface from templates.
- Views → Static UI: uses · Delivers browser scripts and styling.
- Views → Models: reads/writes · Handles chat state and persistent records.
- Scrape cmd → Scraper: invokes · Management command executes scraping logic.
- Scraper → URL loader: loads · Consumes curated and normalized URL lists.
- Scraper → Models: stores · Persists sources and knowledge-related records.
- PDF cmd → PDF service: invokes · Management command executes PDF processing.
- PDF service → PDF store: downloads · Writes downloaded PDFs to local storage.
- PDF service → Models: stores · Creates or updates document and knowledge records.
- Pipeline cmd → Orchestrator: runs · Pipeline command is the executable controller wrapper.
- Orchestrator → Scrape cmd: coordinates · Controls the scraping phase.
- Orchestrator → PDF cmd: coordinates · Controls the PDF processing phase.
- Orchestrator → Model updater: coordinates · Triggers knowledge/model refresh steps.
- Model updater → Ollama service: updates · Manages the active local model through Ollama.
- Views → Ollama service: queries · Chat responses depend on the local model runtime.
- Views → Models: retrieves · Uses stored knowledge and conversation records.
- Deploy script → Project core: deploys · Operates the Django app during VPS deployment.
- Deploy script → Pipeline cmd: runs · Executes the ingestion pipeline after deploy.
- Deploy script → Logs: checks · Uses logs and health checks to validate deployment.
- systemd setup → Deploy script: installs · Creates service and timer units for long-running operation.
- Update script → Auto update: drives · Top-level automation for full updates.
- Auto update → Orchestrator: uses · Automated updates flow through the pipeline controller.
- Orchestrator → Knowledge CSV: exports · Pipeline can emit or consume structured knowledge artifacts.
- Auto update → Logs: writes · Automation outputs operational logs.