Back
intermediate
RAG (Retrieval-Augmented Generation)

Project: Document Q&A System

Build a document Q&A system by learning the RAG pipeline, citation design, no-answer behavior, and evals

45 min read· RAG· Project· PDF· Q&A

Project: Document Q&A System

A document Q&A system answers questions from uploaded or indexed documents.

This is the most common first RAG project. The important part is not the UI framework. The important part is grounding answers in evidence.

What the system must do

text
upload document
  -> extract text
  -> chunk text
  -> embed chunks
  -> retrieve relevant chunks
  -> answer with citations
  -> say "not found" when evidence is missing

User promise

The app should promise:

I answer from your documents and show where the answer came from.

If the document does not contain the answer, the app should say that clearly.

Document ingestion

Ingestion turns files into searchable evidence.

Track:

  • file name
  • document title
  • page number
  • section heading
  • chunk text
  • source span
  • upload time
  • user or workspace permissions

For PDFs, page numbers matter because users want to verify the answer.

Retrieval

For each question:

  1. Rewrite the query if needed.
  2. Retrieve candidate chunks.
  3. Rerank the best evidence.
  4. Remove chunks the user cannot access.
  5. Pass only relevant evidence to the model.

Answer prompt

Use a prompt rule like:

text
Answer only from the provided sources.
If the answer is not in the sources, say "I could not find that in the document."
Include citations using page or section IDs.
Do not invent policy details.

Citation design

Good citations are specific:

text
Refunds are available within 30 days [Refund Policy, page 2].

Weak citations are vague:

text
According to the document...

Failure modes

FailureFix
answer uses model memorystronger grounding prompt and evals
wrong chunk retrievedbetter chunking, hybrid search, reranking
no citationrequire citation format
fake citationvalidate source IDs
answer missingadd no-answer examples
private doc leakenforce permissions before retrieval

Evaluation set

Create 30 questions:

  • 10 direct answers
  • 10 multi-section answers
  • 5 no-answer questions
  • 5 adversarial questions with misleading wording

Score groundedness, citation correctness, and answer usefulness.

Knowledge check

Q1: What is the most important behavior in document Q&A?

Answer from the document and admit when the document does not contain the answer.

Q2: Why are page or section citations important?

They let users verify the answer and catch hallucinations.