Panduan AI Application Engineer: Pemrosesan Dokumen Cerdas (IDP) & Output JSON LLM

Mengekstrak tabel item baris, NPWP, dan nilai laboratorium klinis dari PDF hasil pemindaian multi-halaman, faktur miring, dan formulir medis menggunakan ekspresi reguler yang rapuh akan rusak setiap kali vendor mengubah tata letak faktur mereka. Sebaliknya, memasukkan teks OCR mentah ke LLM tanpa kesadaran kotak pembatas spasial menyebabkan halusinasi perataan kolom dan kesalahan aritmatika. Sebagai AI application engineer dan backend engineer, saya merancang pipa Intelligent Document Processing (IDP) deterministik yang menggabungkan visi/OCR sadar tata letak, penegakan skema Pydantic, dan verifikasi matematis terprogram.

1. Ekstraksi Terkunci Skema dengan Validasi Matematis Lintas Kolom

Jangan pernah mempercayai total faktur yang diekstrak oleh LLM atau mesin OCR kecuali jumlah item baris ditambah pajak secara matematis sama dengan total keseluruhan. Kami menerapkan validasi Pydantic ketat dengan aturan @model_validator khusus yang memicu verifikasi ulang terarah saat terjadi perbedaan:

# idp/invoice_extractor.py
from decimal import Decimal
from pydantic import BaseModel, Field, model_validator

class InvoiceLineItem(BaseModel):
    description: str
    quantity: Decimal = Field(gt=Decimal("0"))
    unit_price: Decimal
    line_total: Decimal

class ExtractedInvoice(BaseModel):
    invoice_number: str
    vendor_tax_id: str | None = None
    currency: str = Field(pattern=r"^[A-Z]{3}$")
    line_items: list[InvoiceLineItem]
    subtotal: Decimal
    tax_amount: Decimal
    grand_total: Decimal
    needs_human_review: bool = False
    review_reason: str | None = None

    @model_validator(mode="after")
    def verify_accounting_identities(self) -> "ExtractedInvoice":
        computed_subtotal = sum((item.line_total for item in self.line_items), Decimal("0.00"))
        if abs(computed_subtotal - self.subtotal) > Decimal("0.02"):
            self.needs_human_review = True
            self.review_reason = f"Line items sum ({computed_subtotal}) != subtotal ({self.subtotal})"
        if abs((self.subtotal + self.tax_amount) - self.grand_total) > Decimal("0.02"):
            self.needs_human_review = True
            self.review_reason = f"Subtotal + tax ({self.subtotal + self.tax_amount}) != grand_total ({self.grand_total})"
        return self

2. Provenans Kotak Pembatas untuk Tinjauan Human-in-the-Loop

Setiap kolom yang diekstrak mempertahankan nomor halaman sumber dan poligon pembatas yang dinormalisasi [x0, y0, x1, y1] sehingga peninjau keuangan atau klinis dapat mengklik kolom mana pun yang ditandai di UI web dan melompat langsung ke wilayah yang disorot pada kanvas PDF asli.

3. Referensi AI Application Engineering Terkait


About the Author — Hire Aldian Fazrihady (Cloud Engineer, GCP Professional Cloud Architect, GCP Professional Data Engineer, Backend, AI & Full Stack Engineer)

Aldian Fazrihady is a Top-Rated Senior Cloud Engineer, GCP Professional Cloud Architect (Google Cloud Certified), GCP Professional Data Engineer (Google Cloud Certified), Backend Engineer (Python, Django, FastAPI, Go, PostgreSQL), AI Application Engineer (LLMs, RAG, MCP, Google ADK, LangGraph), and Full Stack Engineer (React, Next.js, TypeScript) with 15+ years of production software engineering experience.

Hire Aldian Fazrihady on Upwork | Book 1-Day Programming Service ($100 USD / Rp 1.000.000) | View Live AI Portfolio | Send a Direct Message

Related Engineering Articles

Engineering Role Pillar Guides & Category Hubs