# hkuds/minirag architecture

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

- Interactive diagram: https://gitdiagram.com/hkuds/minirag
- Repository: https://github.com/hkuds/minirag

## Overview

The MiniRAG project is a Retrieval-Augmented Generation framework designed for on-device RAG systems. It is focused on enabling small language models to perform efficient retrieval and generation over a heterogeneous graph index. The project leverages lightweight graph-based knowledge retrieval and supports multiple graph database backends, various language model interfaces, and deployment options such as API and Docker-based setups.

##### Project Type and Purpose
- MiniRAG is a specialized full-stack library focused on retrieval-augmented generation. Its core purpose is to enable small language models to benefit from graph-based indexing and retrieval (heterogeneous graph indexing) to boost performance in RAG tasks.
- The project is experimental/research-oriented with production-grade deployment options (API, Docker) and includes benchmark datasets (like LiHua-World).

##### Analyzing the File Structure
- Top-Level Files:
- .gitignore & .pre-commit-config.yaml: Set up version control exclusions and pre-commit hook configurations.
- Dockerfile & docker-compose.yml: Provide containerization and multi-container orchestration instructions for deployment.
- LICENSE, MANIFEST.in, pyproject.toml, requirements.txt, setup.py: Files for legal, packaging, dependency management, and installation purposes.
- README.md, README_CN.md, README_JA.md: Provide multilingual documentation on usage, architecture, and system details.
- main.py: Likely an application entry point for utilizing MiniRAG.
- Directories:
- assets, graph-visuals: These directories store static assets and visualization scripts respectively.
- dataset: Contains the LiHua-World dataset including data files, query sets (CSV/JSON), and notebooks (evaluation.ipynb) that explain how data is organized and used in training or testing.
- reproduce: Holds reproduction scripts (e.g., Step_0_index.py, Step_1_QA.py) used to perform indexing and question-answering experiments.
- minirag: The core source package that comprises:
- api: Contains the API server code (minirag_server.py) along with environment configurations, static files (HTML/JavaScript), and setup documentation (README.md). This supports API and Docker deployment.
- base.py: Defines the base classes or core logic shared across the project.
- exceptions.py: Contains custom exception definitions.
- kg: A subpackage listing multiple implementations (e.g., neo4j_impl.py, oracle_impl.py, redis_impl.py, etc.) that serve as interfaces to different graph databases for heterogeneous graph indexing.
- llm & llm.py: These files provide support for interfacing with different language model services (azure_openai, cuffing with platforms like OpenAI, Hugging Face, etc.) and manage the generation component of the RAG system.
- minirag.py: Likely orchestrates or integrates various modules (retrieval, generation, and data processing) for the complete workflow.
- operate.py, prompt.py, utils.py: Supplementary modules for operational tasks, prompt management, and utility/helper functions.

##### Detailed File-Level Explanation
- .gitignore, .pre-commit-config.yaml: Define ignore patterns and enforce code quality through pre-commit hooks.
- Dockerfile, docker-compose.yml: Provide instructions and configurations for containerizing and orchestrating the application.
- LICENSE, MANIFEST.in, pyproject.toml, requirements.txt, setup.py: Manage licensing, package inclusion, dependency management, and project installation.
- README.md (and its translations in README_CN.md, README_JA.md): Document the project’s purpose, usage, and technical details.
- assets, graph-visuals: Store visual assets and scripts for visualizing the underlying graph structures – useful for understanding graph-based retrieval.
- dataset/LiHua-World: Contains raw data (in LiHuaWorld.zip), evaluation notebooks, and QA files (CSV and JSON) for testing the retrieval performance.
- main.py: Likely orchestrates a high-level execution path to initialize indexing and query-answering.
- minirag/
- __init__.py: Marks the directory as a Python module.
- base.py: Provides shared foundational logic.
- exceptions.py: Contains custom exception classes to handle errors.
- kg/ (e.g., neo4j_impl.py, oracle_impl.py, etc.): Each file implements an interface to a specific graph database for heterogeneous indexing.
- llm/ (e.g., azure_openai.py, bedrock.py, etc.): Each script provides integration with different language model services, forming the generation part of the RAG system.
- minirag.py: Integrates the main components (retrieval and generation) to implement the MiniRAG workflow.
- operate.py: May cover operational tasks such as orchestration or module integration.
- prompt.py: Likely holds prompt templates and example queries for use with the LLMs.
- utils.py: Contains helper functions used throughout the project.
- reproduce/ (Step_0_index.py, Step_1_QA.py): Scripts to reproduce the indexing and QA experiments, useful for benchmarking and testing system performance.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Data Feed & Preprocessing
    subgraph "Data Feed & Preprocessing"
        DS["Dataset Ingestion (raw data, LiHua-World, notebooks)"]:::preprocessing
        RS1["Indexing Experiment (Step_0_index.py: builds graph index)"]:::preprocessing
        RS2["QA Experiment (Step_1_QA.py: evaluates retrieval and QA)"]:::preprocessing
    end

    %% Graph Indexing Module
    subgraph "Graph Indexing Module"
        NEO["Neo4j Implementation (kg interface for graph DB)"]:::indexing
        PST["Postgres Implementation (kg interface for graph DB)"]:::indexing
    end

    %% Retrieval Module
    subgraph "Retrieval Module"
        RET["Retrieval Engine (leverages base.py for lightweight topology-enhanced retrieval)"]:::retrieval
    end

    %% LLM Interface
    subgraph "LLM Interface"
        AZ["Azure OpenAI Adapter (llm service adapter)"]:::llm
        OP["OpenAI Adapter (llm service adapter)"]:::llm
        LLM_SUP["LLM Support Module (llm.py provides additional integration)"]:::llm
    end

    %% API / Serving Layer
    subgraph "API / Serving Layer"
        API_SRV["API Server (minirag_server.py with static assets for endpoint serving)"]:::api
    end

    %% Orchestration / Integration
    subgraph "Orchestration / Integration"
        ORCH["Central Orchestrator (minirag.py ties retrieval & generation modules)"]:::orchestrator
        ENTRY["Application Entry Point (main.py initializes system workflow)"]:::orchestrator
    end

    %% Main Data Flow
    DS -->|"feeds data"| NEO
    DS -->|"feeds data"| PST
    RS1 -->|"triggers indexing"| NEO
    RS2 -->|"triggers QA"| PST

    NEO -->|"builds index"| RET
    PST -->|"builds index"| RET

    RET -->|"provides context"| AZ
    RET -->|"provides context"| OP

    AZ -->|"augments query"| LLM_SUP
    OP -->|"augments query"| LLM_SUP

    LLM_SUP -->|"generates response"| API_SRV

    %% Orchestration integration (dotted arrows)
    ENTRY -->|"initializes"| ORCH
    ORCH -.->|"manages"| NEO
    ORCH -.->|"manages"| PST
    ORCH -.->|"orchestrates"| RET
    ORCH -.->|"orchestrates"| AZ
    ORCH -.->|"orchestrates"| OP
    ORCH -.->|"orchestrates"| LLM_SUP
    ORCH -.->|"orchestrates"| API_SRV

    %% Click Events for component mapping
    click DS "https://github.com/hkuds/minirag/tree/main/dataset/LiHua-World"
    click RS1 "https://github.com/hkuds/minirag/blob/main/reproduce/Step_0_index.py"
    click RS2 "https://github.com/hkuds/minirag/blob/main/reproduce/Step_1_QA.py"
    click NEO "https://github.com/hkuds/minirag/blob/main/minirag/kg/neo4j_impl.py"
    click PST "https://github.com/hkuds/minirag/blob/main/minirag/kg/postgres_impl.py"
    click RET "https://github.com/hkuds/minirag/blob/main/minirag/base.py"
    click AZ "https://github.com/hkuds/minirag/blob/main/minirag/llm/azure_openai.py"
    click OP "https://github.com/hkuds/minirag/blob/main/minirag/llm/openai.py"
    click LLM_SUP "https://github.com/hkuds/minirag/blob/main/minirag/llm.py"
    click API_SRV "https://github.com/hkuds/minirag/blob/main/minirag/api/minirag_server.py"
    click ORCH "https://github.com/hkuds/minirag/blob/main/minirag/minirag.py"
    click ENTRY "https://github.com/hkuds/minirag/blob/main/main.py"

    %% Styles
    classDef preprocessing fill:#FFCCCC,stroke:#C00,stroke-width:2px;
    classDef indexing fill:#CCFFCC,stroke:#0C0,stroke-width:2px;
    classDef retrieval fill:#CCE5FF,stroke:#0077CC,stroke-width:2px;
    classDef llm fill:#FFFFCC,stroke:#CCCC00,stroke-width:2px;
    classDef api fill:#E0E0FF,stroke:#0000FF,stroke-width:2px;
    classDef orchestrator fill:#FFE0CC,stroke:#CC6600,stroke-width:2px;
```
