Contents

Chapter 1

Document Processing Guide

Supported Formats

FormatParserExternal DepsNotes
.txtStdlibNoneSplit by blank lines
.mdStdlib regexNoneHeadings, lists, code blocks, blockquotes
.htmlStdlib HTMLParserNoneExtracts body content with structure
.pdfpdfplumberpip install pdfplumberText + table extraction
.docxpython-docxpip install python-docxParagraphs, headings, tables
ImagesTesseract OCRpip install pytesseractVia OCR interface

Chunking Strategy Selection

Choose the right chunking strategy based on your documents:

Is the document well-structured (headings, sections)?
├── Yes → Use SEMANTIC chunking
│         (each section becomes one or more chunks with heading context)
└── No
    ├── Is it prose (articles, reports)?
    │   └── Use SENTENCE chunking (preserves sentence boundaries)
    ├── Is it technical docs with clear paragraphs?
    │   └── Use PARAGRAPH chunking (atomic paragraph units)
    └── Is it raw/unstructured text?
        └── Use FIXED or RECURSIVE chunking

Chunk Size Guidelines for RAG

Embedding ModelRecommended Chunk SizeMax Tokens
OpenAI text-embedding-3-small500-800 chars8191
Cohere embed-v3500-1000 chars512
sentence-transformers300-500 chars512
E5-large300-500 chars512

Overlap: 10-20% of chunk size prevents information loss at boundaries.

Schema-Guided Extraction

Define a schema for structured data extraction:

python
from src.schema_extractor import ExtractionSchema, SchemaExtractor

schema = ExtractionSchema("contract_data")
schema.add_field("parties", "list", "Names of contracting parties", required=True)
schema.add_field("effective_date", "date", "Contract start date", required=True)
schema.add_field("termination_date", "date", "Contract end date")
schema.add_field("total_value", "currency", "Total contract value")
schema.add_field("governing_law", "string", "Jurisdiction / governing law")

extractor = SchemaExtractor(schema)
result = extractor.extract(document_text)

The regex fallback handles common types (dates, emails, currency, phones) without an LLM.

For complex extraction, provide an llm_fn callback.

Summarization Strategies

StrategyBest ForHow It Works
ExtractiveAny length, no LLM neededPicks top sentences by word frequency + position
DirectShort docs (< 3000 chars)Sends full text to LLM
Map-ReduceMedium docs (3k-30k chars)Summarize chunks, then combine
RefineLong docs (30k+ chars)Iteratively refine summary with each chunk
Document AI Toolkit v1.0.0 — Free Preview