# TrustRAG

> A configurable, modular Python RAG framework built around "reliable input, trusted output", with citations, reranking and a Gradio studio

- Category: Retrieval & Knowledge (RAG) / RAG Frameworks & Platforms
- Language: Python
- Repository: https://github.com/gomate-community/TrustRAG
- Website: https://gomate-community.github.io/TrustRAG
- Canonical page: https://ai-tldr.dev/tools/trustrag/

## In short

TrustRAG is a configurable and modular retrieval-augmented generation (RAG) framework for Python, built by the GoMate team at the Key Laboratory of Network Data Science and Technology. It sits in the RAG Frameworks & Platforms category of the Retrieval & Knowledge (RAG) tools and is open-source, written in Python. To get started: create an environment and install.

## Overview

TrustRAG is a configurable and modular retrieval-augmented generation (RAG) framework for Python, built by the GoMate team at the Key Laboratory of Network Data Science and Technology. Its motto is "reliable input, trusted output": the goal is high-quality, trustworthy answers in retrieval-based question answering, and every stage of the pipeline is a separate module you can swap or tune for your own scenario.

The package is organised as a set of modules that map onto the RAG chain: document parsing and chunking, query rewriting, retrieval, reranking, a judger for document selection, a refiner for summarising context, a generator, prompts, and citation of answers back to their evidence. You can import those pieces directly and wire your own pipeline, or start from the ready-made RagApplication class and its Gradio front end, TrustRAG Studio, which manages a knowledge base and chats over it.

Beyond plain retrieval QA, the project includes a RAPTOR recursive tree retriever, a multimodal RAG example built on GLM-4V-Flash, a web-search engine supporting DuckDuckGo and SearXNG, and a DeepResearch pipeline that splits a question into sub-queries and loops through search, read, reflect, coding and answer actions within a token budget and action-depth limit to produce a research report. The framework is described in the arXiv paper "TrustRAG: An Information Assistant with Retrieval Augmented Generation" (arXiv:2502.13719).

## What it does

- Modular parsing of text, docx, ppt, excel, html, pdf and md files, with MinerU-based PDF parsing and a TextChunker that uses the InfiniFlow huqie tokenizer
- BM25, dense and hybrid retrievers with configurable weights, index building, incremental additions and saved indexes, plus a RAPTOR recursive tree retriever
- Rerankers ranging from BGE cross-encoders to LLM-based pointwise, pairwise and setwise reranking, a HyDE query rewriter and a BGE judge for document relevance
- Citation generation that links answer sentences back to the retrieved evidence
- Vector database engines such as Milvus and Qdrant, and a web-search engine for DuckDuckGo and SearXNG
- TrustRAG Studio, a Gradio app for uploading a corpus, setting chunk size and chatting in RAG or LLM-only mode, plus a Dockerfile for deployment

## Getting started

TrustRAG installs from PyPI or from source. The README walks through building a pipeline in code (parse, chunk, index, retrieve, rerank, generate) and then launching the TrustRAG Studio web app over your own documents.

1. Create an environment and install

   A conda environment is optional. Install the package from PyPI.

```bash
conda create -n trustrag python=3.12.0
conda activate trustrag
pip install trustrag
```

2. Or install from source

   Clone the repository to get the apps, examples and configs, then install it in editable mode.

```bash
git clone https://github.com/gomate-community/TrustRAG.git
cd TrustRAG
pip install -e .
```

3. Configure a hybrid retriever

   HybridRetrieverConfig combines a BM25 config and a dense-embedding config, each with its own weight, because the two scores are not on the same scale.

```python
from trustrag.modules.retrieval.bm25s_retriever import BM25RetrieverConfig
from trustrag.modules.retrieval.dense_retriever import DenseRetrieverConfig
from trustrag.modules.retrieval.hybrid_retriever import HybridRetriever, HybridRetrieverConfig

bm25_config = BM25RetrieverConfig(
    method='lucene',
    index_path='indexs/description_bm25.index',
    k1=1.6,
    b=0.7
)
dense_config = DenseRetrieverConfig(
    model_name_or_path=embedding_model_path,
    dim=1024,
    index_path='indexs/dense_cache'
)
hybrid_config = HybridRetrieverConfig(
    bm25_config=bm25_config,
    dense_config=dense_config,
    bm25_weight=0.7,
    dense_weight=0.3
)
hybrid_retriever = HybridRetriever(config=hybrid_config)
```

4. Build the index and retrieve

   Build the index from your chunked corpus and save it (or call load_index() on later runs), then query it.

```python
hybrid_retriever.build_from_texts(corpus)
hybrid_retriever.save_index()

results = hybrid_retriever.retrieve("Alipay", top_k=10)
for result in results:
    print(f"Text: {result['text']}, Score: {result['score']}")
```

5. Rerank and generate an answer

   Rerank the retrieved passages with a BGE reranker and pass them as context to the generator.

```python
from trustrag.modules.reranker.bge_reranker import BgeReranker, BgeRerankerConfig
from trustrag.modules.generator.llm import GLM4Chat

bge_reranker = BgeReranker(BgeRerankerConfig(model_name_or_path='llms/bge-reranker-large'))
glm4_chat = GLM4Chat(llm_model_path)

search_docs = hybrid_retriever.retrieve(question, top_k=10)
search_docs = bge_reranker.rerank(query=question, documents=[doc['text'] for doc in search_docs])
content = '\n'.join([f'Information[{idx}]：' + doc['text'] for idx, doc in enumerate(search_docs)])
answer = glm4_chat.chat(prompt=question, content=content)
```

6. Launch TrustRAG Studio

   Point the application config at your docs folder, embedding model and reranker, then start the Gradio app and open 127.0.0.1:7860 in a browser.

```bash
python app.py
```

7. Run the DeepResearch pipeline

   The DeepResearch module ships an .env.example for the LLM API and search settings; copy it, fill it in and run the pipeline.

```bash
cd trustrag/modules/deepresearch
cp .env.example .env
python pipeline.py
```

## When to use it

- Build a question-answering assistant over a private document collection with answers cited back to their sources
- Experiment with retrieval and reranking strategies (BM25, dense, hybrid, RAPTOR, LLM rerankers) by swapping one module at a time
- Give non-developers a web UI to upload a knowledge base and chat over it in RAG or LLM-only mode
- Generate research reports from web search with an iterative search, read and reflect loop
