# al3x2023/chatbotaiollama architecture

> Architecture diagram of the GitHub repository al3x2023/chatbotaiollama, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/al3x2023/chatbotaiollama
- Repository: https://github.com/al3x2023/chatbotaiollama

## Overview

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: [`chat/views.py`](https://github.com/al3x2023/chatbotaiollama/blob/main/chat/views.py) (Views), [`chat/services/orchestrator.py`](https://github.com/al3x2023/chatbotaiollama/blob/main/chat/services/orchestrator.py) (Orchestrator), [`scripts/auto_deploy_vps.sh`](https://github.com/al3x2023/chatbotaiollama/blob/main/scripts/auto_deploy_vps.sh) (Deploy script), [`chatbot_project/settings.py`](https://github.com/al3x2023/chatbotaiollama/blob/main/chatbot_project/settings.py) (Project core)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Django runtime"]
  node_manage_py["manage.py<br/>Django CLI entrypoint<br/>[manage.py]"]
  node_project_settings["Project core<br/>Django project<br/>[settings.py]"]
  node_project_urls["Root URLs<br/>HTTP router<br/>[urls.py]"]
  node_asgi_wsgi["ASGI/WSGI<br/>server entrypoints<br/>[asgi.py]"]
end

subgraph group_chat["Chat app"]
  node_views["Views<br/>HTTP handlers<br/>[views.py]"]
  node_chat_urls["Chat URLs<br/>app router<br/>[urls.py]"]
  node_templates["Templates<br/>[index.html]"]
  node_static_ui["Static UI<br/>frontend assets<br/>[chat.js]"]
  node_models[("Models<br/>ORM schema<br/>[models.py]")]
end

subgraph group_pipeline["Ingestion pipeline"]
  node_scraper_service["Scraper<br/>crawler service<br/>[scraper_service.py]"]
  node_url_loader["URL loader<br/>source normalizer<br/>[url_loader.py]"]
  node_pdf_service["PDF service<br/>document processor<br/>[pdf_service.py]"]
  node_ollama_service["Ollama service<br/>LLM adapter<br/>[ollama_service.py]"]
  node_model_updater["Model updater<br/>refresh coordinator<br/>[model_updater.py]"]
  node_orchestrator{{"Orchestrator<br/>pipeline controller<br/>[orchestrator.py]"}}
  node_cmd_pipeline["Pipeline cmd<br/>management command"]
  node_cmd_scrape["Scrape cmd<br/>management command<br/>[scrape_uaemex.py]"]
  node_cmd_pdfs["PDF cmd<br/>management command<br/>[procesar_pdfs.py]"]
  node_cmd_update["Update cmd<br/>management command"]
  node_cmd_auto["Auto update<br/>management command<br/>[actualizar_auto.py]"]
end

subgraph group_ops["Operations"]
  node_deploy_script["Deploy script<br/>VPS automation<br/>[auto_deploy_vps.sh]"]
  node_systemd_script["systemd setup<br/>service installer"]
  node_update_script["Update script<br/>ops script<br/>[actualizar_todo.py]"]
  node_media_pdfs[("PDF store<br/>artifact storage")]
  node_logs["Logs<br/>[chatbot.log]"]
  node_knowledge_csv["Knowledge CSV<br/>data artifact<br/>[conocimiento.csv]"]
end

node_manage_py -->|"boots"| node_project_settings
node_project_settings -->|"routes"| node_project_urls
node_project_settings -->|"serves"| node_asgi_wsgi
node_project_urls -->|"includes"| node_chat_urls
node_chat_urls -->|"dispatches"| node_views
node_views -->|"renders"| node_templates
node_views -->|"uses"| node_static_ui
node_views -->|"reads/writes"| node_models
node_cmd_scrape -->|"invokes"| node_scraper_service
node_scraper_service -->|"loads"| node_url_loader
node_scraper_service -->|"stores"| node_models
node_cmd_pdfs -->|"invokes"| node_pdf_service
node_pdf_service -->|"downloads"| node_media_pdfs
node_pdf_service -->|"stores"| node_models
node_cmd_pipeline -->|"runs"| node_orchestrator
node_orchestrator -->|"coordinates"| node_cmd_scrape
node_orchestrator -->|"coordinates"| node_cmd_pdfs
node_orchestrator -->|"coordinates"| node_model_updater
node_model_updater -->|"updates"| node_ollama_service
node_views -->|"queries"| node_ollama_service
node_views -->|"retrieves"| node_models
node_deploy_script -->|"deploys"| node_project_settings
node_deploy_script -->|"runs"| node_cmd_pipeline
node_deploy_script -->|"checks"| node_logs
node_systemd_script -->|"installs"| node_deploy_script
node_update_script -->|"drives"| node_cmd_auto
node_cmd_auto -->|"uses"| node_orchestrator
node_orchestrator -->|"exports"| node_knowledge_csv
node_cmd_auto -->|"writes"| node_logs

click node_manage_py "https://github.com/al3x2023/chatbotaiollama/blob/main/manage.py"
click node_project_settings "https://github.com/al3x2023/chatbotaiollama/blob/main/chatbot_project/settings.py"
click node_project_urls "https://github.com/al3x2023/chatbotaiollama/blob/main/chatbot_project/urls.py"
click node_asgi_wsgi "https://github.com/al3x2023/chatbotaiollama/blob/main/chatbot_project/asgi.py"
click node_views "https://github.com/al3x2023/chatbotaiollama/blob/main/chat/views.py"
click node_chat_urls "https://github.com/al3x2023/chatbotaiollama/blob/main/chat/urls.py"
click node_templates "https://github.com/al3x2023/chatbotaiollama/blob/main/chat/templates/chat/index.html"
click node_static_ui "https://github.com/al3x2023/chatbotaiollama/blob/main/chat/static/js/chat.js"
click node_models "https://github.com/al3x2023/chatbotaiollama/blob/main/chat/models.py"
click node_scraper_service "https://github.com/al3x2023/chatbotaiollama/blob/main/chat/services/scraper_service.py"
click node_url_loader "https://github.com/al3x2023/chatbotaiollama/blob/main/chat/utils/url_loader.py"
click node_pdf_service "https://github.com/al3x2023/chatbotaiollama/blob/main/chat/services/pdf_service.py"
click node_ollama_service "https://github.com/al3x2023/chatbotaiollama/blob/main/chat/services/ollama_service.py"
click node_model_updater "https://github.com/al3x2023/chatbotaiollama/blob/main/chat/services/model_updater.py"
click node_orchestrator "https://github.com/al3x2023/chatbotaiollama/blob/main/chat/services/orchestrator.py"
click node_cmd_pipeline "https://github.com/al3x2023/chatbotaiollama/blob/main/chat/management/commands/ejecutar_pipeline.py"
click node_cmd_scrape "https://github.com/al3x2023/chatbotaiollama/blob/main/chat/management/commands/scrape_uaemex.py"
click node_cmd_pdfs "https://github.com/al3x2023/chatbotaiollama/blob/main/chat/management/commands/procesar_pdfs.py"
click node_cmd_update "https://github.com/al3x2023/chatbotaiollama/blob/main/chat/management/commands/actualizar_modelo.py"
click node_cmd_auto "https://github.com/al3x2023/chatbotaiollama/blob/main/chat/management/commands/actualizar_auto.py"
click node_deploy_script "https://github.com/al3x2023/chatbotaiollama/blob/main/scripts/auto_deploy_vps.sh"
click node_systemd_script "https://github.com/al3x2023/chatbotaiollama/blob/main/scripts/instalar_systemd_vps.sh"
click node_update_script "https://github.com/al3x2023/chatbotaiollama/blob/main/scripts/actualizar_todo.py"
click node_media_pdfs "https://github.com/al3x2023/chatbotaiollama/tree/main/media/pdfs"
click node_logs "https://github.com/al3x2023/chatbotaiollama/blob/main/logs/chatbot.log"
click node_knowledge_csv "https://github.com/al3x2023/chatbotaiollama/blob/main/conocimiento.csv"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_manage_py,node_project_settings,node_project_urls,node_asgi_wsgi toneBlue
class node_views,node_chat_urls,node_templates,node_static_ui,node_models toneAmber
class node_scraper_service,node_url_loader,node_pdf_service,node_ollama_service,node_model_updater,node_orchestrator,node_cmd_pipeline,node_cmd_scrape,node_cmd_pdfs,node_cmd_update,node_cmd_auto toneMint
class node_deploy_script,node_systemd_script,node_update_script,node_media_pdfs,node_logs,node_knowledge_csv toneRose
```

## Components

### Django runtime

Main web application container and HTTP entrypoints.

- [manage.py](https://github.com/al3x2023/chatbotaiollama/blob/main/manage.py): Django CLI entrypoint · Operational entrypoint for migrations and pipeline commands. · `manage.py`
- [Project core](https://github.com/al3x2023/chatbotaiollama/blob/main/chatbot_project/settings.py): Django project · Settings, root URLs, ASGI, and WSGI for the site. · `chatbot_project/settings.py`
- [Root URLs](https://github.com/al3x2023/chatbotaiollama/blob/main/chatbot_project/urls.py): HTTP router · Top-level URL routing for the Django project. · `chatbot_project/urls.py`
- [ASGI/WSGI](https://github.com/al3x2023/chatbotaiollama/blob/main/chatbot_project/asgi.py): server entrypoints · Async and WSGI application entrypoints. · `chatbot_project/asgi.py`

### Chat app

User-facing chat, models, services, and management commands.

- [Views](https://github.com/al3x2023/chatbotaiollama/blob/main/chat/views.py): HTTP handlers · Serves the chat UI and operational endpoints. · `chat/views.py`
- [Chat URLs](https://github.com/al3x2023/chatbotaiollama/blob/main/chat/urls.py): app router · Routes for chat UI and API endpoints. · `chat/urls.py`
- [Templates](https://github.com/al3x2023/chatbotaiollama/blob/main/chat/templates/chat/index.html): UI templates · HTML templates for the chat interface. · `chat/templates/chat/index.html`
- [Static UI](https://github.com/al3x2023/chatbotaiollama/blob/main/chat/static/js/chat.js): frontend assets · Styles and client scripts for page chat and widget mode. · `chat/static/js/chat.js`
- [Models](https://github.com/al3x2023/chatbotaiollama/blob/main/chat/models.py): ORM schema · Core conversation, source, document, and knowledge entities. · `chat/models.py`

### Ingestion pipeline

Scraping, PDF processing, orchestration, and model refresh.

- [Scraper](https://github.com/al3x2023/chatbotaiollama/blob/main/chat/services/scraper_service.py): crawler service · Discovers and normalizes UAEMEX sources. · `chat/services/scraper_service.py`
- [URL loader](https://github.com/al3x2023/chatbotaiollama/blob/main/chat/utils/url_loader.py): source normalizer · Loads curated URL lists and filters source queues. · `chat/utils/url_loader.py`
- [PDF service](https://github.com/al3x2023/chatbotaiollama/blob/main/chat/services/pdf_service.py): document processor · Downloads, extracts, and processes PDF content. · `chat/services/pdf_service.py`
- [Ollama service](https://github.com/al3x2023/chatbotaiollama/blob/main/chat/services/ollama_service.py): LLM adapter · Boundary to the local Ollama runtime and active model. · `chat/services/ollama_service.py`
- [Model updater](https://github.com/al3x2023/chatbotaiollama/blob/main/chat/services/model_updater.py): refresh coordinator · Controls model refresh after new knowledge ingestion. · `chat/services/model_updater.py`
- [Orchestrator](https://github.com/al3x2023/chatbotaiollama/blob/main/chat/services/orchestrator.py): pipeline controller · Coordinates scraping, PDF processing, knowledge updates, and model refresh. · `chat/services/orchestrator.py`
- [Pipeline cmd](https://github.com/al3x2023/chatbotaiollama/blob/main/chat/management/commands/ejecutar_pipeline.py): management command · Executable wrapper for the end-to-end pipeline. · `chat/management/commands/ejecutar_pipeline.py`
- [Scrape cmd](https://github.com/al3x2023/chatbotaiollama/blob/main/chat/management/commands/scrape_uaemex.py): management command · Runs UAEMEX site crawling and source collection. · `chat/management/commands/scrape_uaemex.py`
- [PDF cmd](https://github.com/al3x2023/chatbotaiollama/blob/main/chat/management/commands/procesar_pdfs.py): management command · Runs PDF extraction and processing. · `chat/management/commands/procesar_pdfs.py`
- [Update cmd](https://github.com/al3x2023/chatbotaiollama/blob/main/chat/management/commands/actualizar_modelo.py): management command · Triggers model update and refresh workflows. · `chat/management/commands/actualizar_modelo.py`
- [Auto update](https://github.com/al3x2023/chatbotaiollama/blob/main/chat/management/commands/actualizar_auto.py): management command · Automated update command for continuous operation. · `chat/management/commands/actualizar_auto.py`

### Operations

Deployment, scheduling, logs, and runtime artifacts.

- [Deploy script](https://github.com/al3x2023/chatbotaiollama/blob/main/scripts/auto_deploy_vps.sh): VPS automation · Pulls code, installs dependencies, migrates, restarts, and checks health. · `scripts/auto_deploy_vps.sh`
- [systemd setup](https://github.com/al3x2023/chatbotaiollama/blob/main/scripts/instalar_systemd_vps.sh): service installer · Creates systemd units and timers for the app and pipeline. · `scripts/instalar_systemd_vps.sh`
- [Update script](https://github.com/al3x2023/chatbotaiollama/blob/main/scripts/actualizar_todo.py): ops script · Additional top-level automation for full updates. · `scripts/actualizar_todo.py`
- [PDF store](https://github.com/al3x2023/chatbotaiollama/tree/main/media/pdfs): artifact storage · Local storage for downloaded PDFs and processed documents. · `media/pdfs`
- [Logs](https://github.com/al3x2023/chatbotaiollama/blob/main/logs/chatbot.log): runtime logs · Persistent application and automation logs. · `logs/chatbot.log`
- [Knowledge CSV](https://github.com/al3x2023/chatbotaiollama/blob/main/conocimiento.csv): data artifact · Exported or intermediate knowledge artifact for pipeline use. · `conocimiento.csv`

## Connections

- 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.
