Metadata-Version: 2.5
Name: brag-rag
Version: 0.1.0
Summary: RAG over DOCX/PPTX/XLSX with Docling, Chonkie and RAG-Anything
Project-URL: Homepage, https://github.com/Pakshalbhandari/brag
Project-URL: Repository, https://github.com/Pakshalbhandari/brag
Project-URL: Issues, https://github.com/Pakshalbhandari/brag/issues
Author: Pakshal
License-Expression: MIT
License-File: LICENSE
Keywords: claude,docling,docx,lightrag,office,pptx,rag,raganything,retrieval-augmented-generation,xlsx
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Text Processing :: Indexing
Classifier: Typing :: Typed
Requires-Python: >=3.12
Requires-Dist: anthropic>=1.14.0
Requires-Dist: chonkie>=1.7.0
Requires-Dist: docling>=2.139.0
Requires-Dist: openpyxl>=3.1.5
Requires-Dist: python-dotenv>=1.2.4
Requires-Dist: raganything>=1.4.2
Requires-Dist: sentence-transformers>=6.1.0
Description-Content-Type: text/markdown

# Office-document RAG

Retrieval-augmented Q&A over **DOCX, PPTX and XLSX** files, built from:

| Stage | Library | Role |
|---|---|---|
| Parse | [Docling](https://docling.ai/) | Converts Office files into a structured document: headings, paragraphs, lists, tables, images, speaker notes, sheets |
| Chunk | [Chonkie](https://github.com/feyninc/chonkie) | `RecursiveChunker` for prose, `TableChunker` for large tables (header repeated in every chunk) |
| Index + query | [RAG-Anything](https://github.com/HKUDS/RAG-Anything) (on LightRAG) | Multimodal knowledge graph + vector index; tables and images are analysed by the LLM / vision model |
| Models | Claude (`claude-opus-5-5`) + local `sentence-transformers` embeddings | LLM, vision and embeddings |

## Architecture

A rendered copy is at [docs/architecture.png](https://github.com/Pakshalbhandari/brag/blob/main/docs/architecture.png), for editors that don't render Mermaid.

```mermaid
flowchart TB
    subgraph SRC["Sources"]
        F["DOCX / PPTX / XLSX<br/>any European language"]
    end

    subgraph PARSE["1 · Parse — Docling"]
        DC["DocumentConverter"] --> DD["DoclingDocument<br/>reading order · BODY + NOTES layers"]
        DD --> W["Walker + normalize_text<br/>NFC · soft hyphens · no-break spaces"]
    end

    subgraph CHUNK["2 · Chunk — Chonkie"]
        RC["RecursiveChunker<br/>EUROPEAN_RULES"]
        TC["TableChunker<br/>row windows, header repeated"]
    end

    subgraph CL["3 · Content list — MinerU format"]
        TXT["text chunks<br/>breadcrumb + chunk separator"]
        TAB["table items"]
        IMG["image items<br/>PNG on disk"]
        EQ["equation items"]
    end

    subgraph RA["4 · Index — RAG-Anything + LightRAG"]
        LR["LightRAG text insert<br/>split only at chunk separators"]
        MP["Modal processors<br/>table · image · equation"]
        EX["Entity + relation extraction<br/>written in KG_LANGUAGE"]
    end

    subgraph MODELS["Models"]
        CL4["Claude claude-opus-5-5<br/>LLM + vision"]
        EMB["bge-m3<br/>local multilingual embeddings"]
    end

    subgraph STORE["5 · Storage — rag_storage/"]
        KV[("KV: docs · chunks · status")]
        VDB[("Vectors: chunks · entities · relations")]
        KG[("Knowledge graph")]
    end

    subgraph Q["6 · Query"]
        QU["Question, any language"] --> KW["Keyword extraction"]
        KW --> RET["Retrieve: vector search + graph traversal<br/>mode mix · hybrid · local · global · naive"]
        RET --> ANS["Answer in the question's language"]
    end

    F --> DC
    W -- "prose · lists · speaker notes" --> RC --> TXT
    W -- "table > 40 rows" --> TC --> TXT
    W -- "table ≤ 40 rows, or 20-row preview" --> TAB
    W -- pictures --> IMG
    W -- formulas --> EQ

    TXT --> LR --> EX
    TAB --> MP
    IMG --> MP
    EQ --> MP
    MP --> EX

    EX -. calls .-> CL4
    MP -. calls .-> CL4
    LR -. embeds .-> EMB

    LR --> KV
    LR --> VDB
    EX --> VDB
    EX --> KG

    KW -. calls .-> CL4
    RET --> VDB
    RET --> KG
    RET --> KV
    RET -. embeds .-> EMB
    ANS -. calls .-> CL4
```

Each Chonkie chunk ends with a `␞` separator, and LightRAG is told to split only on it, so LightRAG keeps Chonkie's chunk boundaries instead of re-chunking. Documents get IDs from a hash of their contents, so re-ingesting an unchanged file is a no-op.

## Installation

```bash
pip install brag-rag        # or: uv tool install brag-rag
```

This installs the `brag` command and the `brag` Python package. Then put your API key in the environment, or in a `.env` file in the directory you run `brag` from (see [.env.example](https://github.com/Pakshalbhandari/brag/blob/main/.env.example)):

```bash
export ANTHROPIC_API_KEY=sk-ant-...
```

The embedding model (`BAAI/bge-m3` by default, about 2.3 GB) downloads on first use. Switching it later means deleting `rag_storage/` and re-ingesting, because vector dimensions change.

### From source

```bash
uv sync
cp .env.example .env   # then set ANTHROPIC_API_KEY
uv run brag --help
```

## Usage

```bash
brag parse samples/sales.xlsx        # preview chunks; no API calls
brag ingest samples/ ~/Docs/q3.pptx   # files and/or folders (recursive)
brag query "Which region grew fastest?"
brag query "Summarise the Widget Pro launch" --mode hybrid
brag chat
```

Query modes: `mix` (graph + vector, default), `hybrid`, `local`, `global`, `naive` (vector only).

The index is stored in `./rag_storage` and parsed images in `./output`, relative to the current directory (set `WORKING_DIR` / `OUTPUT_DIR` to change this).

From Python:

```python
from dotenv import load_dotenv
from brag import RAGPipeline

load_dotenv()  # optional: the library reads settings from the environment only

pipeline = RAGPipeline()
await pipeline.ingest(["docs/"])
answer = await pipeline.query("What is the Q4 outlook?")
await pipeline.close()
```

## Multilingual documents

Documents in any European language (Latin, Greek and Cyrillic scripts, plus Armenian and Georgian) can be mixed in one index. No language detection or per-language setup is needed.

- **Normalisation** (`multilingual.normalize_text`): applies Unicode NFC, so decomposed accents such as `u` + `¨` become `ü`. It strips soft hyphens and zero-width characters, and turns no-break spaces (including French narrow ones before `?` and `!`) into normal spaces. NFC also folds the Greek question mark into `;`.
- **Chunk boundaries** (`multilingual.EUROPEAN_RULES`): splits at paragraphs, then sentences, then clauses, then words, then characters. Sentence ends include `. ! ? … ;` and the Armenian `։`, and also count when followed by a closing quote (`" ” “ » « ’`). Unlike Chonkie's defaults, the rules never split on `'` or `-`, which would break French and Italian elisions (`l'homme`, `dell'anno`) and hyphenated compounds.
- **Embeddings**: `bge-m3` places all languages in one vector space, so an English question retrieves German, French, Greek or Polish passages.
- **Knowledge graph**: entities and descriptions are written in one canonical language (`KG_LANGUAGE`, English by default). That way "Europäische Union" and "Union européenne" merge into a single entity. Proper nouns stay in their original language.
- **Answers** come back in the language of the question.

## Configuration

All settings are environment variables; see `.env.example`. The main ones:

- `LLM_MODEL`, `LLM_EFFORT`: the Claude model and effort used for ingestion (entity extraction, table and image analysis) and for answers. Ingestion makes several LLM calls per chunk, so for large corpora a lower effort or a cheaper model is the main cost lever.
- `CHUNK_SIZE`: Chonkie chunk size in `o200k_base` tokens. LightRAG's limit is set 256 tokens higher to leave room for the breadcrumb.
- `TABLE_MODAL_MAX_ROWS`: the cutoff between analysing a table as one unit and row-chunking it.
- `KG_LANGUAGE`: the language the knowledge graph is written in. Changing it after ingestion mixes languages in the graph, so re-index if you change it.

Requests use Claude's server-side refusal fallback (`fallbacks="default"`). If a safety classifier declines a request, it is retried on a fallback model.

## Layout

```
src/brag/cli.py           `brag` command
src/brag/config.py        settings
src/brag/parsing.py       Docling → Chonkie → RAG-Anything content list
src/brag/multilingual.py  Unicode normalisation + European chunking rules
src/brag/models.py        Claude LLM/vision + local embedding functions
src/brag/pipeline.py      RAGAnything wiring, ingest, query
samples/                  example files in English, German, French, Greek and Polish
```

## Releasing

1. Bump `__version__` in `src/brag/__init__.py`.
2. Optional dry run: run the Publish workflow by hand from the Actions tab; it uploads to [TestPyPI](https://test.pypi.org/project/brag-rag/). TestPyPI rejects re-uploads of the same version, so bump the version before each dry run.
3. Create a GitHub release tagged `v<version>` (e.g. `v0.2.0`).
4. The [publish workflow](https://github.com/Pakshalbhandari/brag/blob/main/.github/workflows/publish.yml) builds the sdist and wheel and uploads them to PyPI with trusted publishing.

To publish by hand instead: `uv build && uv publish` (needs a PyPI API token).

## License

[MIT](https://github.com/Pakshalbhandari/brag/blob/main/LICENSE)
