GitHub

Inside the Build / 02

DocsAI: from PDF to cited answer

An answer starts well before the model writes its first token. Follow how a PDF becomes searchable evidence, and how that evidence returns to the reader as a citation.

FastAPILangGraphQdrantHybrid retrieval
Explore the repository (opens in a new tab)

The task

Upload a legal PDF, then ask a question about its contents. This example follows the architecture documented in the project's overview; it does not use a real contract or a generated legal answer.

Example question
What are the conditions for terminating this agreement?
DOCUMENT INGESTION · STAGES 01–03
Validate → extract and locate → embed and index
CHAT GENERATION · STAGES 04–07
Scope the thread → retrieve → rerank → stream with citations

These are two separate LangGraph workflows. Ingestion prepares the document for search; a chat question uses that prepared index to find supporting passages.

From document to evidence

Expand a stage to inspect its input, output, and purpose. Execution sketches are illustrative pseudocode.

INPUT
An uploaded PDF
OUTPUT
Legal or compliance content accepted for processing

DocsAI runs an AI-assisted pre-check with GPT-4o-mini to determine whether the PDF contains legal or compliance content. This is the entry point to document ingestion, before the search index is built.

Execution sketch · pseudocode
document = receive_pdf(upload)
classification = classify_content(document)
if legal_or_compliance(classification):
  continue_ingestion(document)
else:
  reject_document()

WHY THIS MATTERS

Validate the document before spending resources on embeddings and indexing. This check classifies content; it does not certify a document's legal validity.

What this reveals

The central connection is between an answer and its evidence. Text extraction preserves location, retrieval finds candidate passages, reranking narrows the context, and citations bring the reader back to the PDF.

The quality of that connection depends on each stage. Missing text, irrelevant retrieval, or inaccurate location metadata can weaken the result even when the answer reads fluently.