Projects

Roman Legion RAG

A grounded conversational assistant built on historical sources and website articles

2026·RAG·Python·Pinecone

Built as a 4th-year solo engineering project for Legio Memoriae Aeternae and currently awaiting deployment, this assistant answers questions about the Roman army using the website’s content and historical reference works.

General-purpose language models tend to mix historical periods or invent details when asked about extremely specific topics. Using a RAG (Retrieval-Augmented Generation) pipeline to fix this problem rather than fine-tuning made it possible to ground answers in specific excerpts, while citing clickable sources, and being able to update the knowledge base without retraining a model.

Grounded RAG response: Exact historian figures, clickable citations, and inline glossary links

Data Ingestion & Chunking

Because output accuracy depends directly on the quality of the source, a Python pipeline adapts the chunking strategy to each source format instead of cutting at a fixed character count:

  • Website content: A regex parser extracts the ~200 Latin glossary entries from the TypeScript source (one definition per chunk), while Markdown articles are split by heading and every chunk is prefixed with its page title so excerpts keep their context.
  • Historical books (EPUB & PDF): Five reference works are split at sentence boundaries into ~300-word chunks with a two-sentence sliding overlap. Before splitting, historical abbreviations (J.-C., leg., coh.) are temporarily masked so periods do not trigger false breaks.
  • Cleaning noisy sources: Footnote markers and spacing artifacts are stripped from EPUB HTML with BeautifulSoup, while PDF pages are cropped via pdfplumber bounding boxes to remove headers and page numbers. One book with curved-binding OCR distortion was excluded altogether, and corrupted tables or symbol blocks are filtered out automatically using length and character-ratio thresholds.

Vector Indexing & Multi-Pass Retrieval

Each chunk is embedded with Google’s gemini-embedding-2 model truncated from 3,072 down to 768 dimensions via Matryoshka Representation Learning (MRL) to reduce storage and search cost by a factor of four. Vectors are stored in Pinecone alongside the raw text in metadata.text to avoid querying a second database. To save API calls when updating the sources, a local MD5 hash registry ensures only new or modified chunks are re-embedded.

At query time, follow-up questions in multi-turn conversations are first rewritten into standalone queries using Qwen 3.8 27B on Groq. The top 12 Pinecone matches above a 0.40 similarity threshold are then narrowed down to 6 chunks using a three-pass selection rule:

  1. Up to 3 book chunks and 3 website chunks, with at most 2 per source.
  2. Drops the book/site parity if needed while keeping the 2-per-source limit.
  3. Allows up to 4 chunks from a single major reference book if fewer than 6 chunks remain.

If no document reaches the 0.40 threshold, the pipeline returns a standard refusal without calling the generation model.

Multi-turn follow-up and RAG telemetry: Query rewriting with Groq and stage latencies

Generation, Interface & Safeguards

After testing gpt-oss-120b on Groq, which struggled to follow instructions consistently and produced weak prose, I switched to Gemini 3.8 Flash via Server-Sent Events (SSE) streaming. Google’s free tier introduced multi-minute delays, so the backend runs on a prepaid billing account to keep latency low without uncapped cost risk.

The system prompt enforces a three-tier policy: facts taken from retrieved chunks require numbered citation markers, undisputed general Roman background is allowed without fake citations, and out-of-scope or unverifiable questions are declined directly.

On the frontend, the assistant lives on a dedicated full-screen page matching the aesthetic of the website. A custom Markdown renderer turns citation markers into live clickable badges and links recognized Latin terms to their glossary tooltips as tokens stream in, pausing auto-scroll whenever the user scrolls up. A source summary card appears once generation finishes and conversation history persists in localStorage.