3661831221
- Add .agents/skills/review/SKILL.md with a required review checklist covering project rules, model client rules, pipeline rules, XML rules, and test rules. - Update AGENTS.md with a dedicated 'Review Stage Rules' section that makes review mandatory and maps confidence thresholds to XML generation behavior. - Add review skill to the skills reference. - Add .ruff_cache/ to .gitignore and keep agent XML templates out of the '*.xml' ignore rule.
5.5 KiB
5.5 KiB
AGENTS.md — Odoo OCR Project
This file provides context and rules for AI agents working on odoo_ocr, a local-first invoice OCR pipeline that reads scanned, printed, handwritten, and native-PDF invoices and produces Odoo Enterprise-ready XML.
Project Goal
Build a Python application that:
- Classifies incoming invoice files by type (native PDF, scanned print, handwritten, mixed image).
- Routes each file to a specialized processing branch.
- Runs OCR using local vision-language models (Ollama or llama.cpp).
- Extracts a structured invoice representation.
- Reviews extracted data against the original image using a second vision model.
- Emits valid Odoo XML for import into Odoo Enterprise.
Tech Stack
- Language: Python 3.11+
- Dependency management:
pyproject.toml(PEP 621); useuvorpip. - Core libraries:
pydanticv2 for all data schemas and settings.pymupdfandpdf2imagefor PDF ingestion.Pillowandopencv-python-headlessfor image preprocessing.httpxfor HTTP model clients.lxmlfor XML generation and validation.pytestandpytest-asynciofor tests.
- Local inference:
- Primary runtime: Ollama for fast iteration.
- Optimized/runtime path: llama.cpp server (custom GGUF quants).
- Models:
- Document classifier:
Qwen2.5-VL-3Bor heuristics. - OCR / extraction:
Qwen2.5-VL-7BorGLM-OCR(preferred for handwriting). - Review / validation:
Qwen2.5-VL-7Bor larger (72Bif available).
- Document classifier:
Architecture
Input File
→ Classifier (heuristic + small VLM)
→ Branch: digital_pdf → text/layout extraction
→ Branch: scanned_print → preprocess → VLM OCR
→ Branch: handwritten → preprocess → GLM-OCR / handwriting OCR
→ Branch: mixed_unknown → preprocess → ensemble OCR
→ Structured Extraction VLM
→ Review VLM (image vs extracted JSON)
→ Confidence check
→ XML Builder
→ Odoo XML + sidecar JSON
Code Conventions
- Project layout: keep application code under
src/odoo_ocr/. - Schemas first: define Pydantic models before writing business logic.
- Type hints: use
typingeverywhere; runmypyin strict mode where practical. - Error handling: never swallow exceptions; return structured
Resultobjects or raise domain exceptions. - Configuration: use
pydantic-settingswithconfig.yamland env var overrides. - Logging: use Python standard
logging; log every model call latency and token usage. - No hardcoded secrets: model endpoints, credentials, and paths come from settings.
- Tests: every module must have tests under
tests/. Use fixtures fromtests/fixtures/.
Model Client Rules
- Support both Ollama and llama.cpp with a unified interface (
BaseVLMClient). - Always emit JSON from VLMs when doing extraction/review. Use constrained prompts, not regex scraping.
- Retry on transient failures with exponential backoff.
- Cache model responses by content hash to avoid re-running expensive inference during development.
- Record per-call metrics (model name, tokens, latency, prompt hash).
Data Flow Rules
- Every invoice must pass through the review stage before XML generation. The review model compares the original image with the extracted JSON and produces a
ReviewResult. - Every invoice must produce:
- A Pydantic
ExtractedInvoiceobject. - A review result (
ReviewResult) with confidence score and issue list. - An Odoo XML file (unless blocked by low confidence).
- A sidecar JSON file with metadata, timings, and confidence.
- A Pydantic
- If review confidence is below the configured threshold, mark the invoice for human review and do not generate final XML (or generate a draft with a warning flag).
- Never send invoice data outside the local model endpoints.
Review Stage Rules
- The review stage is mandatory, not optional.
- The review VLM must receive both the original invoice image and the extracted
ExtractedInvoiceJSON. - The review prompt asks the model to verify field presence, arithmetic, and consistency.
- The
ReviewResultmust include a confidence score between 0.0 and 1.0 and a list of issues. - XML generation must respect the confidence thresholds in
config.yaml:confidence >= 0.90: generate final XML.0.75 <= confidence < 0.90: generate XML with a human-review flag.confidence < 0.75: do not generate final XML; emit sidecar JSON only.
- See skill
reviewfor the full review checklist.
Odoo XML Target
Generate Odoo data-import XML compatible with Odoo Enterprise vendor bills:
res.partner(vendor)account.move(vendor bill header)account.move.line(invoice lines)account.taxreferences by percentage/name
See skill odoo-xml-import for detailed field mapping and a sample XML template.
When to Ask the User
Ask for clarification when:
- The requested change would alter the model stack.
- The change affects the Odoo target schema or import method.
- You are unsure whether a file should be committed or a dependency added.
- A requested feature conflicts with the local-only / privacy constraint.
Skills Reference
Agents should load the relevant project skills from .agents/skills/:
odoo-ocr-pipeline— when implementing classifier, branches, OCR, extraction, or review.odoo-xml-import— when generating or validating Odoo XML.local-vlm-client— when writing model clients or prompts.review— when reviewing code, tests, or design decisions before approving changes.