Compare commits
20 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 70913c4b3e | |||
| 2ed419f6e4 | |||
| 45cb87e9e7 | |||
| 74a5b37e92 | |||
| 2bdcc74df0 | |||
| 981c031c6e | |||
| 26d2de824f | |||
| 969d21c51a | |||
| da2b827ba3 | |||
| 9bc1f74978 | |||
| cf282b2437 | |||
| 77d57e80bd | |||
| b202e024a4 | |||
| 2210611f70 | |||
| d8bdb18841 | |||
| d384c1d79b | |||
| 6bd672da61 | |||
| 44d6dc3336 | |||
| d1ff95bd94 | |||
| 09770d3177 |
64
changelog.md
64
changelog.md
@@ -1,5 +1,69 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## 2026-01-20 - 1.16.0 - feat(invoices)
|
||||||
|
add line_items extraction and normalization for invoice parsing
|
||||||
|
|
||||||
|
- Introduce ILineItem interface and add line_items array to IInvoice.
|
||||||
|
- Add extractLineItems helper to normalize item fields (position, product, description, quantity, unit_price, total_price).
|
||||||
|
- Include line_items in parsed invoice output and sample JSON in test, defaulting to [] when absent.
|
||||||
|
- Update logging to include extracted line item count.
|
||||||
|
- Clarify test instructions to extract items from invoice tables and skip subtotal/total rows.
|
||||||
|
|
||||||
|
## 2026-01-20 - 1.15.3 - fix(tests(nanonets))
|
||||||
|
allow '/' when normalizing invoice strings in tests
|
||||||
|
|
||||||
|
- Adjust regex in test/test.invoices.nanonets.ts to preserve forward slashes when cleaning invoice values
|
||||||
|
- Changed pattern from [^A-Z0-9-] to [^A-Z0-9\/-] to prevent accidental removal of '/' characters in invoice identifiers
|
||||||
|
|
||||||
|
## 2026-01-20 - 1.15.2 - fix(dev-deps)
|
||||||
|
bump devDependencies @push.rocks/smartagent to ^1.6.2 and @push.rocks/smartai to ^0.13.3
|
||||||
|
|
||||||
|
- Bumped @push.rocks/smartagent from ^1.5.4 to ^1.6.2 in devDependencies
|
||||||
|
- Bumped @push.rocks/smartai from ^0.13.2 to ^0.13.3 in devDependencies
|
||||||
|
- Updated test/test.invoices.nanonets.ts JSON extraction prompt: instruct not to omit special characters in invoice_number and to use the json validate tool
|
||||||
|
- No breaking changes; only dev dependency updates and test prompt adjustments
|
||||||
|
|
||||||
|
## 2026-01-20 - 1.15.1 - fix(tests)
|
||||||
|
enable progress events in invoice tests and bump @push.rocks/smartagent devDependency to ^1.5.4
|
||||||
|
|
||||||
|
- Added an onProgress handler in test/test.invoices.nanonets.ts to log progress events (console.log(event.logMessage)) so tool calls and progress are visible during tests.
|
||||||
|
- Bumped devDependency @push.rocks/smartagent from ^1.5.2 to ^1.5.4 in package.json.
|
||||||
|
|
||||||
|
## 2026-01-20 - 1.15.0 - feat(tests)
|
||||||
|
integrate SmartAi/DualAgentOrchestrator into extraction tests and add JSON self-validation
|
||||||
|
|
||||||
|
- Integrate SmartAi and DualAgentOrchestrator into bankstatement and invoice tests to perform structured extraction with streaming
|
||||||
|
- Register and use JsonValidatorTool to validate outputs (json.validate) and enforce validation before task completion
|
||||||
|
- Add tryExtractJson parsing fallback, improved extraction prompts, retries and clearer parsing/logging
|
||||||
|
- Initialize and teardown SmartAi and orchestrator in test setup/summary, and enable onToken streaming handlers for real-time output
|
||||||
|
- Bump devDependencies: @push.rocks/smartagent to ^1.3.0 and @push.rocks/smartai to ^0.12.0
|
||||||
|
|
||||||
|
## 2026-01-20 - 1.14.3 - fix(repo)
|
||||||
|
no changes detected in the diff; no files modified and no release required
|
||||||
|
|
||||||
|
- Diff contained no changes
|
||||||
|
- No files were added, removed, or modified
|
||||||
|
- No code, dependency, or documentation updates to release
|
||||||
|
|
||||||
|
## 2026-01-19 - 1.14.2 - fix(readme)
|
||||||
|
update README to document Nanonets-OCR2-3B (replaces Nanonets-OCR-s), adjust VRAM and context defaults, expand feature docs, and update examples/test command
|
||||||
|
|
||||||
|
- Renamed Nanonets-OCR-s -> Nanonets-OCR2-3B throughout README and examples
|
||||||
|
- Updated Nanonets VRAM guidance from ~10GB to ~12-16GB and documented 30K context
|
||||||
|
- Changed documented MAX_MODEL_LEN default from 8192 to 30000
|
||||||
|
- Updated example model identifiers (model strings and curl/example snippets) to nanonets/Nanonets-OCR2-3B
|
||||||
|
- Added MiniCPM and Qwen feature bullets (multilingual, multi-image, flowchart support, expanded context notes)
|
||||||
|
- Replaced README test command from ./test-images.sh to pnpm test
|
||||||
|
|
||||||
|
## 2026-01-19 - 1.14.1 - fix(extraction)
|
||||||
|
improve JSON extraction prompts and model options for invoice and bank statement tests
|
||||||
|
|
||||||
|
- Refactor JSON extraction prompts to be sent after the document text and add explicit 'WHERE TO FIND DATA' and 'RULES' sections for clearer extraction guidance
|
||||||
|
- Change chat message flow to: send document, assistant acknowledgement, then the JSON extraction prompt (avoids concatenating large prompts into one message)
|
||||||
|
- Add model options (num_ctx: 32768, temperature: 0) to give larger context windows and deterministic JSON output
|
||||||
|
- Simplify logging to avoid printing full prompt contents; log document and prompt lengths instead
|
||||||
|
- Increase timeouts for large documents to 600000ms (10 minutes) where applicable
|
||||||
|
|
||||||
## 2026-01-19 - 1.14.0 - feat(docker-images)
|
## 2026-01-19 - 1.14.0 - feat(docker-images)
|
||||||
add vLLM-based Nanonets-OCR2-3B image, Qwen3-VL Ollama image and refactor build/docs/tests to use new runtime/layout
|
add vLLM-based Nanonets-OCR2-3B image, Qwen3-VL Ollama image and refactor build/docs/tests to use new runtime/layout
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@host.today/ht-docker-ai",
|
"name": "@host.today/ht-docker-ai",
|
||||||
"version": "1.14.0",
|
"version": "1.16.0",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "Docker images for AI vision-language models including MiniCPM-V 4.5",
|
"description": "Docker images for AI vision-language models including MiniCPM-V 4.5",
|
||||||
@@ -14,7 +14,9 @@
|
|||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@git.zone/tsrun": "^2.0.1",
|
"@git.zone/tsrun": "^2.0.1",
|
||||||
"@git.zone/tstest": "^3.1.5"
|
"@git.zone/tstest": "^3.1.5",
|
||||||
|
"@push.rocks/smartagent": "^1.6.2",
|
||||||
|
"@push.rocks/smartai": "^0.13.3"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
|||||||
1170
pnpm-lock.yaml
generated
1170
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
57
readme.md
57
readme.md
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
Production-ready Docker images for state-of-the-art AI Vision-Language Models. Run powerful multimodal AI locally with GPU acceleration—**no cloud API keys required**.
|
Production-ready Docker images for state-of-the-art AI Vision-Language Models. Run powerful multimodal AI locally with GPU acceleration—**no cloud API keys required**.
|
||||||
|
|
||||||
> 🔥 **Three VLMs, one registry.** From lightweight document OCR to GPT-4o-level vision understanding—pick the right tool for your task.
|
> 🔥 **Three VLMs, one registry.** From high-performance document OCR to GPT-4o-level vision understanding—pick the right tool for your task.
|
||||||
|
|
||||||
## Issue Reporting and Security
|
## Issue Reporting and Security
|
||||||
|
|
||||||
@@ -15,7 +15,7 @@ For reporting bugs, issues, or security vulnerabilities, please visit [community
|
|||||||
| Model | Parameters | Best For | API | Port | VRAM |
|
| Model | Parameters | Best For | API | Port | VRAM |
|
||||||
|-------|-----------|----------|-----|------|------|
|
|-------|-----------|----------|-----|------|------|
|
||||||
| **MiniCPM-V 4.5** | 8B | General vision understanding, multi-image analysis | Ollama-compatible | 11434 | ~9GB |
|
| **MiniCPM-V 4.5** | 8B | General vision understanding, multi-image analysis | Ollama-compatible | 11434 | ~9GB |
|
||||||
| **Nanonets-OCR-s** | ~4B | Document OCR with semantic markdown output | OpenAI-compatible | 8000 | ~10GB |
|
| **Nanonets-OCR2-3B** | ~3B | Document OCR with semantic markdown, LaTeX, flowcharts | OpenAI-compatible | 8000 | ~12-16GB |
|
||||||
| **Qwen3-VL-30B** | 30B (A3B) | Advanced visual agents, code generation from images | Ollama-compatible | 11434 | ~20GB |
|
| **Qwen3-VL-30B** | 30B (A3B) | Advanced visual agents, code generation from images | Ollama-compatible | 11434 | ~20GB |
|
||||||
|
|
||||||
---
|
---
|
||||||
@@ -29,7 +29,7 @@ code.foss.global/host.today/ht-docker-ai:<tag>
|
|||||||
| Tag | Model | Runtime | Port | VRAM |
|
| Tag | Model | Runtime | Port | VRAM |
|
||||||
|-----|-------|---------|------|------|
|
|-----|-------|---------|------|------|
|
||||||
| `minicpm45v` / `latest` | MiniCPM-V 4.5 | Ollama | 11434 | ~9GB |
|
| `minicpm45v` / `latest` | MiniCPM-V 4.5 | Ollama | 11434 | ~9GB |
|
||||||
| `nanonets-ocr` | Nanonets-OCR-s | vLLM | 8000 | ~10GB |
|
| `nanonets-ocr` | Nanonets-OCR2-3B | vLLM | 8000 | ~12-16GB |
|
||||||
| `qwen3vl` | Qwen3-VL-30B-A3B | Ollama | 11434 | ~20GB |
|
| `qwen3vl` | Qwen3-VL-30B-A3B | Ollama | 11434 | ~20GB |
|
||||||
|
|
||||||
---
|
---
|
||||||
@@ -38,6 +38,13 @@ code.foss.global/host.today/ht-docker-ai:<tag>
|
|||||||
|
|
||||||
A GPT-4o level multimodal LLM from OpenBMB—handles image understanding, OCR, multi-image analysis, and visual reasoning across **30+ languages**.
|
A GPT-4o level multimodal LLM from OpenBMB—handles image understanding, OCR, multi-image analysis, and visual reasoning across **30+ languages**.
|
||||||
|
|
||||||
|
### ✨ Key Features
|
||||||
|
|
||||||
|
- 🌍 **Multilingual:** 30+ languages supported
|
||||||
|
- 🖼️ **Multi-image:** Analyze multiple images in one request
|
||||||
|
- 📊 **Versatile:** Charts, documents, photos, diagrams
|
||||||
|
- ⚡ **Efficient:** Runs on consumer GPUs (9GB VRAM)
|
||||||
|
|
||||||
### Quick Start
|
### Quick Start
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@@ -83,21 +90,22 @@ curl http://localhost:11434/api/chat -d '{
|
|||||||
|
|
||||||
| Mode | VRAM Required |
|
| Mode | VRAM Required |
|
||||||
|------|---------------|
|
|------|---------------|
|
||||||
| int4 quantized | 9GB |
|
| int4 quantized | ~9GB |
|
||||||
| Full precision (bf16) | 18GB |
|
| Full precision (bf16) | ~18GB |
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 🔍 Nanonets-OCR-s
|
## 🔍 Nanonets-OCR2-3B
|
||||||
|
|
||||||
A **Qwen2.5-VL-3B** model fine-tuned specifically for document OCR. Outputs structured markdown with semantic HTML tags—perfect for preserving document structure.
|
The **latest Nanonets document OCR model** (October 2025 release)—based on Qwen2.5-VL-3B, fine-tuned specifically for document extraction with significant improvements over the original OCR-s.
|
||||||
|
|
||||||
### Key Features
|
### ✨ Key Features
|
||||||
|
|
||||||
- 📝 **Semantic output:** Tables → HTML, equations → LaTeX, watermarks/page numbers → tagged
|
- 📝 **Semantic output:** Tables → HTML, equations → LaTeX, flowcharts → structured markup
|
||||||
- 🌍 **Multilingual:** Inherits Qwen's broad language support
|
- 🌍 **Multilingual:** Inherits Qwen's broad language support
|
||||||
- ⚡ **Efficient:** ~10GB VRAM, runs great on consumer GPUs
|
- 📄 **30K context:** Handle large, multi-page documents
|
||||||
- 🔌 **OpenAI-compatible:** Drop-in replacement for existing pipelines
|
- 🔌 **OpenAI-compatible:** Drop-in replacement for existing pipelines
|
||||||
|
- 🎯 **Improved accuracy:** Better semantic tagging and LaTeX equation extraction vs. OCR-s
|
||||||
|
|
||||||
### Quick Start
|
### Quick Start
|
||||||
|
|
||||||
@@ -116,7 +124,7 @@ docker run -d \
|
|||||||
curl http://localhost:8000/v1/chat/completions \
|
curl http://localhost:8000/v1/chat/completions \
|
||||||
-H "Content-Type: application/json" \
|
-H "Content-Type: application/json" \
|
||||||
-d '{
|
-d '{
|
||||||
"model": "nanonets/Nanonets-OCR-s",
|
"model": "nanonets/Nanonets-OCR2-3B",
|
||||||
"messages": [{
|
"messages": [{
|
||||||
"role": "user",
|
"role": "user",
|
||||||
"content": [
|
"content": [
|
||||||
@@ -131,7 +139,7 @@ curl http://localhost:8000/v1/chat/completions \
|
|||||||
|
|
||||||
### Output Format
|
### Output Format
|
||||||
|
|
||||||
Nanonets-OCR-s returns markdown with semantic tags:
|
Nanonets-OCR2-3B returns markdown with semantic tags:
|
||||||
|
|
||||||
| Element | Output Format |
|
| Element | Output Format |
|
||||||
|---------|---------------|
|
|---------|---------------|
|
||||||
@@ -140,13 +148,14 @@ Nanonets-OCR-s returns markdown with semantic tags:
|
|||||||
| Images | `<img>description</img>` |
|
| Images | `<img>description</img>` |
|
||||||
| Watermarks | `<watermark>OFFICIAL COPY</watermark>` |
|
| Watermarks | `<watermark>OFFICIAL COPY</watermark>` |
|
||||||
| Page numbers | `<page_number>14</page_number>` |
|
| Page numbers | `<page_number>14</page_number>` |
|
||||||
|
| Flowcharts | Structured markup |
|
||||||
|
|
||||||
### Performance
|
### Hardware Requirements
|
||||||
|
|
||||||
| Metric | Value |
|
| Config | VRAM |
|
||||||
|--------|-------|
|
|--------|------|
|
||||||
| Speed | 3–8 seconds per page |
|
| 30K context (default) | ~12-16GB |
|
||||||
| VRAM | ~10GB |
|
| Speed | ~3-8 seconds per page |
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -154,7 +163,7 @@ Nanonets-OCR-s returns markdown with semantic tags:
|
|||||||
|
|
||||||
The **most powerful** Qwen vision model—30B parameters with 3B active (MoE architecture). Handles complex visual reasoning, code generation from screenshots, and visual agent capabilities.
|
The **most powerful** Qwen vision model—30B parameters with 3B active (MoE architecture). Handles complex visual reasoning, code generation from screenshots, and visual agent capabilities.
|
||||||
|
|
||||||
### Key Features
|
### ✨ Key Features
|
||||||
|
|
||||||
- 🚀 **256K context** (expandable to 1M tokens!)
|
- 🚀 **256K context** (expandable to 1M tokens!)
|
||||||
- 🤖 **Visual agent capabilities** — can plan and execute multi-step tasks
|
- 🤖 **Visual agent capabilities** — can plan and execute multi-step tasks
|
||||||
@@ -204,7 +213,6 @@ curl http://localhost:11434/api/chat -d '{
|
|||||||
Run multiple VLMs together for maximum flexibility:
|
Run multiple VLMs together for maximum flexibility:
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
version: '3.8'
|
|
||||||
services:
|
services:
|
||||||
# General vision tasks
|
# General vision tasks
|
||||||
minicpm:
|
minicpm:
|
||||||
@@ -259,10 +267,10 @@ volumes:
|
|||||||
|
|
||||||
| Variable | Default | Description |
|
| Variable | Default | Description |
|
||||||
|----------|---------|-------------|
|
|----------|---------|-------------|
|
||||||
| `MODEL_NAME` | `nanonets/Nanonets-OCR-s` | HuggingFace model ID |
|
| `MODEL_NAME` | `nanonets/Nanonets-OCR2-3B` | HuggingFace model ID |
|
||||||
| `HOST` | `0.0.0.0` | API bind address |
|
| `HOST` | `0.0.0.0` | API bind address |
|
||||||
| `PORT` | `8000` | API port |
|
| `PORT` | `8000` | API port |
|
||||||
| `MAX_MODEL_LEN` | `8192` | Maximum sequence length |
|
| `MAX_MODEL_LEN` | `30000` | Maximum sequence length |
|
||||||
| `GPU_MEMORY_UTILIZATION` | `0.9` | GPU memory usage (0-1) |
|
| `GPU_MEMORY_UTILIZATION` | `0.9` | GPU memory usage (0-1) |
|
||||||
|
|
||||||
---
|
---
|
||||||
@@ -283,7 +291,7 @@ This dual-VLM approach catches extraction errors that single models miss.
|
|||||||
### Why Multi-Model Works
|
### Why Multi-Model Works
|
||||||
|
|
||||||
- **Different architectures:** Independent models cross-validate each other
|
- **Different architectures:** Independent models cross-validate each other
|
||||||
- **Specialized strengths:** Nanonets-OCR-s excels at document structure; MiniCPM-V handles general vision
|
- **Specialized strengths:** Nanonets-OCR2-3B excels at document structure; MiniCPM-V handles general vision
|
||||||
- **Native processing:** All VLMs see original images—no intermediate structure loss
|
- **Native processing:** All VLMs see original images—no intermediate structure loss
|
||||||
|
|
||||||
### Model Selection Guide
|
### Model Selection Guide
|
||||||
@@ -291,10 +299,11 @@ This dual-VLM approach catches extraction errors that single models miss.
|
|||||||
| Task | Recommended Model |
|
| Task | Recommended Model |
|
||||||
|------|-------------------|
|
|------|-------------------|
|
||||||
| General image understanding | MiniCPM-V 4.5 |
|
| General image understanding | MiniCPM-V 4.5 |
|
||||||
| Document OCR with structure preservation | Nanonets-OCR-s |
|
| Document OCR with structure preservation | Nanonets-OCR2-3B |
|
||||||
| Complex visual reasoning / code generation | Qwen3-VL-30B |
|
| Complex visual reasoning / code generation | Qwen3-VL-30B |
|
||||||
| Multi-image analysis | MiniCPM-V 4.5 |
|
| Multi-image analysis | MiniCPM-V 4.5 |
|
||||||
| Visual agent tasks | Qwen3-VL-30B |
|
| Visual agent tasks | Qwen3-VL-30B |
|
||||||
|
| Large documents (30K+ tokens) | Nanonets-OCR2-3B |
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -309,7 +318,7 @@ cd ht-docker-ai
|
|||||||
./build-images.sh
|
./build-images.sh
|
||||||
|
|
||||||
# Run tests
|
# Run tests
|
||||||
./test-images.sh
|
pnpm test
|
||||||
```
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
/**
|
/**
|
||||||
* Bank statement extraction using MiniCPM-V (visual extraction)
|
* Bank statement extraction using MiniCPM-V via smartagent DualAgentOrchestrator
|
||||||
*
|
*
|
||||||
* JSON per-page approach:
|
* Uses vision-capable orchestrator with JsonValidatorTool for self-validation:
|
||||||
* 1. Ask for structured JSON of all transactions per page
|
* 1. Process each page with the orchestrator
|
||||||
* 2. Consensus: extract twice, compare, retry if mismatch
|
* 2. Driver extracts transactions and validates JSON before completing
|
||||||
|
* 3. Streaming output during extraction
|
||||||
*/
|
*/
|
||||||
import { tap, expect } from '@git.zone/tstest/tapbundle';
|
import { tap, expect } from '@git.zone/tstest/tapbundle';
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
@@ -11,6 +12,8 @@ import * as path from 'path';
|
|||||||
import { execSync } from 'child_process';
|
import { execSync } from 'child_process';
|
||||||
import * as os from 'os';
|
import * as os from 'os';
|
||||||
import { ensureMiniCpm } from './helpers/docker.js';
|
import { ensureMiniCpm } from './helpers/docker.js';
|
||||||
|
import { SmartAi } from '@push.rocks/smartai';
|
||||||
|
import { DualAgentOrchestrator, JsonValidatorTool } from '@push.rocks/smartagent';
|
||||||
|
|
||||||
const OLLAMA_URL = 'http://localhost:11434';
|
const OLLAMA_URL = 'http://localhost:11434';
|
||||||
const MODEL = 'openbmb/minicpm-v4.5:q8_0';
|
const MODEL = 'openbmb/minicpm-v4.5:q8_0';
|
||||||
@@ -21,21 +24,9 @@ interface ITransaction {
|
|||||||
amount: number;
|
amount: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
const JSON_PROMPT = `Extract ALL transactions from this bank statement page as a JSON array.
|
// SmartAi instance and orchestrator (initialized in setup)
|
||||||
|
let smartAi: SmartAi;
|
||||||
IMPORTANT RULES:
|
let orchestrator: DualAgentOrchestrator;
|
||||||
1. Each transaction has: date, description/counterparty, and an amount
|
|
||||||
2. Amount is NEGATIVE for money going OUT (debits, payments, withdrawals)
|
|
||||||
3. Amount is POSITIVE for money coming IN (credits, deposits, refunds)
|
|
||||||
4. Date format: YYYY-MM-DD
|
|
||||||
5. Do NOT include: opening balance, closing balance, subtotals, headers, or summary rows
|
|
||||||
6. Only include actual transactions with a specific date and amount
|
|
||||||
|
|
||||||
Return ONLY this JSON format, no explanation:
|
|
||||||
[
|
|
||||||
{"date": "2021-06-01", "counterparty": "COMPANY NAME", "amount": -25.99},
|
|
||||||
{"date": "2021-06-02", "counterparty": "DEPOSIT FROM", "amount": 100.00}
|
|
||||||
]`;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert PDF to PNG images using ImageMagick
|
* Convert PDF to PNG images using ImageMagick
|
||||||
@@ -65,206 +56,31 @@ function convertPdfToImages(pdfPath: string): string[] {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
const EXTRACTION_PROMPT = `Extract ALL transactions from this bank statement page as a JSON array.
|
||||||
* Query for JSON extraction
|
|
||||||
*/
|
|
||||||
async function queryJson(image: string, queryId: string): Promise<string> {
|
|
||||||
console.log(` [${queryId}] Sending request to ${MODEL}...`);
|
|
||||||
const startTime = Date.now();
|
|
||||||
|
|
||||||
const response = await fetch(`${OLLAMA_URL}/api/chat`, {
|
IMPORTANT RULES:
|
||||||
method: 'POST',
|
1. Each transaction has: date, counterparty (description), and an amount
|
||||||
headers: { 'Content-Type': 'application/json' },
|
2. Amount is NEGATIVE for money going OUT (debits, payments, withdrawals)
|
||||||
body: JSON.stringify({
|
3. Amount is POSITIVE for money coming IN (credits, deposits, refunds)
|
||||||
model: MODEL,
|
4. Date format: YYYY-MM-DD
|
||||||
messages: [{
|
5. Do NOT include: opening balance, closing balance, subtotals, headers, or summary rows
|
||||||
role: 'user',
|
6. Only include actual transactions with a specific date and amount
|
||||||
content: JSON_PROMPT,
|
|
||||||
images: [image],
|
|
||||||
}],
|
|
||||||
stream: false,
|
|
||||||
options: {
|
|
||||||
num_predict: 4000,
|
|
||||||
temperature: 0.1,
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
const elapsed = ((Date.now() - startTime) / 1000).toFixed(1);
|
Before completing, validate your JSON output:
|
||||||
|
|
||||||
if (!response.ok) {
|
<tool_call>
|
||||||
console.log(` [${queryId}] ERROR: ${response.status} (${elapsed}s)`);
|
<tool>json</tool>
|
||||||
throw new Error(`Ollama API error: ${response.status}`);
|
<action>validate</action>
|
||||||
}
|
<params>{"jsonString": "YOUR_JSON_ARRAY_HERE"}</params>
|
||||||
|
</tool_call>
|
||||||
|
|
||||||
const data = await response.json();
|
Output format (must be a valid JSON array):
|
||||||
const content = (data.message?.content || '').trim();
|
[
|
||||||
console.log(` [${queryId}] Response received (${elapsed}s, ${content.length} chars)`);
|
{"date": "2021-06-01", "counterparty": "COMPANY NAME", "amount": -25.99},
|
||||||
return content;
|
{"date": "2021-06-02", "counterparty": "DEPOSIT FROM", "amount": 100.00}
|
||||||
}
|
]
|
||||||
|
|
||||||
/**
|
Only complete after validation passes. Output the final JSON array in <task_complete> tags.`;
|
||||||
* Sanitize JSON string - fix common issues from vision model output
|
|
||||||
*/
|
|
||||||
function sanitizeJson(jsonStr: string): string {
|
|
||||||
let s = jsonStr;
|
|
||||||
|
|
||||||
// Fix +number (e.g., +93.80 -> 93.80) - JSON doesn't allow + prefix
|
|
||||||
// Handle various whitespace patterns
|
|
||||||
s = s.replace(/"amount"\s*:\s*\+/g, '"amount": ');
|
|
||||||
s = s.replace(/:\s*\+(\d)/g, ': $1');
|
|
||||||
|
|
||||||
// Fix European number format with thousands separator (e.g., 1.000.00 -> 1000.00)
|
|
||||||
// Pattern: "amount": X.XXX.XX where X.XXX is thousands and .XX is decimal
|
|
||||||
s = s.replace(/"amount"\s*:\s*(-?)(\d{1,3})\.(\d{3})\.(\d{2})\b/g, '"amount": $1$2$3.$4');
|
|
||||||
// Also handle larger numbers like 10.000.00
|
|
||||||
s = s.replace(/"amount"\s*:\s*(-?)(\d{1,3})\.(\d{3})\.(\d{3})\.(\d{2})\b/g, '"amount": $1$2$3$4.$5');
|
|
||||||
|
|
||||||
// Fix trailing commas before ] or }
|
|
||||||
s = s.replace(/,\s*([}\]])/g, '$1');
|
|
||||||
|
|
||||||
// Fix unescaped newlines inside strings (replace with space)
|
|
||||||
s = s.replace(/"([^"\\]*)\n([^"]*)"/g, '"$1 $2"');
|
|
||||||
|
|
||||||
// Fix unescaped tabs inside strings
|
|
||||||
s = s.replace(/"([^"\\]*)\t([^"]*)"/g, '"$1 $2"');
|
|
||||||
|
|
||||||
// Fix unescaped backslashes (but not already escaped ones)
|
|
||||||
s = s.replace(/\\(?!["\\/bfnrtu])/g, '\\\\');
|
|
||||||
|
|
||||||
// Fix common issues with counterparty names containing special chars
|
|
||||||
s = s.replace(/"counterparty":\s*"([^"]*)'([^"]*)"/g, '"counterparty": "$1$2"');
|
|
||||||
|
|
||||||
// Remove control characters except newlines (which we handle above)
|
|
||||||
s = s.replace(/[\x00-\x08\x0B\x0C\x0E-\x1F]/g, ' ');
|
|
||||||
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Parse JSON response into transactions
|
|
||||||
*/
|
|
||||||
function parseJsonResponse(response: string, queryId: string): ITransaction[] {
|
|
||||||
console.log(` [${queryId}] Parsing response...`);
|
|
||||||
|
|
||||||
// Try to find JSON in markdown code block
|
|
||||||
const codeBlockMatch = response.match(/```(?:json)?\s*([\s\S]*?)```/);
|
|
||||||
let jsonStr = codeBlockMatch ? codeBlockMatch[1].trim() : response.trim();
|
|
||||||
|
|
||||||
if (codeBlockMatch) {
|
|
||||||
console.log(` [${queryId}] Found JSON in code block`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sanitize JSON (fix +number issue)
|
|
||||||
jsonStr = sanitizeJson(jsonStr);
|
|
||||||
|
|
||||||
try {
|
|
||||||
const parsed = JSON.parse(jsonStr);
|
|
||||||
if (Array.isArray(parsed)) {
|
|
||||||
const txs = parsed.map(tx => ({
|
|
||||||
date: String(tx.date || ''),
|
|
||||||
counterparty: String(tx.counterparty || tx.description || ''),
|
|
||||||
amount: parseAmount(tx.amount),
|
|
||||||
}));
|
|
||||||
console.log(` [${queryId}] Parsed ${txs.length} transactions (direct)`);
|
|
||||||
return txs;
|
|
||||||
}
|
|
||||||
console.log(` [${queryId}] Parsed JSON is not an array`);
|
|
||||||
} catch (e) {
|
|
||||||
const errMsg = (e as Error).message;
|
|
||||||
console.log(` [${queryId}] Direct parse failed: ${errMsg}`);
|
|
||||||
|
|
||||||
// Log problematic section with context
|
|
||||||
const posMatch = errMsg.match(/position (\d+)/);
|
|
||||||
if (posMatch) {
|
|
||||||
const pos = parseInt(posMatch[1]);
|
|
||||||
const start = Math.max(0, pos - 40);
|
|
||||||
const end = Math.min(jsonStr.length, pos + 40);
|
|
||||||
const context = jsonStr.substring(start, end);
|
|
||||||
const marker = ' '.repeat(pos - start) + '^';
|
|
||||||
console.log(` [${queryId}] Context around error position ${pos}:`);
|
|
||||||
console.log(` [${queryId}] ...${context}...`);
|
|
||||||
console.log(` [${queryId}] ${marker}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Try to find JSON array pattern
|
|
||||||
const arrayMatch = jsonStr.match(/\[[\s\S]*\]/);
|
|
||||||
if (arrayMatch) {
|
|
||||||
console.log(` [${queryId}] Found array pattern, trying to parse...`);
|
|
||||||
const sanitizedArray = sanitizeJson(arrayMatch[0]);
|
|
||||||
try {
|
|
||||||
const parsed = JSON.parse(sanitizedArray);
|
|
||||||
if (Array.isArray(parsed)) {
|
|
||||||
const txs = parsed.map(tx => ({
|
|
||||||
date: String(tx.date || ''),
|
|
||||||
counterparty: String(tx.counterparty || tx.description || ''),
|
|
||||||
amount: parseAmount(tx.amount),
|
|
||||||
}));
|
|
||||||
console.log(` [${queryId}] Parsed ${txs.length} transactions (array match)`);
|
|
||||||
return txs;
|
|
||||||
}
|
|
||||||
} catch (e2) {
|
|
||||||
const errMsg2 = (e2 as Error).message;
|
|
||||||
console.log(` [${queryId}] Array parse failed: ${errMsg2}`);
|
|
||||||
const posMatch2 = errMsg2.match(/position (\d+)/);
|
|
||||||
if (posMatch2) {
|
|
||||||
const pos2 = parseInt(posMatch2[1]);
|
|
||||||
console.log(` [${queryId}] Context around error: ...${sanitizedArray.substring(Math.max(0, pos2 - 30), pos2 + 30)}...`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Try to extract individual objects from the malformed array
|
|
||||||
console.log(` [${queryId}] Attempting object-by-object extraction...`);
|
|
||||||
const extracted = extractTransactionsFromMalformedJson(sanitizedArray, queryId);
|
|
||||||
if (extracted.length > 0) {
|
|
||||||
console.log(` [${queryId}] Recovered ${extracted.length} transactions via object extraction`);
|
|
||||||
return extracted;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
console.log(` [${queryId}] No array pattern found in response`);
|
|
||||||
console.log(` [${queryId}] Raw response preview: ${response.substring(0, 200)}...`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log(` [${queryId}] PARSE FAILED - returning empty array`);
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Extract transactions from malformed JSON by parsing objects individually
|
|
||||||
*/
|
|
||||||
function extractTransactionsFromMalformedJson(jsonStr: string, queryId: string): ITransaction[] {
|
|
||||||
const transactions: ITransaction[] = [];
|
|
||||||
|
|
||||||
// Match individual transaction objects
|
|
||||||
const objectPattern = /\{\s*"date"\s*:\s*"([^"]+)"\s*,\s*"counterparty"\s*:\s*"([^"]+)"\s*,\s*"amount"\s*:\s*([+-]?\d+\.?\d*)\s*\}/g;
|
|
||||||
let match;
|
|
||||||
|
|
||||||
while ((match = objectPattern.exec(jsonStr)) !== null) {
|
|
||||||
transactions.push({
|
|
||||||
date: match[1],
|
|
||||||
counterparty: match[2],
|
|
||||||
amount: parseFloat(match[3]),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Also try with different field orders (amount before counterparty, etc.)
|
|
||||||
if (transactions.length === 0) {
|
|
||||||
const altPattern = /\{\s*"date"\s*:\s*"([^"]+)"[^}]*"amount"\s*:\s*([+-]?\d+\.?\d*)[^}]*\}/g;
|
|
||||||
while ((match = altPattern.exec(jsonStr)) !== null) {
|
|
||||||
// Try to extract counterparty from the match
|
|
||||||
const counterpartyMatch = match[0].match(/"counterparty"\s*:\s*"([^"]+)"/);
|
|
||||||
const descMatch = match[0].match(/"description"\s*:\s*"([^"]+)"/);
|
|
||||||
transactions.push({
|
|
||||||
date: match[1],
|
|
||||||
counterparty: counterpartyMatch?.[1] || descMatch?.[1] || 'UNKNOWN',
|
|
||||||
amount: parseFloat(match[2]),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return transactions;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse amount from various formats
|
* Parse amount from various formats
|
||||||
@@ -284,102 +100,101 @@ function parseAmount(value: unknown): number {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compare two transaction arrays for consensus
|
* Extract JSON from response (handles markdown code blocks and task_complete tags)
|
||||||
*/
|
*/
|
||||||
function transactionArraysMatch(a: ITransaction[], b: ITransaction[]): boolean {
|
function extractJsonFromResponse(response: string): unknown[] | null {
|
||||||
if (a.length !== b.length) return false;
|
// Try to find JSON in task_complete tags
|
||||||
|
const completeMatch = response.match(/<task_complete>([\s\S]*?)<\/task_complete>/);
|
||||||
for (let i = 0; i < a.length; i++) {
|
if (completeMatch) {
|
||||||
const dateMatch = a[i].date === b[i].date;
|
const content = completeMatch[1].trim();
|
||||||
const amountMatch = Math.abs(a[i].amount - b[i].amount) < 0.01;
|
// Try to find JSON in the content
|
||||||
if (!dateMatch || !amountMatch) return false;
|
const codeBlockMatch = content.match(/```(?:json)?\s*([\s\S]*?)```/);
|
||||||
|
const jsonStr = codeBlockMatch ? codeBlockMatch[1].trim() : content;
|
||||||
|
try {
|
||||||
|
const parsed = JSON.parse(jsonStr);
|
||||||
|
if (Array.isArray(parsed)) return parsed;
|
||||||
|
} catch {
|
||||||
|
// Try to find JSON array pattern
|
||||||
|
const jsonMatch = jsonStr.match(/\[[\s\S]*\]/);
|
||||||
|
if (jsonMatch) {
|
||||||
|
try {
|
||||||
|
const parsed = JSON.parse(jsonMatch[0]);
|
||||||
|
if (Array.isArray(parsed)) return parsed;
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
// Try to find JSON in markdown code block
|
||||||
|
const codeBlockMatch = response.match(/```(?:json)?\s*([\s\S]*?)```/);
|
||||||
|
const jsonStr = codeBlockMatch ? codeBlockMatch[1].trim() : response.trim();
|
||||||
|
|
||||||
|
try {
|
||||||
|
const parsed = JSON.parse(jsonStr);
|
||||||
|
if (Array.isArray(parsed)) return parsed;
|
||||||
|
} catch {
|
||||||
|
// Try to find JSON array pattern
|
||||||
|
const jsonMatch = jsonStr.match(/\[[\s\S]*\]/);
|
||||||
|
if (jsonMatch) {
|
||||||
|
try {
|
||||||
|
const parsed = JSON.parse(jsonMatch[0]);
|
||||||
|
if (Array.isArray(parsed)) return parsed;
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compare two transaction arrays and log differences
|
* Parse JSON response into transactions
|
||||||
*/
|
*/
|
||||||
function compareAndLogDifferences(txs1: ITransaction[], txs2: ITransaction[], pageNum: number): void {
|
function parseJsonToTransactions(response: string): ITransaction[] {
|
||||||
if (txs1.length !== txs2.length) {
|
const parsed = extractJsonFromResponse(response);
|
||||||
console.log(` [Page ${pageNum}] Length mismatch: Q1=${txs1.length}, Q2=${txs2.length}`);
|
if (!parsed || !Array.isArray(parsed)) return [];
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (let i = 0; i < txs1.length; i++) {
|
return parsed.map((tx: any) => ({
|
||||||
const dateMatch = txs1[i].date === txs2[i].date;
|
date: String(tx.date || ''),
|
||||||
const amountMatch = Math.abs(txs1[i].amount - txs2[i].amount) < 0.01;
|
counterparty: String(tx.counterparty || tx.description || ''),
|
||||||
|
amount: parseAmount(tx.amount),
|
||||||
if (!dateMatch || !amountMatch) {
|
}));
|
||||||
console.log(` [Page ${pageNum}] Tx ${i + 1} differs:`);
|
|
||||||
console.log(` Q1: ${txs1[i].date} | ${txs1[i].amount}`);
|
|
||||||
console.log(` Q2: ${txs2[i].date} | ${txs2[i].amount}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extract transactions from a single page with consensus
|
* Extract transactions from a single page using smartagent orchestrator
|
||||||
*/
|
*/
|
||||||
async function extractTransactionsFromPage(image: string, pageNum: number): Promise<ITransaction[]> {
|
async function extractTransactionsFromPage(image: string, pageNum: number): Promise<ITransaction[]> {
|
||||||
const MAX_ATTEMPTS = 5;
|
|
||||||
console.log(`\n ======== Page ${pageNum} ========`);
|
console.log(`\n ======== Page ${pageNum} ========`);
|
||||||
console.log(` [Page ${pageNum}] Starting JSON extraction...`);
|
|
||||||
|
|
||||||
for (let attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) {
|
const startTime = Date.now();
|
||||||
console.log(`\n [Page ${pageNum}] --- Attempt ${attempt}/${MAX_ATTEMPTS} ---`);
|
|
||||||
|
|
||||||
// Extract twice in parallel
|
const result = await orchestrator.run(EXTRACTION_PROMPT, { images: [image] });
|
||||||
const q1Id = `P${pageNum}A${attempt}Q1`;
|
|
||||||
const q2Id = `P${pageNum}A${attempt}Q2`;
|
|
||||||
|
|
||||||
const [response1, response2] = await Promise.all([
|
const elapsed = ((Date.now() - startTime) / 1000).toFixed(1);
|
||||||
queryJson(image, q1Id),
|
console.log(`\n [Page ${pageNum}] Completed in ${elapsed}s (${result.iterations} iterations, status: ${result.status})`);
|
||||||
queryJson(image, q2Id),
|
|
||||||
]);
|
|
||||||
|
|
||||||
const txs1 = parseJsonResponse(response1, q1Id);
|
const transactions = parseJsonToTransactions(result.result);
|
||||||
const txs2 = parseJsonResponse(response2, q2Id);
|
|
||||||
|
|
||||||
console.log(` [Page ${pageNum}] Results: Q1=${txs1.length} txs, Q2=${txs2.length} txs`);
|
console.log(` [Page ${pageNum}] Extracted ${transactions.length} transactions:`);
|
||||||
|
for (let i = 0; i < Math.min(transactions.length, 10); i++) {
|
||||||
if (txs1.length > 0 && transactionArraysMatch(txs1, txs2)) {
|
const tx = transactions[i];
|
||||||
console.log(` [Page ${pageNum}] ✓ CONSENSUS REACHED: ${txs1.length} transactions`);
|
|
||||||
console.log(` [Page ${pageNum}] Transactions:`);
|
|
||||||
for (let i = 0; i < txs1.length; i++) {
|
|
||||||
const tx = txs1[i];
|
|
||||||
console.log(` ${(i + 1).toString().padStart(2)}. ${tx.date} | ${tx.counterparty.substring(0, 30).padEnd(30)} | ${tx.amount >= 0 ? '+' : ''}${tx.amount.toFixed(2)}`);
|
console.log(` ${(i + 1).toString().padStart(2)}. ${tx.date} | ${tx.counterparty.substring(0, 30).padEnd(30)} | ${tx.amount >= 0 ? '+' : ''}${tx.amount.toFixed(2)}`);
|
||||||
}
|
}
|
||||||
return txs1;
|
if (transactions.length > 10) {
|
||||||
|
console.log(` ... and ${transactions.length - 10} more transactions`);
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(` [Page ${pageNum}] ✗ NO CONSENSUS`);
|
return transactions;
|
||||||
compareAndLogDifferences(txs1, txs2, pageNum);
|
|
||||||
|
|
||||||
if (attempt < MAX_ATTEMPTS) {
|
|
||||||
console.log(` [Page ${pageNum}] Retrying...`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fallback: use last response
|
|
||||||
console.log(`\n [Page ${pageNum}] === FALLBACK (no consensus after ${MAX_ATTEMPTS} attempts) ===`);
|
|
||||||
const fallbackId = `P${pageNum}FALLBACK`;
|
|
||||||
const fallbackResponse = await queryJson(image, fallbackId);
|
|
||||||
const fallback = parseJsonResponse(fallbackResponse, fallbackId);
|
|
||||||
console.log(` [Page ${pageNum}] ~ FALLBACK RESULT: ${fallback.length} transactions`);
|
|
||||||
for (let i = 0; i < fallback.length; i++) {
|
|
||||||
const tx = fallback[i];
|
|
||||||
console.log(` ${(i + 1).toString().padStart(2)}. ${tx.date} | ${tx.counterparty.substring(0, 30).padEnd(30)} | ${tx.amount >= 0 ? '+' : ''}${tx.amount.toFixed(2)}`);
|
|
||||||
}
|
|
||||||
return fallback;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extract all transactions from bank statement
|
* Extract all transactions from bank statement
|
||||||
*/
|
*/
|
||||||
async function extractTransactions(images: string[]): Promise<ITransaction[]> {
|
async function extractTransactions(images: string[]): Promise<ITransaction[]> {
|
||||||
console.log(` [Vision] Processing ${images.length} page(s) with ${MODEL} (JSON consensus)`);
|
console.log(` [Vision] Processing ${images.length} page(s) with smartagent DualAgentOrchestrator`);
|
||||||
|
|
||||||
const allTransactions: ITransaction[] = [];
|
const allTransactions: ITransaction[] = [];
|
||||||
|
|
||||||
@@ -474,6 +289,80 @@ tap.test('setup: ensure Docker containers are running', async () => {
|
|||||||
console.log('\n[Setup] All containers ready!\n');
|
console.log('\n[Setup] All containers ready!\n');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
tap.test('setup: initialize smartagent orchestrator', async () => {
|
||||||
|
console.log('[Setup] Initializing SmartAi and DualAgentOrchestrator...');
|
||||||
|
|
||||||
|
smartAi = new SmartAi({
|
||||||
|
ollama: {
|
||||||
|
baseUrl: OLLAMA_URL,
|
||||||
|
model: MODEL,
|
||||||
|
defaultOptions: {
|
||||||
|
num_ctx: 32768,
|
||||||
|
num_predict: 4000,
|
||||||
|
temperature: 0.1,
|
||||||
|
},
|
||||||
|
defaultTimeout: 300000, // 5 minutes for vision tasks
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
await smartAi.start();
|
||||||
|
|
||||||
|
orchestrator = new DualAgentOrchestrator({
|
||||||
|
smartAiInstance: smartAi,
|
||||||
|
defaultProvider: 'ollama',
|
||||||
|
guardianPolicyPrompt: `You are a Guardian agent overseeing bank statement extraction tasks.
|
||||||
|
|
||||||
|
APPROVE all tool calls that:
|
||||||
|
- Use the json.validate action to verify JSON output
|
||||||
|
- Are reasonable attempts to complete the extraction task
|
||||||
|
|
||||||
|
REJECT tool calls that:
|
||||||
|
- Attempt to access external resources
|
||||||
|
- Try to execute arbitrary code
|
||||||
|
- Are clearly unrelated to bank statement extraction`,
|
||||||
|
driverSystemMessage: `You are an AI assistant that extracts bank transactions from statement images.
|
||||||
|
|
||||||
|
Your task is to analyze bank statement images and extract transaction data.
|
||||||
|
You have access to a json.validate tool to verify your JSON output.
|
||||||
|
|
||||||
|
IMPORTANT: Always validate your JSON before completing the task.
|
||||||
|
|
||||||
|
## Tool Usage Format
|
||||||
|
When you need to validate JSON, output:
|
||||||
|
|
||||||
|
<tool_call>
|
||||||
|
<tool>json</tool>
|
||||||
|
<action>validate</action>
|
||||||
|
<params>{"jsonString": "YOUR_JSON_ARRAY"}</params>
|
||||||
|
</tool_call>
|
||||||
|
|
||||||
|
## Completion Format
|
||||||
|
After validation passes, complete the task:
|
||||||
|
|
||||||
|
<task_complete>
|
||||||
|
[{"date": "YYYY-MM-DD", "counterparty": "...", "amount": -123.45}, ...]
|
||||||
|
</task_complete>`,
|
||||||
|
maxIterations: 5,
|
||||||
|
maxConsecutiveRejections: 3,
|
||||||
|
onToken: (token, source) => {
|
||||||
|
if (source === 'driver') {
|
||||||
|
process.stdout.write(token);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onProgress: (event) => {
|
||||||
|
if (event.logLevel === 'error') {
|
||||||
|
console.error(event.logMessage);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Register the JsonValidatorTool
|
||||||
|
orchestrator.registerTool(new JsonValidatorTool());
|
||||||
|
|
||||||
|
await orchestrator.start();
|
||||||
|
console.log('[Setup] Orchestrator initialized!\n');
|
||||||
|
});
|
||||||
|
|
||||||
tap.test('should have MiniCPM-V model loaded', async () => {
|
tap.test('should have MiniCPM-V model loaded', async () => {
|
||||||
const response = await fetch(`${OLLAMA_URL}/api/tags`);
|
const response = await fetch(`${OLLAMA_URL}/api/tags`);
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
@@ -482,7 +371,7 @@ tap.test('should have MiniCPM-V model loaded', async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const testCases = findTestCases();
|
const testCases = findTestCases();
|
||||||
console.log(`\nFound ${testCases.length} bank statement test cases (MiniCPM-V)\n`);
|
console.log(`\nFound ${testCases.length} bank statement test cases (smartagent + MiniCPM-V)\n`);
|
||||||
|
|
||||||
let passedCount = 0;
|
let passedCount = 0;
|
||||||
let failedCount = 0;
|
let failedCount = 0;
|
||||||
@@ -514,7 +403,10 @@ for (const testCase of testCases) {
|
|||||||
// Log counterparty variations (names that differ but date/amount matched)
|
// Log counterparty variations (names that differ but date/amount matched)
|
||||||
if (result.variations.length > 0) {
|
if (result.variations.length > 0) {
|
||||||
console.log(` Counterparty variations (${result.variations.length}):`);
|
console.log(` Counterparty variations (${result.variations.length}):`);
|
||||||
result.variations.forEach((v) => console.log(` ${v}`));
|
result.variations.slice(0, 5).forEach((v) => console.log(` ${v}`));
|
||||||
|
if (result.variations.length > 5) {
|
||||||
|
console.log(` ... and ${result.variations.length - 5} more variations`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
expect(result.matches).toEqual(result.total);
|
expect(result.matches).toEqual(result.total);
|
||||||
@@ -522,12 +414,20 @@ for (const testCase of testCases) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tap.test('cleanup: stop orchestrator', async () => {
|
||||||
|
if (orchestrator) {
|
||||||
|
await orchestrator.stop();
|
||||||
|
}
|
||||||
|
console.log('[Cleanup] Orchestrator stopped');
|
||||||
|
});
|
||||||
|
|
||||||
tap.test('summary', async () => {
|
tap.test('summary', async () => {
|
||||||
const total = testCases.length;
|
const total = testCases.length;
|
||||||
console.log(`\n======================================================`);
|
console.log(`\n======================================================`);
|
||||||
console.log(` Bank Statement Summary (${MODEL})`);
|
console.log(` Bank Statement Summary`);
|
||||||
|
console.log(` (smartagent + ${MODEL})`);
|
||||||
console.log(`======================================================`);
|
console.log(`======================================================`);
|
||||||
console.log(` Method: JSON per-page + consensus`);
|
console.log(` Method: DualAgentOrchestrator with vision`);
|
||||||
console.log(` Passed: ${passedCount}/${total}`);
|
console.log(` Passed: ${passedCount}/${total}`);
|
||||||
console.log(` Failed: ${failedCount}/${total}`);
|
console.log(` Failed: ${failedCount}/${total}`);
|
||||||
console.log(`======================================================\n`);
|
console.log(`======================================================\n`);
|
||||||
|
|||||||
@@ -11,7 +11,9 @@ import * as fs from 'fs';
|
|||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import { execSync } from 'child_process';
|
import { execSync } from 'child_process';
|
||||||
import * as os from 'os';
|
import * as os from 'os';
|
||||||
import { ensureNanonetsOcr, ensureMiniCpm, removeContainer, isContainerRunning } from './helpers/docker.js';
|
import { ensureNanonetsOcr, ensureMiniCpm, isContainerRunning } from './helpers/docker.js';
|
||||||
|
import { SmartAi } from '@push.rocks/smartai';
|
||||||
|
import { DualAgentOrchestrator, JsonValidatorTool } from '@push.rocks/smartagent';
|
||||||
|
|
||||||
const NANONETS_URL = 'http://localhost:8000/v1';
|
const NANONETS_URL = 'http://localhost:8000/v1';
|
||||||
const NANONETS_MODEL = 'nanonets/Nanonets-OCR2-3B';
|
const NANONETS_MODEL = 'nanonets/Nanonets-OCR2-3B';
|
||||||
@@ -22,6 +24,22 @@ const EXTRACTION_MODEL = 'gpt-oss:20b';
|
|||||||
// Temp directory for storing markdown between stages
|
// Temp directory for storing markdown between stages
|
||||||
const TEMP_MD_DIR = path.join(os.tmpdir(), 'nanonets-markdown');
|
const TEMP_MD_DIR = path.join(os.tmpdir(), 'nanonets-markdown');
|
||||||
|
|
||||||
|
// SmartAi instance for Ollama with optimized settings
|
||||||
|
const smartAi = new SmartAi({
|
||||||
|
ollama: {
|
||||||
|
baseUrl: OLLAMA_URL,
|
||||||
|
model: EXTRACTION_MODEL,
|
||||||
|
defaultOptions: {
|
||||||
|
num_ctx: 32768, // Larger context for long statements + thinking
|
||||||
|
temperature: 0, // Deterministic for JSON extraction
|
||||||
|
},
|
||||||
|
defaultTimeout: 600000, // 10 minute timeout for large documents
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// DualAgentOrchestrator for structured task execution
|
||||||
|
let orchestrator: DualAgentOrchestrator;
|
||||||
|
|
||||||
interface ITransaction {
|
interface ITransaction {
|
||||||
date: string;
|
date: string;
|
||||||
counterparty: string;
|
counterparty: string;
|
||||||
@@ -51,11 +69,21 @@ If there is an image in the document and image caption is not present, add a sma
|
|||||||
Watermarks should be wrapped in brackets. Ex: <watermark>OFFICIAL COPY</watermark>.
|
Watermarks should be wrapped in brackets. Ex: <watermark>OFFICIAL COPY</watermark>.
|
||||||
Page numbers should be wrapped in brackets. Ex: <page_number>14</page_number>.`;
|
Page numbers should be wrapped in brackets. Ex: <page_number>14</page_number>.`;
|
||||||
|
|
||||||
// JSON extraction prompt for GPT-OSS 20B
|
// JSON extraction prompt for GPT-OSS 20B (sent AFTER the statement text is provided)
|
||||||
const JSON_EXTRACTION_PROMPT = `Extract ALL transactions from this bank statement as JSON array. Each transaction: {"date": "YYYY-MM-DD", "counterparty": "NAME", "amount": -25.99}. Amount negative for debits, positive for credits. Only include actual transactions, not balances. Return ONLY JSON array, no explanation.
|
const JSON_EXTRACTION_PROMPT = `Extract ALL transactions from the bank statement. Return ONLY valid JSON array.
|
||||||
|
|
||||||
STATEMENT:
|
WHERE TO FIND DATA:
|
||||||
`;
|
- Transactions are typically in TABLES with columns: Date, Description/Counterparty, Debit, Credit, Balance
|
||||||
|
- Look for rows with actual money movements, NOT header rows or summary totals
|
||||||
|
|
||||||
|
RULES:
|
||||||
|
1. date: Convert to YYYY-MM-DD format
|
||||||
|
2. counterparty: The name/description of who the money went to/from
|
||||||
|
3. amount: NEGATIVE for debits/withdrawals, POSITIVE for credits/deposits
|
||||||
|
4. Only include actual transactions, NOT opening/closing balances
|
||||||
|
|
||||||
|
JSON array only:
|
||||||
|
[{"date":"YYYY-MM-DD","counterparty":"NAME","amount":-25.99}]`;
|
||||||
|
|
||||||
// Constants for smart batching
|
// Constants for smart batching
|
||||||
const MAX_VISUAL_TOKENS = 28000; // ~32K context minus prompt/output headroom
|
const MAX_VISUAL_TOKENS = 28000; // ~32K context minus prompt/output headroom
|
||||||
@@ -242,93 +270,107 @@ async function ensureExtractionModel(): Promise<boolean> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extract transactions from markdown using GPT-OSS 20B (streaming)
|
* Try to extract valid JSON from a response string
|
||||||
|
*/
|
||||||
|
function tryExtractJson(response: string): unknown[] | null {
|
||||||
|
// Remove thinking tags
|
||||||
|
let clean = response.replace(/<think>[\s\S]*?<\/think>/g, '').trim();
|
||||||
|
|
||||||
|
// Try task_complete tags first
|
||||||
|
const completeMatch = clean.match(/<task_complete>([\s\S]*?)<\/task_complete>/);
|
||||||
|
if (completeMatch) {
|
||||||
|
clean = completeMatch[1].trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try code block
|
||||||
|
const codeBlockMatch = clean.match(/```(?:json)?\s*([\s\S]*?)```/);
|
||||||
|
const jsonStr = codeBlockMatch ? codeBlockMatch[1].trim() : clean;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const parsed = JSON.parse(jsonStr);
|
||||||
|
if (Array.isArray(parsed)) return parsed;
|
||||||
|
} catch {
|
||||||
|
// Try to find JSON array
|
||||||
|
const jsonMatch = jsonStr.match(/\[[\s\S]*\]/);
|
||||||
|
if (jsonMatch) {
|
||||||
|
try {
|
||||||
|
const parsed = JSON.parse(sanitizeJson(jsonMatch[0]));
|
||||||
|
if (Array.isArray(parsed)) return parsed;
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extract transactions from markdown using smartagent DualAgentOrchestrator
|
||||||
|
* Validates JSON and retries if invalid
|
||||||
*/
|
*/
|
||||||
async function extractTransactionsFromMarkdown(markdown: string, queryId: string): Promise<ITransaction[]> {
|
async function extractTransactionsFromMarkdown(markdown: string, queryId: string): Promise<ITransaction[]> {
|
||||||
const startTime = Date.now();
|
const startTime = Date.now();
|
||||||
const fullPrompt = JSON_EXTRACTION_PROMPT + markdown;
|
|
||||||
|
|
||||||
// Log exact prompt
|
console.log(` [${queryId}] Statement: ${markdown.length} chars`);
|
||||||
console.log(`\n [${queryId}] ===== PROMPT =====`);
|
|
||||||
console.log(fullPrompt);
|
|
||||||
console.log(` [${queryId}] ===== END PROMPT (${fullPrompt.length} chars) =====\n`);
|
|
||||||
|
|
||||||
const response = await fetch(`${OLLAMA_URL}/api/chat`, {
|
// Build the extraction task with document context
|
||||||
method: 'POST',
|
const taskPrompt = `Extract all transactions from this bank statement document and output ONLY the JSON array:
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify({
|
|
||||||
model: EXTRACTION_MODEL,
|
|
||||||
messages: [
|
|
||||||
{ role: 'user', content: 'Hi there, how are you?' },
|
|
||||||
{ role: 'assistant', content: 'Good, how can I help you today?' },
|
|
||||||
{ role: 'user', content: fullPrompt },
|
|
||||||
],
|
|
||||||
stream: true,
|
|
||||||
}),
|
|
||||||
signal: AbortSignal.timeout(600000), // 10 minute timeout
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
${markdown}
|
||||||
const elapsed = ((Date.now() - startTime) / 1000).toFixed(1);
|
|
||||||
console.log(` [${queryId}] ERROR: ${response.status} (${elapsed}s)`);
|
|
||||||
throw new Error(`Ollama API error: ${response.status}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Stream the response
|
${JSON_EXTRACTION_PROMPT}
|
||||||
let content = '';
|
|
||||||
let thinkingContent = '';
|
Before completing, validate your JSON using the json.validate tool:
|
||||||
let thinkingStarted = false;
|
|
||||||
let outputStarted = false;
|
<tool_call>
|
||||||
const reader = response.body!.getReader();
|
<tool>json</tool>
|
||||||
const decoder = new TextDecoder();
|
<action>validate</action>
|
||||||
|
<params>{"jsonString": "YOUR_JSON_ARRAY_HERE"}</params>
|
||||||
|
</tool_call>
|
||||||
|
|
||||||
|
Only complete after validation passes. Output the final JSON array in <task_complete></task_complete> tags.`;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
while (true) {
|
const result = await orchestrator.run(taskPrompt);
|
||||||
const { done, value } = await reader.read();
|
|
||||||
if (done) break;
|
|
||||||
|
|
||||||
const chunk = decoder.decode(value, { stream: true });
|
|
||||||
|
|
||||||
// Each line is a JSON object
|
|
||||||
for (const line of chunk.split('\n').filter(l => l.trim())) {
|
|
||||||
try {
|
|
||||||
const json = JSON.parse(line);
|
|
||||||
|
|
||||||
// Stream thinking tokens
|
|
||||||
const thinking = json.message?.thinking || '';
|
|
||||||
if (thinking) {
|
|
||||||
if (!thinkingStarted) {
|
|
||||||
process.stdout.write(` [${queryId}] THINKING: `);
|
|
||||||
thinkingStarted = true;
|
|
||||||
}
|
|
||||||
process.stdout.write(thinking);
|
|
||||||
thinkingContent += thinking;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Stream content tokens
|
|
||||||
const token = json.message?.content || '';
|
|
||||||
if (token) {
|
|
||||||
if (!outputStarted) {
|
|
||||||
if (thinkingStarted) process.stdout.write('\n');
|
|
||||||
process.stdout.write(` [${queryId}] OUTPUT: `);
|
|
||||||
outputStarted = true;
|
|
||||||
}
|
|
||||||
process.stdout.write(token);
|
|
||||||
content += token;
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
// Ignore parse errors for partial chunks
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
if (thinkingStarted || outputStarted) process.stdout.write('\n');
|
|
||||||
}
|
|
||||||
|
|
||||||
const elapsed = ((Date.now() - startTime) / 1000).toFixed(1);
|
const elapsed = ((Date.now() - startTime) / 1000).toFixed(1);
|
||||||
console.log(` [${queryId}] Done: ${thinkingContent.length} thinking chars, ${content.length} output chars (${elapsed}s)`);
|
console.log(` [${queryId}] Status: ${result.status}, Iterations: ${result.iterations} (${elapsed}s)`);
|
||||||
|
|
||||||
return parseJsonResponse(content, queryId);
|
// Try to parse JSON from result
|
||||||
|
let jsonData: unknown[] | null = null;
|
||||||
|
let responseText = result.result || '';
|
||||||
|
|
||||||
|
if (result.success && responseText) {
|
||||||
|
jsonData = tryExtractJson(responseText);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback: try parsing from history
|
||||||
|
if (!jsonData && result.history?.length > 0) {
|
||||||
|
const lastMessage = result.history[result.history.length - 1];
|
||||||
|
if (lastMessage?.content) {
|
||||||
|
responseText = lastMessage.content;
|
||||||
|
jsonData = tryExtractJson(responseText);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!jsonData) {
|
||||||
|
console.log(` [${queryId}] Failed to parse JSON`);
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert to transactions
|
||||||
|
const txs = jsonData.map((tx: any) => ({
|
||||||
|
date: String(tx.date || ''),
|
||||||
|
counterparty: String(tx.counterparty || tx.description || ''),
|
||||||
|
amount: parseAmount(tx.amount),
|
||||||
|
}));
|
||||||
|
console.log(` [${queryId}] Parsed ${txs.length} transactions`);
|
||||||
|
return txs;
|
||||||
|
} catch (error) {
|
||||||
|
const elapsed = ((Date.now() - startTime) / 1000).toFixed(1);
|
||||||
|
console.log(` [${queryId}] ERROR: ${error} (${elapsed}s)`);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -579,6 +621,53 @@ tap.test('Stage 2: Setup Ollama + GPT-OSS 20B', async () => {
|
|||||||
|
|
||||||
const extractionOk = await ensureExtractionModel();
|
const extractionOk = await ensureExtractionModel();
|
||||||
expect(extractionOk).toBeTrue();
|
expect(extractionOk).toBeTrue();
|
||||||
|
|
||||||
|
// Initialize SmartAi and DualAgentOrchestrator
|
||||||
|
console.log(' [SmartAgent] Starting SmartAi...');
|
||||||
|
await smartAi.start();
|
||||||
|
|
||||||
|
console.log(' [SmartAgent] Creating DualAgentOrchestrator...');
|
||||||
|
orchestrator = new DualAgentOrchestrator({
|
||||||
|
smartAiInstance: smartAi,
|
||||||
|
defaultProvider: 'ollama',
|
||||||
|
guardianPolicyPrompt: `
|
||||||
|
JSON EXTRACTION POLICY:
|
||||||
|
- APPROVE all JSON extraction tasks
|
||||||
|
- APPROVE all json.validate tool calls
|
||||||
|
- This is a read-only operation - no file system or network access needed
|
||||||
|
- The task is to extract structured transaction data from document text
|
||||||
|
`,
|
||||||
|
driverSystemMessage: `You are a precise JSON extraction assistant. Your only job is to extract transaction data from bank statements.
|
||||||
|
|
||||||
|
CRITICAL RULES:
|
||||||
|
1. Output valid JSON array with the exact format requested
|
||||||
|
2. Amounts should be NEGATIVE for debits/withdrawals, POSITIVE for credits/deposits
|
||||||
|
3. IMPORTANT: Before completing, validate your JSON using the json.validate tool:
|
||||||
|
|
||||||
|
<tool_call>
|
||||||
|
<tool>json</tool>
|
||||||
|
<action>validate</action>
|
||||||
|
<params>{"jsonString": "YOUR_JSON_ARRAY"}</params>
|
||||||
|
</tool_call>
|
||||||
|
|
||||||
|
4. Only complete after validation passes
|
||||||
|
|
||||||
|
When done, wrap your JSON array in <task_complete></task_complete> tags.`,
|
||||||
|
maxIterations: 5,
|
||||||
|
// Enable streaming for real-time progress visibility
|
||||||
|
onToken: (token, source) => {
|
||||||
|
if (source === 'driver') {
|
||||||
|
process.stdout.write(token);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Register JsonValidatorTool for self-validation
|
||||||
|
orchestrator.registerTool(new JsonValidatorTool());
|
||||||
|
|
||||||
|
console.log(' [SmartAgent] Starting orchestrator...');
|
||||||
|
await orchestrator.start();
|
||||||
|
console.log(' [SmartAgent] Ready for extraction');
|
||||||
});
|
});
|
||||||
|
|
||||||
let passedCount = 0;
|
let passedCount = 0;
|
||||||
@@ -630,11 +719,19 @@ for (const tc of testCases) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
tap.test('Summary', async () => {
|
tap.test('Summary', async () => {
|
||||||
|
// Cleanup orchestrator and SmartAi
|
||||||
|
if (orchestrator) {
|
||||||
|
console.log('\n [SmartAgent] Stopping orchestrator...');
|
||||||
|
await orchestrator.stop();
|
||||||
|
}
|
||||||
|
console.log(' [SmartAgent] Stopping SmartAi...');
|
||||||
|
await smartAi.stop();
|
||||||
|
|
||||||
console.log(`\n======================================================`);
|
console.log(`\n======================================================`);
|
||||||
console.log(` Bank Statement Summary (Nanonets + GPT-OSS 20B Sequential)`);
|
console.log(` Bank Statement Summary (Nanonets + SmartAgent)`);
|
||||||
console.log(`======================================================`);
|
console.log(`======================================================`);
|
||||||
console.log(` Stage 1: Nanonets-OCR-s (document -> markdown)`);
|
console.log(` Stage 1: Nanonets-OCR-s (document -> markdown)`);
|
||||||
console.log(` Stage 2: GPT-OSS 20B (markdown -> JSON)`);
|
console.log(` Stage 2: GPT-OSS 20B + SmartAgent (markdown -> JSON)`);
|
||||||
console.log(` Passed: ${passedCount}/${testCases.length}`);
|
console.log(` Passed: ${passedCount}/${testCases.length}`);
|
||||||
console.log(` Failed: ${failedCount}/${testCases.length}`);
|
console.log(` Failed: ${failedCount}/${testCases.length}`);
|
||||||
console.log(`======================================================\n`);
|
console.log(`======================================================\n`);
|
||||||
|
|||||||
@@ -197,6 +197,10 @@ async function extractInvoiceFromMarkdown(markdown: string, queryId: string): Pr
|
|||||||
{ role: 'user', content: JSON_EXTRACTION_PROMPT },
|
{ role: 'user', content: JSON_EXTRACTION_PROMPT },
|
||||||
],
|
],
|
||||||
stream: true,
|
stream: true,
|
||||||
|
options: {
|
||||||
|
num_ctx: 32768, // Larger context for long invoices + thinking
|
||||||
|
temperature: 0, // Deterministic for JSON extraction
|
||||||
|
},
|
||||||
}),
|
}),
|
||||||
signal: AbortSignal.timeout(120000), // 2 min timeout
|
signal: AbortSignal.timeout(120000), // 2 min timeout
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
/**
|
/**
|
||||||
* Invoice extraction test using MiniCPM-V (visual extraction)
|
* Invoice extraction test using MiniCPM-V via smartagent DualAgentOrchestrator
|
||||||
*
|
*
|
||||||
* Consensus approach:
|
* Uses vision-capable orchestrator with JsonValidatorTool for self-validation:
|
||||||
* 1. Pass 1: Fast JSON extraction
|
* 1. Pass images to the orchestrator
|
||||||
* 2. Pass 2: Confirm with thinking enabled
|
* 2. Driver extracts invoice data and validates JSON before completing
|
||||||
* 3. If mismatch: repeat until consensus or max attempts
|
* 3. If validation fails, driver retries within the same task
|
||||||
*/
|
*/
|
||||||
import { tap, expect } from '@git.zone/tstest/tapbundle';
|
import { tap, expect } from '@git.zone/tstest/tapbundle';
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
@@ -12,6 +12,8 @@ import * as path from 'path';
|
|||||||
import { execSync } from 'child_process';
|
import { execSync } from 'child_process';
|
||||||
import * as os from 'os';
|
import * as os from 'os';
|
||||||
import { ensureMiniCpm } from './helpers/docker.js';
|
import { ensureMiniCpm } from './helpers/docker.js';
|
||||||
|
import { SmartAi } from '@push.rocks/smartai';
|
||||||
|
import { DualAgentOrchestrator, JsonValidatorTool } from '@push.rocks/smartagent';
|
||||||
|
|
||||||
const OLLAMA_URL = 'http://localhost:11434';
|
const OLLAMA_URL = 'http://localhost:11434';
|
||||||
const MODEL = 'openbmb/minicpm-v4.5:q8_0';
|
const MODEL = 'openbmb/minicpm-v4.5:q8_0';
|
||||||
@@ -26,6 +28,10 @@ interface IInvoice {
|
|||||||
total_amount: number;
|
total_amount: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SmartAi instance and orchestrator (initialized in setup)
|
||||||
|
let smartAi: SmartAi;
|
||||||
|
let orchestrator: DualAgentOrchestrator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert PDF to PNG images using ImageMagick
|
* Convert PDF to PNG images using ImageMagick
|
||||||
*/
|
*/
|
||||||
@@ -54,7 +60,9 @@ function convertPdfToImages(pdfPath: string): string[] {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const JSON_PROMPT = `Extract invoice data from this image. Return ONLY a JSON object with these exact fields:
|
const EXTRACTION_PROMPT = `Extract invoice data from the provided image(s).
|
||||||
|
|
||||||
|
IMPORTANT: You must output a valid JSON object with these exact fields:
|
||||||
{
|
{
|
||||||
"invoice_number": "the invoice number (not VAT ID, not customer ID)",
|
"invoice_number": "the invoice number (not VAT ID, not customer ID)",
|
||||||
"invoice_date": "YYYY-MM-DD format",
|
"invoice_date": "YYYY-MM-DD format",
|
||||||
@@ -64,67 +72,16 @@ const JSON_PROMPT = `Extract invoice data from this image. Return ONLY a JSON ob
|
|||||||
"vat_amount": 0.00,
|
"vat_amount": 0.00,
|
||||||
"total_amount": 0.00
|
"total_amount": 0.00
|
||||||
}
|
}
|
||||||
Return only the JSON, no explanation.`;
|
|
||||||
|
|
||||||
/**
|
Before completing, use the json.validate tool to verify your output is valid JSON with all required fields.
|
||||||
* Query MiniCPM-V for JSON output (fast, no thinking)
|
|
||||||
*/
|
|
||||||
async function queryJsonFast(images: string[]): Promise<string> {
|
|
||||||
const response = await fetch(`${OLLAMA_URL}/api/chat`, {
|
|
||||||
method: 'POST',
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify({
|
|
||||||
model: MODEL,
|
|
||||||
messages: [{
|
|
||||||
role: 'user',
|
|
||||||
content: JSON_PROMPT,
|
|
||||||
images: images,
|
|
||||||
}],
|
|
||||||
stream: false,
|
|
||||||
options: {
|
|
||||||
num_predict: 1000,
|
|
||||||
temperature: 0.1,
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
<tool_call>
|
||||||
throw new Error(`Ollama API error: ${response.status}`);
|
<tool>json</tool>
|
||||||
}
|
<action>validate</action>
|
||||||
|
<params>{"jsonString": "YOUR_JSON_HERE", "requiredFields": ["invoice_number", "invoice_date", "vendor_name", "currency", "net_amount", "vat_amount", "total_amount"]}</params>
|
||||||
|
</tool_call>
|
||||||
|
|
||||||
const data = await response.json();
|
Only complete the task after validation passes. Output the final JSON in <task_complete> tags.`;
|
||||||
return (data.message?.content || '').trim();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Query MiniCPM-V for JSON output with thinking enabled (slower, more accurate)
|
|
||||||
*/
|
|
||||||
async function queryJsonWithThinking(images: string[]): Promise<string> {
|
|
||||||
const response = await fetch(`${OLLAMA_URL}/api/chat`, {
|
|
||||||
method: 'POST',
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify({
|
|
||||||
model: MODEL,
|
|
||||||
messages: [{
|
|
||||||
role: 'user',
|
|
||||||
content: `Think carefully about this invoice image, then ${JSON_PROMPT}`,
|
|
||||||
images: images,
|
|
||||||
}],
|
|
||||||
stream: false,
|
|
||||||
options: {
|
|
||||||
num_predict: 2000,
|
|
||||||
temperature: 0.1,
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(`Ollama API error: ${response.status}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const data = await response.json();
|
|
||||||
return (data.message?.content || '').trim();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse amount from string (handles European format)
|
* Parse amount from string (handles European format)
|
||||||
@@ -190,9 +147,31 @@ function extractCurrency(s: string | undefined): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extract JSON from response (handles markdown code blocks)
|
* Extract JSON from response (handles markdown code blocks and task_complete tags)
|
||||||
*/
|
*/
|
||||||
function extractJsonFromResponse(response: string): Record<string, unknown> | null {
|
function extractJsonFromResponse(response: string): Record<string, unknown> | null {
|
||||||
|
// Try to find JSON in task_complete tags
|
||||||
|
const completeMatch = response.match(/<task_complete>([\s\S]*?)<\/task_complete>/);
|
||||||
|
if (completeMatch) {
|
||||||
|
const content = completeMatch[1].trim();
|
||||||
|
// Try to find JSON in the content
|
||||||
|
const codeBlockMatch = content.match(/```(?:json)?\s*([\s\S]*?)```/);
|
||||||
|
const jsonStr = codeBlockMatch ? codeBlockMatch[1].trim() : content;
|
||||||
|
try {
|
||||||
|
return JSON.parse(jsonStr);
|
||||||
|
} catch {
|
||||||
|
// Try to find JSON object pattern
|
||||||
|
const jsonMatch = jsonStr.match(/\{[\s\S]*\}/);
|
||||||
|
if (jsonMatch) {
|
||||||
|
try {
|
||||||
|
return JSON.parse(jsonMatch[0]);
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Try to find JSON in markdown code block
|
// Try to find JSON in markdown code block
|
||||||
const codeBlockMatch = response.match(/```(?:json)?\s*([\s\S]*?)```/);
|
const codeBlockMatch = response.match(/```(?:json)?\s*([\s\S]*?)```/);
|
||||||
const jsonStr = codeBlockMatch ? codeBlockMatch[1].trim() : response.trim();
|
const jsonStr = codeBlockMatch ? codeBlockMatch[1].trim() : response.trim();
|
||||||
@@ -232,76 +211,27 @@ function parseJsonToInvoice(response: string): IInvoice | null {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compare two invoices for consensus (key fields must match)
|
* Extract invoice data using smartagent orchestrator with vision
|
||||||
*/
|
|
||||||
function invoicesMatch(a: IInvoice, b: IInvoice): boolean {
|
|
||||||
const numMatch = a.invoice_number.toLowerCase() === b.invoice_number.toLowerCase();
|
|
||||||
const dateMatch = a.invoice_date === b.invoice_date;
|
|
||||||
const totalMatch = Math.abs(a.total_amount - b.total_amount) < 0.02;
|
|
||||||
return numMatch && dateMatch && totalMatch;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Extract invoice data using consensus approach:
|
|
||||||
* 1. Pass 1: Fast JSON extraction
|
|
||||||
* 2. Pass 2: Confirm with thinking enabled
|
|
||||||
* 3. If mismatch: repeat until consensus or max 5 attempts
|
|
||||||
*/
|
*/
|
||||||
async function extractInvoiceFromImages(images: string[]): Promise<IInvoice> {
|
async function extractInvoiceFromImages(images: string[]): Promise<IInvoice> {
|
||||||
console.log(` [Vision] Processing ${images.length} page(s) with ${MODEL} (consensus)`);
|
console.log(` [Vision] Processing ${images.length} page(s) with smartagent DualAgentOrchestrator`);
|
||||||
|
|
||||||
const MAX_ATTEMPTS = 5;
|
const startTime = Date.now();
|
||||||
let attempt = 0;
|
|
||||||
|
|
||||||
while (attempt < MAX_ATTEMPTS) {
|
const result = await orchestrator.run(EXTRACTION_PROMPT, { images });
|
||||||
attempt++;
|
|
||||||
console.log(` [Attempt ${attempt}/${MAX_ATTEMPTS}]`);
|
|
||||||
|
|
||||||
// PASS 1: Fast JSON extraction
|
const elapsed = ((Date.now() - startTime) / 1000).toFixed(1);
|
||||||
console.log(` [Pass 1] Fast extraction...`);
|
console.log(` [Vision] Completed in ${elapsed}s (${result.iterations} iterations, status: ${result.status})`);
|
||||||
const fastResponse = await queryJsonFast(images);
|
|
||||||
const fastInvoice = parseJsonToInvoice(fastResponse);
|
|
||||||
|
|
||||||
if (!fastInvoice) {
|
const invoice = parseJsonToInvoice(result.result);
|
||||||
console.log(` [Pass 1] JSON parsing failed, retrying...`);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
console.log(` [Pass 1] Result: ${fastInvoice.invoice_number} | ${fastInvoice.invoice_date} | ${fastInvoice.total_amount} ${fastInvoice.currency}`);
|
|
||||||
|
|
||||||
// PASS 2: Confirm with thinking
|
if (invoice) {
|
||||||
console.log(` [Pass 2] Thinking confirmation...`);
|
console.log(` [Result] ${invoice.invoice_number} | ${invoice.invoice_date} | ${invoice.total_amount} ${invoice.currency}`);
|
||||||
const thinkResponse = await queryJsonWithThinking(images);
|
return invoice;
|
||||||
const thinkInvoice = parseJsonToInvoice(thinkResponse);
|
|
||||||
|
|
||||||
if (!thinkInvoice) {
|
|
||||||
console.log(` [Pass 2] JSON parsing failed, retrying...`);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
console.log(` [Pass 2] Result: ${thinkInvoice.invoice_number} | ${thinkInvoice.invoice_date} | ${thinkInvoice.total_amount} ${thinkInvoice.currency}`);
|
|
||||||
|
|
||||||
// Check consensus
|
|
||||||
if (invoicesMatch(fastInvoice, thinkInvoice)) {
|
|
||||||
console.log(` [Consensus] MATCH - using result`);
|
|
||||||
return thinkInvoice; // Prefer thinking result
|
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(` [Consensus] MISMATCH - repeating...`);
|
// Return empty invoice if parsing failed
|
||||||
console.log(` Fast: ${fastInvoice.invoice_number} | ${fastInvoice.invoice_date} | ${fastInvoice.total_amount}`);
|
console.log(` [Result] Parsing failed, returning empty invoice`);
|
||||||
console.log(` Think: ${thinkInvoice.invoice_number} | ${thinkInvoice.invoice_date} | ${thinkInvoice.total_amount}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Max attempts reached - do one final thinking pass and use that
|
|
||||||
console.log(` [Final] Max attempts reached, using final thinking pass`);
|
|
||||||
const finalResponse = await queryJsonWithThinking(images);
|
|
||||||
const finalInvoice = parseJsonToInvoice(finalResponse);
|
|
||||||
|
|
||||||
if (finalInvoice) {
|
|
||||||
console.log(` [Final] Result: ${finalInvoice.invoice_number} | ${finalInvoice.invoice_date} | ${finalInvoice.total_amount} ${finalInvoice.currency}`);
|
|
||||||
return finalInvoice;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return empty invoice if all else fails
|
|
||||||
console.log(` [Final] All parsing failed, returning empty`);
|
|
||||||
return {
|
return {
|
||||||
invoice_number: '',
|
invoice_number: '',
|
||||||
invoice_date: '',
|
invoice_date: '',
|
||||||
@@ -410,6 +340,79 @@ tap.test('setup: ensure Docker containers are running', async () => {
|
|||||||
console.log('\n[Setup] All containers ready!\n');
|
console.log('\n[Setup] All containers ready!\n');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
tap.test('setup: initialize smartagent orchestrator', async () => {
|
||||||
|
console.log('[Setup] Initializing SmartAi and DualAgentOrchestrator...');
|
||||||
|
|
||||||
|
smartAi = new SmartAi({
|
||||||
|
ollama: {
|
||||||
|
baseUrl: OLLAMA_URL,
|
||||||
|
model: MODEL,
|
||||||
|
defaultOptions: {
|
||||||
|
num_ctx: 32768,
|
||||||
|
temperature: 0.1,
|
||||||
|
},
|
||||||
|
defaultTimeout: 300000, // 5 minutes for vision tasks
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
await smartAi.start();
|
||||||
|
|
||||||
|
orchestrator = new DualAgentOrchestrator({
|
||||||
|
smartAiInstance: smartAi,
|
||||||
|
defaultProvider: 'ollama',
|
||||||
|
guardianPolicyPrompt: `You are a Guardian agent overseeing invoice extraction tasks.
|
||||||
|
|
||||||
|
APPROVE all tool calls that:
|
||||||
|
- Use the json.validate action to verify JSON output
|
||||||
|
- Are reasonable attempts to complete the extraction task
|
||||||
|
|
||||||
|
REJECT tool calls that:
|
||||||
|
- Attempt to access external resources
|
||||||
|
- Try to execute arbitrary code
|
||||||
|
- Are clearly unrelated to invoice extraction`,
|
||||||
|
driverSystemMessage: `You are an AI assistant that extracts invoice data from images.
|
||||||
|
|
||||||
|
Your task is to analyze invoice images and extract structured data.
|
||||||
|
You have access to a json.validate tool to verify your JSON output.
|
||||||
|
|
||||||
|
IMPORTANT: Always validate your JSON before completing the task.
|
||||||
|
|
||||||
|
## Tool Usage Format
|
||||||
|
When you need to validate JSON, output:
|
||||||
|
|
||||||
|
<tool_call>
|
||||||
|
<tool>json</tool>
|
||||||
|
<action>validate</action>
|
||||||
|
<params>{"jsonString": "YOUR_JSON", "requiredFields": ["invoice_number", "invoice_date", "vendor_name", "currency", "net_amount", "vat_amount", "total_amount"]}</params>
|
||||||
|
</tool_call>
|
||||||
|
|
||||||
|
## Completion Format
|
||||||
|
After validation passes, complete the task:
|
||||||
|
|
||||||
|
<task_complete>
|
||||||
|
{"invoice_number": "...", "invoice_date": "YYYY-MM-DD", ...}
|
||||||
|
</task_complete>`,
|
||||||
|
maxIterations: 5,
|
||||||
|
maxConsecutiveRejections: 3,
|
||||||
|
onToken: (token, source) => {
|
||||||
|
if (source === 'driver') {
|
||||||
|
process.stdout.write(token);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onProgress: (event) => {
|
||||||
|
if (event.logLevel === 'error') {
|
||||||
|
console.error(event.logMessage);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Register the JsonValidatorTool
|
||||||
|
orchestrator.registerTool(new JsonValidatorTool());
|
||||||
|
|
||||||
|
await orchestrator.start();
|
||||||
|
console.log('[Setup] Orchestrator initialized!\n');
|
||||||
|
});
|
||||||
|
|
||||||
tap.test('should have MiniCPM-V model loaded', async () => {
|
tap.test('should have MiniCPM-V model loaded', async () => {
|
||||||
const response = await fetch(`${OLLAMA_URL}/api/tags`);
|
const response = await fetch(`${OLLAMA_URL}/api/tags`);
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
@@ -418,7 +421,7 @@ tap.test('should have MiniCPM-V model loaded', async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const testCases = findTestCases();
|
const testCases = findTestCases();
|
||||||
console.log(`\nFound ${testCases.length} invoice test cases (MiniCPM-V)\n`);
|
console.log(`\nFound ${testCases.length} invoice test cases (smartagent + MiniCPM-V)\n`);
|
||||||
|
|
||||||
let passedCount = 0;
|
let passedCount = 0;
|
||||||
let failedCount = 0;
|
let failedCount = 0;
|
||||||
@@ -455,6 +458,13 @@ for (const testCase of testCases) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tap.test('cleanup: stop orchestrator', async () => {
|
||||||
|
if (orchestrator) {
|
||||||
|
await orchestrator.stop();
|
||||||
|
}
|
||||||
|
console.log('[Cleanup] Orchestrator stopped');
|
||||||
|
});
|
||||||
|
|
||||||
tap.test('summary', async () => {
|
tap.test('summary', async () => {
|
||||||
const totalInvoices = testCases.length;
|
const totalInvoices = testCases.length;
|
||||||
const accuracy = totalInvoices > 0 ? (passedCount / totalInvoices) * 100 : 0;
|
const accuracy = totalInvoices > 0 ? (passedCount / totalInvoices) * 100 : 0;
|
||||||
@@ -462,9 +472,10 @@ tap.test('summary', async () => {
|
|||||||
const avgTimeSec = processingTimes.length > 0 ? totalTimeMs / processingTimes.length / 1000 : 0;
|
const avgTimeSec = processingTimes.length > 0 ? totalTimeMs / processingTimes.length / 1000 : 0;
|
||||||
|
|
||||||
console.log(`\n========================================`);
|
console.log(`\n========================================`);
|
||||||
console.log(` Invoice Extraction Summary (${MODEL})`);
|
console.log(` Invoice Extraction Summary`);
|
||||||
|
console.log(` (smartagent + ${MODEL})`);
|
||||||
console.log(`========================================`);
|
console.log(`========================================`);
|
||||||
console.log(` Method: Consensus (fast + thinking)`);
|
console.log(` Method: DualAgentOrchestrator with vision`);
|
||||||
console.log(` Passed: ${passedCount}/${totalInvoices}`);
|
console.log(` Passed: ${passedCount}/${totalInvoices}`);
|
||||||
console.log(` Failed: ${failedCount}/${totalInvoices}`);
|
console.log(` Failed: ${failedCount}/${totalInvoices}`);
|
||||||
console.log(` Accuracy: ${accuracy.toFixed(1)}%`);
|
console.log(` Accuracy: ${accuracy.toFixed(1)}%`);
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ import * as path from 'path';
|
|||||||
import { execSync } from 'child_process';
|
import { execSync } from 'child_process';
|
||||||
import * as os from 'os';
|
import * as os from 'os';
|
||||||
import { ensureNanonetsOcr, ensureMiniCpm, isContainerRunning } from './helpers/docker.js';
|
import { ensureNanonetsOcr, ensureMiniCpm, isContainerRunning } from './helpers/docker.js';
|
||||||
|
import { SmartAi } from '@push.rocks/smartai';
|
||||||
|
import { DualAgentOrchestrator, JsonValidatorTool } from '@push.rocks/smartagent';
|
||||||
|
|
||||||
const NANONETS_URL = 'http://localhost:8000/v1';
|
const NANONETS_URL = 'http://localhost:8000/v1';
|
||||||
const NANONETS_MODEL = 'nanonets/Nanonets-OCR2-3B';
|
const NANONETS_MODEL = 'nanonets/Nanonets-OCR2-3B';
|
||||||
@@ -19,8 +21,35 @@ const NANONETS_MODEL = 'nanonets/Nanonets-OCR2-3B';
|
|||||||
const OLLAMA_URL = 'http://localhost:11434';
|
const OLLAMA_URL = 'http://localhost:11434';
|
||||||
const EXTRACTION_MODEL = 'gpt-oss:20b';
|
const EXTRACTION_MODEL = 'gpt-oss:20b';
|
||||||
|
|
||||||
// Temp directory for storing markdown between stages
|
// Persistent cache directory for storing markdown between runs
|
||||||
const TEMP_MD_DIR = path.join(os.tmpdir(), 'nanonets-invoices-markdown');
|
const MD_CACHE_DIR = path.join(process.cwd(), '.nogit/invoices-md');
|
||||||
|
|
||||||
|
// SmartAi instance for Ollama with optimized settings
|
||||||
|
const smartAi = new SmartAi({
|
||||||
|
ollama: {
|
||||||
|
baseUrl: OLLAMA_URL,
|
||||||
|
model: EXTRACTION_MODEL,
|
||||||
|
defaultOptions: {
|
||||||
|
num_ctx: 65536, // 64K context for long invoices + reasoning chains
|
||||||
|
temperature: 0, // Deterministic for JSON extraction
|
||||||
|
repeat_penalty: 1.3, // Penalty to prevent repetition loops
|
||||||
|
think: true, // Enable thinking mode for GPT-OSS reasoning
|
||||||
|
},
|
||||||
|
defaultTimeout: 600000, // 10 minute timeout for large documents
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// DualAgentOrchestrator for structured task execution
|
||||||
|
let orchestrator: DualAgentOrchestrator;
|
||||||
|
|
||||||
|
interface ILineItem {
|
||||||
|
position: number;
|
||||||
|
product: string;
|
||||||
|
description: string;
|
||||||
|
quantity: number;
|
||||||
|
unit_price: number;
|
||||||
|
total_price: number;
|
||||||
|
}
|
||||||
|
|
||||||
interface IInvoice {
|
interface IInvoice {
|
||||||
invoice_number: string;
|
invoice_number: string;
|
||||||
@@ -30,6 +59,7 @@ interface IInvoice {
|
|||||||
net_amount: number;
|
net_amount: number;
|
||||||
vat_amount: number;
|
vat_amount: number;
|
||||||
total_amount: number;
|
total_amount: number;
|
||||||
|
line_items: ILineItem[];
|
||||||
}
|
}
|
||||||
|
|
||||||
interface IImageData {
|
interface IImageData {
|
||||||
@@ -54,34 +84,43 @@ If there is an image in the document and image caption is not present, add a sma
|
|||||||
Watermarks should be wrapped in brackets. Ex: <watermark>OFFICIAL COPY</watermark>.
|
Watermarks should be wrapped in brackets. Ex: <watermark>OFFICIAL COPY</watermark>.
|
||||||
Page numbers should be wrapped in brackets. Ex: <page_number>14</page_number>.`;
|
Page numbers should be wrapped in brackets. Ex: <page_number>14</page_number>.`;
|
||||||
|
|
||||||
// JSON extraction prompt for GPT-OSS 20B
|
// JSON extraction prompt for GPT-OSS 20B (sent AFTER the invoice text is provided)
|
||||||
const JSON_EXTRACTION_PROMPT = `You are an invoice data extractor. Below is an invoice document converted to text/markdown. Extract the key invoice fields as JSON.
|
const JSON_EXTRACTION_PROMPT = `Extract key fields from the invoice. Return ONLY valid JSON.
|
||||||
|
|
||||||
IMPORTANT RULES:
|
WHERE TO FIND DATA:
|
||||||
1. invoice_number: The unique invoice/document number (NOT VAT ID, NOT customer ID)
|
- invoice_number, invoice_date, vendor_name: Look in the HEADER section at the TOP of PAGE 1 (near "Invoice no.", "Invoice date:", "Rechnungsnummer"). Use common sense. Btw. an invoice number might start on INV* . Also be sure to not omit special chars like / - and sp on. They are part of the invoice number.
|
||||||
2. invoice_date: Format as YYYY-MM-DD
|
- net_amount, vat_amount, total_amount: Look in the SUMMARY section at the BOTTOM (look for "Total", "Amount due", "Gesamtbetrag")
|
||||||
3. vendor_name: The company that issued the invoice
|
- line_items: Look in the TABLE(s) with columns like Pos, Product, Description, Quantity, Unit Price, Price
|
||||||
|
|
||||||
|
RULES:
|
||||||
|
1. Use common sense.
|
||||||
|
2. invoice_date: Convert to YYYY-MM-DD format (e.g., "14/04/2022" → "2022-04-14")
|
||||||
|
3. vendor_name: The company issuing the invoice
|
||||||
4. currency: EUR, USD, or GBP
|
4. currency: EUR, USD, or GBP
|
||||||
5. net_amount: Amount before tax
|
5. net_amount: Total before tax
|
||||||
6. vat_amount: Tax/VAT amount
|
6. vat_amount: Tax amount
|
||||||
7. total_amount: Final total (gross amount)
|
7. total_amount: Final total with tax
|
||||||
|
8. line_items: Array of items from the invoice table. Skip subtotal/total rows.
|
||||||
|
|
||||||
Return ONLY this JSON format, no explanation:
|
JSON format:
|
||||||
{
|
{
|
||||||
"invoice_number": "INV-2024-001",
|
"invoice_number": "X",
|
||||||
"invoice_date": "2024-01-15",
|
"invoice_date": "YYYY-MM-DD",
|
||||||
"vendor_name": "Company Name",
|
"vendor_name": "X",
|
||||||
"currency": "EUR",
|
"currency": "EUR",
|
||||||
"net_amount": 100.00,
|
"net_amount": 0,
|
||||||
"vat_amount": 19.00,
|
"vat_amount": 0,
|
||||||
"total_amount": 119.00
|
"total_amount": 0,
|
||||||
|
"line_items": [
|
||||||
|
{"position": 1, "product": "X", "description": "X", "quantity": 1, "unit_price": 0, "total_price": 0}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
INVOICE TEXT:
|
Double check for valid JSON syntax. use the json validate tool.
|
||||||
|
|
||||||
`;
|
`;
|
||||||
|
|
||||||
// Constants for smart batching
|
// Constants for smart batching
|
||||||
const MAX_VISUAL_TOKENS = 28000; // ~32K context minus prompt/output headroom
|
|
||||||
const PATCH_SIZE = 14; // Qwen2.5-VL uses 14x14 patches
|
const PATCH_SIZE = 14; // Qwen2.5-VL uses 14x14 patches
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -294,7 +333,7 @@ function extractInvoiceNumber(s: string | undefined): string {
|
|||||||
const match = clean.match(pattern);
|
const match = clean.match(pattern);
|
||||||
if (match) return match[1];
|
if (match) return match[1];
|
||||||
}
|
}
|
||||||
return clean.replace(/[^A-Z0-9-]/gi, '').trim() || clean;
|
return clean.replace(/[^A-Z0-9\/-]/gi, '').trim() || clean;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -325,16 +364,35 @@ function extractCurrency(s: string | undefined): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extract JSON from response
|
* Extract and normalize line items array
|
||||||
*/
|
*/
|
||||||
function extractJsonFromResponse(response: string): Record<string, unknown> | null {
|
function extractLineItems(items: unknown): ILineItem[] {
|
||||||
let cleanResponse = response.replace(/<think>[\s\S]*?<\/think>/g, '').trim();
|
if (!Array.isArray(items)) return [];
|
||||||
const codeBlockMatch = cleanResponse.match(/```(?:json)?\s*([\s\S]*?)```/);
|
return items.map((item: Record<string, unknown>, index: number) => ({
|
||||||
const jsonStr = codeBlockMatch ? codeBlockMatch[1].trim() : cleanResponse;
|
position: typeof item.position === 'number' ? item.position : index + 1,
|
||||||
|
product: String(item.product || '').trim(),
|
||||||
|
description: String(item.description || '').trim(),
|
||||||
|
quantity: parseAmount(item.quantity as string | number) || 1,
|
||||||
|
unit_price: parseAmount(item.unit_price as string | number),
|
||||||
|
total_price: parseAmount(item.total_price as string | number),
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Try to extract valid JSON from a response string
|
||||||
|
*/
|
||||||
|
function tryExtractJson(response: string): Record<string, unknown> | null {
|
||||||
|
// Remove thinking tags
|
||||||
|
let clean = response.replace(/<think>[\s\S]*?<\/think>/g, '').trim();
|
||||||
|
|
||||||
|
// Try code block
|
||||||
|
const codeBlockMatch = clean.match(/```(?:json)?\s*([\s\S]*?)```/);
|
||||||
|
const jsonStr = codeBlockMatch ? codeBlockMatch[1].trim() : clean;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return JSON.parse(jsonStr);
|
return JSON.parse(jsonStr);
|
||||||
} catch {
|
} catch {
|
||||||
|
// Try to find JSON object
|
||||||
const jsonMatch = jsonStr.match(/\{[\s\S]*\}/);
|
const jsonMatch = jsonStr.match(/\{[\s\S]*\}/);
|
||||||
if (jsonMatch) {
|
if (jsonMatch) {
|
||||||
try {
|
try {
|
||||||
@@ -348,111 +406,93 @@ function extractJsonFromResponse(response: string): Record<string, unknown> | nu
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse JSON response into IInvoice
|
* Extract invoice from markdown using smartagent DualAgentOrchestrator
|
||||||
*/
|
* Validates JSON and retries if invalid
|
||||||
function parseJsonToInvoice(response: string): IInvoice | null {
|
|
||||||
const parsed = extractJsonFromResponse(response);
|
|
||||||
if (!parsed) return null;
|
|
||||||
|
|
||||||
return {
|
|
||||||
invoice_number: extractInvoiceNumber(String(parsed.invoice_number || '')),
|
|
||||||
invoice_date: extractDate(String(parsed.invoice_date || '')),
|
|
||||||
vendor_name: String(parsed.vendor_name || '').replace(/\*\*/g, '').replace(/`/g, '').trim(),
|
|
||||||
currency: extractCurrency(String(parsed.currency || '')),
|
|
||||||
net_amount: parseAmount(parsed.net_amount as string | number),
|
|
||||||
vat_amount: parseAmount(parsed.vat_amount as string | number),
|
|
||||||
total_amount: parseAmount(parsed.total_amount as string | number),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Extract invoice from markdown using GPT-OSS 20B (streaming)
|
|
||||||
*/
|
*/
|
||||||
async function extractInvoiceFromMarkdown(markdown: string, queryId: string): Promise<IInvoice | null> {
|
async function extractInvoiceFromMarkdown(markdown: string, queryId: string): Promise<IInvoice | null> {
|
||||||
const startTime = Date.now();
|
const startTime = Date.now();
|
||||||
const fullPrompt = JSON_EXTRACTION_PROMPT + markdown;
|
const maxRetries = 2;
|
||||||
|
|
||||||
// Log exact prompt
|
console.log(` [${queryId}] Invoice: ${markdown.length} chars`);
|
||||||
console.log(`\n [${queryId}] ===== PROMPT =====`);
|
|
||||||
console.log(fullPrompt);
|
|
||||||
console.log(` [${queryId}] ===== END PROMPT (${fullPrompt.length} chars) =====\n`);
|
|
||||||
|
|
||||||
const response = await fetch(`${OLLAMA_URL}/api/chat`, {
|
// Build the extraction task with document context
|
||||||
method: 'POST',
|
const taskPrompt = `Extract the invoice data from this document and output ONLY the JSON:
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify({
|
|
||||||
model: EXTRACTION_MODEL,
|
|
||||||
messages: [
|
|
||||||
{ role: 'user', content: 'Hi there, how are you?' },
|
|
||||||
{ role: 'assistant', content: 'Good, how can I help you today?' },
|
|
||||||
{ role: 'user', content: fullPrompt },
|
|
||||||
],
|
|
||||||
stream: true,
|
|
||||||
}),
|
|
||||||
signal: AbortSignal.timeout(600000), // 10 minute timeout for large documents
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
${markdown}
|
||||||
const elapsed = ((Date.now() - startTime) / 1000).toFixed(1);
|
|
||||||
console.log(` [${queryId}] ERROR: ${response.status} (${elapsed}s)`);
|
|
||||||
throw new Error(`Ollama API error: ${response.status}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Stream the response
|
${JSON_EXTRACTION_PROMPT}`;
|
||||||
let content = '';
|
|
||||||
let thinkingContent = '';
|
|
||||||
let thinkingStarted = false;
|
|
||||||
let outputStarted = false;
|
|
||||||
const reader = response.body!.getReader();
|
|
||||||
const decoder = new TextDecoder();
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
while (true) {
|
let result = await orchestrator.run(taskPrompt);
|
||||||
const { done, value } = await reader.read();
|
let elapsed = ((Date.now() - startTime) / 1000).toFixed(1);
|
||||||
if (done) break;
|
console.log(` [${queryId}] Status: ${result.status}, Iterations: ${result.iterations} (${elapsed}s)`);
|
||||||
|
|
||||||
const chunk = decoder.decode(value, { stream: true });
|
// Try to parse JSON from result
|
||||||
|
let jsonData: Record<string, unknown> | null = null;
|
||||||
|
let responseText = result.result || '';
|
||||||
|
|
||||||
// Each line is a JSON object
|
if (result.success && responseText) {
|
||||||
for (const line of chunk.split('\n').filter(l => l.trim())) {
|
jsonData = tryExtractJson(responseText);
|
||||||
try {
|
|
||||||
const json = JSON.parse(line);
|
|
||||||
|
|
||||||
// Stream thinking tokens
|
|
||||||
const thinking = json.message?.thinking || '';
|
|
||||||
if (thinking) {
|
|
||||||
if (!thinkingStarted) {
|
|
||||||
process.stdout.write(` [${queryId}] THINKING: `);
|
|
||||||
thinkingStarted = true;
|
|
||||||
}
|
|
||||||
process.stdout.write(thinking);
|
|
||||||
thinkingContent += thinking;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stream content tokens
|
// Fallback: try parsing from history
|
||||||
const token = json.message?.content || '';
|
if (!jsonData && result.history?.length > 0) {
|
||||||
if (token) {
|
const lastMessage = result.history[result.history.length - 1];
|
||||||
if (!outputStarted) {
|
if (lastMessage?.content) {
|
||||||
if (thinkingStarted) process.stdout.write('\n');
|
responseText = lastMessage.content;
|
||||||
process.stdout.write(` [${queryId}] OUTPUT: `);
|
jsonData = tryExtractJson(responseText);
|
||||||
outputStarted = true;
|
|
||||||
}
|
}
|
||||||
process.stdout.write(token);
|
|
||||||
content += token;
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
// Ignore parse errors for partial chunks
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
if (thinkingStarted || outputStarted) process.stdout.write('\n');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If JSON is invalid, retry with correction request
|
||||||
|
let retries = 0;
|
||||||
|
while (!jsonData && retries < maxRetries) {
|
||||||
|
retries++;
|
||||||
|
console.log(` [${queryId}] Invalid JSON, requesting correction (retry ${retries}/${maxRetries})...`);
|
||||||
|
|
||||||
|
result = await orchestrator.continueTask(
|
||||||
|
`Your response was not valid JSON. Please output ONLY the JSON object with no markdown, no explanation, no thinking tags. Just the raw JSON starting with { and ending with }. Format:
|
||||||
|
{"invoice_number":"X","invoice_date":"YYYY-MM-DD","vendor_name":"X","currency":"EUR","net_amount":0,"vat_amount":0,"total_amount":0}`
|
||||||
|
);
|
||||||
|
|
||||||
|
elapsed = ((Date.now() - startTime) / 1000).toFixed(1);
|
||||||
|
console.log(` [${queryId}] Retry ${retries}: ${result.status} (${elapsed}s)`);
|
||||||
|
|
||||||
|
responseText = result.result || '';
|
||||||
|
if (responseText) {
|
||||||
|
jsonData = tryExtractJson(responseText);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!jsonData && result.history?.length > 0) {
|
||||||
|
const lastMessage = result.history[result.history.length - 1];
|
||||||
|
if (lastMessage?.content) {
|
||||||
|
responseText = lastMessage.content;
|
||||||
|
jsonData = tryExtractJson(responseText);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!jsonData) {
|
||||||
|
console.log(` [${queryId}] Failed to get valid JSON after ${retries} retries`);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(` [${queryId}] Valid JSON extracted`);
|
||||||
|
return {
|
||||||
|
invoice_number: extractInvoiceNumber(String(jsonData.invoice_number || '')),
|
||||||
|
invoice_date: extractDate(String(jsonData.invoice_date || '')),
|
||||||
|
vendor_name: String(jsonData.vendor_name || '').replace(/\*\*/g, '').replace(/`/g, '').trim(),
|
||||||
|
currency: extractCurrency(String(jsonData.currency || '')),
|
||||||
|
net_amount: parseAmount(jsonData.net_amount as string | number),
|
||||||
|
vat_amount: parseAmount(jsonData.vat_amount as string | number),
|
||||||
|
total_amount: parseAmount(jsonData.total_amount as string | number),
|
||||||
|
line_items: extractLineItems(jsonData.line_items),
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
const elapsed = ((Date.now() - startTime) / 1000).toFixed(1);
|
const elapsed = ((Date.now() - startTime) / 1000).toFixed(1);
|
||||||
console.log(` [${queryId}] Done: ${thinkingContent.length} thinking chars, ${content.length} output chars (${elapsed}s)`);
|
console.log(` [${queryId}] ERROR: ${error} (${elapsed}s)`);
|
||||||
|
throw error;
|
||||||
return parseJsonToInvoice(content);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -470,6 +510,7 @@ async function extractInvoice(markdown: string, docName: string): Promise<IInvoi
|
|||||||
net_amount: 0,
|
net_amount: 0,
|
||||||
vat_amount: 0,
|
vat_amount: 0,
|
||||||
total_amount: 0,
|
total_amount: 0,
|
||||||
|
line_items: [],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
console.log(` [${docName}] Extracted: ${invoice.invoice_number}`);
|
console.log(` [${docName}] Extracted: ${invoice.invoice_number}`);
|
||||||
@@ -561,23 +602,45 @@ function findTestCases(): ITestCase[] {
|
|||||||
const testCases = findTestCases();
|
const testCases = findTestCases();
|
||||||
console.log(`\nFound ${testCases.length} invoice test cases\n`);
|
console.log(`\nFound ${testCases.length} invoice test cases\n`);
|
||||||
|
|
||||||
// Ensure temp directory exists
|
// Ensure cache directory exists
|
||||||
if (!fs.existsSync(TEMP_MD_DIR)) {
|
if (!fs.existsSync(MD_CACHE_DIR)) {
|
||||||
fs.mkdirSync(TEMP_MD_DIR, { recursive: true });
|
fs.mkdirSync(MD_CACHE_DIR, { recursive: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
// -------- STAGE 1: OCR with Nanonets --------
|
// -------- STAGE 1: OCR with Nanonets --------
|
||||||
|
|
||||||
tap.test('Stage 1: Setup Nanonets', async () => {
|
tap.test('Stage 1: Convert invoices to markdown (with caching)', async () => {
|
||||||
console.log('\n========== STAGE 1: Nanonets OCR ==========\n');
|
console.log('\n========== STAGE 1: Nanonets OCR ==========\n');
|
||||||
const ok = await ensureNanonetsOcr();
|
|
||||||
expect(ok).toBeTrue();
|
|
||||||
});
|
|
||||||
|
|
||||||
tap.test('Stage 1: Convert all invoices to markdown', async () => {
|
// Check which invoices need OCR conversion
|
||||||
console.log('\n Converting all invoice PDFs to markdown with Nanonets-OCR-s...\n');
|
const needsConversion: ITestCase[] = [];
|
||||||
|
let cachedCount = 0;
|
||||||
|
|
||||||
for (const tc of testCases) {
|
for (const tc of testCases) {
|
||||||
|
const mdPath = path.join(MD_CACHE_DIR, `${tc.name}.md`);
|
||||||
|
if (fs.existsSync(mdPath)) {
|
||||||
|
cachedCount++;
|
||||||
|
tc.markdownPath = mdPath;
|
||||||
|
console.log(` [CACHED] ${tc.name} - using cached markdown`);
|
||||||
|
} else {
|
||||||
|
needsConversion.push(tc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`\n Summary: ${cachedCount} cached, ${needsConversion.length} need conversion\n`);
|
||||||
|
|
||||||
|
if (needsConversion.length === 0) {
|
||||||
|
console.log(' All invoices already cached, skipping Nanonets OCR\n');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start Nanonets only if there are files to convert
|
||||||
|
console.log(' Starting Nanonets for OCR conversion...\n');
|
||||||
|
const ok = await ensureNanonetsOcr();
|
||||||
|
expect(ok).toBeTrue();
|
||||||
|
|
||||||
|
// Convert only the invoices that need conversion
|
||||||
|
for (const tc of needsConversion) {
|
||||||
console.log(`\n === ${tc.name} ===`);
|
console.log(`\n === ${tc.name} ===`);
|
||||||
|
|
||||||
const images = convertPdfToImages(tc.pdfPath);
|
const images = convertPdfToImages(tc.pdfPath);
|
||||||
@@ -585,13 +648,13 @@ tap.test('Stage 1: Convert all invoices to markdown', async () => {
|
|||||||
|
|
||||||
const markdown = await convertDocumentToMarkdown(images, tc.name);
|
const markdown = await convertDocumentToMarkdown(images, tc.name);
|
||||||
|
|
||||||
const mdPath = path.join(TEMP_MD_DIR, `${tc.name}.md`);
|
const mdPath = path.join(MD_CACHE_DIR, `${tc.name}.md`);
|
||||||
fs.writeFileSync(mdPath, markdown);
|
fs.writeFileSync(mdPath, markdown);
|
||||||
tc.markdownPath = mdPath;
|
tc.markdownPath = mdPath;
|
||||||
console.log(` Saved: ${mdPath}`);
|
console.log(` Saved: ${mdPath}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('\n Stage 1 complete: All invoices converted to markdown\n');
|
console.log(`\n Stage 1 complete: ${needsConversion.length} invoices converted to markdown\n`);
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('Stage 1: Stop Nanonets', async () => {
|
tap.test('Stage 1: Stop Nanonets', async () => {
|
||||||
@@ -610,6 +673,50 @@ tap.test('Stage 2: Setup Ollama + GPT-OSS 20B', async () => {
|
|||||||
|
|
||||||
const extractionOk = await ensureExtractionModel();
|
const extractionOk = await ensureExtractionModel();
|
||||||
expect(extractionOk).toBeTrue();
|
expect(extractionOk).toBeTrue();
|
||||||
|
|
||||||
|
// Initialize SmartAi and DualAgentOrchestrator
|
||||||
|
console.log(' [SmartAgent] Starting SmartAi...');
|
||||||
|
await smartAi.start();
|
||||||
|
|
||||||
|
console.log(' [SmartAgent] Creating DualAgentOrchestrator with native tool calling...');
|
||||||
|
orchestrator = new DualAgentOrchestrator({
|
||||||
|
smartAiInstance: smartAi,
|
||||||
|
defaultProvider: 'ollama',
|
||||||
|
guardianPolicyPrompt: `
|
||||||
|
JSON EXTRACTION POLICY:
|
||||||
|
- APPROVE all JSON extraction tasks
|
||||||
|
- APPROVE all json.validate tool calls
|
||||||
|
- This is a read-only operation - no file system or network access needed
|
||||||
|
- The task is to extract structured data from document text
|
||||||
|
`,
|
||||||
|
driverSystemMessage: `You are a precise JSON extraction assistant. Your only job is to extract invoice data from documents.
|
||||||
|
|
||||||
|
CRITICAL RULES:
|
||||||
|
1. Output valid JSON with the exact format requested
|
||||||
|
2. If you cannot find a value, use empty string "" or 0 for numbers
|
||||||
|
3. Before completing, validate your JSON using the json_validate tool
|
||||||
|
4. Only complete after validation passes`,
|
||||||
|
maxIterations: 5,
|
||||||
|
// Enable native tool calling for GPT-OSS (uses Harmony format instead of XML)
|
||||||
|
useNativeToolCalling: true,
|
||||||
|
// Enable streaming for real-time progress visibility
|
||||||
|
onToken: (token, source) => {
|
||||||
|
if (source === 'driver') {
|
||||||
|
process.stdout.write(token);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
// Enable progress events to see tool calls
|
||||||
|
onProgress: (event: { logMessage: string }) => {
|
||||||
|
console.log(event.logMessage);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Register JsonValidatorTool for self-validation
|
||||||
|
orchestrator.registerTool(new JsonValidatorTool());
|
||||||
|
|
||||||
|
console.log(' [SmartAgent] Starting orchestrator...');
|
||||||
|
await orchestrator.start();
|
||||||
|
console.log(' [SmartAgent] Ready for extraction');
|
||||||
});
|
});
|
||||||
|
|
||||||
let passedCount = 0;
|
let passedCount = 0;
|
||||||
@@ -624,7 +731,7 @@ for (const tc of testCases) {
|
|||||||
|
|
||||||
const startTime = Date.now();
|
const startTime = Date.now();
|
||||||
|
|
||||||
const mdPath = path.join(TEMP_MD_DIR, `${tc.name}.md`);
|
const mdPath = path.join(MD_CACHE_DIR, `${tc.name}.md`);
|
||||||
if (!fs.existsSync(mdPath)) {
|
if (!fs.existsSync(mdPath)) {
|
||||||
throw new Error(`Markdown not found: ${mdPath}. Run Stage 1 first.`);
|
throw new Error(`Markdown not found: ${mdPath}. Run Stage 1 first.`);
|
||||||
}
|
}
|
||||||
@@ -636,7 +743,7 @@ for (const tc of testCases) {
|
|||||||
const elapsedMs = Date.now() - startTime;
|
const elapsedMs = Date.now() - startTime;
|
||||||
processingTimes.push(elapsedMs);
|
processingTimes.push(elapsedMs);
|
||||||
|
|
||||||
console.log(` Extracted: ${extracted.invoice_number} | ${extracted.invoice_date} | ${extracted.total_amount} ${extracted.currency}`);
|
console.log(` Extracted: ${extracted.invoice_number} | ${extracted.invoice_date} | ${extracted.total_amount} ${extracted.currency} | ${extracted.line_items.length} items`);
|
||||||
|
|
||||||
const result = compareInvoice(extracted, expected);
|
const result = compareInvoice(extracted, expected);
|
||||||
|
|
||||||
@@ -654,6 +761,14 @@ for (const tc of testCases) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
tap.test('Summary', async () => {
|
tap.test('Summary', async () => {
|
||||||
|
// Cleanup orchestrator and SmartAi
|
||||||
|
if (orchestrator) {
|
||||||
|
console.log('\n [SmartAgent] Stopping orchestrator...');
|
||||||
|
await orchestrator.stop();
|
||||||
|
}
|
||||||
|
console.log(' [SmartAgent] Stopping SmartAi...');
|
||||||
|
await smartAi.stop();
|
||||||
|
|
||||||
const totalInvoices = testCases.length;
|
const totalInvoices = testCases.length;
|
||||||
const accuracy = totalInvoices > 0 ? (passedCount / totalInvoices) * 100 : 0;
|
const accuracy = totalInvoices > 0 ? (passedCount / totalInvoices) * 100 : 0;
|
||||||
const totalTimeMs = processingTimes.reduce((a, b) => a + b, 0);
|
const totalTimeMs = processingTimes.reduce((a, b) => a + b, 0);
|
||||||
@@ -663,7 +778,7 @@ tap.test('Summary', async () => {
|
|||||||
console.log(` Invoice Summary (Nanonets + GPT-OSS 20B)`);
|
console.log(` Invoice Summary (Nanonets + GPT-OSS 20B)`);
|
||||||
console.log(`========================================`);
|
console.log(`========================================`);
|
||||||
console.log(` Stage 1: Nanonets-OCR-s (doc -> md)`);
|
console.log(` Stage 1: Nanonets-OCR-s (doc -> md)`);
|
||||||
console.log(` Stage 2: GPT-OSS 20B (md -> JSON)`);
|
console.log(` Stage 2: GPT-OSS 20B + SmartAgent (md -> JSON)`);
|
||||||
console.log(` Passed: ${passedCount}/${totalInvoices}`);
|
console.log(` Passed: ${passedCount}/${totalInvoices}`);
|
||||||
console.log(` Failed: ${failedCount}/${totalInvoices}`);
|
console.log(` Failed: ${failedCount}/${totalInvoices}`);
|
||||||
console.log(` Accuracy: ${accuracy.toFixed(1)}%`);
|
console.log(` Accuracy: ${accuracy.toFixed(1)}%`);
|
||||||
@@ -671,14 +786,7 @@ tap.test('Summary', async () => {
|
|||||||
console.log(` Total time: ${(totalTimeMs / 1000).toFixed(1)}s`);
|
console.log(` Total time: ${(totalTimeMs / 1000).toFixed(1)}s`);
|
||||||
console.log(` Avg per inv: ${avgTimeSec.toFixed(1)}s`);
|
console.log(` Avg per inv: ${avgTimeSec.toFixed(1)}s`);
|
||||||
console.log(`========================================\n`);
|
console.log(`========================================\n`);
|
||||||
|
console.log(` Cache location: ${MD_CACHE_DIR}\n`);
|
||||||
// Cleanup temp files
|
|
||||||
try {
|
|
||||||
fs.rmSync(TEMP_MD_DIR, { recursive: true, force: true });
|
|
||||||
console.log(` Cleaned up temp directory: ${TEMP_MD_DIR}\n`);
|
|
||||||
} catch {
|
|
||||||
// Ignore
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
export default tap.start();
|
export default tap.start();
|
||||||
|
|||||||
Reference in New Issue
Block a user