Thesis RAG

A self-hosted retrieval system over a physics thesis library: 79 sources in four languages, searched by meaning and by keyword, every passage cited to the page, reachable from any MCP client.

Type
AI infrastructure / RAG
Technologies
  • Python
  • PostgreSQL
  • pgvector
  • BGE-M3
  • sentence-transformers
  • PyMuPDF
  • Whisper
  • Model Context Protocol
  • OAuth 2.1
  • Docker
Thesis RAG: local retrieval with citations Documents are extracted page by page, chunked inside page boundaries, embedded on the CPU and stored in PostgreSQL. Questions arrive over an authenticated MCP server, and a hybrid retriever returns passages cited to the page. Nothing leaves the network. EXTRACT CHUNK UPSERT ASK SEARCH QUERY SCORES Sources books · papers · transcripts Claude chat · Claude Code any MCP client Page extractor PDF · slides · Whisper AUTH MCP server per-user logins, read-only BGE-M3 embeddings multilingual, on CPU Eval harness 23 labelled questions Hybrid retriever vector + keyword, RRF DB PostgreSQL pgvector HNSW + full text Access and ranking Local store Client
Ingestion on the left, questions on the right, one PostgreSQL database between them. The MCP server and the retriever are highlighted because they decide who can ask and what comes back; the evaluation harness beside them is what shows whether a ranking change helped.

Overview

Daniel’s physics thesis is on violin acoustics, and its reading list grew past what anyone can keep in their head: textbooks on waves and elasticity, decades of violin acoustics papers, lecture slides, and hours of recorded meetings and lectures. This system indexes all of it locally and answers a question in Spanish or English with the passages that support it, each cited to its source and page. It is used from a chat assistant and from coding agents through the Model Context Protocol, and a second researcher has his own login. It is a research tool for two people, not a product, and it is Daniel’s first RAG system.

Problem

General chat assistants answer physics questions fluently and cite nothing, which is useless for a thesis where every claim needs a source and a page. The corpus also resists the obvious tooling. It mixes English, Spanish, French and Italian. Much of it is copyrighted books that cannot be uploaded to a hosted service. Scanned PDFs carry broken text layers. And some of the most useful material is spoken, in meetings and lectures, so it does not exist as text at all.

Solution

Keep everything on one home server. Extract each document page by page, so a citation always points at a real page, and transcribe recordings with a local Whisper model into five-minute pages. Chunk inside page boundaries, embed with a multilingual model on the CPU, and store vectors and full text side by side in PostgreSQL. A question runs a vector search and a keyword search, merges them with weighted reciprocal rank fusion, keeps one passage per page, and returns passages with a citation the assistant is told to quote. Adding a document is one entry in a registry file and one idempotent command.

Architecture

Ingestion: PyMuPDF extracts text per PDF page and takes section names from the document outline, slides become one page each, and transcripts are split into five-minute pages; chunks are about 220 words with a 40-word overlap and never cross a page. Embeddings: BGE-M3, 1024 dimensions, normalised, run locally. Storage: PostgreSQL with pgvector, an HNSW cosine index for the vectors and a generated full-text column with a GIN index for keywords. Retrieval: the top 40 from each side, fused by weighted rank and deduplicated by page, in about a tenth of a second once the model is warm. Access: a read-only MCP server with three tools (search, read a page, list sources) over stateless HTTP behind the same OAuth edge as the rest of the MCP fleet, with per-user logins: hashed passwords, tokens bound to a user and re-checked on every call, revocation that ends existing tokens at once, and an optional per-user list of sources.

Key decisions

Local over hosted, because copyrighted books cannot leave the network and the corpus is small enough for a CPU. One database for vectors and text, so hybrid search is a query rather than a second service. Cite pages, not chunks, because a reader checks a page. Measure before tuning: a labelled set of 23 questions in both languages, each tagged as conceptual or exact-term, scores every change on hit at 1, hit at 5 and mean reciprocal rank, and the comparison against a vector-only baseline is built into the harness. Keep the server read-only, and put the user check inside the token check, so revoking a collaborator ends his access on the next call rather than when a token expires.

Challenges

The instructive moment was a drop in the scores after a large batch of new sources. The easy reading was that the new documents were noise. The evaluation said otherwise, in three parts. Two questions had stale labels: new books legitimately answered them, so the page marked as the only correct answer no longer was. The keyword side ranked by a function that ignores how rare a term is, so a page with a rare exact term lost to pages full of common words; ranking by inverse document frequency fixed that and earned the keyword side a full weight in the fusion. And several chunks of one page could fill the top five, which deduplicating by page removed. Hit at 5 ended above where it started. Earlier lessons were smaller but real: on a bilingual corpus keyword search only matches the question’s own language, OCR text layers carry NUL bytes that PostgreSQL rejects, and a local folder named after the protocol SDK silently shadowed it.

Outcome

In daily use for the thesis since late October 2026: 79 sources, about 7,300 pages and 18,000 chunks in English, Spanish, French and Italian. On the 23-question set it puts a correct page in the top five for 19 questions. The weak spots are written down rather than hidden: a cross-encoder reranker would help but is too slow on this CPU, and a few exact-term questions still miss because a table caption and its row land in different chunks. As evidence it covers the whole arc of a RAG system, from ingestion and evaluation to access control, built and measured on a real corpus rather than a demo set.

Wondering if Daniel is a fit? Ask his assistant, hiring, contracts, or collaboration.