LexExtract is a high-precision document extraction engine designed to turn unstructured legal PDFs into structured, actionable data. Unlike simple RAG systems, LexExtract uses an agentic loop to self-correct and verify its own findings, ensuring enterprise-grade accuracy.
- Multi-Document Processing: Batch upload and process multiple PDFs simultaneously. Each document is indexed and extracted in isolation to prevent data leakage.
- Enterprise-Grade Accuracy:
- Self-Correction Loop: Validates extraction results against expected schemas and automatically triggers "deep search" retries for missing or ambiguous data.
- Preamble Injection: Automatically identifies and injects the document header (where the most critical context like dates and parties usually reside) into every target query.
- State-of-the-Art PDF Parsing: Leverages
pymupdf4llmto transform complex PDF layouts into clean LLM-ready Markdown, preserving tables and structure. - Smart Vector Isolation: Uses Qdrant with specific metadata filtering to ensure that queries for Document A never retrieve context from Document B.
- Flexible Model Backend: Switch seamlessly between local models (Ollama) and cloud providers (OpenAI, Anthropic) without changing code.
- Parallel Execution: Optimized for speed with a configurable parallel extraction mode.
graph TD
START((Start)) --> Ingest[Ingest Node: PDF to MD + Indexing]
Ingest --> Extract[Extract Node: Targeted LLM Query]
Extract --> Validate[Validate Node: Check for Missing Fields]
Validate -- Missing Fields & Retry < 2 --> Retry[Retry Node: Expanded Search]
Retry --> Validate
Validate -- All Found OR Retry >= 2 --> Output[Output Node: Compile JSON]
Output --> END((End))
- Python 3.12
- Ollama (running locally with the required model)
- Qdrant (local storage is handled automatically)
- Clone the repository.
- Install dependencies:
pip install -r requirements.txt
- Create a
.envfile from.env.example:cp .env.example .env
Create a .env file in the root directory and configure the following variables. You can find a template in .env.example.
The application uses LangChain's init_chat_model and supports multiple LLM backends:
| Provider | MODEL_PROVIDER |
MODEL_NAME (Example) |
Required Key |
|---|---|---|---|
| Ollama (Local) | ollama |
gemma2:9b |
N/A (Local) |
| OpenAI | openai |
gpt-4o |
OPENAI_API_KEY |
| Anthropic | anthropic |
claude-3-5-sonnet-20240620 |
ANTHROPIC_API_KEY |
MODEL_PROVIDER: The backend provider (see table above).MODEL_NAME: The specific model version you wish to use.OLLAMA_BASE_URL: (Only for Ollama) Usuallyhttp://localhost:11434.OPENAI_API_KEY/ANTHROPIC_API_KEY: Your respective API keys.PDF_PATH: The default file path for the CLI runner.EXTRACTION_MODE: Set toparallel(faster) orsequential(lower resource usage).
To enable debugging and tracing of your extraction chains:
LANGCHAIN_TRACING_V2: Set totrue.LANGCHAIN_API_KEY: Your LangSmith API key.
Launch the interactive dashboard to upload documents and define extraction attributes:
python main.pyFor automated batch processing or testing without the UI:
- Place your target PDF in the
input/directory. - Update the
PDF_PATHin.env. - Run the CLI runner:
python src/cli_runner.py
The final extracted data will be saved to output.json.
main.py: Interactive entry point (launches Streamlit UI).src/cli_runner.py: CLI-based extraction workflow runner.src/nodes.py: Implementation of LangGraph nodes (Ingest, Extract, Validate, Retry).src/graph.py: Definition of the StateGraph and routing logic.src/schema.py: Pydantic models defining the extraction target (SharePurchaseAgreement).src/utils/: Helper utilities for PDF processing, chat models, and document indexing.input/: Directory for source PDF documents.frontend/: Streamlit web application files.
This project is licensed under the Apache License 2.0. See the LICENSE file for details.
Built for specialized legal document extraction workflows.
