Header image for lds

lds

Prompt

You are an expert Principal Full-Stack Engineer and AI Systems Architect. Your task is to build the complete "AI Legal Document Intelligence Platform" (Fact Summarization & Case Workspace with Citation Grounding) based on the provided project specification documents (SRS v2.0, PRD v2.0, and Development Plan v2.0). --- ### Core Architecture & Tech Stack Rules You must strictly follow the tech stack outlined in the project specifications: * **Backend:** Python with FastAPI (async support for pipeline jobs). * **Database & Migrations:** PostgreSQL with SQLAlchemy and Alembic. * **Vector & Hybrid Search:** Chroma (migratable to Qdrant/Milvus later) for vector embeddings, and `rank_bm25` (or Elasticsearch) for keyword search. * **Orchestration & Processing:** LangChain, Celery + Redis for background job orchestration. * **Document Parsing:** PyMuPDF (`fitz`) and `python-docx` with strict page-number preservation. * **Frontend:** React (Next.js) with Tailwind CSS matching professional design tokens. * **Authentication:** JWT-based sessions with 30-minute inactivity expiry, bcrypt password hashing, rate limiting on login (5 attempts / 15 min), and support for SAML/OIDC SSO in Enterprise stages. --- ### Non-Negotiable Core Principles & Business Rules 1. **Grounding & Trust First (Fail-Safe Architecture):** - No fact may be presented to the user without an associated citation (document + page + chunk reference). - **Citation Verification is a Hard Gate:** Every generated fact must be programmatically verified against its cited source chunk. If verification fails or throws an error, it **must** default to "Unverified" (fail-safe). Never display an unverified fact as confirmed. - Grounding constraint: The LLM prompt must be constrained ("cite or omit") so it never draws on general world knowledge outside retrieved content. 2. **Multi-Tenancy & Permissions:** - Workspace/org data isolation must be designed in from Day 1. Enforce strict role-based access control (Viewer, Editor, Owner, Org Admin). Unauthorized access attempts must return an HTTP `403`. - Org Admins have **zero default access** to document content unless an explicitly logged, time-boxed elevated support workflow is invoked. 3. **Execution Methodology:** - Follow the **Stage Roadmap** sequentially (Stage 1: Core Single-Document Engine -> Stage 2: Multi-Document Workspaces & Collaboration -> Stage 3: Enterprise Readiness -> Stage 4: Knowledge Reuse & Platform). Do not introduce multi-document or collaboration features until Stage 1's trust guarantees and citation accuracy (≄95%) are fully addressed. --- ### Instructions for Code Generation & Implementation Please begin by generating the complete foundational setup for **Stage 1 (Core Document Intelligence)**. Structure your output clearly with the following deliverables: #### 1. Repository Structure & Docker Setup (`Phase 1.0`) * Provide the complete directory tree (`backend/`, `frontend/`, `infra/`). * Provide a production-ready `docker-compose.yml` orchestrating FastAPI, PostgreSQL, Redis, and Chroma. #### 2. Database Schema & Models (`Phase 1.0 & 1.1`) * Write SQLAlchemy models and Alembic migration scripts for: * `User` (email, hashed password using bcrypt, role, created_at). * `Document` (id, filename, upload_date, page_count, status enum: `uploaded`, `parsing`, `chunked`, `summarized`, `failed`). * `Chunk` (id, document_id, page_number, section_ref, text, sequence_index, embedding vector). * `Fact` & `Summary` tables linking generated claims to precise chunk IDs and persistence verification statuses (`verified` vs `unverified`). #### 3. Authentication & Security Layer (`Phase 1.1`) * Implement FastAPI auth endpoints (`/register`, `/login`) with bcrypt password hashing and strict input validation rules (passwords $\ge$ 10 chars, 1 number, 1 special character). * Implement login rate-limiting (lockout after 5 failed attempts within 15 minutes). * Write JWT session middleware with strict user scoping and a 403-forbidden handler for cross-user access attempts. #### 4. Document Parsing & Pipeline Stubs (`Phase 1.2 & 1.3`) * Implement the file upload endpoint with strict validations: * Restrict file formats to `.pdf` and `.docx` (enforce MIME-type/content sniffing). * Reject files $> 50$MB with message: *"File exceeds maximum size of 50MB."* * Reject files $> 100$ pages with message: *"Document exceeds maximum of 100 pages."* * Implement PyMuPDF/python-docx parsing logic ensuring page numbers and section structures are accurately preserved. * Set up structure-aware chunking and hybrid retrieval (combining vector embeddings via Chroma and keyword search via `rank_bm25`). #### 5. Grounded Summarization & Fail-Safe Verification Engine (`Phase 1.4 & 1.5`) * Write the LLM prompt template enforcing strict citation grounding ("cite or omit"). * Implement the **Verification Service**: a programmatic check comparing the generated fact against the cited chunk text. Ensure that any error or mismatch defaults safely to an `"Unverified"` label (`fail-safe` protocol). --- ### Response Format Requirements * Provide production-ready, clean, well-commented code blocks. * Include required environment variables (`.env.example`) and setup instructions. * Strictly adhere to the exact error message strings specified in the SRS for validation failures. Let's begin by building **Stage 1 (Phases 1.0 to 1.3)**. Generate the repository scaffolding, database configuration, authentication module, and document upload/parsing pipeline now.

Drag to resize
Drag to resize