# MiniRAG

> Graph-based RAG built so small, open language models can do the retrieval work

- Category: Retrieval & Knowledge (RAG) / RAG Frameworks & Platforms
- Language: Python
- Licence: MIT
- Repository: https://github.com/HKUDS/MiniRAG
- Website: https://arxiv.org/abs/2501.06713
- Canonical page: https://ai-tldr.dev/tools/minirag/
- Updated: 2025-02-27

## In short

MiniRAG is an open-source Python framework for retrieval-augmented generation (RAG) from HKUDS, accompanying the paper "MiniRAG: Towards Extremely Simple Retrieval-Augmented Generation" (arXiv 2501.06713). It sits in the RAG Frameworks & Platforms category of the Retrieval & Knowledge (RAG) tools and is open-source, written in Python and licensed MIT. To get started: install from source.

## Overview

MiniRAG is an open-source Python framework for retrieval-augmented generation (RAG) from HKUDS, accompanying the paper "MiniRAG: Towards Extremely Simple Retrieval-Augmented Generation" (arXiv 2501.06713). Its goal is narrow and practical: make RAG work well when the model doing the reading is a small language model (SLM) such as Phi-3.5-mini, Qwen2.5-3B or MiniCPM3-4B rather than a large hosted LLM. It is published on PyPI as minirag-hku and builds on the codebase of its sibling project LightRAG.

The design rests on two ideas. First, a semantic-aware heterogeneous graph index puts text chunks and named entities into one graph, with entity-entity and entity-chunk edges, so the index does not depend on a model's ability to write rich relationship descriptions. Second, a lightweight topology-enhanced retrieval step maps the query to likely answer types and entities, then walks the graph structure to find key relationships and the chunks attached to them, before handing the highest-scoring nodes and edges to the model to generate an answer.

According to the paper's abstract, this lets MiniRAG reach performance comparable to LLM-based methods even when using SLMs, while requiring only 25% of the storage space. The project also contributes LiHua-World, a benchmark of one year of chat records from a virtual user, with single-hop, multi-hop and summary questions for evaluating on-device RAG. Beyond the research scripts, the repository ships an optional FastAPI server (minirag-server) with document upload, query and Ollama-compatible chat endpoints, plus a Dockerfile and docker-compose file.

## What it does

- Heterogeneous graph indexing that links text chunks and named entities in one structure, reducing reliance on the model's semantic understanding
- Topology-enhanced graph retrieval (the "mini" query mode) that finds answers by walking graph paths rather than asking the model to reason over long context
- Pluggable storage backends in the minirag/kg package, including Neo4j, PostgreSQL, MongoDB, Redis, Milvus, Chroma, Weaviate, Oracle and NetworkX
- LLM and embedding bindings for Hugging Face models, Ollama, OpenAI, Azure OpenAI, Bedrock, LMDeploy, Jina, Zhipu and more
- Optional minirag-server REST API with /documents and /query endpoints, streaming, API-key protection and an Ollama-compatible interface for chat front ends such as Open WebUI
- Ships the LiHua-World dataset and reproduce scripts for benchmarking lightweight RAG against NaiveRAG, GraphRAG and LightRAG

## Getting started

The README recommends installing from source. You can then use MiniRAG as a Python library (see main.py), reproduce the paper's LiHua-World results, or run the optional API server against Ollama, OpenAI or another backend.

1. Install from source

   Clone the repository and install it in editable mode. Add the [api] extra if you want the minirag-server REST API. The package is also on PyPI as minirag-hku.

```bash
git clone https://github.com/HKUDS/MiniRAG.git
cd MiniRAG
pip install -e .
# with the API server:
pip install -e ".[api]"
```

2. Index and query with a small model

   Create a MiniRAG instance with a Hugging Face small model and embedding function, insert text, then query in "mini" mode. This is condensed from the repository's main.py.

```python
from minirag import MiniRAG, QueryParam
from minirag.llm.hf import hf_model_complete, hf_embed
from minirag.utils import EmbeddingFunc
from transformers import AutoModel, AutoTokenizer

EMBEDDING_MODEL = "sentence-transformers/all-MiniLM-L6-v2"

rag = MiniRAG(
    working_dir="./LiHua-World",
    llm_model_func=hf_model_complete,
    llm_model_max_token_size=200,
    llm_model_name="microsoft/Phi-3.5-mini-instruct",
    embedding_func=EmbeddingFunc(
        embedding_dim=384,
        max_token_size=1000,
        func=lambda texts: hf_embed(
            texts,
            tokenizer=AutoTokenizer.from_pretrained(EMBEDDING_MODEL),
            embed_model=AutoModel.from_pretrained(EMBEDDING_MODEL),
        ),
    ),
)

with open("notes.txt") as f:
    rag.insert(f.read())

print(rag.query("Your question here", param=QueryParam(mode="mini")))
```

3. Reproduce the LiHua-World benchmark

   The LiHua-World data ships in ./dataset/LiHua-World/data/ as LiHuaWorld.zip. Put any other dataset under ./dataset, then run the indexing and QA scripts.

```bash
python ./reproduce/Step_0_index.py
python ./reproduce/Step_1_QA.py
```

4. Run the API server

   With the [api] extra installed and Ollama running, minirag-server starts on port 9721 using mistral-nemo:latest for generation and bge-m3:latest for embeddings by default. Switch backends with flags, then upload documents and query over HTTP.

```bash
minirag-server
# or with OpenAI for both LLM and embeddings:
minirag-server --llm-binding openai --llm-model GPT-4o-mini --embedding-binding openai --embedding-model text-embedding-3-small

curl -X POST "http://localhost:9721/documents/file" -F "file=@/path/to/your/document.txt"
curl -X POST "http://localhost:9721/query" -H "Content-Type: application/json" -d '{"query": "Your question here", "mode": "hybrid"}'
```

## When to use it

- Run question answering over personal or company documents on a laptop or edge device with a 1.5B-4B parameter model instead of a hosted LLM
- Cut index storage for a graph-RAG deployment where LightRAG- or GraphRAG-style indexes are too heavy
- Benchmark small language models on multi-hop and summary questions with the bundled LiHua-World dataset
- Expose a local RAG index to Open WebUI or other Ollama-compatible chat front ends through minirag-server

## Version history

- 2025-02-27 v0.0.2: Release adding the API server and PyPI packaging: MiniRAG became installable with pip install minirag-hku.
- 2025-01-16 v0.0.1: First tagged release of the MiniRAG code accompanying the paper.
