### Basic Vision Classification Setup Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/classification/vision.md Initialize classifications with example images and set up a process with a vision-capable LLM for document classification. Enable vision processing by setting `image=True`. ```python from extract_thinker import Process, Classification from extract_thinker.document_loader import DocumentLoaderTesseract # Define classifications with example images classifications = [ Classification( name="Driver License", description="This is a driver license", contract=DriverLicense, image="path/to/example_license.png" # Example image helps model understand ), Classification( name="Invoice", description="This is an invoice", contract=InvoiceContract, image="path/to/example_invoice.png" ) ] # Initialize process with vision-capable model process = Process() process.add_classify_extractor([[ Extractor(DocumentLoaderTesseract(tesseract_path)) .load_llm("gpt-4o") # Vision-capable model ]]) # Classify with vision enabled result = process.classify( "document.pdf", classifications, image=True # Enable vision processing ) ``` -------------------------------- ### Basic Teacher-Student Evaluation Setup Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/evals/teacher-student.md Initialize student and teacher extractors with different configurations and run a comparative evaluation on a dataset. This setup is useful for benchmarking performance differences. ```python from extract_thinker import Extractor, Contract from extract_thinker.eval import TeacherStudentEvaluator, FileSystemDataset from extract_thinker.document_loader import DocumentLoaderPyPdf, DocumentLoaderAWSTextract # Define your contract class InvoiceContract(Contract): invoice_number: str date: str total_amount: float line_items: List[dict] # Initialize "student" extractor with standard configuration student_extractor = Extractor() student_extractor.load_document_loader(DocumentLoaderPyPdf()) student_extractor.load_llm("gpt-4o-mini") # More affordable model # Initialize "teacher" extractor with superior configuration teacher_extractor = Extractor() teacher_extractor.load_document_loader(DocumentLoaderAWSTextract()) teacher_extractor.load_llm("gpt-4o") # More capable model # Create dataset dataset = FileSystemDataset( documents_dir="./test_invoices/", labels_path="./test_invoices/labels.json", name="Invoice Test Set" ) # Set up teacher-student evaluator evaluator = TeacherStudentEvaluator( student_extractor=student_extractor, teacher_extractor=teacher_extractor, response_model=InvoiceContract ) # Run comparative evaluation report = evaluator.evaluate(dataset) # Print comparative summary report.print_summary() ``` -------------------------------- ### Install EasyOCR and PyTorch Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/document-loaders/easy_ocr.md Install the necessary libraries for EasyOCR functionality using pip. For GPU support, ensure PyTorch is installed with a compatible CUDA version. ```bash pip install easyocr torch ``` -------------------------------- ### Teacher-Student Evaluation Setup Source: https://context7.com/enoch3712/extractthinker/llms.txt Configures a teacher-student evaluation using two `Extractor` instances with different LLMs. This setup is useful for comparing model performance. ```python # --- Teacher-Student evaluation --- student_extractor = Extractor() student_extractor.load_llm("gpt-4o-mini") teacher_extractor = Extractor() teacher_extractor.load_llm("gpt-4o") ts_evaluator = TeacherStudentEvaluator( student_extractor=student_extractor, teacher_extractor=teacher_extractor, response_model=InvoiceContract, ) ts_report = ts_evaluator.evaluate(dataset) ts_report.print_summary() ``` -------------------------------- ### Install ipywidgets and IPython Source: https://github.com/enoch3712/extractthinker/blob/main/examples/notebooks/basic_example.ipynb Install ipywidgets and IPython packages. These are often required for interactive notebook environments. ```bash !pip install ipywidgets !pip install IPython ``` -------------------------------- ### Basic Evaluation Setup and Execution Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/evals/index.md Set up and run a basic evaluation by defining a contract, initializing an extractor, creating a dataset, and using the Evaluator. Ensure deterministic outputs by setting model temperature to 0.0. ```python from extract_thinker import Extractor, Contract from extract_thinker.eval import Evaluator, FileSystemDataset from typing import List # 1. Define your contract class class InvoiceContract(Contract): invoice_number: str date: str total_amount: float line_items: List[dict] # 2. Initialize your extractor extractor = Extractor() extractor.load_llm("gpt-4o") # 3. Create a dataset dataset = FileSystemDataset( documents_dir="./test_invoices/", labels_path="./test_invoices/labels.json", name="Invoice Test Set" ) # 4. Set up evaluator evaluator = Evaluator( extractor=extractor, response_model=InvoiceContract ) # 5. Run evaluation report = evaluator.evaluate(dataset) # 6. Print summary and save detailed report report.print_summary() evaluator.save_report(report, "evaluation_results.json") ``` -------------------------------- ### Teacher-Student Evaluation Setup Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/evals/index.md Initialize a TeacherStudentEvaluator to benchmark a student model against a more capable teacher model. Requires separate extractor instances for both. ```python from extract_thinker.eval import TeacherStudentEvaluator evaluator = TeacherStudentEvaluator( student_extractor=student_extractor, teacher_extractor=teacher_extractor, response_model=InvoiceContract ) ``` -------------------------------- ### Initialize LLM with Default or Pydantic AI Stack Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/llm-integration/index.md Initialize the LLM with either the default instructor + litellm stack or the Pydantic AI stack. Ensure `pydantic-ai` is installed if using the Pydantic AI stack. ```python from extract_thinker import LLM from extract_thinker.llm_engine import LLMEngine # Initialize with default stack (instructor + litellm) llm = LLM("gpt-4o") # Or use Pydantic AI stack (Beta) llm = LLM("openai:gpt-4o", backend=LLMEngine.PYDANTIC_AI) ``` -------------------------------- ### Initialize LLM with Router Support Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/llm-integration/index.md Initialize the LLM with LiteLLM's router functionality to enable seamless fallbacks between different model providers if a request fails. Ensure `litellm` is installed. ```python from extract_thinker import LLM from litellm import Router # Initialize router with fallbacks router = Router( model_list=[ {"model_name": "gpt-4o", "litellm_params": {"model": "gpt-4o"}}, {"model_name": "claude-3-opus-20240229", "litellm_params": {"model": "claude-3-opus-20240229"}}, ], fallbacks=[ {"gpt-4o": "claude-3-opus-20240229"} ] ) # Initialize LLM with router llm = LLM("gpt-4o") llm.load_router(router) ``` -------------------------------- ### Initialize and Use Azure Document Intelligence with Phi-3 Source: https://github.com/enoch3712/extractthinker/blob/main/docs/examples/azure-stack.md This example shows how to initialize the Extractor with Azure Document Intelligence, configure environment variables, load the Phi-3 model, and process a document. ```python from extract_thinker import Extractor, Contract, LLM, DocumentLoaderAzureForm from typing import List from pydantic import Field class InvoiceContract(Contract): invoice_number: str = Field("Invoice number") invoice_date: str = Field("Invoice date") total_amount: float = Field("Total amount") lines: List[LineItem] = Field("List of line items") # Initialize Azure Document Intelligence subscription_key = os.getenv("AZURE_SUBSCRIPTION_KEY") endpoint = os.getenv("AZURE_ENDPOINT") api_key = os.getenv("AZURE_AI_API_KEY") extractor = Extractor() extractor.load_document_loader( DocumentLoaderAzureForm(subscription_key, endpoint) ) # Configure environment variables for Azure import os os.environ["AZURE_API_KEY"] = api_key os.environ["AZURE_API_BASE"] = "https://your-endpoint.inference.ai.azure.com" os.environ["AZURE_API_VERSION"] = "v1" # Configure Phi-3 mini model extractor.load_llm("azure/Phi-3-mini-128k-instruct") # Process document result = extractor.extract("invoice.pdf", InvoiceContract) ``` -------------------------------- ### Install ExtractThinker Library Source: https://github.com/enoch3712/extractthinker/blob/main/examples/notebooks/basic_example.ipynb Use this command to install the extract-thinker library and its dependencies. ```python !pip install extract-thinker ``` -------------------------------- ### Install Spreadsheet Loader Dependencies Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/document-loaders/spreadsheet.md Install the necessary Python packages for the spreadsheet loader to function. This includes libraries for handling Excel files. ```bash pip install openpyxl xlrd ``` -------------------------------- ### Load LLM and Extract Summary Source: https://context7.com/enoch3712/extractthinker/llms.txt Initializes an LLM with Pydantic AI backend and uses an extractor to get a summary from a PDF document based on a SummaryContract. ```python llm_pydantic = LLM("openai:gpt-4o", backend=LLMEngine.PYDANTIC_AI) extractor.load_llm(llm_pydantic) result = extractor.extract("doc.pdf", SummaryContract) ``` -------------------------------- ### Basic MoM Usage with Multiple Extractors Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/classification/mom.md Initialize multiple extractors with different models and add them to a process. This example demonstrates setting up a two-layer MoM with GPT-3.5, Claude, and GPT-4 models for classification. ```python from extract_thinker import Process, Classification, ClassificationStrategy from extract_thinker.document_loader import DocumentLoaderTesseract # Define classifications classifications = [ Classification( name="Driver License", description="This is a driver license", ), Classification( name="Invoice", description="This is an invoice", ), ] # Initialize document loader tesseract_path = os.getenv("TESSERACT_PATH") document_loader = DocumentLoaderTesseract(tesseract_path) # Initialize multiple extractors with different models gpt_35_extractor = Extractor(document_loader) gpt_35_extractor.load_llm("gpt-3.5-turbo") claude_extractor = Extractor(document_loader) claude_extractor.load_llm("claude-3-haiku-20240307") gpt4_extractor = Extractor(document_loader) gpt4_extractor.load_llm("gpt-4o") # Create process with multiple extractors process = Process() process.add_classify_extractor([ [gpt_35_extractor, claude_3_haiku_extractor], # First layer [gpt4_extractor], # Second layer ]) # Classify with consensus strategy result = process.classify( "document.pdf", classifications, strategy=ClassificationStrategy.CONSENSUS_WITH_THRESHOLD, threshold=9 ) ``` -------------------------------- ### Setup Evaluator for Cost Tracking Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/evals/index.md Configure the Evaluator to track token usage and associated costs. Ensure the `response_model` is correctly defined. ```python evaluator = Evaluator( extractor=extractor, response_model=InvoiceContract, track_costs=True ) ``` -------------------------------- ### Process Implementation Example Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/process/index.md This snippet includes the full implementation of the Process class from the extract_thinker library. ```python --8<-- "extract_thinker/process.py" ``` -------------------------------- ### Example Thinking Output Format Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/llm-integration/thinking-models.md When thinking is enabled, the LLM output includes a reasoning section within `` tags, followed by the structured JSON output. ```text Let me analyze this invoice... I can see the invoice number at the top right: INV-2023-0042 The date appears to be January 15, 2024 The total amount is $1,234.56 ##JSON OUTPUT { "invoice_number": "INV-2023-0042", "date": "2024-01-15", "total_amount": 1234.56 } ``` -------------------------------- ### Enable Cost Tracking in Teacher-Student Evaluations Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/evals/cost-tracking.md Initialize a `TeacherStudentEvaluator` with `track_costs=True` to compare the cost implications of using different models in a teacher-student setup. ```python from extract_thinker.eval import TeacherStudentEvaluator # Set up teacher-student evaluator with cost tracking evaluator = TeacherStudentEvaluator( student_extractor=student_extractor, teacher_extractor=teacher_extractor, response_model=InvoiceContract, track_costs=True # Enable cost tracking ) # Run evaluation report = evaluator.evaluate(dataset) # The report will include cost differences between teacher and student models student_cost = report.metrics["student_average_cost"] teacher_cost = report.metrics["teacher_average_cost"] cost_ratio = teacher_cost / student_cost print(f"Cost ratio (teacher/student): {cost_ratio:.2f}x") print(f"Accuracy improvement: {report.metrics['document_accuracy_improvement']:.2f}%") ``` -------------------------------- ### Basic Setup and Document Extraction with Google Document AI Source: https://github.com/enoch3712/extractthinker/blob/main/docs/examples/google-stack.md Initializes Google Document AI and configures an extractor with a custom Pydantic contract for invoice data. It's recommended to use Gemini models for enhanced extraction accuracy. ```python from extract_thinker import Extractor, Contract from extract_thinker.document_loader import DocumentLoaderGoogleDocumentAI from typing import List from pydantic import Field class InvoiceLineItem(Contract): description: str = Field(description="Description of the item") quantity: int = Field(description="Quantity of items purchased") unit_price: float = Field(description="Price per unit") amount: float = Field(description="Total amount for this line") class InvoiceContract(Contract): invoice_number: str = Field(description="Unique invoice identifier") invoice_date: str = Field(description="Date of the invoice") total_amount: float = Field(description="Overall total amount") line_items: List[InvoiceLineItem] = Field(description="List of items in this invoice") # Initialize Google Document AI extractor = Extractor() extractor.load_document_loader( DocumentLoaderGoogleDocumentAI( project_id=os.getenv("DOCUMENTAI_PROJECT_ID"), location=os.getenv("DOCUMENTAI_LOCATION"), # 'us' or 'eu' processor_id=os.getenv("DOCUMENTAI_PROCESSOR_ID"), credentials=os.getenv("DOCUMENTAI_GOOGLE_CREDENTIALS") ) ) # Configure Gemini model (recommended for enhanced extraction) extractor.load_llm("vertex_ai/gemini-2.0-flash-exp") # Process document result = extractor.extract( source="invoice.pdf", response_model=InvoiceContract, vision=True # Enable vision mode for better results with Gemini ) ``` -------------------------------- ### Configurable EasyOCR Document Loading Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/document-loaders/easy_ocr.md Configure the EasyOCR loader with specific language support, GPU acceleration, caching, and bounding box inclusion. This example demonstrates loading a PDF and processing detailed extraction results. ```python from extract_thinker import DocumentLoaderEasyOCR, EasyOCRConfig # Create configuration config = EasyOCRConfig( lang_list=['en', 'fr'], # Use English and French gpu=True, # Enable GPU acceleration cache_ttl=600, # Cache results for 10 minutes include_bbox=True # Include bounding box details ) # Initialize loader with configuration loader = DocumentLoaderEasyOCR(config) # Load a PDF document pages = loader.load("path/to/your/document.pdf") # Process extracted content with details for page in pages: print(f"Content: {page['content']}") if 'detail' in page: for detail in page['detail']: print(f" - Text: {detail['text']}, BBox: {detail['bbox']}") ``` -------------------------------- ### Quick Start: Extract Invoice Data Source: https://github.com/enoch3712/extractthinker/blob/main/docs/getting-started/index.md A basic example demonstrating how to extract structured data from an invoice PDF using Extract Thinker. Define a contract for the data you want to extract, initialize the extractor, load a document loader and LLM, and then call the extract method. ```python from extract_thinker import Extractor, DocumentLoaderPyPdf, Contract # Define what data you want to extract class InvoiceContract(Contract): invoice_number: str invoice_date: str total_amount: float # Initialize the extractor extractor = Extractor() extractor.load_document_loader(DocumentLoaderPyPdf()) extractor.load_llm("gpt-4") # or any other supported model # Extract data from your document result = extractor.extract("invoice.pdf", InvoiceContract) print(f"Invoice #{result.invoice_number}") print(f"Date: {result.invoice_date}") print(f"Total: ${result.total_amount}") ``` -------------------------------- ### Initialize Document Loader with Configuration Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/document-loaders/index.md Demonstrates how to initialize a Document Loader with specific configuration settings, such as AWS access keys and feature types for Textract. ```python from extract_thinker import DocumentLoaderAWSTextract, TextractConfig # Create configuration config = TextractConfig( aws_access_key_id="your_key", feature_types=["TABLES", "FORMS"], cache_ttl=600 ) # Initialize with configuration loader = DocumentLoaderAWSTextract(config) ``` -------------------------------- ### Install ExtractThinker Source: https://github.com/enoch3712/extractthinker/blob/main/README.md Install the extract_thinker library using pip. Ensure you have Python 3.9 or later. ```bash pip install extract_thinker ``` -------------------------------- ### Basic Process Workflow Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/process/index.md This demonstrates the fundamental pipeline for loading, splitting, and extracting data from a document. ```python split_content = process.load_file(path) .split(classifications) .extract() ``` -------------------------------- ### Using PAGINATE Strategy for Extraction Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/completion-strategies/paginate.md Demonstrates how to initialize the Extractor, load an LLM, and perform extraction using the PAGINATE completion strategy. Ensure 'file_path' and 'ResponseModel' are defined elsewhere. ```python from extract_thinker import Extractor from extract_thinker.models.completion_strategy import CompletionStrategy extractor = Extractor() extractor.load_llm("gpt-4") result = extractor.extract( file_path, ResponseModel, completion_strategy=CompletionStrategy.PAGINATE ) ``` -------------------------------- ### Initialize and Use Image Splitter Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/splitters/image.md Demonstrates initializing the Process with Tesseract OCR, loading the ImageSplitter with a specified vision model, and performing document splitting and extraction. ```python from extract_thinker import ImageSplitter, Process, SplittingStrategy from extract_thinker.document_loader import DocumentLoaderTesseract # Initialize process and loader process = Process() process.load_document_loader(DocumentLoaderTesseract(tesseract_path)) # Initialize image splitter with vision model process.load_splitter(ImageSplitter("claude-3-5-sonnet-20241022")) # Split document result = process.load_file("document.pdf")\ .split(classifications, strategy=SplittingStrategy.EAGER)\ .extract() ``` -------------------------------- ### Basic Text Splitting with Tesseract Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/splitters/text.md Demonstrates initializing the Process, loading a Tesseract document loader, initializing the TextSplitter with a specified model, and then splitting and extracting content from a PDF document using the EAGER strategy. ```python from extract_thinker import TextSplitter, Process, SplittingStrategy from extract_thinker.document_loader import DocumentLoaderTesseract # Initialize process and loader process = Process() process.load_document_loader(DocumentLoaderTesseract(tesseract_path)) # Initialize text splitter with model process.load_splitter(TextSplitter("claude-3-5-sonnet-20241022")) # Split document result = process.load_file("document.pdf") .split(classifications, strategy=SplittingStrategy.EAGER) .extract() ``` -------------------------------- ### Verify Tesseract Installation on Windows Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/document-loaders/tesseract.md Verify that Tesseract OCR is correctly installed and accessible in the system's PATH by running the 'where.exe tesseract' command in PowerShell. ```powershell where.exe tesseract ``` -------------------------------- ### Initialize and Load Document with MarkItDown Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/document-loaders/markitdown.md Demonstrates basic initialization of the MarkItDown loader and loading a document. Access the extracted text content from the 'content' field of each page. ```python from extract_thinker import DocumentLoaderMarkItDown # Initialize with default settings loader = DocumentLoaderMarkItDown() # Load document pages = loader.load("path/to/your/document.pdf") # Process extracted content for page in pages: # Access text content text = page["content"] ``` -------------------------------- ### Windows Tesseract Installation Environment Variable Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/document-loaders/tesseract.md Set the TESSERACT_PATH environment variable in a .env file to point to the Tesseract executable. This is crucial for the loader to find the Tesseract installation on Windows. ```bash TESSERACT_PATH="C:\\ Program Files\\Tesseract-OCR\\tesseract.exe" ``` -------------------------------- ### Configure Router with Multiple Models and Fallbacks Source: https://github.com/enoch3712/extractthinker/blob/main/docs/examples/resume-processing.md Set up a production-ready router with a list of models, including rate limits and API keys. Configure fallback models for robustness and specify context window fallbacks for different model interactions. ```python import os from extract_thinker import Router def config_router(): rpm = 5000 # Rate limit in requests per minute model_list = [ { "model_name": "Meta-Llama-3-8B-Instruct", "litellm_params": { "model": "deepinfra/meta-llama/Meta-Llama-3-8B-Instruct", "api_key": os.getenv("DEEPINFRA_API_KEY"), "rpm": rpm, }, }, { "model_name": "Mistral-7B-Instruct-v0.2", "litellm_params": { "model": "deepinfra/mistralai/Mistral-7B-Instruct-v0.2", "api_key": os.getenv("DEEPINFRA_API_KEY"), "rpm": rpm, } }, { "model_name": "groq-llama3-8b-8192", "litellm_params": { "model": "groq/llama3-8b-8192", "api_key": os.getenv("GROQ_API_KEY"), "rpm": rpm, } }, ] # Add fallback models fallback_models = [ { "model_name": "claude-3-haiku-20240307", "litellm_params": { "model": "claude-3-haiku-20240307", "api_key": os.getenv("CLAUDE_API_KEY"), } } ] router = Router( model_list=model_list + fallback_models, default_fallbacks=["claude-3-haiku-20240307"], context_window_fallbacks=[ {"Meta-Llama-3-8B-Instruct": ["claude-3-haiku-20240307"]}, {"groq-llama3-8b-8192": ["claude-3-haiku-20240307"]}, {"Mistral-7B-Instruct-v0.2": ["claude-3-haiku-20240307"]} ], set_verbose=True ) return router ``` -------------------------------- ### Setup Evaluator for Hallucination Detection Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/evals/index.md Configure the Evaluator to detect hallucinations in the response. Ensure the `response_model` is correctly defined. ```python evaluator = Evaluator( extractor=extractor, response_model=InvoiceContract, detect_hallucinations=True ) ``` -------------------------------- ### CLI Evaluation Configuration Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/evals/index.md Example JSON configuration file for the ExtractThinker CLI evaluation. This defines dataset, model, and output parameters. ```json { "evaluation_name": "Invoice Extraction Test", "dataset_name": "Invoice Dataset", "contract_path": "./contracts/invoice_contract.py", "documents_dir": "./test_invoices/", "labels_path": "./test_invoices/labels.json", "file_pattern": "*.pdf", "llm": { "model": "gpt-4o", "params": { "temperature": 0.0 } }, "vision": false, "skip_failures": false } ``` -------------------------------- ### Initialize LLM with Default Stack Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/llm-integration/index.md Initialize the LLM using the default stack, which combines instructor for structured outputs and litellm for model interfacing. ```python llm = LLM("gpt-4o", backend=LLMEngine.DEFAULT) ``` -------------------------------- ### Custom Loader with Cache TTL Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/document-loaders/index.md Example of creating a custom document loader that inherits from the base DocumentLoader and utilizes the cache_ttl parameter for cache expiration. ```python from extract_thinker.document_loader import DocumentLoader from typing import Any class MyCustomLoader(DocumentLoader): def __init__(self, content: Any = None, cache_ttl: int = 300): super().__init__(content, cache_ttl) # 300 seconds default TTL ``` -------------------------------- ### Basic Batch Processing with Extractor Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/extractors/batch.md Initialize the Extractor, load an LLM, and create a batch job to process PDF files. Monitor the job's status and retrieve the results. ```python from extract_thinker import Extractor, Contract class InvoiceContract(Contract): invoice_number: str total_amount: float # Initialize batch processing extractor = Extractor() extractor.load_llm("gpt-4o-mini") # Create batch job batch_job = extractor.extract_batch( "invoices/*.pdf", InvoiceContract ) # Monitor status and get results status = await batch_job.get_status() results = await batch_job.get_result() ``` -------------------------------- ### Process Candidate Resume with Context Source: https://github.com/enoch3712/extractthinker/blob/main/docs/examples/resume-processing.md Initialize another Extractor for candidate resumes, load a PDF document loader, and configure an LLM. Process the resume, providing the previously extracted job requirements as context for matching. ```python # Initialize extractor for candidate resume extractor_candidate = Extractor() extractor_candidate.load_document_loader(DocumentLoaderPyPdf()) # Configure LLM with Groq llm = LLM("groq/llama3-8b-8192") # default model extractor_candidate.load_llm(llm) # Process resume with job context result = extractor_candidate.extract( "CV_Candidate.pdf", ResumeContract, content=job_role_content # Provide job requirements as context ) ``` -------------------------------- ### Adding Context to Extraction Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/extractors/index.md Provide additional context to the extractor to guide the extraction process, such as job requirements for resume analysis. The context is passed using the 'content' parameter. ```python from extract_thinker import Extractor, Contract class ResumeContract(Contract): name: str skills: List[str] experience: List[dict] #Add context about the job requirements job_description = { "role": "Software Engineer", "required_skills": ["Python", "AWS", "Docker"] } result = extractor.extract( "resume.pdf", ResumeContract, content=job_description # Add extra context ) ``` -------------------------------- ### Configure MarkItDown Loader with Custom Settings Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/document-loaders/markitdown.md Shows how to initialize the MarkItDown loader with custom configurations using MarkItDownConfig. This allows for options like custom page separators, whitespace preservation, MIME type detection, default extensions, LLM client integration, and cache TTL. ```python from extract_thinker import DocumentLoaderMarkItDown, MarkItDownConfig # Create configuration config = MarkItDownConfig( page_separator="---", # Custom page separator preserve_whitespace=True, # Preserve original whitespace mime_type_detection=True, # Enable MIME type detection default_extension=".md", # Default file extension llm_client="gpt-4", # LLM client for enhanced parsing cache_ttl=600 # Cache results for 10 minutes ) # Initialize loader with configuration loader = DocumentLoaderMarkItDown(config) # Load and process document pages = loader.load("path/to/your/document.md") ``` -------------------------------- ### Define Pydantic Contracts for Job Role and Resume Source: https://github.com/enoch3712/extractthinker/blob/main/docs/examples/resume-processing.md Define Pydantic models for structuring job role requirements and candidate resume data. These contracts guide the extraction process. ```python from extract_thinker import Extractor, Contract, DocumentLoaderPyPdf, LLM from typing import List, Optional from pydantic import Field # Define the job role contract class RoleContract(Contract): company_name: str = Field("Company name") years_of_experience: int = Field("Years of experience required. If not mention, calculate with start date and end date") is_remote: bool = Field("Is the role remote?") country: str = Field("Country of the role") city: Optional[str] = Field("City of the role") list_of_skills: List[str] = Field(""" list of strings, e.g ["5 years experience", "3 years in React", "Typescript"] Make the lists of skills to be a yes/no list for matching with candidates """) # Define the resume contract class ResumeContract(Contract): name: str = Field("First and Last Name") age: Optional[str] = Field("Age with format DD/MM/YYYY. Empty if not available") email: str = Field("Email address") phone: Optional[str] = Field("Phone number") address: Optional[str] = Field("Address") city: Optional[str] = Field("City") total_experience: int = Field("Total experience in years") can_go_to_office: Optional[bool] = Field("Can go to office. If city/location is not provider, is false. If is the same city, is true") list_of_skills: List[bool] = Field("Takes the list of skills and returns a list of true/false, if the candidate has that skill") ``` -------------------------------- ### Initialize MarkdownConverter Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/markdown-conversion/index.md Initialize the MarkdownConverter with or without pre-configured document loaders and LLMs. Components can also be loaded after initialization. ```python from extract_thinker.markdown import MarkdownConverter from extract_thinker.document_loader import DocumentLoaderPyPdf # Example loader from extract_thinker.llm import LLM from extract_thinker.global_models import get_lite_model, get_big_model # Helpers for model config # Initialize with or without components markdown_converter = MarkdownConverter() # Load components later loader = DocumentLoaderPyPdf() # Configure as needed # Use helper functions to get model configurations # Replace with your actual logic for selecting/configuring models if needed llm = LLM(get_lite_model()) markdown_converter.load_document_loader(loader) markdown_converter.load_llm(llm) # Or initialize directly markdown_converter = MarkdownConverter(document_loader=loader, llm=llm) ``` -------------------------------- ### Document Extraction with Groq Source: https://github.com/enoch3712/extractthinker/blob/main/docs/examples/groq-processing.md Initialize the Extractor with PyPDF loader, configure Groq, and process a PDF document for extraction into a specified contract. ```python from extract_thinker import Extractor from extract_thinker.document_loader.document_loader_pypdf import DocumentLoaderPyPdf from typing import List from pydantic import Field class InvoiceContract(Contract): lines: List[LineItem] = Field("List of line items in the invoice") # Initialize extractor with PyPDF loader extractor = Extractor() extractor.load_document_loader(DocumentLoaderPyPdf()) # Configure Groq extractor.load_llm("groq/llama-3.2-11b-vision-preview") # Process document result = extractor.extract("invoice.pdf", InvoiceContract) ``` -------------------------------- ### Basic Tesseract Document Loading Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/document-loaders/tesseract.md Initialize the Tesseract loader with default settings and load text content from an image file. ```python from extract_thinker import DocumentLoaderTesseract # Initialize with default settings loader = DocumentLoaderTesseract() # Load document pages = loader.load("path/to/your/image.png") # Process extracted content for page in pages: # Access text content text = page["content"] ``` -------------------------------- ### Template Structure for Dynamic Parsing Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/llm-integration/dynamic-parsing.md This template guides the LLM to provide a thinking process within `` tags followed by the JSON output. Ensure your JSON structure is clearly defined. ```text Please provide your thinking process within tags, followed by your JSON output. JSON structure: {your_structure} OUTPUT example: Your step-by-step reasoning and analysis goes here... ##JSON OUTPUT { ... } ``` -------------------------------- ### Configuration-based Data Document Loader Initialization Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/document-loaders/data.md Initialize the loader with a custom configuration, specifying cache TTL and vision support. Load and process raw text content. ```python from extract_thinker import DocumentLoaderData, DataLoaderConfig # Create configuration config = DataLoaderConfig( content=None, # Initial content cache_ttl=600, # Cache results for 10 minutes supports_vision=True # Enable vision support ) # Initialize loader with configuration loader = DocumentLoaderData(config) # Load and process content pages = loader.load("raw text content") ``` -------------------------------- ### Define Pydantic Contracts for Classification Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/classification/basic.md Define Pydantic models that inherit from `extract_thinker.models.contract.Contract` to provide structured data expectations for classification. This enhances accuracy by guiding the LLM on the expected output format. ```python from typing import List from extract_thinker.models.contract import Contract class InvoiceContract(Contract): invoice_number: str invoice_date: str lines: List[LineItem] total_amount: float class DriverLicense(Contract): name: str age: int license_number: str ``` -------------------------------- ### Configure Document Loaders Source: https://context7.com/enoch3712/extractthinker/llms.txt Demonstrates loading various document loaders into an Extractor instance, including PyPDF, PDFPlumber, Tesseract OCR, Azure Form Recognizer, AWS Textract, Spreadsheets, plain text, and Mistral OCR. ```python from extract_thinker import ( Extractor, Contract, DocumentLoaderPyPdf, PyPDFConfig, DocumentLoaderPdfPlumber, PDFPlumberConfig, DocumentLoaderTesseract, TesseractConfig, DocumentLoaderAzureForm, AzureConfig, DocumentLoaderAWSTextract, TextractConfig, DocumentLoaderSpreadSheet, DocumentLoaderTxt, DocumentLoaderMarkItDown, DocumentLoaderBeautifulSoup, DocumentLoaderLLMImage, LLMImageConfig, DocumentLoaderMistralOCR, MistralOCRConfig, DocumentLoaderGoogleDocumentAI, GoogleDocAIConfig, ) class DocContract(Contract): title: str content_summary: str extractor = Extractor() extractor.load_llm("gpt-4o-mini") # --- PyPDF (pure Python, no dependencies) --- extractor.load_document_loader(DocumentLoaderPyPdf()) # --- PDFPlumber (better table extraction) --- extractor.load_document_loader(DocumentLoaderPdfPlumber()) # --- Tesseract OCR --- extractor.load_document_loader( DocumentLoaderTesseract("/usr/bin/tesseract") ) # --- Azure Form Recognizer / Document Intelligence --- extractor.load_document_loader(DocumentLoaderAzureForm( AzureConfig( endpoint="https://your-resource.cognitiveservices.azure.com/", key="your-key" ) )) # --- AWS Textract --- extractor.load_document_loader(DocumentLoaderAWSTextract( TextractConfig( aws_access_key_id="AKIA...", aws_secret_access_key="secret", region_name="us-east-1" ) )) # --- Spreadsheets (xlsx, csv) --- extractor.load_document_loader(DocumentLoaderSpreadSheet()) # --- Plain text --- extractor.load_document_loader(DocumentLoaderTxt()) # --- Mistral OCR (cloud-based) --- extractor.load_document_loader( DocumentLoaderMistralOCR(MistralOCRConfig(api_key="your-key")) ) ``` -------------------------------- ### Initialize AWS Textract Loader Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/document-loaders/aws-textract.md Initialize the loader with AWS credentials and region. Then, load a document and access its content and tables. ```python from extract_thinker import DocumentLoaderAWSTextract # Initialize with AWS credentials loader = DocumentLoaderAWSTextract( aws_access_key_id="your_access_key", aws_secret_access_key="your_secret_key", region_name="your_region" ) # Load document pages = loader.load("path/to/your/document.pdf") # Process extracted content for page in pages: # Access text content text = page["content"] # Access tables if extracted tables = page.get("tables", []) ``` -------------------------------- ### Configure Docling Document Loader Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/document-loaders/docling.md Initialize the Docling loader with custom configurations, including OCR settings, Tesseract path, and format-specific options. This allows for fine-tuned document processing. ```python from extract_thinker import DocumentLoaderDocling, DoclingConfig # Create configuration config = DoclingConfig( ocr_enabled=True, # Enable OCR processing table_structure_enabled=True, # Enable table structure detection tesseract_cmd="path/to/tesseract", # Custom Tesseract path force_full_page_ocr=False, # Use selective OCR do_cell_matching=True, # Enable cell content matching format_options={ "pdf": {"dpi": 300}, "image": {"enhance": True} }, cache_ttl=600 # Cache results for 10 minutes ) # Initialize loader with configuration loader = DocumentLoaderDocling(config) # Load and process document pages = loader.load("path/to/your/document.pdf") ``` -------------------------------- ### Initialize and Configure AWS Textract and Claude Source: https://github.com/enoch3712/extractthinker/blob/main/docs/examples/aws-stack.md Set up the Extractor with AWS Textract document loader using environment variables for credentials and region. Configure the Claude LLM with its model name and API key. ```python from extract_thinker import Extractor, Contract, LLM, DocumentLoaderTextract from typing import List from pydantic import Field import os class InvoiceContract(Contract): invoice_number: str = Field("Invoice number") invoice_date: str = Field("Invoice date") total_amount: float = Field("Total amount") lines: List[LineItem] = Field("List of line items") # Initialize AWS Textract extractor = Extractor() extractor.load_document_loader( DocumentLoaderTextract( aws_access_key_id=os.getenv("AWS_ACCESS_KEY_ID"), aws_secret_access_key=os.getenv("AWS_SECRET_ACCESS_KEY"), region_name=os.getenv("AWS_REGION") ) ) # Configure Claude llm = LLM( "anthropic/claude-3-haiku-20240307", api_key=os.getenv("ANTHROPIC_API_KEY") ) extractor.load_llm(llm) # Process document result = extractor.extract("invoice.pdf", InvoiceContract) ``` -------------------------------- ### FORBIDDEN Strategy Example Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/completion-strategies/index.md Demonstrates the default FORBIDDEN strategy. This will raise a ValueError if the content exceeds the model's context window. Ensure content fits within the context window or use other strategies. ```python from extract_thinker import Extractor from extract_thinker.models.completion_strategy import CompletionStrategy extractor = Extractor() extractor.load_llm("gpt-4o") # Will raise ValueError if content is too large result = extractor.extract( file_path, ResponseModel, completion_strategy=CompletionStrategy.FORBIDDEN # Default ) ``` -------------------------------- ### Initialize and Load Document with Mistral OCR Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/document-loaders/mistral-ocr.md Demonstrates basic usage of the DocumentLoaderMistralOCR by initializing it with configuration and loading a document from a URL or file path. Shows how to access extracted markdown text and images. ```python from extract_thinker import DocumentLoaderMistralOCR, MistralOCRConfig # Create configuration config = MistralOCRConfig( api_key="your_mistral_api_key", model="mistral-ocr-latest" ) # Initialize loader loader = DocumentLoaderMistralOCR(config) # Load from URL pages = loader.load("https://example.com/document.pdf") # Load from file path pages = loader.load("path/to/your/document.pdf") # Process extracted content for page in pages: # Access text content (in markdown format) markdown_text = page["content"] # Access images if available if "images" in page: for image in page["images"]: image_id = image["id"] image_base64 = image["image_base64"] # If include_image_base64=True ``` -------------------------------- ### Integrate Local LLM with ExtractThinker Source: https://github.com/enoch3712/extractthinker/blob/main/README.md Use ExtractThinker with a local LLM provider like Ollama. This example demonstrates setting up the environment variables for the local API and loading a custom LLM for data extraction from an image. ```python from extract_thinker import Extractor, LLM, DocumentLoaderTesseract, Contract class InvoiceContract(Contract): invoice_number: str invoice_date: str # Initialize the extractor extractor = Extractor() extractor.load_document_loader(DocumentLoaderTesseract(os.getenv("TESSERACT_PATH"))) # Load a custom LLM (e.g., Ollama) os.environ['API_BASE'] = "http://localhost:11434" llm = LLM('ollama/phi3') extractor.load_llm(llm) # Extract data result = extractor.extract("invoice.png", InvoiceContract) print("Invoice Number:", result.invoice_number) print("Invoice Date:", result.invoice_date) ``` -------------------------------- ### Batch Process Documents with ExtractThinker Source: https://github.com/enoch3712/extractthinker/blob/main/README.md Perform batch processing on a list of documents using ExtractThinker. This example shows how to submit a batch job and retrieve the parsed results, suitable for processing many similar documents efficiently. ```python from extract_thinker import Extractor, Contract class ReceiptContract(Contract): store_name: str total_amount: float extractor = Extractor() extractor.load_llm("gpt-4o-mini") # List of file paths or streams document = "receipt1.jpg" batch_job = extractor.extract_batch( source=document, response_model=ReceiptContract, vision=True, ) # Monitor the batch job status print("Batch Job Status:", await batch_job.get_status()) # Retrieve results once processing is complete results = await batch_job.get_result() for result in results.parsed_results: print("Store Name:", result.store_name) print("Total Amount:", result.total_amount) ``` -------------------------------- ### Tesseract Document Loading with Configuration Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/document-loaders/tesseract.md Initialize the Tesseract loader with custom configurations for language, page segmentation, OCR engine mode, and additional parameters. This allows for fine-tuning the OCR process. ```python from extract_thinker import DocumentLoaderTesseract, TesseractConfig # Create configuration config = TesseractConfig( lang="eng+fra", # Use English and French psm=6, # Assume uniform block of text oem=3, # Default LSTM OCR Engine Mode config_params={ "tessedit_char_whitelist": "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" }, timeout=30, # OCR timeout in seconds cache_ttl=600 # Cache results for 10 minutes ) # Initialize loader with configuration loader = DocumentLoaderTesseract(config) # Load and process document pages = loader.load("path/to/your/image.png") ``` -------------------------------- ### Configure LLM Image Loader Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/document-loaders/llm-image.md Initialize the LLM Image loader with custom configurations for image processing and LLM interaction. This allows for fine-tuning parameters like image size, format, compression, and the target LLM model. ```python from extract_thinker import DocumentLoaderLLMImage, LLMImageConfig # Create configuration config = LLMImageConfig( max_image_size=1024 * 1024, # Maximum image size in bytes image_format="jpeg", # Target image format compression_quality=85, # JPEG compression quality llm="gpt-4-vision", # Target LLM model cache_ttl=600 # Cache results for 10 minutes ) # Initialize loader with configuration loader = DocumentLoaderLLMImage(config) # Load and process document pages = loader.load("path/to/your/image.jpg") ``` -------------------------------- ### Basic Docling Document Loading Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/document-loaders/docling.md Initialize the Docling loader with default settings and load a document. Access extracted text and tables from the returned pages. ```python from extract_thinker import DocumentLoaderDocling # Initialize with default settings loader = DocumentLoaderDocling() # Load document pages = loader.load("path/to/your/document.pdf") # Process extracted content for page in pages: # Access text content text = page["content"] # Access tables if available tables = page.get("tables", []) ``` -------------------------------- ### Invoice Extraction using Dynamic Parsing Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/llm-integration/dynamic-parsing.md An example demonstrating invoice extraction using dynamic parsing. It defines a Pydantic model for the invoice data, initializes the LLM with dynamic parsing enabled, and uses an Extractor to process a PDF invoice. ```python from extract_thinker import LLM, Extractor from extract_thinker.document_loader import DocumentLoaderPyPdf from pydantic import BaseModel from typing import List, Optional # Define your invoice model class InvoiceData(BaseModel): invoice_number: str date: str total_amount: float vendor_name: str line_items: List[dict] payment_terms: Optional[str] # Initialize LLM with dynamic parsing llm = LLM("ollama/deepseek-r1:1.5b") llm.set_dynamic(True) # Enable dynamic JSON parsing # Setup document loader and extractor document_loader = DocumentLoaderPyPdf() extractor = Extractor(document_loader=document_loader, llm=llm) # Extract information from invoice result = extractor.extract("path/to/invoice.pdf", response_model=InvoiceData) ``` -------------------------------- ### PDFPlumber Document Loading with Custom Configuration Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/document-loaders/pdf-plumber.md Initialize the loader with custom configurations for table extraction, vision mode, and caching. This allows for fine-tuning the extraction process based on specific needs. ```python from extract_thinker import DocumentLoaderPdfPlumber, PDFPlumberConfig # Create configuration config = PDFPlumberConfig( table_settings={ "vertical_strategy": "text", "horizontal_strategy": "lines", "intersection_y_tolerance": 10 }, vision_enabled=True, extract_tables=True, cache_ttl=600 ) # Initialize loader with configuration loader = DocumentLoaderPdfPlumber(config) # Load and process document pages = loader.load("path/to/your/document.pdf") ``` -------------------------------- ### Initialize Extractor Source: https://github.com/enoch3712/extractthinker/blob/main/examples/notebooks/basic_example.ipynb Create an Extractor instance, which coordinates the extraction process. It requires a DocumentLoader and an LLM. ```python from extract_thinker import Extractor extractor = Extractor() ``` -------------------------------- ### PyPDF Document Loader with Configuration Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/document-loaders/pypdf.md Initialize the loader with custom configurations, including password for protected PDFs, enabling vision mode for images, and setting cache TTL. Load and process the document using these settings. ```python from extract_thinker import DocumentLoaderPyPdf, PyPDFConfig # Create configuration config = PyPDFConfig( password="your_password", # For password-protected PDFs vision_enabled=True, # Enable vision mode for images extract_text=True, # Enable text extraction cache_ttl=600 # Cache results for 10 minutes ) # Initialize loader with configuration loader = DocumentLoaderPyPdf(config) # Load and process document pages = loader.load("path/to/your/document.pdf") ``` -------------------------------- ### Basic Azure Document Intelligence Loader Usage Source: https://github.com/enoch3712/extractthinker/blob/main/docs/core-concepts/document-loaders/azure-form.md Initialize the loader with Azure credentials and load a document. Access extracted content, tables, and forms from the returned pages. ```python from extract_thinker import DocumentLoaderAzureForm # Initialize with Azure credentials loader = DocumentLoaderAzureForm( subscription_key="your_subscription_key", endpoint="your_endpoint", model_id="prebuilt-document" # Use prebuilt document model ) # Load document pages = loader.load("path/to/your/document.pdf") # Process extracted content for page in pages: # Access text content text = page["content"] # Access tables if available tables = page.get("tables", []) # Access form fields if available forms = page.get("forms", {}) ```