# superlinear-ai/raglite architecture

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

- Interactive diagram: https://gitdiagram.com/superlinear-ai/raglite
- Repository: https://github.com/superlinear-ai/raglite

## Overview

##### Main Components
- Core RAG Engine: At the heart of the project lies the orchestration of retrieval and generation flows. Modules such as _rag.py, _embed.py, _search.py, _split_chunks.py, and _split_sentences.py make up the core pipeline that performs document ingestion, search (hybrid, keyword, vector), reranking, and generation.
- Database & Storage: The components in _database.py manage interactions with either PostgreSQL or SQLite. This part is responsible for storing documents, indexes, and evaluations.
- LLM & Embedding Integrations: With support for multiple LLM providers through integration with LiteLLM and specific modules like _lazy_llama.py and _litellm.py, as well as embedding utilities, these layers manage the communication with external language models and embedding tools.
- Configuration & CLI: Module _config.py centralizes configurable parameters (database URL, LLM identifiers, embedder details) while _cli.py exposes command-line interface capabilities for both running pipelines and server options.
- External Interfaces and Extensions:
- Chat & Frontend Integration: Modules like _chainlit.py support a chainlit powered ChatGPT-like front end.
- Model Context Protocol (MCP) Server: Implemented via _mcp.py, this enables connections for MCP clients like Claude desktop.
- Utility and Conversion Tools: Modules such as _markdown.py (for PDF-to-Markdown conversion) facilitate document preprocessing.
- Query Adapter: The _query_adapter.py encapsulates the optimized query adapter functionality, solving a closed-form problem to adjust retrieval requests.
- Testing and Validation: The tests directory contains scripts that validate the functionality of these components.

##### Mapping Relationships and Interactions
- User interaction (CLI, Chat frontend, or MCP calls) will trigger the configuration loader and then push commands into the RAG pipeline.
- The RAG pipeline begins with document insertion and conversion (via _extract.py, _split_chunks.py, etc.), where documents are ingested, processed, and stored into the database.
- For retrieval, components perform different types of search (hybrid, keyword, vector) using _search.py combined with reranking logic (with support from external packages).
- Once relevant chunks are retrieved, the LLM integration layer (including _litellm.py and _lazy_llama.py) generates responses, possibly after applying any query adaptations computed in _query_adapter.py.
- Optional modules (chainlit and MCP) demonstrate how the toolkit can serve responses via a frontend or expose tool endpoints for LLMs.

##### Architectural Patterns and Design Principles
- Modularity and Layering: Each functional responsibility is encapsulated in its own module. This separation supports extensibility (e.g., adding new LLM providers or reranker implementations).
- Configurable Pipeline: The design allows for both “adaptive” and “programmable” RAG flows, making it flexible for different use cases.
- Extensibility and Integration: By providing options such as Chainlit integration for ChatGPT-like frontends and an MCP server, the toolkit is built to be easily integrated into larger stacks or to serve as a building block for more complex systems.

##### Key Technologies and Libraries to Note
- Python’s standard ecosystem for packaging and dependency management with pyproject.toml.
- Docker, Dev Containers, and GitHub workflows for development, testing, and deployment.
- External tools like LiteLLM, rerankers, PDF conversion libraries (pdftext, pypdfium2), and the Chainlit frontend.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Configuration & CLI Layer
    subgraph "Configuration & CLI"
        Config["Configuration (_config.py)"]:::config
        CLI["CLI (_cli.py)"]:::config
    end

    %% Core RAG Engine
    subgraph "Core RAG Engine"
        RAGEngine["RAG Engine (_rag.py)"]:::core
        Embed["Embedding (_embed.py)"]:::core
        Search["Search (_search.py)"]:::core
        SplitChunks["Split Chunks (_split_chunks.py)"]:::core
        SplitSentences["Split Sentences (_split_sentences.py)"]:::core
        Extract["Extract (_extract.py)"]:::core
        Insert["Insert (_insert.py)"]:::core
        QueryAdapter["Query Adapter (_query_adapter.py)"]:::core
    end

    %% Database & Storage
    subgraph "Database & Storage"
        Database["Database Module (_database.py)"]:::database
    end

    %% LLM & Embedding Integration
    subgraph "LLM & Embedding Integration"
        LiteLLM["LiteLLM Integration (_litellm.py)"]:::llm
        LazyLlama["Lazy Llama Integration (_lazy_llama.py)"]:::llm
    end

    %% Extensions
    subgraph "Extensions"
        Chainlit["Chainlit Integration (_chainlit.py)"]:::extensions
        MCP["MCP Server Integration (_mcp.py)"]:::extensions
    end

    %% Utility/Conversion Tools
    subgraph "Utility/Conversion Tools"
        Markdown["Markdown Conversion (_markdown.py)"]:::utility
    end

    %% Testing and Validation
    Tests["Testing and Validation (tests)"]:::tests

    %% Data Flow Connections
    CLI -->|"triggers"| RAGEngine
    Config -->|"configures"| RAGEngine

    Extract -->|"produces chunks"| SplitChunks
    Extract -->|"produces sentences"| SplitSentences
    SplitChunks --> Insert
    SplitSentences --> Insert
    Insert -->|"stores documents"| Database

    RAGEngine -->|"orchestrates"| Search
    RAGEngine -->|"handles embedding"| Embed

    Search --> QueryAdapter
    QueryAdapter -->|"adjusted query"| LiteLLM
    QueryAdapter -->|"adjusted query"| LazyLlama

    %% Utility feeds into ingestion pipeline
    Markdown -.->|"converts content"| Extract

    %% Optional Extensions triggered by CLI
    CLI ---|"optional frontend"| Chainlit
    CLI ---|"optional MCP server"| MCP

    %% Styling Classes
    classDef config fill:#cce5ff,stroke:#333,stroke-width:2px;
    classDef core fill:#ffedcc,stroke:#333,stroke-width:2px;
    classDef database fill:#d1e7dd,stroke:#333,stroke-width:2px;
    classDef llm fill:#e2e3e5,stroke:#333,stroke-width:2px;
    classDef extensions fill:#fff3cd,stroke:#333,stroke-width:2px;
    classDef utility fill:#d1c4e9,stroke:#333,stroke-width:2px;
    classDef tests fill:#f8d7da,stroke:#333,stroke-width:2px;

    %% Click Events for Component Mapping
    click Config "https://github.com/superlinear-ai/raglite/blob/main/src/raglite/_config.py"
    click CLI "https://github.com/superlinear-ai/raglite/blob/main/src/raglite/_cli.py"
    click RAGEngine "https://github.com/superlinear-ai/raglite/blob/main/src/raglite/_rag.py"
    click Embed "https://github.com/superlinear-ai/raglite/blob/main/src/raglite/_embed.py"
    click Search "https://github.com/superlinear-ai/raglite/blob/main/src/raglite/_search.py"
    click SplitChunks "https://github.com/superlinear-ai/raglite/blob/main/src/raglite/_split_chunks.py"
    click SplitSentences "https://github.com/superlinear-ai/raglite/blob/main/src/raglite/_split_sentences.py"
    click Extract "https://github.com/superlinear-ai/raglite/blob/main/src/raglite/_extract.py"
    click Insert "https://github.com/superlinear-ai/raglite/blob/main/src/raglite/_insert.py"
    click QueryAdapter "https://github.com/superlinear-ai/raglite/blob/main/src/raglite/_query_adapter.py"
    click Database "https://github.com/superlinear-ai/raglite/blob/main/src/raglite/_database.py"
    click LiteLLM "https://github.com/superlinear-ai/raglite/blob/main/src/raglite/_litellm.py"
    click LazyLlama "https://github.com/superlinear-ai/raglite/blob/main/src/raglite/_lazy_llama.py"
    click Chainlit "https://github.com/superlinear-ai/raglite/blob/main/src/raglite/_chainlit.py"
    click MCP "https://github.com/superlinear-ai/raglite/blob/main/src/raglite/_mcp.py"
    click Markdown "https://github.com/superlinear-ai/raglite/blob/main/src/raglite/_markdown.py"
    click Tests "https://github.com/superlinear-ai/raglite/tree/main/tests"
```
