# theopensi/cosmic architecture

> Architecture diagram of the GitHub repository theopensi/cosmic, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-08). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/theopensi/cosmic
- Repository: https://github.com/theopensi/cosmic
- Video tour (about a minute): https://gitdiagram.com/theopensi/cosmic/video

## Overview

##### Main Components
- User Interface & Entry Points:
- Frontend chatbot UI (powered by Open-WebUI, as noted in the README with assets like chatbot_ui.png)
- Demo script (demo.py) and local scripts for initiating the CoSMIC pipeline (local_run.sh, docker_run.sh)
- Query Analysis:
- The LLM-based query analyser (located under src/query_analyser/ with key files query_analyser.py and user_prompt.py)
- Core Services:
- Chess service: Handles chess-specific tasks (services/chess.py and modules/chess/* for puzzle analysis, FEN generation, and chain-of-thought prediction)
- Vector database & RAG: For document retrieval and context use (src/services/vector_database.py and src/services/rag.py)
- General QA: For answering questions using LLMs (src/services/qa.py)
- Code generation: Integrates with the PyCapsule service (modules/code_generation/code_generation.py)
- External Dependencies & Tools:
- Stockfish chess engine (in third_party/stockfish)
- Fen-To-Board-Image (third_party/Fen-To-Board-Image) for visual representations of chess boards
- Docker components and related scripts (Dockerfile, docker_compile.sh, docker_run.sh, and modules/docker/*)

##### Relationships and Interactions
- The user query enters the system via the chatbot front end or demo scripts.
- The query analyser (LLM-based) takes the user input and routes it to the appropriate service(s).
- Each service (chess, QA, vector database, code generation) processes queries, possibly interacting with external dependencies (e.g., Stockfish for chess moves, PyCapsule for code generation).
- The vector database service underpins both context retrieval and document-based queries.
- There is a clear separation between modules that handle different aspects (data handling, LLM interfacing, and specific service logic).
- The system configuration is controlled centrally via config.yaml (found in scripts/configs), reinforcing a modular but integrated design strategy.

##### Architectural Patterns and Design Principles
- Modular architecture: Each service (chess, vector database, RAG, code generation, QA) is separated into its own Python module with dedicated functionality.
- Service-orientation and pipeline design: The query analyser acts as a router to select the most relevant service for each query.
- Containerization: Deployment is designed around Docker containers for encapsulation and isolation, with scripts provided for building and running containers.
- Extensibility: The use of submodules and integration with external services (such as PyCapsule and Open-WebUI) implies a design that easily invites extension and integration.
- Separation of concerns: Distinguishing between core logic (in src/ and modules/) and supporting utilities (in utils/ and scripts/).

##### Technologies and Frameworks
- Programming Language: Python 3.8+
- Deployment: Docker (with build and run scripts)
- External Libraries: Stockfish for chess engine, Fen-To-Board-Image for board rendering, various LLM libraries (Hugging Face, OpenAI, Ollama)
- Web Framework (for user interface): Open-WebUI integration (backend chatbot container)

##### Elements
- Clear labels for each component:
- An “External Services” block for dependencies like Stockfish and Fen-To-Board-Image.
- Data flow back from the services to the UI.
- Special notation for containerization and deployment:
- Inter-container communication (if applicable) or note that some components are deployed as independent services.

- User Interaction Layer (UI, CLI via demo.py)
- Query Routing and Analysis (LLM-based dispatcher)
- Core Service Modules (chess analysis, QA, vector database, code generation)
- External and Supporting Services (Stockfish, Fen-To-Board-Image, Docker orchestration)
- Configuration and Deployment (config.yaml, Docker scripts)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Interaction Layer
    subgraph "User Interaction Layer"
        subgraph "UI"
            ChatbotUI["Chatbot UI"]:::ui
            click ChatbotUI "https://github.com/theopensi/cosmic/tree/release/scripts/chatbot"
        end
        subgraph "Entry Scripts"
            Demo["demo.py"]:::script
            click Demo "https://github.com/theopensi/cosmic/blob/release/demo.py"
            Local["local_run.sh"]:::script
            click Local "https://github.com/theopensi/cosmic/blob/release/local_run.sh"
            DockerRun["docker_run.sh"]:::script
            click DockerRun "https://github.com/theopensi/cosmic/blob/release/docker_run.sh"
        end
    end

    %% Query Routing and Analysis
    subgraph "Query Routing and Analysis"
        QAOL["Query Analyser"]:::analyser
        click QAOL "https://github.com/theopensi/cosmic/blob/release/src/query_analyser/query_analyser.py"
    end

    %% Core Services
    subgraph "Core Services"
        Chess["Chess Service"]:::service
        click Chess "https://github.com/theopensi/cosmic/blob/release/src/services/chess.py"
        VectorDB["Vector Database"]:::service
        click VectorDB "https://github.com/theopensi/cosmic/blob/release/src/services/vector_database.py"
        RAG["RAG Service"]:::service
        click RAG "https://github.com/theopensi/cosmic/blob/release/src/services/rag.py"
        QAService["QA Service"]:::service
        click QAService "https://github.com/theopensi/cosmic/blob/release/src/services/qa.py"
        CodeGen["Code Generation"]:::service
        click CodeGen "https://github.com/theopensi/cosmic/blob/release/modules/code_generation/code_generation.py"
    end

    %% External Services
    subgraph "External Services"
        Stockfish["Stockfish Engine"]:::external
        click Stockfish "https://github.com/theopensi/cosmic/tree/release/third_party/stockfish"
        FenBoard["Fen-To-Board-Image"]:::external
        click FenBoard "https://github.com/theopensi/cosmic/tree/release/third_party/Fen-To-Board-Image"
    end

    %% Deployment & Configuration
    subgraph "Deployment & Configuration"
        DockerComponents["Docker Components"]:::deployment
        click DockerComponents "https://github.com/theopensi/cosmic/tree/release/Dockerfile"
        ConfigMgmt["Config Management"]:::config
        click ConfigMgmt "https://github.com/theopensi/cosmic/blob/release/scripts/configs/config.yaml"
    end

    %% Relationships from User Interaction Layer to Query Analyser
    ChatbotUI -->|"sends query"| QAOL
    Demo -->|"sends query"| QAOL
    Local -->|"sends query"| QAOL
    DockerRun -->|"sends query"| QAOL

    %% Relationships from Query Analyser to Core Services
    QAOL -->|"routes to"| Chess
    QAOL -->|"routes to"| VectorDB
    QAOL -->|"routes to"| RAG
    QAOL -->|"routes to"| QAService
    QAOL -->|"routes to"| CodeGen

    %% Relationships from Chess Service to External Services
    Chess -->|"uses"| Stockfish
    Chess -->|"uses"| FenBoard

    %% Deployment influences all major layers
    DockerComponents --- ChatbotUI
    DockerComponents --- QAOL
    DockerComponents --- Chess
    DockerComponents --- VectorDB
    DockerComponents --- RAG
    DockerComponents --- QAService
    DockerComponents --- CodeGen

    %% Configuration management links
    ConfigMgmt --- QAOL
    ConfigMgmt --- Chess
    ConfigMgmt --- VectorDB
    ConfigMgmt --- RAG
    ConfigMgmt --- QAService
    ConfigMgmt --- CodeGen

    %% Styles
    classDef ui fill:#AED6F1,stroke:#2E86C1,stroke-width:2px;
    classDef script fill:#D5F5E3,stroke:#27AE60,stroke-width:2px;
    classDef analyser fill:#F9E79F,stroke:#F1C40F,stroke-width:2px;
    classDef service fill:#F5B7B1,stroke:#E74C3C,stroke-width:2px;
    classDef external fill:#E8DAEF,stroke:#8E44AD,stroke-width:2px;
    classDef deployment fill:#D6EAF8,stroke:#3498DB,stroke-width:2px;
    classDef config fill:#FCF3CF,stroke:#F39C12,stroke-width:2px;
```
