PHPLaravelFilamentOllamaQwen3SQLitePHPUnitLLMs

InvoicePilot

A Laravel application that uses a local LLM to extract invoice data from PDFs, check the results, and let users review and approve them.

InvoicePilot invoice overview with review statuses and extracted invoice data

The Problem

Extracting data from an invoice with an LLM is fairly straightforward. Getting data you can actually trust is harder. A model can return valid JSON while still getting an invoice number wrong, missing a field, or returning amounts that do not add up. I wanted to build a workflow that catches these issues and lets users check the result before approving it.

  • Invoices use different layouts for the same information.
  • Valid JSON does not mean the extracted values are correct.
  • Dates, amounts, currencies, and possible duplicates need separate checks.
  • Users need to be able to correct mistakes before approving an invoice.

The Solution

I built InvoicePilot with Laravel and Filament. It extracts text from PDF invoices and sends it to a local Qwen3 model through Ollama. The model returns seven fields using a JSON Schema, which Laravel then validates. Users can review the results, fix mistakes, and manually approve the invoice. The original model output is kept separately so corrections do not overwrite it.

  • Upload PDF invoices with selectable text and process them locally.
  • Extract supplier details, invoice numbers, dates, amounts, and currency.
  • Check for missing fields, invalid dates, inconsistent amounts, unsupported currency, and possible duplicates.
  • Review warnings and edit extracted values while keeping the original output.
  • Approve invoices once validation passes.
  • Switch the Ollama model through environment configuration.

Architecture

How It Works

The application first extracts the text from an uploaded PDF. It sends that text to Ollama with a JSON Schema describing the expected fields. A separate PHP validator checks the response and compares it with existing invoices for possible duplicates. Amounts are checked using integer cents to avoid floating-point rounding issues. SQLite stores the original AI response, editable invoice data, validation warnings, model name, and processing time. Users review the invoice in Filament, and validation runs again before approval.

PDF Text ExtractionOllamaJSON SchemaPHP ValidationSQLiteFilament
InvoiceValidator.php
$amounts = [];

foreach (['subtotal_ex_vat', 'vat_amount', 'total_in_vat'] as $field) {
    $value = $data[$field] ?? null;

    if (!is_string($value) || trim($value) === '') {
        continue;
    }

    if (!preg_match(
        '/\A(?:0|[1-9][0-9]{0,11})\.[0-9]{2}\z/',
        $value
    )) {
        $errors[] = "Invalid monetary amount: {$field}.";
        continue;
    }

    [$euros, $cents] = explode('.', $value);

    $amounts[$field] = (int) $euros * 100 + (int) $cents;
}

if (count($amounts) === 3) {
    $difference = abs(
        $amounts['subtotal_ex_vat']
            + $amounts['vat_amount']
            - $amounts['total_in_vat']
    );

    if ($difference > 2) {
        $errors[] = 'Subtotal + VAT does not match total.';
    }
}

Screenshots

Filament invoice overview displaying suppliers, extracted amounts, and review statuses
Invoice review form showing extracted fields and a monetary validation warning

Technical Decisions

Running The Model Locally

I used Qwen3 through Ollama to run the extraction locally. The application requests seven fields using a JSON Schema, with null for missing or uncertain values. The model can be changed through environment configuration, which makes it easy to try another model without changing the extraction code.

Validating The Results In PHP

The model handles extraction, while PHP checks the returned values. These checks cover required fields, dates, currency, possible duplicates, and whether the amounts add up. I convert monetary values to integer cents before comparing them to avoid floating-point rounding issues.

Keeping Review And Approval Explicit

Users can correct extracted values in Filament without changing the original AI response. Validation runs again after edits and before approval. Approval is a manual action, and approved invoices can no longer be edited.

Testing The Workflow And The Model

I added 40 automated tests covering PDF handling, mocked Ollama responses, validation, duplicates, and approval rules. I also evaluated the model on 12 synthetic invoice cases. A repeated invoice-number error led me to adjust the prompt, improving extraction from 83/84 to 84/84 correct fields on that dataset.

What Went Wrong

One invoice had the number HC-2026-500. The PDF parser read it correctly, but Qwen3 repeatedly returned HC-2026-50. In another run, the model used the document title as an invoice number when no invoice number was present. I updated the prompt to copy identifiers exactly and return null when a value could not be identified. This improved the result from 83/84 to 84/84 correct fields on the same 12 test cases. It also showed why the application needs separate validation and manual review. The dataset is small and synthetic, so this result does not show how accurately the model would handle real invoices in production.

What I Learned

Connecting An LLM To An Application

I built a Laravel workflow around a local model, including structured responses, explicit field types, timeout handling, and configurable model selection.

Handling Incorrect Model Output

A response can match the expected JSON structure and still contain incorrect data. I kept validation and approval separate from extraction so the application does not automatically accept whatever the model returns.

Improving The Prompt Through Evaluation

Comparing extracted fields against known values helped me find a repeatable error and test a specific prompt change. Recording the evaluation runs made the improvement easier to check.

Testing The Full Workflow

I separated PDF extraction, model calls, validation, storage, and approval into components that can be tested individually. Feature tests also check that invalid or duplicate invoices cannot bypass the approval rules.

Damien Hensen

A Bit About Me

Outside of software development, I enjoy gaming, anime, and working on personal projects that help me learn something new.

Whether it's building applications, automating workflows, or experimenting with new technologies, I enjoy creating things and understanding how they work.

Let's connect.