This project has been created as part of the 42 curriculum by dgaillet.
RAG Against the Machine is a local Retrieval-Augmented Generation (RAG) system designed to answer questions about a source-code repository.
The project indexes the provided vLLM corpus, retrieves relevant source-code or documentation chunks, and passes those chunks to the local Qwen/Qwen3-0.6B language model. The model is instructed to answer only from the retrieved evidence to reduce hallucinations.
The pipeline contains four main stages:
- Indexing — read files, divide them into searchable chunks, and persist the indexes.
- Retrieval — rank chunks using BM25, semantic similarity, or hybrid retrieval.
- Augmentation — insert the retrieved source text into the model prompt.
- Generation — use Qwen to produce a concise, grounded answer.
Retrieval quality can be measured with recall@k, using file-path equality and character-range overlap against a ground-truth dataset.
- Python 3.10 or later
- Pydantic data models
- Python Fire command-line interface
- Progress bars with
tqdm - Configurable chunk size, with a maximum of 2,000 characters
- Separate Python and Markdown/text chunking strategies
- BM25 lexical retrieval
- Single-query and dataset retrieval
- Local answer generation with
Qwen/Qwen3-0.6B - Recall@k evaluation
- Persistent indexes under
data/processed/ - Graceful handling of common invalid inputs
- Semantic embeddings using
all-MiniLM-L6-v2 - Semantic retrieval through cosine similarity
- Hybrid BM25 and semantic retrieval
- Weighted Reciprocal Rank Fusion
- Incremental file chunking
- Search-result caching
- Answer caching
- Experimental local FastAPI interface
data/raw/
|
v
+---------------------+
| Indexer |
|---------------------|
| File discovery |
| Type-aware chunking |
| Character positions |
+---------------------+
| |
v v
+-----------+ +------------------+
| BM25 index| | Semantic vectors |
+-----------+ +------------------+
\ /
\ /
v v
+---------------+
Question ----------> | Search |
|---------------|
| BM25 |
| Semantic |
| Hybrid RRF |
+---------------+
|
v
Retrieved sources
|
v
+---------------+
| Answer |
|---------------|
| Build context |
| Ground prompt |
| Local Qwen |
+---------------+
|
v
Grounded answer
The indexer scans supported files under data/raw/, creates chunks, records their exact character positions, and persists:
- BM25 index data
- BM25 tokenizer vocabulary and stop words
- Semantic embeddings
- Chunk metadata
- File modification metadata
Generated files are stored under data/processed/.
The search engine loads the persisted indexes and supports three retrieval modes:
bm25semantichybrid
It returns MinimalSource objects containing:
- The exact corpus-relative file path
- The first character index
- The last character index
- An internal chunk identifier
The internal chunk_id is useful for index management, while the other three fields form the minimal source location expected by the project format.
The answer generator:
- Retrieves the top-k source chunks, unless sources are already supplied.
- Reads the exact text represented by each source range.
- Builds a prompt containing the question and retrieved evidence.
- Instructs Qwen to use only the supplied sources.
- Generates a concise answer.
- Caches the generated result.
The evaluator compares retrieved sources with ground-truth sources. A source is considered found when:
- Both sources have exactly the same
file_path. - Their character ranges have an Intersection over Union of at least
0.05.
Recall is reported at:
- Recall@1
- Recall@3
- Recall@5
- Recall@10
.
├── data/
│ ├── raw/
│ ├── processed/
│ ├── datasets/
│ │ ├── AnsweredQuestions/
│ │ └── UnansweredQuestions/
│ └── output/
│ ├── search_results/
│ └── search_results_and_answer/
├── src/
│ ├── classes/
│ │ └── models.py
│ ├── __init__.py
│ ├── __main__.py
│ ├── answer.py
│ ├── evaluate.py
│ ├── indexer.py
│ ├── local_llm.py
│ └── search.py
├── Makefile
├── pyproject.toml
├── uv.lock
└── README.md
The corpus contains both source code and natural-language documentation. These formats have different structures, so the project uses type-specific recursive splitters.
Python files use LangChain's Python-aware recursive splitter:
RecursiveCharacterTextSplitter.from_language(
language=Language.PYTHON,
...
)The splitter attempts to preserve meaningful Python boundaries such as classes, functions, and blocks before falling back to smaller separators.
Markdown files use the Markdown-aware splitter. It prioritizes structural boundaries such as:
- Headings
- Sections
- Paragraphs
- Lines
This helps keep related documentation together.
.rstfiles use the RST-aware splitter..txtfiles use the generic recursive character splitter.
Chunks use an overlap of 200 characters. The overlap reduces the chance that an important definition or explanation is lost at a chunk boundary.
Whitespace stripping is disabled, and each generated document includes its starting index. The source range is calculated as:
first_character_index = start_index
last_character_index = start_index + length_of_chunk
The last index is treated as an exclusive boundary when reading the source with Python slicing.
The default maximum chunk size is 2,000 characters:
uv run python -m src index --max_chunk_size 2000A smaller size may improve retrieval precision but can separate related information. A larger size provides more context but increases noise.
BM25 is the default mandatory lexical retrieval method.
During indexing:
- Chunk text is tokenized.
- English stemming is applied with PyStemmer.
- The BM25 index is created with
bm25s. - The index and tokenizer data are persisted.
During retrieval:
- The question is tokenized with the saved tokenizer.
- BM25 scores the indexed chunks.
- Results are ranked by relevance.
- The top-k source locations are returned.
BM25 is particularly effective when a question contains:
- Function names
- Class names
- Configuration options
- Command-line flags
- Error messages
- Other identifiers found directly in the corpus
Semantic retrieval uses:
sentence-transformers/all-MiniLM-L6-v2
Every indexed chunk is converted into a normalized embedding. A query is encoded with the same model, and cosine similarity is calculated using the dot product between normalized vectors.
Semantic retrieval can help when the question paraphrases the source instead of using its exact vocabulary.
Hybrid mode combines BM25 and semantic rankings using weighted Reciprocal Rank Fusion:
score(document) =
lexical_weight / (60 + lexical_rank)
+ semantic_weight / (60 + semantic_rank)
The default semantic weight is 0.05, giving BM25 a stronger influence:
lexical_weight = 0.95
semantic_weight = 0.05
Rank fusion is used instead of directly adding BM25 and cosine-similarity scores because those scores have different numerical scales.
Before selecting the final top-k results, the retriever can consider a larger candidate pool:
candidate_count = k × candidate_multiplier
The default candidate multiplier is 10.
File metadata is saved in:
data/processed/file_inf_cache.json
For each source file, the cache records:
- File path
- Modification timestamp in nanoseconds
- File size
On a later indexing run:
- Unchanged files reuse their existing chunks.
- New files are chunked.
- Modified files are chunked again.
- Chunks belonging to deleted files are removed.
The searchable indexes are then rebuilt from the resulting current chunk collection.
Search results are cached in:
data/processed/search_cache.json
Repeated identical queries can reuse previously retrieved sources.
Generated answers are cached in:
data/processed/answer_cache.json
The answer cache also records the value of k, because changing the number of retrieved sources can change the generated answer.
Delete these files when comparing retrieval configurations to avoid reusing results produced by an older configuration.
- Python 3.10 or later
uv- Sufficient disk space for:
- The vLLM corpus
- Transformer dependencies
- Qwen model weights
- Sentence-transformer weights
- Generated indexes
A GPU is optional. Qwen uses CUDA when it is available and otherwise runs on the CPU.
All mandatory commands use Python Fire and follow this form:
uv run python -m src <command> [arguments]uv run python -m src index --max_chunk_size 2000This reads supported files under data/raw/ and stores generated index files under data/processed/.
Do not set max_chunk_size above 2,000.
BM25 search:
uv run python -m src search \
"How is the OpenAI-compatible server configured?" \
--k 5Semantic search:
uv run python -m src search \
"How is the OpenAI-compatible server configured?" \
--k 5 \
--retrieval_mode semanticHybrid search:
uv run python -m src search \
"How is the OpenAI-compatible server configured?" \
--k 5 \
--retrieval_mode hybrid \
--semantic_weight 0.05 \
--candidate_multiplier 10Each result contains the exact file path and character range of a retrieved chunk.
uv run python -m src search_dataset \
--dataset_path data/datasets/UnansweredQuestions/dataset_docs_public.json \
--k 10 \
--save_directory data/output/search_results/UnansweredQuestionsHybrid dataset retrieval can be selected with:
uv run python -m src search_dataset \
--dataset_path data/datasets/UnansweredQuestions/dataset_docs_public.json \
--k 10 \
--save_directory data/output/search_results/UnansweredQuestions \
--retrieval_mode hybrid \
--semantic_weight 0.05 \
--candidate_multiplier 10The output file follows the StudentSearchResults Pydantic model.
uv run python -m src evaluate \
--student_search_results_path \
data/output/search_results/UnansweredQuestions/dataset_docs_public.json \
--dataset_path \
data/datasets/AnsweredQuestions/dataset_docs_public.jsonThis reports local recall at several values of k.
For the official evaluation, use the provided moulinette:
./moulinette evaluate_student_search_results \
data/output/search_results/UnansweredQuestions/dataset_docs_public.json \
data/datasets/AnsweredQuestions/dataset_docs_public.json \
--k 10 \
--max_context_length 2000uv run python -m src answer \
"How is the OpenAI-compatible server configured?" \
--k 5The answer generator retrieves sources with BM25 by default and passes their contents to Qwen/Qwen3-0.6B.
The first execution may download the model from Hugging Face.
uv run python -m src answer_dataset \
--student_search_results_path \
data/output/search_results/UnansweredQuestions \
--save_directory \
data/output/search_results_and_answer/UnansweredQuestionsThe generated output follows the StudentSearchResultsAndAnswer model.
Create the destination directory first if necessary:
mkdir -p \
data/output/search_results_and_answer/UnansweredQuestionsDataset search produces JSON in this form:
{
"search_results": [
{
"question_id": "q1",
"question": "How is the OpenAI-compatible server configured?",
"retrieved_sources": [
{
"file_path": "data/raw/vllm-0.10.1/docs/serving/openai_compatible_server.md",
"first_character_index": 9867,
"last_character_index": 10100,
"chunk_id": 42
}
]
}
],
"k": 5
}Dataset answer generation produces:
{
"search_results": [
{
"question_id": "q1",
"question": "How is the OpenAI-compatible server configured?",
"retrieved_sources": [
{
"file_path": "data/raw/vllm-0.10.1/docs/serving/openai_compatible_server.md",
"first_character_index": 9867,
"last_character_index": 10100,
"chunk_id": 42
}
],
"answer": "The server is configured by..."
}
],
"k": 5
}The additional chunk_id field is internal metadata permitted by the extensible output models.
make installmake runmake debugmake lintThis executes:
flake8 .
mypy . \
--warn-return-any \
--warn-unused-ignores \
--ignore-missing-imports \
--disallow-untyped-defs \
--check-untyped-defsmake lint-strictmake testmake cleanWarning: the current clean rule removes generated indexes, outputs, and files under data/datasets/. Preserve any dataset files you need before running it.
An experimental FastAPI interface is included as an additional feature.
Start it with:
make apiThe interactive API documentation is normally available at:
http://127.0.0.1:8000/docs
The mandatory and primary interface remains the Python Fire CLI.
The project requirements are:
| Metric | Required result |
|---|---|
| Full indexing time | At most 5 minutes |
| Retrieval time for 200 questions | At most 90 seconds |
| Documentation recall@5 | At least 80% |
| Code recall@5 | At least 50% |
| Maximum returned chunk length | 2,000 characters |
Performance should be measured on the target evaluation machine because CPU model speed, disk performance, corpus state, and cache state can significantly affect results.
Use the following commands to measure indexing and retrieval:
time uv run python -m src index --max_chunk_size 2000time uv run python -m src search_dataset \
--dataset_path data/datasets/UnansweredQuestions/dataset_docs_public.json \
--k 10 \
--save_directory data/output/search_results/UnansweredQuestionsRecord measured results before submission:
| Configuration | Index time | 200-query search time | Docs recall@5 | Code recall@5 |
|---|---|---|---|---|
| BM25, 2,000-character chunks | 5s | 7.5s | 84% | 53.5% |
| Semantic, 2,000-character chunks | 2.33m | 30s | 64% | 36.4% |
| Hybrid, semantic weight 0.05 | 2.38m | 30s | 85% | 54.5% |
- Larger chunks preserve more context but can introduce irrelevant terms.
- Smaller chunks improve precision but can divide related definitions and explanations.
- BM25 is fast and strong for exact identifiers.
- Semantic retrieval handles paraphrased questions better but is more expensive.
- Hybrid retrieval can improve coverage by combining both signals.
- Caching speeds up repeated queries but must be cleared for fair cold-run benchmarks.
- Semantic indexing adds model-encoding time to the indexing stage.
No benchmark values should be claimed until they have been measured on the actual corpus and machine.
Chunk metadata stores a file path and character range rather than duplicating every chunk's text in chunks.json.
Advantages include:
- Smaller metadata files
- Exact traceability to the original source
- Easy validation of returned ranges
- Grounding answers in the original file content
The evaluator compares file paths exactly. The project therefore stores paths as discovered under data/raw/, such as:
data/raw/vllm-0.10.1/docs/...
Python slicing naturally uses an exclusive end index:
content[first_character_index:last_character_index]Using the same convention during indexing and reading avoids off-by-one differences.
BM25 is fast, explainable, persistent, and particularly appropriate for code questions containing exact identifiers.
BM25 and cosine similarity scores are not directly comparable. Reciprocal Rank Fusion combines their rank positions without requiring score normalization.
Pydantic validates the data exchanged between indexing, retrieval, answer generation, dataset processing, and evaluation.
Recursive chunking is useful, but retrieval evaluation requires exact source locations. Whitespace stripping was disabled, and the splitters were configured to return each chunk's starting position.
Code and prose do not have the same natural boundaries. Separate language-aware splitters were used for Python, Markdown, and RST files.
The evaluator rejects any retrieved source wider than 2,000 characters. The index uses a configurable maximum size, and metadata is validated when the search engine loads.
BM25 scores and cosine similarities have different ranges. Weighted Reciprocal Rank Fusion avoids comparing their raw values directly.
Large repositories are expensive to process repeatedly. Modification timestamps and file sizes are stored so unchanged files can reuse their existing chunks.
Both required models can run on CPU, but model loading and generation may be slow. The embedding model uses batching, semantic embeddings are persisted, and repeated answers can be cached.
The Qwen system prompt explicitly tells the model to:
- Use only retrieved sources
- Avoid inventing information
- Admit when the sources are insufficient
Retrieval quality remains essential because generation cannot recover information that was not retrieved.
The CLI is designed to handle common invalid situations without exposing an unhandled traceback, including:
- Empty queries
- Non-positive
k - Missing index files
- Missing dataset files
- Malformed JSON
- Invalid retrieval modes
- Invalid semantic weights
- Unreadable source files
- Corrupted cache files
- Model-loading and generation errors
When a cache is malformed, the project reports a warning and rebuilds it rather than relying on invalid data.
- Lewis et al., Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks
https://arxiv.org/abs/2005.11401 - Robertson and Zaragoza, The Probabilistic Relevance Framework: BM25 and Beyond
https://www.staff.city.ac.uk/~sbrp622/papers/foundations_bm25_review.pdf - Reciprocal Rank Fusion
https://plg.uwaterloo.ca/~gvcormac/cormacksigir09-rrf.pdf bm25sdocumentation
https://github.com/xhluca/bm25s
- Qwen3-0.6B model page
https://huggingface.co/Qwen/Qwen3-0.6B - Sentence Transformers documentation
https://www.sbert.net/ all-MiniLM-L6-v2model page
https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2- Hugging Face Transformers documentation
https://huggingface.co/docs/transformers/
- Pydantic documentation
https://docs.pydantic.dev/ - Python Fire documentation
https://google.github.io/python-fire/ - LangChain text splitters
https://python.langchain.com/docs/concepts/text_splitters/ - tqdm documentation
https://tqdm.github.io/ - uv documentation
https://docs.astral.sh/uv/ - FastAPI documentation
https://fastapi.tiangolo.com/
AI was used as a development assistant for:
- Reviewing the project subject and extracting mandatory requirements
- Discussing RAG architecture and retrieval terminology
- Suggesting documentation structure
- Reviewing possible edge cases
- Explaining BM25, semantic retrieval, and Reciprocal Rank Fusion
- Helping draft and improve this README
AI-generated suggestions were treated as proposals rather than authoritative answers. The implementation, behavior, tests, performance measurements, and final submitted content were reviewed and remain the responsibility of the project author.
Before submission, verify the complete workflow:
uv sync
make lintuv run python -m src index --max_chunk_size 2000uv run python -m src search_dataset \
--dataset_path data/datasets/UnansweredQuestions/dataset_docs_public.json \
--k 10 \
--save_directory data/output/search_results/UnansweredQuestionsuv run python -m src evaluate \
--student_search_results_path \
data/output/search_results/UnansweredQuestions/dataset_docs_public.json \
--dataset_path \
data/datasets/AnsweredQuestions/dataset_docs_public.jsonmkdir -p \
data/output/search_results_and_answer/UnansweredQuestionsuv run python -m src answer_dataset \
--student_search_results_path \
data/output/search_results/UnansweredQuestions \
--save_directory \
data/output/search_results_and_answer/UnansweredQuestions