PROJECT 01 / AI infrastructure & retrieval
RAG Document Assistant.
From a PDF chatbot to an evaluated AI backend.
An evaluated document intelligence backend with persistent vector retrieval, asynchronous processing, configurable LLM integrations and MCP tooling.
¹ In one documented 38-question experiment (IVFFlat probes 1 → 10). This measures queries returning fewer than 3 chunks, not whether the retrieved chunks were relevant or answers accurate.
01 / OVERVIEW
A PDF chatbot, taken seriously.
The RAG Document Assistant started as a single-document prototype built on FAISS and Ollama. It grew into a backend with persistent, additive ingestion, an asynchronous request path, deployment and a repeatable evaluation harness.
In the browser, a user uploads PDFs and asks questions with streamed answers. Separately, an MCP stdio server exposes a search_documents tool that reuses the same retrieval and returns chunks directly, with no generation step.
- 01
Prototype
Single-document chat over a local FAISS index with Ollama.
- 02
Persistence
Additive ingestion into PostgreSQL with pgvector, so documents accumulate and survive a redeploy.
- 03
Async backend
FastAPI with async SQLAlchemy and asyncpg; CPU-bound work moved off the event loop.
- 04
Deployment
Dockerised, with a Render and Neon demo and an AWS EC2 and RDS migration exercise.
- 05
Evaluation
A 38-question golden set, a CI suite on real PostgreSQL and Langfuse tracing.
02 / THE PROBLEM
An answer is only the beginning.
A fluent answer to an uploaded PDF makes a convincing demo. It does not show whether the right passage was retrieved, whether the index survives a redeploy, or whether one slow operation is blocking every other request.
I built the Document Assistant to work through those questions. The goal is not to claim perfect answers; it is to make the behaviour inspectable and the failures actionable.
03 / ARCHITECTURE
A small system with clear boundaries.
Ingestion: PDF parsing, then chunking, then fastembed ONNX embeddings, then storage in PostgreSQL with pgvector. Question answering: embed the query, cosine retrieval from pgvector, a relevance guard, then a streamed response from a configurable provider, with the response and a log written. Langfuse records retrieval and generation spans. A separate MCP stdio tool, search_documents, reuses retrieval and returns chunks without generating an answer.
FastAPI orchestrates the request path. Async SQLAlchemy and asyncpg handle the database. Langfuse separates retrieval from generation, so a retrieval problem can be told apart from a model or cold-start problem.
The MCP tool shows the same retrieval from another interface. Its results are returned directly, which is also how the duplicate-ingestion issue became visible: two near-identical passages carry different document IDs.
04 / DECISIONS & TRADE-OFFS
The decisions behind the stack.
PostgreSQL and pgvector, rather than a local index.
A local FAISS index does not survive an ephemeral-host redeploy. PostgreSQL keeps vectors next to application metadata, supports many documents and makes persistence an explicit part of the architecture.
Trade-off. A database is one more moving part to run, migrate and secure than a file on disk.
Async across the whole request path.
Async SQLAlchemy, asyncpg and an asynchronous LLM client handle I/O. PDF parsing and embedding have no native async equivalent here, so they are offloaded with asyncio.to_thread.
Trade-off. Offloading to threads relieves the event loop but still competes for the same CPU. This is not a throughput claim.
fastembed and ONNX instead of a torch embedding stack.
The deployment exceeded its memory budget with a torch-based dependency. Swapping to ONNX-based fastembed kept the vector schema unchanged.
Trade-off. A smaller runtime footprint in exchange for a narrower choice of embedding models.
Five configurable generation paths, without hiding their differences.
Groq, Azure AI Foundry, direct OpenAI, Bedrock InvokeModel and Bedrock Converse are selectable through the API (not a UI dropdown). A background-thread to asyncio-queue bridge handles boto3's blocking streaming. I also packaged the provider-addition workflow as a Claude Code skill and plugin (add-llm-provider, rag-provider-tools).
Trade-off. Breadth of integration is not breadth of verification; each path has a different evidence level.
Guard at generation, not at retrieval.
The answer path can decline to call the model when context is insufficient. The MCP tool can still return low-similarity chunks for callers that want them.
Trade-off. The similarity threshold is a starting point that has not been tuned on labelled data.
05 / DEBUGGING
Where the real engineering happened.
The event loop was not as asynchronous as the routes looked. Blocking work inside async routes stopped other requests making progress. In a synthetic event-loop experiment the loop was blocked for 814 ms before the fix and 20–24 ms after thread offloading. That measures loop blocking, not request latency.
Memory use exceeded the budget. Local PDF embedding peaked at 669 MB; reducing the embedding batch size from 256 to 8 brought it to 283 MB. This is a local measurement, not a benchmark of the live host.
Retrieval was silently returning too few rows. The golden-set run showed IVFFlat returning fewer than 3 chunks for 5 of 38 questions. Raising ivfflat.probes from 1 to 10 brought that to 0 of 38 in the documented experiment. It removes a truncation failure mode; it does not prove the retrieved chunks are relevant.
06 / TESTING & EVALUATION
Measured, not assumed.
16 automated tests exercise ingestion, retrieval and the API against a real PostgreSQL database with deterministic embeddings and mocked model responses. A separate live AI evaluation covers real model behaviour. GitHub Actions starts a fresh database for each run.
A 38-question golden set ran against Groq, recorded in Langfuse. Faithfulness and relevance come from an LLM judge, so they describe judged behaviour, not general accuracy. Precision, recall and MRR use real chunk identifiers, over the 35 questions that have expected chunks.
| Metric | Value | What it is |
|---|---|---|
| Faithfulness | 0.945 | LLM-judge score |
| Relevance | 1.000 | LLM-judge score |
| Precision@3 | 0.143 | Chunk-ID match, 35 questions |
| Recall@3 | 0.386 | Chunk-ID match, 35 questions |
| MRR@3 | 0.295 | Chunk-ID match, 35 questions |
HOW TO READ THIS
The strict chunk-match scores are reported as recorded, next to the judge scores, not hidden behind them. The evaluation also exposed cross-document mixing, so removing one failure mode did not remove every quality issue.
07 / DEPLOYMENT
Beyond a running container.
The documented public demo runs on Render with a Neon-hosted PostgreSQL database. Free-tier services can sleep, so a first request may take 30–60 seconds. CI runs the tests; application deployment is manual.
A separate AWS EC2 and RDS deployment was a migration exercise, not a second permanent production service. It covered Docker restart behaviour, private RDS access through security-group references, IAM fundamentals and moving data with pg_dump and pg_restore.
DEMO LINK
The public demo is not linked from this site. Its upload route has no authentication or tenant isolation, so it must not be treated as a safe place for confidential documents.
08 / LIMITATIONS
What this system does not promise.
- Cross-document contamination and duplicate ingestion. The same file can be ingested more than once, and retrieval can mix documents.
- No tenant isolation or authentication in the inspected public upload route, and limited resource limits.
- Provider verification varies. Groq is documented live; Azure was confirmed with one short call; Bedrock live verification was blocked by an account restriction and direct OpenAI was not tested against the real service.
- A small evaluation corpus. The scores are not a general accuracy claim, and it is unclear whether the “insufficient context” answer shown above was right.
- Streaming failure handling has a gap. A provider failure after headers are sent can surface as an empty HTTP 200, and failed generations are not written to the query log.
09 / NEXT STEPS
What I would do next.
- Scope retrieval to a document, and de-duplicate ingestion by content hash.
- Add upload safeguards, authentication and per-user isolation before any real documents.
- Broaden the evaluation set, and tune the similarity threshold on labelled data.
- Close the streaming error gap and finish live verification of the remaining providers.
These are planned improvements, not completed work.
Read the source and documentation on GitHubWORK WITH ME
Need something like this built?
I take on focused tasks as well as larger implementations. Tell me what you are trying to build.
06GET IN TOUCH
Let's build something
that works.
Open to full-time engineering roles. Relocation across India is fine.
View résuméFocused tasks or full builds. Tell me what you are trying to ship.
See what I can build