RAG against the machine
Python project · featured · RAG, BM25, LLM
Answers questions about the vLLM codebase: its own BM25 finds the right chunks among 11,582, then a 0.6B model answers from only those sources.





- 0.88recall@5 on docs (target 0.80)
- 0.85recall@5 on code (target 0.50)
- 11,582chunks from 1,179 files
- 4.4 sto index the whole corpus
The problem
Answer questions about vLLM, a codebase of more than a thousand files, with Qwen3-0.6B: a model far too small to know that code. The system has to find the two or three snippets that answer each question, and the grader only counts one when it is in the right file and overlaps the reference span (an IoU of at least 0.05).
What I built
- Two chunkers. Python files are parsed with ast and cut between top-level statements, with a class that is too big cut on its methods; Markdown is cut on headings outside code fences. The chunks tile each file exactly, so every result is a file path and two character offsets.
- A BM25 index of my own: term counts in a sparse SciPy matrix, weights computed once when it loads, and a query that adds up a few columns before argpartition picks the top k.
- A tokenizer that keeps an identifier whole and adds its snake_case and camelCase parts, so a question can match
max_num_seqsexactly or "maximum number of sequences" in words. - Answers from Qwen3-0.6B: the sources in rank order up to a 3,000-token budget, thinking turned off, greedy decoding, and an instruction to say so when the context does not hold the answer.
- A Fire CLI (index, search, answer and evaluate, on one question or a whole dataset) that survives empty queries, k=0 and broken JSON, plus all five bonuses: embeddings, hybrid ranking, incremental indexing, caching and an HTTP API.
- A workbench in the browser to study it: each stage explained and run live on the real index, a BM25 score split term by term with the formula filled in, and a dataset mode that shows which questions are missed and where their answer really is.
The hard part
Recall on code questions. Without the file path in the index, recall@5 on code was 0.72: many questions name the module they ask about, and code rarely repeats its own file name. Indexing each chunk with its path in front of it, twice, took code to 0.85 and docs from 0.84 to 0.88. Chunk size was the other half. Tiny chunks such as import blocks were noise, but merging too much hurt as well, because a correct 2,000-character chunk around a 60-character reference overlaps it by an IoU of 0.03 and does not count. Merging small neighbours up to 800 characters was the best of what I measured; without merging, code was at 0.78.
What it looks like
$ make evaluate Student data is valid: True Evaluation Results ======================================== Recall@1: 0.680 Recall@3: 0.830 Recall@5: 0.880 Recall@10: 0.910
Built with
- Python 3.10+
- NumPy + SciPy (sparse BM25)
- Qwen3-0.6B (transformers)
- Pydantic v2
- Python Fire
- FastAPI
- uv
- mypy --strict
- pytest
More projects
- metro · Lisbon metro sim in pixel art: watch the four lines run, step into any station, board a train and ride it.
- pac-man · Pac-Man in Python, built as a duo with sylvzzz: a new maze every level and four ghosts that each hunt you differently.
- call_me_maybe · Turns a sentence into a JSON function call with a 0.6B model using constrained decoding — parseable by construction, not by luck.
- codexion · Multithreaded twist on the Dining Philosophers: coders compete for shared dongles, with FIFO/EDF scheduling.
- dbaltaza.pt · A game-console portfolio where every project is a pre-rendered disc: Vite + TypeScript, no framework, and a paint budget the tests measure.
- Stock Loja · A stock app for PTDF's branded stores at Lisbon Airport: every size on the shop floor and in two stockrooms, sales by barcode, and six stores in one database that never see each other's data.
- fly-in · Drone fleet routing: Dijkstra pathfinding, turn-based scheduling and a pygame replay.
- a-maze-ing · Maze generator with multiple algorithms, pathfinding solvers and terminal visualization.
- push_swap · Highly optimized sorting with two stacks and a tiny set of allowed operations.
- ft_printf · printf rebuilt from scratch: chars, strings, pointers, decimals, unsigned and hex.
- get_next_line · Reads a file descriptor one line at a time with careful dynamic memory handling.
- Libft · My own C standard library — the foundation every later 42 project is built on.