356 lines
11 KiB
TypeScript
356 lines
11 KiB
TypeScript
|
|
/**
|
||
|
|
* Invoice extraction using Ministral 3 Vision (Direct)
|
||
|
|
*
|
||
|
|
* NO PaddleOCR needed - Ministral 3 has built-in vision encoder:
|
||
|
|
* 1. Convert PDF to images
|
||
|
|
* 2. Send images directly to Ministral 3 via Ollama
|
||
|
|
* 3. Extract structured JSON with native schema support
|
||
|
|
*
|
||
|
|
* This is the simplest possible pipeline.
|
||
|
|
*/
|
||
|
|
import { tap, expect } from '@git.zone/tstest/tapbundle';
|
||
|
|
import * as fs from 'fs';
|
||
|
|
import * as path from 'path';
|
||
|
|
import { execSync } from 'child_process';
|
||
|
|
import * as os from 'os';
|
||
|
|
import { ensureMinistral3 } from './helpers/docker.js';
|
||
|
|
|
||
|
|
const OLLAMA_URL = 'http://localhost:11434';
|
||
|
|
const VISION_MODEL = 'ministral-3:8b';
|
||
|
|
|
||
|
|
interface IInvoice {
|
||
|
|
invoice_number: string;
|
||
|
|
invoice_date: string;
|
||
|
|
vendor_name: string;
|
||
|
|
currency: string;
|
||
|
|
net_amount: number;
|
||
|
|
vat_amount: number;
|
||
|
|
total_amount: number;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Convert PDF to PNG images using ImageMagick
|
||
|
|
*/
|
||
|
|
function convertPdfToImages(pdfPath: string): string[] {
|
||
|
|
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'pdf-convert-'));
|
||
|
|
const outputPattern = path.join(tempDir, 'page-%d.png');
|
||
|
|
|
||
|
|
try {
|
||
|
|
execSync(
|
||
|
|
`convert -density 200 -quality 90 "${pdfPath}" -background white -alpha remove "${outputPattern}"`,
|
||
|
|
{ stdio: 'pipe' }
|
||
|
|
);
|
||
|
|
|
||
|
|
const files = fs.readdirSync(tempDir).filter((f) => f.endsWith('.png')).sort();
|
||
|
|
const images: string[] = [];
|
||
|
|
|
||
|
|
for (const file of files) {
|
||
|
|
const imagePath = path.join(tempDir, file);
|
||
|
|
const imageData = fs.readFileSync(imagePath);
|
||
|
|
images.push(imageData.toString('base64'));
|
||
|
|
}
|
||
|
|
|
||
|
|
return images;
|
||
|
|
} finally {
|
||
|
|
fs.rmSync(tempDir, { recursive: true, force: true });
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Extract invoice data directly from images using Ministral 3 Vision
|
||
|
|
*/
|
||
|
|
async function extractInvoiceFromImages(images: string[]): Promise<IInvoice> {
|
||
|
|
console.log(` [Vision] Processing ${images.length} page(s) with Ministral 3`);
|
||
|
|
|
||
|
|
// JSON schema for structured output
|
||
|
|
const invoiceSchema = {
|
||
|
|
type: 'object',
|
||
|
|
properties: {
|
||
|
|
invoice_number: { type: 'string' },
|
||
|
|
invoice_date: { type: 'string' },
|
||
|
|
vendor_name: { type: 'string' },
|
||
|
|
currency: { type: 'string' },
|
||
|
|
net_amount: { type: 'number' },
|
||
|
|
vat_amount: { type: 'number' },
|
||
|
|
total_amount: { type: 'number' },
|
||
|
|
},
|
||
|
|
required: ['invoice_number', 'invoice_date', 'vendor_name', 'currency', 'net_amount', 'vat_amount', 'total_amount'],
|
||
|
|
};
|
||
|
|
|
||
|
|
const prompt = `Extract invoice data from this document image(s).
|
||
|
|
|
||
|
|
Find and return:
|
||
|
|
- invoice_number: The invoice number/ID (look for "Invoice No", "Invoice #", "Rechnung Nr")
|
||
|
|
- invoice_date: The invoice date in YYYY-MM-DD format
|
||
|
|
- vendor_name: The company issuing the invoice (in letterhead)
|
||
|
|
- currency: EUR, USD, or GBP
|
||
|
|
- total_amount: The FINAL total amount due
|
||
|
|
- net_amount: Amount before VAT/tax
|
||
|
|
- vat_amount: VAT/tax amount
|
||
|
|
|
||
|
|
Return ONLY valid JSON.`;
|
||
|
|
|
||
|
|
const response = await fetch(`${OLLAMA_URL}/api/chat`, {
|
||
|
|
method: 'POST',
|
||
|
|
headers: { 'Content-Type': 'application/json' },
|
||
|
|
body: JSON.stringify({
|
||
|
|
model: VISION_MODEL,
|
||
|
|
messages: [
|
||
|
|
{
|
||
|
|
role: 'user',
|
||
|
|
content: prompt,
|
||
|
|
images: images, // Send all page images
|
||
|
|
},
|
||
|
|
],
|
||
|
|
format: invoiceSchema,
|
||
|
|
stream: true,
|
||
|
|
options: {
|
||
|
|
num_predict: 512,
|
||
|
|
temperature: 0.0,
|
||
|
|
},
|
||
|
|
}),
|
||
|
|
});
|
||
|
|
|
||
|
|
if (!response.ok) {
|
||
|
|
throw new Error(`Ollama API error: ${response.status}`);
|
||
|
|
}
|
||
|
|
|
||
|
|
const reader = response.body?.getReader();
|
||
|
|
if (!reader) {
|
||
|
|
throw new Error('No response body');
|
||
|
|
}
|
||
|
|
|
||
|
|
const decoder = new TextDecoder();
|
||
|
|
let fullText = '';
|
||
|
|
|
||
|
|
while (true) {
|
||
|
|
const { done, value } = await reader.read();
|
||
|
|
if (done) break;
|
||
|
|
|
||
|
|
const chunk = decoder.decode(value, { stream: true });
|
||
|
|
const lines = chunk.split('\n').filter((l) => l.trim());
|
||
|
|
|
||
|
|
for (const line of lines) {
|
||
|
|
try {
|
||
|
|
const json = JSON.parse(line);
|
||
|
|
if (json.message?.content) {
|
||
|
|
fullText += json.message.content;
|
||
|
|
}
|
||
|
|
} catch {
|
||
|
|
// Skip invalid JSON lines
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Parse JSON response
|
||
|
|
let jsonStr = fullText.trim();
|
||
|
|
|
||
|
|
if (jsonStr.startsWith('```json')) jsonStr = jsonStr.slice(7);
|
||
|
|
else if (jsonStr.startsWith('```')) jsonStr = jsonStr.slice(3);
|
||
|
|
if (jsonStr.endsWith('```')) jsonStr = jsonStr.slice(0, -3);
|
||
|
|
jsonStr = jsonStr.trim();
|
||
|
|
|
||
|
|
const startIdx = jsonStr.indexOf('{');
|
||
|
|
const endIdx = jsonStr.lastIndexOf('}') + 1;
|
||
|
|
|
||
|
|
if (startIdx < 0 || endIdx <= startIdx) {
|
||
|
|
throw new Error(`No JSON found: ${fullText.substring(0, 200)}`);
|
||
|
|
}
|
||
|
|
|
||
|
|
const parsed = JSON.parse(jsonStr.substring(startIdx, endIdx));
|
||
|
|
|
||
|
|
return {
|
||
|
|
invoice_number: parsed.invoice_number || null,
|
||
|
|
invoice_date: parsed.invoice_date || null,
|
||
|
|
vendor_name: parsed.vendor_name || null,
|
||
|
|
currency: parsed.currency || 'EUR',
|
||
|
|
net_amount: parseFloat(parsed.net_amount) || 0,
|
||
|
|
vat_amount: parseFloat(parsed.vat_amount) || 0,
|
||
|
|
total_amount: parseFloat(parsed.total_amount) || 0,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Extract with consensus voting (2 agreeing passes)
|
||
|
|
*/
|
||
|
|
async function extractWithConsensus(images: string[], name: string, maxPasses: number = 3): Promise<IInvoice> {
|
||
|
|
const results: Array<{ invoice: IInvoice; hash: string }> = [];
|
||
|
|
const hashCounts: Map<string, number> = new Map();
|
||
|
|
|
||
|
|
for (let pass = 1; pass <= maxPasses; pass++) {
|
||
|
|
try {
|
||
|
|
const invoice = await extractInvoiceFromImages(images);
|
||
|
|
const hash = `${invoice.invoice_number}|${invoice.invoice_date}|${invoice.total_amount?.toFixed(2)}`;
|
||
|
|
results.push({ invoice, hash });
|
||
|
|
hashCounts.set(hash, (hashCounts.get(hash) || 0) + 1);
|
||
|
|
|
||
|
|
console.log(` [Pass ${pass}] ${invoice.invoice_number} | ${invoice.invoice_date} | ${invoice.total_amount} ${invoice.currency}`);
|
||
|
|
|
||
|
|
if (hashCounts.get(hash)! >= 2) {
|
||
|
|
console.log(` [Consensus] Reached after ${pass} passes`);
|
||
|
|
return invoice;
|
||
|
|
}
|
||
|
|
} catch (err) {
|
||
|
|
console.log(` [Pass ${pass}] Error: ${err}`);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Return most common result
|
||
|
|
let bestHash = '';
|
||
|
|
let bestCount = 0;
|
||
|
|
for (const [hash, count] of hashCounts) {
|
||
|
|
if (count > bestCount) {
|
||
|
|
bestCount = count;
|
||
|
|
bestHash = hash;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!bestHash) throw new Error(`No valid results for ${name}`);
|
||
|
|
|
||
|
|
console.log(` [No consensus] Using best result (${bestCount}/${maxPasses})`);
|
||
|
|
return results.find((r) => r.hash === bestHash)!.invoice;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Normalize date to YYYY-MM-DD
|
||
|
|
*/
|
||
|
|
function normalizeDate(dateStr: string | null): string {
|
||
|
|
if (!dateStr) return '';
|
||
|
|
if (/^\d{4}-\d{2}-\d{2}$/.test(dateStr)) return dateStr;
|
||
|
|
|
||
|
|
const monthMap: Record<string, string> = {
|
||
|
|
JAN: '01', FEB: '02', MAR: '03', APR: '04', MAY: '05', JUN: '06',
|
||
|
|
JUL: '07', AUG: '08', SEP: '09', OCT: '10', NOV: '11', DEC: '12',
|
||
|
|
};
|
||
|
|
|
||
|
|
let match = dateStr.match(/^(\d{1,2})-([A-Z]{3})-(\d{4})$/i);
|
||
|
|
if (match) {
|
||
|
|
return `${match[3]}-${monthMap[match[2].toUpperCase()] || '01'}-${match[1].padStart(2, '0')}`;
|
||
|
|
}
|
||
|
|
|
||
|
|
match = dateStr.match(/^(\d{1,2})[\/.](\d{1,2})[\/.](\d{4})$/);
|
||
|
|
if (match) {
|
||
|
|
return `${match[3]}-${match[2].padStart(2, '0')}-${match[1].padStart(2, '0')}`;
|
||
|
|
}
|
||
|
|
|
||
|
|
return dateStr;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Compare extracted vs expected
|
||
|
|
*/
|
||
|
|
function compareInvoice(extracted: IInvoice, expected: IInvoice): { match: boolean; errors: string[] } {
|
||
|
|
const errors: string[] = [];
|
||
|
|
|
||
|
|
const extNum = extracted.invoice_number?.replace(/\s/g, '').toLowerCase() || '';
|
||
|
|
const expNum = expected.invoice_number?.replace(/\s/g, '').toLowerCase() || '';
|
||
|
|
if (extNum !== expNum) {
|
||
|
|
errors.push(`invoice_number: expected "${expected.invoice_number}", got "${extracted.invoice_number}"`);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (normalizeDate(extracted.invoice_date) !== normalizeDate(expected.invoice_date)) {
|
||
|
|
errors.push(`invoice_date: expected "${expected.invoice_date}", got "${extracted.invoice_date}"`);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (Math.abs(extracted.total_amount - expected.total_amount) > 0.02) {
|
||
|
|
errors.push(`total_amount: expected ${expected.total_amount}, got ${extracted.total_amount}`);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (extracted.currency?.toUpperCase() !== expected.currency?.toUpperCase()) {
|
||
|
|
errors.push(`currency: expected "${expected.currency}", got "${extracted.currency}"`);
|
||
|
|
}
|
||
|
|
|
||
|
|
return { match: errors.length === 0, errors };
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Find test cases
|
||
|
|
*/
|
||
|
|
function findTestCases(): Array<{ name: string; pdfPath: string; jsonPath: string }> {
|
||
|
|
const testDir = path.join(process.cwd(), '.nogit/invoices');
|
||
|
|
if (!fs.existsSync(testDir)) return [];
|
||
|
|
|
||
|
|
const files = fs.readdirSync(testDir);
|
||
|
|
const testCases: Array<{ name: string; pdfPath: string; jsonPath: string }> = [];
|
||
|
|
|
||
|
|
for (const pdf of files.filter((f) => f.endsWith('.pdf'))) {
|
||
|
|
const baseName = pdf.replace('.pdf', '');
|
||
|
|
const jsonFile = `${baseName}.json`;
|
||
|
|
if (files.includes(jsonFile)) {
|
||
|
|
testCases.push({
|
||
|
|
name: baseName,
|
||
|
|
pdfPath: path.join(testDir, pdf),
|
||
|
|
jsonPath: path.join(testDir, jsonFile),
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return testCases.sort((a, b) => a.name.localeCompare(b.name));
|
||
|
|
}
|
||
|
|
|
||
|
|
// Tests
|
||
|
|
|
||
|
|
tap.test('setup: ensure Ministral 3 is running', async () => {
|
||
|
|
console.log('\n[Setup] Checking Ministral 3...\n');
|
||
|
|
const ok = await ensureMinistral3();
|
||
|
|
expect(ok).toBeTrue();
|
||
|
|
console.log('\n[Setup] Ready!\n');
|
||
|
|
});
|
||
|
|
|
||
|
|
const testCases = findTestCases();
|
||
|
|
console.log(`\nFound ${testCases.length} invoice test cases (Ministral 3 Vision Direct)\n`);
|
||
|
|
|
||
|
|
let passedCount = 0;
|
||
|
|
let failedCount = 0;
|
||
|
|
const times: number[] = [];
|
||
|
|
|
||
|
|
for (const testCase of testCases) {
|
||
|
|
tap.test(`should extract invoice: ${testCase.name}`, async () => {
|
||
|
|
const expected: IInvoice = JSON.parse(fs.readFileSync(testCase.jsonPath, 'utf-8'));
|
||
|
|
console.log(`\n=== ${testCase.name} ===`);
|
||
|
|
console.log(`Expected: ${expected.invoice_number} | ${expected.invoice_date} | ${expected.total_amount} ${expected.currency}`);
|
||
|
|
|
||
|
|
const start = Date.now();
|
||
|
|
const images = convertPdfToImages(testCase.pdfPath);
|
||
|
|
console.log(` Pages: ${images.length}`);
|
||
|
|
|
||
|
|
const extracted = await extractWithConsensus(images, testCase.name);
|
||
|
|
const elapsed = Date.now() - start;
|
||
|
|
times.push(elapsed);
|
||
|
|
|
||
|
|
const result = compareInvoice(extracted, expected);
|
||
|
|
|
||
|
|
if (result.match) {
|
||
|
|
passedCount++;
|
||
|
|
console.log(` Result: MATCH (${(elapsed / 1000).toFixed(1)}s)`);
|
||
|
|
} else {
|
||
|
|
failedCount++;
|
||
|
|
console.log(` Result: MISMATCH (${(elapsed / 1000).toFixed(1)}s)`);
|
||
|
|
result.errors.forEach((e) => console.log(` - ${e}`));
|
||
|
|
}
|
||
|
|
|
||
|
|
expect(result.match).toBeTrue();
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
tap.test('summary', async () => {
|
||
|
|
const total = testCases.length;
|
||
|
|
const accuracy = total > 0 ? (passedCount / total) * 100 : 0;
|
||
|
|
const totalTime = times.reduce((a, b) => a + b, 0) / 1000;
|
||
|
|
const avgTime = times.length > 0 ? totalTime / times.length : 0;
|
||
|
|
|
||
|
|
console.log(`\n======================================================`);
|
||
|
|
console.log(` Invoice Extraction Summary (Ministral 3 Vision)`);
|
||
|
|
console.log(`======================================================`);
|
||
|
|
console.log(` Method: Ministral 3 8B Vision (Direct)`);
|
||
|
|
console.log(` Passed: ${passedCount}/${total}`);
|
||
|
|
console.log(` Failed: ${failedCount}/${total}`);
|
||
|
|
console.log(` Accuracy: ${accuracy.toFixed(1)}%`);
|
||
|
|
console.log(`------------------------------------------------------`);
|
||
|
|
console.log(` Total time: ${totalTime.toFixed(1)}s`);
|
||
|
|
console.log(` Avg per inv: ${avgTime.toFixed(1)}s`);
|
||
|
|
console.log(`======================================================\n`);
|
||
|
|
});
|
||
|
|
|
||
|
|
export default tap.start();
|