# owais-kamdar/mistral-chat architecture

> Architecture diagram of the GitHub repository owais-kamdar/mistral-chat, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/owais-kamdar/mistral-chat
- Repository: https://github.com/owais-kamdar/mistral-chat

## Overview

##### Project Type and Purpose
- This is a full-stack, local Q&A chatbot system with two “frontends”:
a) A command-line chat interface (main.py)
b) A Streamlit-based dashboard (dashboard.py)
- It orchestrates local inference against a self-hosted Mistral 7B model, performs document processing, captures metrics, persists logs, and visualizes them.

##### Core Components
a. User Interfaces (Frontends)
- CLI Chat (main.py): Read/write loop, upload PDF/TXT, commands (exit, clear, stats)
- Dashboard (dashboard.py): Streamlit app showing plots (Plotly+Pandas) of logged metrics
b. Backend Services
- Pipeline (pipeline.py): Orchestrates end-to-end flow: document enrichment, LLM calls, fallback/error handling
- Document Processor (document_processor.py): PDF/TXT parsing (PyPDF2), text chunking, embedding (sentence-transformers), semantic retrieval
- LLM Interface (llm.py): HTTP client to local Mistral model server (requests)
- System Monitor (system_monitor.py): psutil-based decorators for response time, memory, char counts, errors, session stats
- Logger (logger.py): File-based logging via loguru, writes to logs/app.log, metrics_*.jsonl, summary_*.json
- Utils (utils.py): Retry with exponential backoff
- Config (config.py): Centralized parameters (paths, LLM settings, doc-chunk sizes, retry policy)
c. Data Stores
- uploads/: Raw user documents (PDF/TXT)
- logs/:
- app.log (general events)
- metrics_*.jsonl (per-session granular records)
- summary_*.json (session summaries)
d. External Service

##### Interactions and Data Flows
1) User ➔ CLI Chat
- Uploads file ➔ stored in uploads/
- Enters query ➔ main.py
2) main.py ➔ Pipeline
- Passes user input + history
3) Pipeline ➔ Document Processor
- If document query, returns context chunks + embeddings
4) Pipeline ➔ LLM Interface
- Builds prompt with context + user query
- Requests generation via HTTP POST
5) System Monitor (decorators) wrap Pipeline & LLM calls
- Emits metrics (response time, memory, counts, errors)
6) Logger
- Receives events & metrics ➔ writes to logs/
7) Session continues or terminates
8) Dashboard reads logs/
- Builds Pandas DataFrame, uses Plotly to render time series, histograms
- Provides filtering by session timestamp

##### Architectural Patterns & Principles
- Modular single-responsibility components (each .py has one domain)
- Orchestration layer (pipeline.py) separates control flow from implementation
- Decorator-based instrumentation (system_monitor) for non-intrusive metrics
- Configuration centralization (config.py) for easy parameter tuning
- File‐based persistence for simplicity (no external DB)
- Retry/exponential backoff (utils.py) for resilience

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Frontend Layer
    subgraph "Frontends"
        direction TB
        CLI["CLI Chat (main.py)"]:::frontend
        Dash["Streamlit Dashboard (dashboard.py)"]:::frontend
    end

    %% Processing Pipeline Layer
    subgraph "Processing Pipeline"
        direction TB
        Plat["Pipeline Orchestrator (pipeline.py)"]:::processing
        DocProc["Document Processor (document_processor.py)"]:::processing
        LLM["LLM Interface (llm.py)"]:::processing
        Monitor["System Monitor (system_monitor.py)"]:::processing
        Logger["Logger (logger.py)"]:::processing
        Utils["Utils (utils.py)"]:::processing
        Config["Configuration (config.py)"]:::processing
    end

    %% Data Stores Layer
    subgraph "Data Stores"
        direction TB
        Uploads["uploads/"]:::storage
        Logs["logs/"]:::storage
    end

    %% External Services
    Mistral["Mistral-7B-Instruct Server\n(host:8080)"]:::external

    %% Flows
    CLI -->|"query / upload"| Plat
    CLI -->|"upload file"| Uploads
    Plat -->|"document query"| DocProc
    DocProc -->|"context chunks\n+ embeddings"| Plat
    Plat -->|"build prompt"| LLM
    LLM -->|"HTTP POST /prompt"| Mistral
    Mistral -->|"generated text"| LLM
    LLM -->|"response"| Plat
    Monitor -.->|"metrics decorators"| Plat
    Monitor -.->|"metrics decorators"| LLM
    Plat -->|"events / metrics"| Logger
    LLM -->|"errors / metrics"| Logger
    Logger -->|"write logs"| Logs
    Logs -->|"read metrics"| Dash
    Utils -->|"retry/backoff"| Plat
    Utils -->|"retry/backoff"| DocProc
    Utils -->|"retry/backoff"| LLM
    Config --> Plat
    Config --> DocProc
    Config --> LLM
    Config --> Monitor
    Config --> Logger
    Config --> Utils

    %% Click Events
    click CLI "https://github.com/owais-kamdar/mistral-chat/blob/main/main.py"
    click Dash "https://github.com/owais-kamdar/mistral-chat/blob/main/dashboard.py"
    click Plat "https://github.com/owais-kamdar/mistral-chat/blob/main/pipeline.py"
    click DocProc "https://github.com/owais-kamdar/mistral-chat/blob/main/document_processor.py"
    click LLM "https://github.com/owais-kamdar/mistral-chat/blob/main/llm.py"
    click Monitor "https://github.com/owais-kamdar/mistral-chat/blob/main/system_monitor.py"
    click Logger "https://github.com/owais-kamdar/mistral-chat/blob/main/logger.py"
    click Utils "https://github.com/owais-kamdar/mistral-chat/blob/main/utils.py"
    click Config "https://github.com/owais-kamdar/mistral-chat/blob/main/config.py"
    click Uploads "https://github.com/owais-kamdar/mistral-chat/tree/main/uploads/"

    %% Styles
    classDef frontend fill:#AEDFF7,stroke:#0366d6,color:#000;
    classDef processing fill:#CFFFCF,stroke:#028A0F,color:#000;
    classDef storage fill:#FFE0A3,stroke:#D17300,color:#000;
    classDef external fill:#E0E0E0,stroke:#7F7F7F,color:#000;
```
