Writings

We built a RAG chat for our archive, then replaced it

· 3 min read

At Jawafdehi we wanted people to ask our archive of corruption cases questions in plain language, and get answers that point back to the documents. This is how the first version was built, and why we stopped maintaining it.

version one: an agent with tools

The first version was a chat API in our Django backend. A model got the question and a set of tools from our own MCP server: search cases, look up a person, open a document. Which tools it could use, and with which prompt, were set by configuration rather than code, so we could try changes without a deploy.

Tools alone were weak for one kind of question: ones answered somewhere inside a long document. Search could find the right report, but not the right paragraph.

adding retrieval

So I added retrieval. Documents were split into chunks of about 1,200 characters with some overlap, or page by page where pages made sense, and each chunk was embedded and stored in Postgres with pgvector. Keeping it in the database we already ran meant no new service to operate.

Retrieval was hybrid. Postgres full-text search, weighted so a match in a title counts more than one in body text, was combined with vector similarity. Full-text catches exact names and case numbers that embeddings blur, and vectors catch questions worded differently from the document.

Then MMR picked the final chunks, balancing how relevant each was against how much it repeated the ones already picked. Without it, the context often filled up with near-identical chunks from the same page.

Answers had to cite their chunks, and citations were deduplicated by chunk and page so one passage didn't appear three times. The retriever also respected access rules, so a public chat could only see public material.

simplifying

At first a routing step classified each question and sent it down a different path, with a fixed set of rules as a fallback. It added a layer that could be wrong on its own. I replaced it with a single agent that had retrieval as one more tool, and let the model decide.

Some smaller things mattered more than expected: passing document conversion errors back to the model so it could say it couldn't read a file instead of making something up, and returning a clear message when we ran out of model quota rather than a generic failure.

why we replaced it

The retrieval worked. Everything around it was the problem. A chat product is an interface, accounts, conversation history, model configuration, quotas and abuse limits, and we were maintaining all of that in a backend whose real job is the archive.

Open WebUI already does all of it well. So we deployed it on our own cluster behind an LLM proxy, and connected it to the archive. The team uses it to ask the archive questions, and we maintain only what's specific to Jawafdehi.

what I took from it

  • Hybrid beats either alone for legal documents. Names, case numbers and amounts need exact matching, and questions need meaning.
  • Fewer moving parts. A router in front of an agent was one more thing to be wrong. One agent with good tools was simpler and better.
  • Build the part that's yours. The interesting work was retrieval over our documents. The chat shell around it was never going to be better than a maintained open-source one.

The one thing I'd do differently is measure. I tested retrieval by trying questions and reading answers. Next time I'd start with a small set of real questions and known correct sources, so every change to chunking or ranking shows up as a number.

book a chat