FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

sumeromer/rag-tutorial · GitHub

Latest commit

 

History

8 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EU AI Act RAG — Tutorial

A self-contained Retrieval-Augmented Generation pipeline over Verordnung (EU) 2024/1689 (KI-Verordnung), built with open-source models and FAISS.

Slides

View the accompanying tutorial talk (in German): https://sumeromer.github.io/rag-tutorial/

Pipeline overview

Step Module What it does
Load src/load.py Downloads (or reads cached) EU AI Act HTML from EUR-Lex
Chunk src/chunk.py Structural chunking: one chunk per Erwägungsgrund / Artikel / Anhang, with Absatz-level sub-splitting for long articles
Index src/retrieve.py BGE-M3 dense embeddings + FAISS
Retrieve src/retrieve.py BM25 + dense → RRF fusion → cross-encoder reranking (top-20 → top-5)
Generate src/generate.py Qwen2.5-14B-Instruct with grounded citation prompt

Quick start

1. Create the environment

conda env create -f environment.yaml
conda activate rag-tutorial

Or, with an existing Python 3.12 environment (the dependencies are declared in pyproject.toml, which environment.yaml installs from):

pip install -e ".[dev]"

2. Build the index

python run.py build

On first run the EU AI Act HTML (~1.3 MB) is downloaded automatically from EUR-Lex and cached to data/eu_ai_act_de.html. The FAISS index is then built over ~460 chunks with BAAI/bge-m3 and saved to faiss_index/.

Expected build time: ~5–15 min on CPU · ~2–5 min on GPU/MPS.

3. Ask a single question

# with RAG (default)
python run.py ask "Welche KI-Praktiken sind nach Artikel 5 verboten?"

# without RAG — model knowledge only
python run.py ask "Welche KI-Praktiken sind nach Artikel 5 verboten?" --no-rag

# side-by-side comparison
python run.py ask "Welche KI-Praktiken sind nach Artikel 5 verboten?" --compare

4. Run the 15-question benchmark

Note: This is not a rigorous evaluation; these 15 questions serve qualitative illustration and tutorial purposes only. A quantitative assessment of how much retrieval improves answer accuracy and reliability would require a larger, representative benchmark spanning the breadth of the EU AI Act.

python compare.py

Runs 15 sample questions (10 in-scope + 5 out-of-scope) with and without RAG. Results are saved incrementally to data/comparison_output.json after each question, so a partial run is not lost.

Models

Role Model Notes
Embedder BAAI/bge-m3 ~570 MB; multilingual MTEB SOTA
Reranker BAAI/bge-reranker-v2-m3 Cross-encoder; top-20 → top-5
Reader Qwen/Qwen2.5-14B-Instruct ~28 GB bf16

All models are downloaded automatically on first use via Hugging Face.

Hardware

Tested on Apple M2 Max (96 GB) with MPS backend (PYTORCH_ENABLE_MPS_FALLBACK=1).
On CUDA: bitsandbytes is installed for optional quantization (not enabled by default).
On CPU only: generation is very slow (~5–10 min/question).

File structure

.
├── src/
│   ├── config.py      # paths, model names, device selection
│   ├── load.py        # HTML download + text normalization
│   ├── chunk.py       # structural + Absatz-level chunking
│   ├── retrieve.py    # FAISS index, BM25, RRF fusion, reranker
│   └── generate.py    # Qwen2.5-14B reader + system prompt
├── data/
│   └── eu_ai_act_de.html        # auto-downloaded on first run
├── faiss_index/                 # created by `python run.py build`
├── slides/
│   ├── 2026-05-29-RAG-Tutorial.qmd   # public deck (speaker notes stripped)
│   └── build-public.py              # regenerates the public deck from the local one
├── run.py                       # CLI: build / ask / compare
├── compare.py                   # 15-question benchmark
├── pyproject.toml               # dependencies + ruff config
└── environment.yaml             # conda env (installs from pyproject.toml)

Development

Dependencies live in pyproject.toml (single source of truth). Lint with ruff:

ruff check .          # report issues
ruff check . --fix    # apply safe auto-fixes

The slide deck has two versions: a private *.local.qmd (git-ignored) that contains the speaker notes, and the committed 2026-05-29-RAG-Tutorial.qmd with those notes stripped. Regenerate the public deck after editing the local one:

python slides/build-public.py

Output format (data/comparison_output.json)

[
  {
    "id": 1,
    "scope": "in-scope",
    "question": "Was ist das Hauptziel der KI-Verordnung der EU?",
    "with_rag": "...",
    "sources": ["Erwägungsgrund (1)", "Artikel 1 — Gegenstand"],
    "without_rag": "..."
  },
  ...
]

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages


Back | FazBrowse Home | New Git URL