
DocumentLens utilizes Llama to extract structured data from rich documents and store it in various machine-readable formats like JSON, SQL and vector databases. It is designed to handle complex documents with high accuracy and provide flexible configuration options to tailor extraction tasks to specific needs.
The architecture of DocumentLens is modular, comprising several core components that work together to achieve the desired functionality.
To get started with DocumentLens, follow these steps:
1. Clone the repository:
pythongit clone https://github.com/meta-llama/llama-cookbook.gitcd llama-cookbook
2. Install project-specific dependencies:
bashcd end-to-end-use-cases/structured_parserpip install -r requirements.txt
3. Configure the recipe by editing src/config.yaml to set up your model, API keys, and other parameters.
Extract text from a PDF:
bashpython src/structured_extraction.py path/to/document.pdf text
Extract charts and tables and save them as CSV files:
bashpython src/structured_extraction.py path/to/document.pdf charts,tables --save_tables_as_csv
Process a directory of PDFs and export tables to Excel:
bashpython src/structured_extraction.py path/to/pdf_directory text,tables --export_excel
pythonfrom src.structured_extraction import ArtifactExtractorfrom src.utils import PDFUtils# Extract pages from a PDFpages = PDFUtils.extract_pages("document.pdf")# Process specific pagesfor page in pages[10:20]:artifacts = ArtifactExtractor.from_image(page["image_path"],["text", "tables"])# Custom processing of artifacts...
pythonfrom src.json_to_sql import DatabaseManager# Query all text artifactstext_df = DatabaseManager.sql_query("sqlite3.db","SELECT * FROM document_artifacts WHERE artifact_type = 'text'")# Query tables containing specific contentrevenue_tables = DatabaseManager.sql_query("sqlite3.db","SELECT * FROM document_artifacts WHERE artifact_type = 'table' AND table_info LIKE '%revenue%'")
Note: The SQLite3 database engine is bundled with the sqlite3 Python package.
The recipe is configured via src/config.yaml. Key configuration options include model configuration, inference parameters, database configuration, and artifact configuration.
yamlmodel:backend: openai-compat # [offline-vllm, openai-compat]# For openai-compatbase_url: "https://api.llama.com/compat/v1"api_key: "YOUR_API_KEY"model_id: "Llama-4-Maverick-17B-128E-Instruct-FP8"# For offline-vllmpath: "/path/to/checkpoint"tensor_parallel_size: 4max_model_len: 32000max_num_seqs: 32
DocumentLens is designed to be extensible. You can add new artifact types, customize extraction logic, and use different models.
1. Add a new artifact type configuration in config.yaml:
yamlartifacts:my_new_artifact:prompts:system: "Your system prompt here..."user: "Your user prompt with {schema} placeholder..."output_schema: { # Your JSON schema here }use_json_decoding: true
2. Update the CLI, which is implemented in src/structured_extraction.py, to include your new artifact type.
Common issues include model capacity errors, extraction quality issues, and configuration errors. Refer to the Getting Started for more details.
Subscribe to our newsletter to keep up with the latest AI updates, releases and more.