📂 Local Document Extraction

SLM Document Parser

Extract tabular metrics, metadata structures, and key paragraphs from PDFs, Word docs, and plain text files locally under strict MIT-licensing parameters.

 

🚀 Overview & Capabilities

The SLM Document Parser leverages Microsoft's MIT-licensed Phi-3.5-mini-instruct and Florence-2-large models optimized via ONNX Runtime GenAI to parse multi-formatted documents offline. It supports high-fidelity layout preservation for scanned PDFs and legacied formats (.pdf, .docx, .doc, .pptx, .ppt, .txt) to output structured text and semantic RAG database chunks.

🤖 Truly Agentic Hybrid Visual Pipeline

The Document Parser handles layout mapping by combining visual coordinate classifiers with standard extraction loops:

  • LibreOffice Conversion Layer: Converts complex formats (DOC/PPT/PPTX) into intermediate PDFs in the background using headless soffice conversions.
  • Page Image Rendering: Renders PDF page indices to images sequentially using pypdfium2.
  • Florence-2 OCR & OD: Scans rendered page layouts, running Object Detection (<OD>) to locate table and figure boxes. Crops table boxes to OCR them individually, and runs detailed caption generation (<DETAILED_CAPTION>) for diagrams.
  • Layout Reconstruction: Passes text, parsed tables, and diagram captions to Phi-3.5 to synthesize a unified, formatted Markdown page representation.
  • Semantic Chunker & Linker: Segments markdown into structured paragraph chunks, extracts heading hierarchies, resolves product entities, and links related sections together.

📊 Complex Table Visual OCR Extraction

When the visual parser encounters complex tables inside a document scan, it crops the table coordinates, routes it to the vision parser, and synthesizes a natural language text description. Below is an example of an input expense table and its corresponding parsed description:

Input Corporate Expense Table
Extracted Natural Language Table Description Output:
### Q2 FY2024 Company Expense Summary
The table outlines corporate expenditures across multiple categories:
- **Infrastructure**: Spent $1,195,450 against a $1,250,000 budget (a decrease of 4.36%).
- **AI Compute Nodes**: Spent $3,842,910 against a $3,500,000 budget (an increase of 9.80%).
- **Model Conversion**: Spent $412,300 against a $450,000 budget (a decrease of 8.38%).
- **Data Storage**: Spent $845,600 against a $800,000 budget (an increase of 5.70%).
- **GPU Clusters**: Spent $2,215,800 against a $2,100,000 budget (an increase of 5.51%).

⚡ CPU Performance Tuning Guidelines

Follow these configuration rules to optimize latency on standard CPUs:

  • Core Thread Cap: Set n_threads strictly to the number of physical cores to avoid context switching thread collisions.
  • Sequential Processing: Process multi-page images sequentially instead of in parallel batches to keep the memory footprint under 2.0 GB.
  • OpenMP Settings: Keep thread counts aligned in your shell environment:
    export OMP_NUM_THREADS=4
    export MKL_NUM_THREADS=4

API Reference

`SLMDocumentParser` Initialization

from slm_document_parser.document_parser import SLMDocumentParser

parser = SLMDocumentParser(n_ctx=4096, n_threads=4)
ParameterType / DefaultDescription
model_pathstr | NoneLocal path to Phi-3.5 weight checkpoints. Defaults to "../../models/phi-3.5-mini-instruct-onnx".
n_ctxint | 4096Inference token window size. Phi-3.5 supports up to 128K context tokens.
n_threadsint | 4Allocated CPU cores for ORT execution.
system_promptstr | NoneOptional custom system prompt instructions overriding the default template.
user_inputstr | NoneOptional additional user-supplied target parameters or variables.

`extract_text` Method

Converts document layouts and images into a single reconstructed Markdown text string:

markdown_content = parser.extract_text("financial_report.pdf")

`chunk_document` Method

Extracts markdown text and slices it into linked semantic chunks with metadata properties:

chunks = parser.chunk_document("financial_report.pdf")
print(chunks[0])
Extracted Semantic Chunk Output (JSON):
{
  "text": "SpaceX successfully launched the Falcon 9 rocket from Cape Canaveral Space Force Station, landing the booster return flight for the 15th time. The mission delivered communication payloads into low Earth orbit.",
  "metadata": {
    "source": "financial_report.pdf",
    "heading": "1. Launch Milestones",
    "subheading": "Falcon 9 Performance",
    "product": "SpaceX",
    "key_terms": ["Falcon 9", "Cape Canaveral", "booster"],
    "format": "pdf",
    "chunk_index": 0,
    "related_chunks": [1, 2]
  }
}

`parse_and_chunk_stream` Method

Streaming generator yielding semantic chunks page-by-page as they are processed to reduce first-chunk response times (TTFT):

for chunk in parser.parse_and_chunk_stream("multi_page_manual.pdf"):
    print(f"Processed Chunk: {chunk['text'][:100]}...")

`export_chunks_to_excel` Method

Saves the parsed chunks to an Excel spreadsheet:

# Exports to workbook. Set append=True to write to an existing sheet
parser.export_chunks_to_excel(chunks, "rag_dataset.xlsx", append=False)
Output Excel Table Layout:
Chunk IndexSource FileHeadingSubheadingProductRelated ChunksText
0financial_report.pdf1. Launch MilestonesFalcon 9 PerformanceSpaceX1,2SpaceX successfully launched the Falcon 9...
system_promptstr | NoneOptional custom system prompt instructions overriding the default template.
user_inputstr | NoneOptional additional user-supplied target parameters or variables.

© 2026 SLM Agents. Built with Apache 2.0 Permissive Open Source License.

🐙 Checkout from GitHub

Clone only this agent's folder from the monorepo using Git sparse-checkout — no need to download the full repository:

Option 1 — Sparse Checkout (Recommended)

Terminal — Git Sparse Checkout
# 1. Create and enter a new directory
$ mkdir slm_document_parser && cd slm_document_parser

# 2. Initialise empty git repo and add remote
$ git init
$ git remote add origin https://github.com/t00114218-stack/SLMAgents.git

# 3. Enable sparse-checkout and set target folder
$ git sparse-checkout init --cone
$ git sparse-checkout set slm_document_parser

# 4. Pull only that agent's source
$ git pull origin main

Option 2 — Full Repository Clone

Terminal — Full Clone
$ git clone https://github.com/t00114218-stack/SLMAgents.git
$ cd SLMAgents/slm_document_parser

💡 Tip: After checkout, install the package locally with pip install -e ./slm_document_parser to run in editable mode without publishing to PyPI.