Skip to content
reaatech

Files · Mistral AI Invoice Extraction for SMB Accounting

40 (0 binary, 376.1 kB total)attempt 2

README.md·3422 B·markdown
markdown
# Mistral AI Invoice Extraction for SMB Accounting
 
Automatically extract vendor, amount, and line items from invoices using Mistral Large, with cost-aware processing and human-in-the-loop review for low-confidence results.
 
## Problem
 
SMB accountants spend hours manually entering invoice data into accounting software. Errors and delays cost money. This solution automates extraction using Mistral AI's LLM, routes ambiguous invoices for human review, and tracks spending to keep costs predictable.
 
## Architecture
 
The pipeline flows: **Upload → Parse → Extract → Score → Handoff/Budget**
 
- **`src/api/webhook.ts`** — Express webhook that accepts PDF, image, or DOCX files via `POST /api/invoices/webhook`
- **`src/lib/document-parser.ts`** — Parses PDFs (via `unpdf`), images (via `sharp` + `@llamaindex/cloud`), and DOCX (via `mammoth`)
- **`src/lib/memory-extractor.ts`** — Uses `@reaatech/agent-memory-extraction` with the Mistral LLM adapter to extract structured invoice data
- **`src/lib/llm-adapter.ts`** — Mistral client (`@mistralai/mistralai`) with retry logic, error classification, and spend tracking
- **`src/lib/confidence.ts`** — Rule-based confidence scoring (vendor presence, total > 0, line item total matching, etc.)
- **`src/lib/handoff.ts`**`@reaatech/agent-handoff-protocol` integration for queueing low-confidence invoices for human review
- **`src/lib/budget.ts`**`@reaatech/agent-budget-spend-tracker` integration with budget cap enforcement
- **`src/lib/pending-store.ts`** — In-memory pending review store (reference implementation — no database)
- **`src/api/invoices.ts`** — Pending review list and confirmation endpoints
 
## Setup
 
```bash
pnpm install
cp .env.example .env
# Edit .env with your API keys
pnpm typecheck
pnpm lint
pnpm test
```
 
## Environment Variables
 
| Variable | Required | Default | Description |
|---|---|---|---|
| `MISTRAL_API_KEY` | Yes | — | Mistral AI API key |
| `LLAMA_CLOUD_API_KEY` | Yes | — | LlamaParse API key (for image invoices) |
| `HANDOFF_RECIPIENT_URL` | Yes | — | URL for human review handoff endpoint |
| `PORT` | No | 3000 | HTTP server port |
| `BUDGET_CAP_MONTHLY_USD` | No | 50 | Monthly spend cap in USD |
| `CONFIDENCE_THRESHOLD` | No | 0.7 | Minimum confidence score for auto-approval |
| `MISTRAL_MODEL` | No | mistral-large-latest | Mistral model identifier |
 
## API Endpoints
 
### `POST /api/invoices/webhook`
Upload an invoice file (PDF, PNG, JPEG, DOCX). Returns `202` with `fileId` and `statusUrl` for polling.
 
### `GET /api/invoices/:fileId`
Poll processing status. Returns `202 { status: "processing" }` while in progress, or the full extraction result when complete.
 
### `GET /api/invoices/pending`
List invoices awaiting human review.
 
### `POST /api/invoices/:fileId/confirm`
Confirm a pending review. Returns `200 { status: "confirmed" }`.
 
### `GET /health`
Health check. Returns `{ status: "ok", uptime: ... }`.
 
## Notes
 
- This is a **reference implementation**: stores are in-memory, no authentication, single-process. Not production-ready without adding a database, auth layer, and proper error recovery.
- `@llamaindex/cloud` is deprecated as of v4.1.0. Production deployments should migrate to `llama-cloud-services`.
- Budget cap tracking is accumulated since process start (not a true calendar month) — documented limitation of the in-memory reference implementation.