MiniRAG: Retrieval-Augmented Generation for Small Language Models

Ketan Kumar Last Updated : 23 Jan, 2025
5 min read

The growing demand for efficient and lightweight Retrieval-Augmented Generation (RAG) systems in
resource-constrained environments has revealed significant challenges. Existing frameworks depend heavily on Large Language Models (LLMs), resulting in high computational costs and limited scalability for edge devices. Addressing this, researchers from the University of Hong Kong introduce MiniRAG,
a novel framework optimized for simplicity and efficiency.

Learning Objectives

  • Understand the challenges faced by traditional RAG systems and the need for lightweight frameworks like MiniRAG.
  • Learn how MiniRAG integrates Small Language Models (SLMs) with graph-based indexing for efficient retrieval and generation.
  • Explore the core components of MiniRAG, including Heterogeneous Graph Indexing and Topology-Enhanced Retrieval.
  • Gain insight into the advantages of MiniRAG in resource-constrained environments, such as edge devices.
  • Understand the implementation process and hands-on setup for MiniRAG to deploy on-device AI applications.

This article was published as a part of the Data Science Blogathon.

Problem with Current RAG Systems

LLM-centric RAG frameworks perform well in tasks requiring semantic understanding and reasoning. However, they are resource-intensive and unsuitable for scenarios involving edge devices or privacy-sensitive applications. Attempts to replace LLMs with Small Language Models (SLMs) often fail due to:

  • Reduced semantic understanding.
  • Difficulty in processing large noisy datasets.
  • Ineffectiveness in multi-step reasoning.

MiniRAG Framework

The MiniRAG framework represents a significant departure from traditional Retrieval-Augmented Generation (RAG) systems by designing a lightweight, efficient architecture tailored for Small
Language Models (SLMs). It achieves this through two core components: Heterogeneous Graph Indexing and Lightweight Graph-Based Knowledge Retrieval.

MiniRAG Framework: Simplifying Retrieval-Augmented Generation
Source: MINIRAG

Heterogeneous Graph Indexing

At the heart of MiniRAG is its innovative Heterogeneous Graph Indexing mechanism, which simplifies knowledge representation while addressing SLMs’ limitations in semantic understanding.

Key Features

  • Dual-Node Design:
    • Text Chunk Nodes: Segments of the source text that retain context and coherence, ensuring relevant information is preserved.
    • Entity Nodes: Key semantic element extracted from text chunks, such as events, locations, or concepts, that anchor retrieval efforts.
  • Edge Connections:
    • Entity-Entity Edges: Capture relationships, hierarchies, and dependencies between entities.
    • Entity-Chunk Edges: Links entities to their originating text chunks, preserving contextual relevance.

How It Works?

  • Entity and Chunk Extraction: Text is segmented into chunks, and entities are identified within these chunks.
  • Graph Construction: Nodes (chunks and entities) are connected via edges that represent relationships or contextual links.
  • Semantic Enrichment: Edges are annotated with semantic descriptions, providing additional context to enhance retrieval accuracy.

Advantages

  • Reduced Dependence on Semantic Understanding: By focusing on structural relationships instead of complex semantics, this indexing method compensates for SLMs’ limitations.
  • Efficient Representation: The compact graph structure minimizes the computational load, making it ideal for on-device applications.

Lightweight Graph-Based Knowledge Retrieval

MiniRAG’s retrieval mechanism leverages the graph structure to enable precise and efficient query resolution. This component is designed to maximize the strengths of SLMs in localized reasoning and pattern matching.

Key Features

  • Query Semantic Mapping:
    • SLMs extract entities and predict answer types from the query.
    • The query is aligned with the graph’s nodes through a lightweight sentence embedding model.
  • Reasoning Path Discovery:
    • Relevant entities and their connections are identified by analyzing graph topology and semantic relevance.
    • Paths between nodes are score based on their importance to the query.
  • Topology-Enhanced Retrieval:
    • Combines semantic relevance with structural coherence to discover meaningful reasoning paths.
    • Reduces noise in retrieval by focusing on key relationships and connections within the graph.

How It Works?

  • Query Processing: The system extracts entities and expected answer types from the input query.
  • Path Exploration: The system traverses the graph to identify reasoning paths that connect query-related nodes.
  • Text Chunk Retrieval: The system identifies and ranks relevant text chunks based on their alignment with the query and graph structure.
  • Response Generation: The system generates a response using the retrieved information, integrating key insights from the graph.

Advantages

  • Precision and Efficiency: By relying on graph topology, MiniRAG minimizes reliance on embeddings and advanced semantic processing.
  • Adaptability: The lightweight retrieval mechanism ensures robust performance across diverse datasets and use cases.

MiniRAG Workflow

The overall process integrates the above components into a streamlined pipeline:

  • Input Query: The system receives a query and predicts relevant entities and answer types.
  • Graph Interaction: The query is mapped onto the heterogeneous graph to identify relevant nodes, edges, and paths.
  • Knowledge Retrieval: The system retrieves text chunks and relationships most relevant to the query.
  • Output Generation: Using the retrieved information, MiniRAG generates a response tailored to the input query.

Significance of the MiniRAG Framework

The MiniRAG framework’s innovative design ensures:

  • Scalability: Operates efficiently with resource-constrained SLMs.
  • Robustness: Maintains performance across various data types and scenarios.
  • Privacy: Suitable for on-device deployment without reliance on external servers.

By prioritizing simplicity and efficiency, MiniRAG sets a new benchmark for RAG systems in low-resource environments.

Hands-On with MiniRAG

MiniRAG is a lightweight framework for Retrieval-Augmented Generation (RAG), designed to work efficiently with Small Language Models (SLMs). Here’s a step-by-step guide to demonstrate its capabilities.

Step 1: Install MiniRAG

Install the required dependencies:

# Install MiniRAG and dependencies
!git https://github.com/HKUDS/MiniRAG.git

Step 2: Initialize MiniRAG

First, initialize MiniRAG:

cd MiniRAG
pip install -e .

Step 3: Running Scripts

  • All the code can be found in the ./reproduce
  • Download the dataset you need.
  • Put the dataset in the ./dataset directory.
  • Note: We have already put the LiHua-World dataset in ./dataset/LiHua-World/data/ as LiHuaWorld.zip. If you want to use other dataset, you can put it in the ./dataset/xxx

Then use the following bash command to index the dataset:

python ./reproduce/Step_0_index.py
python ./reproduce/Step_1_QA.py

Or, use the code in ./main.py to initialize MiniRAG.

miniRAG Output

Implications for the Future

MiniRAG’s lightweight design opens avenues for deploying RAG systems on edge devices, balancing efficiency, privacy, and accuracy. Its contributions include:

  • A novel approach to indexing and retrieval optimized for SLMs.
  • A comprehensive benchmark dataset for evaluating on-device RAG capabilities.

Conclusion

MiniRAG bridges the gap between computational efficiency and semantic understanding, enabling scalable and robust RAG systems for resource-constrained environments. By prioritizing simplicity and leveraging graph-based structures, it offers a transformative solution for on-device AI applications, ensuring privacy and accessibility.

Key Takeaways

  • MiniRAG optimizes Small Language Models (SLMs) to enable efficient Retrieval-Augmented Generation (RAG) systems.
  • It combines Heterogeneous Graph Indexing and Topology-Enhanced Retrieval to enhance performance without relying on large models.
  • MiniRAG’s graph-based approach significantly reduces computational costs and storage requirements compared to traditional RAG systems.
  • It provides a scalable, robust solution for resource-constrained environments, particularly edge devices, while ensuring privacy.
  • By simplifying retrieval and leveraging graph structures, MiniRAG addresses the challenges of using SLMs for semantic understanding and reasoning.
Q1. What is MiniRAG?

A. The MiniRAG framework integrates Small Language Models (SLMs) with graph-based indexing and retrieval, making it a lightweight solution for Retrieval-Augmented Generation (RAG). It is designed to function efficiently in resource-constrained environments, such as edge devices, where large language models (LLMs) may not be practical.

Q2. What are the key features of MiniRAG?

A. MiniRAG features Heterogeneous Graph Indexing, which combines text chunks and named entities in a unified graph structure, reducing reliance on complex semantic understanding. It also utilizes Topology-Enhanced Retrieval, leveraging graph structures to efficiently retrieve relevant information, ensuring high performance even with limited model capabilities. The framework’s lightweight design requires only 25% of the storage space compared to traditional LLM-based RAG systems.

Q3. How does MiniRAG differ from other RAG systems?

A. Most RAG systems rely heavily on LLMs for semantic understanding and complex reasoning, which can be computationally expensive. MiniRAG prioritizes structural knowledge representation and graph-based retrieval, enabling robust performance with small models.

Q4. What models does MiniRAG support?

A. MiniRAG supports several Small Language Models (SLMs), including:
microsoft/Phi-3.5-mini-instruct
THUDM/glm-edge-1.5b-chat
openbmb/MiniCPM3-4B
Qwen/Qwen2.5-3B-Instruct

The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.

I'm a Data Scientist at Syngene International Limited. I have completed my Master's in Data Science from VIT AP and I have a burning passion for Generative AI. My expertise lies in building robust machine learning and NLP models for innovative projects. Currently, I'm putting this knowledge to work in drug discovery research at Syngene, exploring the potential of LLMs. Always eager to learn and delve deeper into the ever-evolving world of data science and AI!

Responses From Readers

Clear

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details