Skip to main content

Tests

LLMAIxLib

LLMAIxLib is a Python toolkit for automated document preprocessing (including OCR) and information extraction using large language models. It is designed for users who need to extract structured facts from arbitrary documents (PDF, DOCX, images, etc.) and output them as Markdown, plain text, or validated JSON.

[!CAUTION]

Under active development. Best suited for research or prototyping. Always validate results.


🚀 What LLMAIxLib Does

  • Preprocessing: Extracts human-readable Markdown or plain text from a wide range of document types, automatically falling back to OCR for scanned or image-based files.
  • Information Extraction: Uses a large language model (LLM) to transform unstructured or semi-structured text into structured data—validated by Pydantic models or JSON Schema—via an OpenAI-compatible API.

❗ What You Need

  • Python ≥3.12

  • OCR tools: Tesseract (for OCRmyPDF), a GPU for faster OCR (Marker and PaddleOCR)

  • OpenAI-compatible API endpoint: Required for information extraction! This can be:

    • The official OpenAI API (or Azure OpenAI or ...)
    • A self-hosted API that matches the OpenAI chat/completions format, e.g. vllm, llama.cpp server, or other compatible backends
    • Your endpoint must support structured output (based on JSON schema).

🛠 Installation

Install base:

pip install llmaix

Add extras for advanced features:

pip install llmaix[docling]      # advanced layout + VLM support
pip install llmaix[marker]        # Marker (surya-OCR)
pip install llmaix[paddleocr]    # PaddleOCR
pip install llmaix[docling,marker,paddleocr] # all extras

📚 Usage

CLI Examples

Environment variables are the recommended way to provide your API settings (see below).

llmaix preprocess file.pdf                # extract as Markdown, fast mode
llmaix preprocess scan.pdf --force-ocr --ocr-engine paddleocr -o out.md
llmaix preprocess paper.pdf --mode advanced --enable-picture-description
llmaix extract --input "Patient was a 73-year-old male..." --json-schema patient_schema.json

Python API Example

from llmaix.preprocess import DocumentPreprocessor
from llmaix import extract_info
from pydantic import BaseModel

# Preprocessing: get Markdown or text
proc = DocumentPreprocessor(mode="advanced", ocr_engine="marker")
markdown = proc.process("scan.pdf")

# Information extraction: structured JSON from text via LLM
class PersonInfo(BaseModel):
    name: str
    affiliation: str
    position: str

result = extract_info(
    prompt="Alice Smith is a Professor of AI at TU Dresden.",
    pydantic_model=PersonInfo,
    llm_model="o4-mini"
)
print(result.json(indent=2))

🔑 API Configuration

You must provide your LLM API settings by environment variable (recommended) or CLI flag:

export OPENAI_API_KEY=sk-xxx
export OPENAI_API_BASE=https://api.example.com/v1  # optional, default: OpenAI endpoint
export OPENAI_MODEL=gpt-4                         # optional, default: set in CLI or code

Or pass directly:

llmaix extract --input "..." --llm-model llama-3-8b-instruct --base-url http://localhost:8000/v1 --api-key sk-xxx --json-schema schema.json

🗂 Architecture Overview

Preprocessing

  • DocumentPreprocessor:

    • Detects MIME type and routes to the appropriate handler.
    • For PDFs: tries fast text extraction first, falls back to OCR (OCRmyPDF, PaddleOCR, Marker) if needed.
    • DOCX, TXT, and image formats supported.
    • Advanced mode: integrates Docling for tables, formulas, and (optionally) vision-language model for image captioning.
  • OCR Engines: Pluggable; use Tesseract, Marker, PaddleOCR as needed.

Information Extraction

  • extract_info:

    • Sends text and a schema (Pydantic or JSON Schema) to an OpenAI-compatible API endpoint.
    • Validates output as structured JSON.
    • CLI can load schema from file or as literal string.
    • Your API endpoint must support structured outputs!
    • Can be used with hosted (OpenAI, Azure) or self-hosted (e.g. llama.cpp, vllm) models that follow the OpenAI API.

🧩 JSON Schema Example

{
  "type": "object",
  "properties": {
    "experiment_id": { "type": "string" },
    "date": { "type": "string", "format": "date" },
    "findings": { "type": "array", "items": { "type": "string" } }
  },
  "required": ["experiment_id", "findings"]
}

✅ Quick Checklist

  1. Set up API credentials (see above).
  2. Install OCR backends as required for your documents.
  3. Use llmaix preprocess for robust text/Markdown extraction from documents.
  4. Use llmaix extract (with prompt + schema or model) for LLM-powered structured extraction.

🧪 Testing

uv run pytest
uv run pytest tests/test_preprocess.py -k paddleocr

⚠️ Caveats & Notes

  • Preprocessing only: No LLM API needed if you just want Markdown/text from documents.
  • Information extraction: Requires an OpenAI-compatible API endpoint that supports structured outputs.
  • If your LLM or endpoint does not support structured output via reponse_format, information extraction will not work as expected.
    • You can still use the extract_info function and provide a prompt or system_prompt argument which teaches the model to respond with valid JSON only in the desired format!

📄 License

MIT.

Contributions welcome.

Repo: github.com/KatherLab/llmaixlib


Metadata

Release files for llmaix 0.0.26

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for llmaix 0.0.26
File Size Uploaded
llmaix-0.0.26.tar.gz 1.7 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for llmaix 0.0.26
File Interpreter ABI Platform
llmaix-0.0.26-py3-none-any.whl Python 3 none any Details

Total release size: 1.7 MB

Release files / llmaix-0.0.26.tar.gz

Download URL llmaix-0.0.26.tar.gz
Size 1.7 MB
Tags Source
SHA-256 checksum
How to use checksums
68df6dab6044eeaa6b8606e8389b61571e03861127b2fe76b667515c81aab820
BLAKE2b-256 checksum
How to use checksums
005cd4fcbead3310c032433877a532bee2716a2917028a02bce02898ca48fae1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 28, 2025.

Transparency log

Release files / llmaix-0.0.26-py3-none-any.whl

Download URL llmaix-0.0.26-py3-none-any.whl
Size 27.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
43d5aa00778ee2c84bdba17c7daf3a56cd452397a87f3405cc359e11fa106569
BLAKE2b-256 checksum
How to use checksums
26f31cf1dbe2026dc7c7100ab53cb3fd79fc7319e1c09c58440f681abd3173c9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 28, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

0.0.26 This release

2 release files

0.0.22

2 release files

0.0.21

2 release files

0.0.20

2 release files

0.0.19

2 release files

0.0.18

2 release files

0.0.17

2 release files

0.0.16

2 release files

0.0.14

2 release files

0.0.12

2 release files

0.0.11

2 release files

0.0.10

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.3

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page