generated from john/python-template
2.5 KiB
2.5 KiB
AI Coding Assistant Project Briefing & Context
Project Mission
This application is a family history archival and transcription platform. Its primary goal is to accept scanned document images (letters, postcards, logbooks, diaries), execute OCR and structured transcription via AI vision models (OpenAI GPT-4o, Anthropic Claude 3.5 Sonnet), and manage historical metadata (authors, recipients, dates, and locations).
Technical Stack & Architecture
- Database: PostgreSQL 13+ with native
UUID(gen_random_uuid()) andJSONBcolumns. - Backend Runtime / Concurrency: Python utilizing
asynciofor concurrent HTTP API calls to AI providers, with strict rate-limiting viaasyncio.Semaphore. - Validation & Types: Python with Pydantic model definitions. Incoming AI responses must be parsed and validated with Pydantic models before database insertion.
- ORM / Database Access: SQLModel and SQLAlchemy, using parameterized statements and PostgreSQL-native types.
Core System Directives for AI Code Generation
1. Data Immutability vs. Human Corrections
job_source.raw_transcriptionandsource.raw_transcriptionrepresent original, point-in-time machine outputs and are immutable.- Human corrections occur on
source.revised_text. - When fetching text for the UI, always display
COALESCE(source.revised_text, source.raw_transcription).
2. Async Execution & Batching Rules
- A
jobrepresents an overarching execution run for a folder/group of images belonging to a singledocument. - Images are submitted to AI APIs one at a time in rapid succession using
asyncioworker pools. - Each single-image API call populates a row in
job_sourcewith its ownstatus,raw_transcription,ai_metadata, andraw_api_response. - If 9 of 10 pages succeed and 1 fails,
job_source.statusfor the failed image becomes'failed', whilejob.statusbecomes'partial_success'. Do not mark the entire batch as failed if partial results exist.
3. Entity Relationships
- Authors/Recipients: A
documentcan have multiple authors and recipients. Do NOT put directauthor_idforeign keys ondocument. Query authors/recipients viadocument_personwhererole = 'author'orrole = 'recipient'. - Page Ordering: Multi-page documents must always be queried using
ORDER BY page_number ASC.
4. Database Mutations
- Always use parameterized SQL queries (
$1,$2) to prevent SQL injection. - Store datetimes using UTC ISO 8601 strings or native PostgreSQL
TIMESTAMPTZ.