DhimanSoft
← All work

Case study

RAG Document Chatbot

Multi-tenant document Q&A system

A multi-tenant document Q&A system with per-user vector isolation, local ONNX embeddings at zero API cost, and automatic failover across six LLM providers. API keys are Fernet-encrypted; answers always cite the source document, page and snippet.

  • FastAPI
  • ChromaDB
  • OpenAI
  • Claude API
  • Docker

The problem

Off-the-shelf retrieval-augmented generation (RAG) tools make three trade-offs that do not suit a product with many users.

  • One model vendor

    Most libraries tie you to a single LLM provider, so an outage or a price change becomes your problem.

  • Per-token embedding costs

    Cloud embeddings are billed per token, which grows with every document uploaded.

  • Mixed documents

    A shared vector index can return one user's documents to another.

How it is built

  1. Sign-in

    OAuth 2.0 with Google or GitHub, JWT sessions, SQLite by default with an option to switch to Postgres.

  2. Ingestion

    PDF, TXT and DOCX parsing, token-aware chunking, and local ONNX embeddings.

  3. Vector store

    A separate ChromaDB collection per user, cosine similarity and top-K retrieval.

  4. LLM layer

    A chain of providers with automatic failover, including a local Ollama option.

  5. Response layer

    Answers come back with their sources and a rolling summary of the conversation.

What it does

  • Local embeddings

    384-dimensional ONNX MiniLM vectors computed on the server, with no embedding API bill.

  • Automatic failover

    If one provider fails, the next in the chain answers.

  • Per-user isolation

    Each user's documents live in their own collection.

  • Encrypted keys

    User-supplied API keys are encrypted at rest with Fernet.

  • Token-aware chunking

    800-token chunks with a 150-token overlap.

  • Rolling summaries

    Long conversations are summarised after six turns to bound the context.

  • Source citations

    Every answer names the document, page and a short snippet.

  • Offline tests

    The test suite runs without live API calls.

The hard parts

  • Six providers, six different APIs

    The common base class was designed only after all six had been integrated, so it fits how they really behave.

  • Unbounded context

    Rolling summaries keep the prompt a predictable size.

  • Testing without live models

    Deterministic fake embeddings and a mock LLM make the tests repeatable and free.

  • Storing users' API keys

    Keys are encrypted with Fernet and are never returned to the browser.

Decisions we made

  • Local ONNX embeddings over cloud embeddings, for cost and privacy.
  • A provider chain instead of a single LLM.
  • A separate collection per user instead of filtering rows in a shared index.
  • Rolling summaries instead of sending the full history.
  • When nothing relevant is retrieved, return a fixed message instead of letting the model answer without sources.

What we learned

  • Design an abstraction after you have seen the concrete cases it must cover.
  • Build security into the architecture instead of adding it afterwards.
  • Retrieval quality matters more than prompt wording.
  • Free serverless hosting has real limits for persistent storage and in-memory models.

Need something built like this?

Tell us what you are trying to build and we will reply with how we would approach it.