Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

compactrag

Python arXiv 2 calls Ollama MIT

Verified end-to-end on Windows with local Ollama (llama3.1) — outputs below are from real runs.

compactrag implements CompactRAG (arXiv 2602.05728): multi-hop RAG normally burns an LLM call per reasoning hop (retrieve → think → retrieve → think...). CompactRAG's trade is to atomize the corpus offline into self-contained question→answer facts, so at query time a chain of hops becomes a chain of vector lookups — bounded by exactly two LLM calls: one to decompose the question, one to synthesize the answer.

For anyone paying per token at scale, "2 calls regardless of hop count" is a pricing model, not just a paper result.

Pipeline

flowchart LR
    subgraph offline [offline — paid once]
        C[corpus] --> A[atomize chunks into<br/>self-contained Q→A facts]
        A --> S[(question embeddings<br/>+ answer store)]
    end
    Q[multi-hop question] --> D["LLM call 1: decompose<br/>subs may reference {1}, {2}"]
    D --> L[vector lookups per sub<br/>0 LLM calls]
    S --> L
    L --> Y["LLM call 2: synthesize<br/>cited answer"]

    style D stroke:#ffbd2e,stroke-width:2px
    style L stroke:#7ee787,stroke-width:2px
    style Y stroke:#d2a8ff,stroke-width:2px
Loading

The detail that makes it work: atoms are embedded as questions, so sub-questions match in question-space (question↔question similarity is much tighter than question↔passage), and each atom is pronoun-free and self-contained so a lookup can't drag in ambiguity.

Quickstart

pip install -r requirements.txt
ollama pull llama3.1 && ollama pull nomic-embed-text

python cli.py build docs/       # offline: ~1 LLM call per chunk, once ever
python cli.py ask "Who manages the team that ships the MR-4?"
hop 1: Which team ships the MR-4?
   ↳ The MR-4 ships from the Austin facility, managed by the fulfilment team  [handbook_products.md#0]
hop 2: Who manages the fulfilment team?
   ↳ Marcus Chen, VP of Operations, manages the fulfilment team  [handbook_people.md#0]
┌ answer · 2 LLM calls total ┐

The bundled corpus is a small fictional company handbook with deliberate cross-document hops (product → team → manager; policy → role → person).

Configuration

Variable Default Purpose
OLLAMA_MODEL llama3.1 Decompose/synthesize/atomize model
EMBED_MODEL nomic-embed-text Question-space embeddings
GROQ_API_KEY Switch chat calls to Groq free tier

Honest scope note

The paper evaluates on HotpotQA/2Wiki-scale corpora with careful atomization QA. This implementation reproduces the mechanism — offline atomization, question-space matching, slot-referencing decomposition, the 2-call bound — at handbook scale where you can read every atom it created.


Built by Ahmad Bukhari — AI & Automation Architect · agentic systems that run real businesses, not just demos

About

Implementation of CompactRAG (arXiv 2602.05728): atomize the corpus into self-contained Q→A facts offline, then answer multi-hop questions in exactly 2 LLM calls — decompose + synthesize, with vector lookups doing the hops. Free local Ollama.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages