SLM PDF Chat
Securely parses complex PDF documents. Assembles layouts, reads tables, and lets you chat with local legal contracts, research articles, or receipts.
🚀 Overview & Capabilities
Securely parses complex PDF documents. Assembles layouts, reads tables, and lets you chat with local legal contracts, research articles, or receipts.
Key Features
- Locally extracts layout text and multi-column paragraphs
- Parses database tables inside PDFs directly to list-of-dicts
- Built-in RAG chunk generator for offline querying
- Supports scanned image PDFs via local OCR integration
💻 Installation
Install the local CPU-optimized package using pip:
# Install local CPU-optimized package
pip install slm-pdf
🐙 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)
Option 2 — Full Repository Clone
💡 Tip: After checkout, install the package locally with pip install -e ./slm_pdf to run in editable mode without publishing to PyPI.
⚙️ Configuration API
Constructor Parameters
Instantiate SLMPDFChat with performance options:
| Parameter | Type / Default | Description |
|---|---|---|
| model_path | str | None | Explicit path to ONNX model weights. If omitted, downloads standard checkpoints. |
| cache_dir | str | None | Directory to store model weights offline. Defaults to ~/.cache/slm-pdf/. Also settable via SLM_PDF_CHAT_CACHE_DIR. |
| n_threads | int | 4 | CPU thread count for ONNX inference. Optimize for CPU core count. Also settable via SLM_PDF_CHAT_N_THREADS. |
Methods
| Method Signature | Return Type | Description |
|---|---|---|
load(pdf_path) | None | Saves document configurations locally and performs text extraction mappings. |
ask(question, system_prompt=None, user_input=None) | str | Executes vector search on the document text chunks to synthesize local responses. |
Method Parameters (Execution Customization)
All main execution methods accept optional system routing parameters:
| Parameter | Type / Default | Description |
|---|---|---|
| system_prompt | str | None | Optional custom system prompt instruction to override the default system template response parameters. |
| user_input | str | None | Optional additional user-supplied target text variables or contextual keys. |
Quick Start
from slm_pdf import SLMPDFChat
pdf = SLMPDFChat()
pdf.load("invoice.pdf")
ans = pdf.ask(
"What is the total due amount?",
system_prompt="Answer format: $XX.XX",
user_input="Extract tax detail explicitly"
)
print(ans)
Environment Variables
Configure agent parameters globally using environment values:
| Environment Variable | Default | Purpose |
|---|---|---|
| SLM_PDF_CHAT_N_THREADS | 4 | Sets CPU inference execution threads. |
| SLM_PDF_CHAT_CACHE_DIR | ~/.cache/slm-pdf/ | Default directory to store downloaded ONNX weights. |
CPU Performance Tuning
To run the SLMPDFChat engine efficiently on CPU under 1.5 GB memory footprint:
- Match Threads to Core Count: Set
n_threadsorSLM_PDF_CHAT_N_THREADSto match the physical CPU core count. - Sequential Processing: Avoid concurrent processing when batch files are large.
- Garbage Collection: Clear variables and run
gc.collect()to release model RAM blocks after execution.
Verified Input & Output Logs
Diagnostic execution console response running locally on CPU:
→ INPUT (Ask before load):
"What is total revenue?"
← OUTPUT:
"No PDF document loaded. Please call `.load(pdf_path)` first."