Skip to content

About

Automated Structured Content Generator

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

πŸš€ Automated Structured Content Generator (ASCGenerator)

Python 3.11+ FastAPI Pydantic V2 Poetry Ruff Mypy strict Docker Multi-Stage Next.js

Short Executive Summary: Enterprise-grade production microservice that ingests noisy, unstructured news payloads (raw text, HTML, RSS feeds) and transforms them into strictly validated, typed JSON records with zero runtime schema drift.


🎯 Key Features & Specifications

  • ⚑ Zero-Setup Onboarding: Onboard & run in < 5 minutes (make install ──> ready to develop).
  • πŸ”’ Type Safety & Quality Gates: 100% Mypy --strict coverage and Ruff linting enforced across all microservice modules.
  • πŸ›‘οΈ Defensive Security & Guardrails: Prompt injection defenses, XML tag isolation (<untrusted_news_payload>), and developer authority enforcement.
  • 🧱 Pydantic V2 Validation Firewall: Strict contract validation, bounded ranges (e.g., confidence_score $\in [0.0, 1.0]$), and structured enum taxonomies.
  • ⚑ Two-Tiered Fault Tolerance: Decorated Tenacity retry policy (exponential backoff on HTTP 429/5xx) paired with Instructor schema self-correction feedback loops.
  • πŸ“Š Integrated Monitoring Dashboard: Next.js frontend interface providing real-time schema inspection, batch execution testing, and metrics visualization.
  • 🐳 Hardened Containerization: Multi-stage non-root Docker build (python:3.11-slim) ready for production container orchestrators.

πŸ—οΈ System Architecture Flow

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                                 INGESTION LAYER                                  β”‚
β”‚                 FastAPI Microservice (POST /v1/extract, /v1/extract/batch)       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                         β”‚
                                         β–Ό
                               β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                               β”‚  Config & Schemas β”‚
                               β”‚ (config.py,       β”‚
                               β”‚  schemas.py)      β”‚
                               β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                         β”‚
                  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                  β”‚                      β”‚                      β”‚
                  β–Ό                      β–Ό                      β–Ό
        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β”‚ Defensive Prompt β”‚   β”‚ Deduplication    β”‚   β”‚ Structlog JSON   β”‚
        β”‚ Isolation Layer  β”‚   β”‚ Cache (SHA256)   β”‚   β”‚ FinOps Logger    β”‚
        β”‚ (security.py)    β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚ (logger.py)      β”‚
        β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜            β”‚              β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                 β”‚                      β”‚ Hit                   β”‚
                 β”‚                      β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
                 β–Ό                      β”‚ Miss                  β”‚
        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β”‚          LLM ENGINE & TWO-TIERED RESILIENCE           β”‚
        β”‚                  (engine.ExtractionEngine)            β”‚
        β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
        β”‚  β”‚ Tier 1: Tenacity Exponential Backoff Retry       β”‚  β”‚
        β”‚  β”‚         (Network, Rate Limits 429, 5xx)         β”‚  β”‚
        β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
        β”‚                           β”‚                           β”‚
        β”‚                           β–Ό                           β”‚
        β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
        β”‚  β”‚ Instructor SDK Constrained LLM Inference        β”‚  β”‚
        β”‚  β”‚ (AsyncOpenAI / Anthropic / Mistral, temp=0.0)   β”‚  β”‚
        β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
        β”‚                           β”‚ Raw Output                β”‚
        β”‚                           β–Ό                           β”‚
        β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
        β”‚  β”‚ Tier 2: Pydantic V2 Validation Firewall &       β”‚  β”‚
        β”‚  β”‚         Schema Self-Correction Loop (Max 2)     β”‚  β”‚
        β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                    β”‚ Validated Record
                                    β–Ό
                       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                       β”‚ ArticleExtractionRecord β”‚
                       β”‚ (JSON Response / DB)    β”‚
                       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“‚ Repository Structure

.
β”œβ”€β”€ docs/                       # Architecture & technical documentation
β”‚   β”œβ”€β”€ architecture_en.md      # Software architecture specification & invariants
β”‚   β”œβ”€β”€ specifications_en.md    # Product requirements & data model specification
β”‚   β”œβ”€β”€ code_en.md              # Source code architecture reference
β”‚   β”œβ”€β”€ glossary_en.md          # Domain & technical terminology glossary
β”‚   └── code_fr.md / glossary_fr.md # French documentation variants
β”‚
β”œβ”€β”€ src/                        # Microservice source code
β”‚   β”œβ”€β”€ main.py                 # FastAPI application setup, routes & exception handlers
β”‚   β”œβ”€β”€ config.py               # Central configuration (Pydantic BaseSettings)
β”‚   β”œβ”€β”€ engine.py               # Extraction engine, Instructor integration & retries
β”‚   β”œβ”€β”€ schemas.py              # Pydantic V2 schemas & API DTOs
β”‚   β”œβ”€β”€ security.py             # Prompt isolation & XML sanitization logic
β”‚   └── logger.py               # Structlog JSON logging configuration
β”‚
β”œβ”€β”€ dashboard/                  # Monitoring & testing frontend (Next.js 14)
β”‚   β”œβ”€β”€ src/                    # UI components & API bindings
β”‚   └── package.json            # Dashboard dependencies
β”‚
β”œβ”€β”€ tests/                      # Automated test suite
β”‚   β”œβ”€β”€ conftest.py             # Pytest fixtures & mock clients
β”‚   β”œβ”€β”€ test_schemas.py         # Schema field validation unit tests
β”‚   β”œβ”€β”€ test_engine.py          # Extraction engine & retry integration tests
β”‚   └── test_security.py        # Defensive security & prompt injection suite
β”‚
β”œβ”€β”€ data/                       # Validation datasets
β”‚   └── dataset_100_articles.jsonl # 100 sample article extraction benchmark
β”‚
β”œβ”€β”€ Dockerfile                  # Multi-stage non-root container configuration
β”œβ”€β”€ docker-compose.yml          # Local microservice orchestration
β”œβ”€β”€ Makefile                    # Unified command interface
β”œβ”€β”€ pyproject.toml              # Poetry dependencies, Mypy strict & Ruff rules
└── README.md                   # Project overview & quickstart guide

πŸš€ Quickstart Guide

1. Installation & Environment Setup

# Clone repository and enter directory
cd projets/4_ASCGenerator

# Automatic onboarding (installs dependencies via Poetry)
make install

2. Configure Environment Variables

Copy .env.example to .env and set your LLM API keys.

Option A: Use OpenAI (default)

LLM_PROVIDER=openai
OPENAI_API_KEY=sk-...
DEFAULT_MODEL=gpt-4o-mini
LOG_LEVEL=INFO

Option B: Use Google Gemini

LLM_PROVIDER=google
GOOGLE_API_KEY=your_google_gemini_api_key_here
DEFAULT_MODEL=gemini-2.0-flash
LOG_LEVEL=INFO

πŸ’‘ Where to change the model: Set the DEFAULT_MODEL environment variable in your .env file.

  • OpenAI models: gpt-4o-mini, gpt-4o, gpt-4-turbo, etc.
  • Google Gemini models: gemini-2.0-flash, gemini-2.5-pro, gemini-1.5-pro, etc.

You can also override the model at runtime by passing model to ExtractionEngine(model="...").

πŸ”‘ Get a Gemini API key: Visit https://aistudio.google.com/app/apikey to create one.

3. Run Quality & Type Checks

make lint

4. Run Automated Test Suite

make test

5. Start the FastAPI Service

make dev

Open API documentation at http://localhost:8000/docs.

6. Launch the Next.js Monitoring Dashboard

make dashboard

Access the interactive dashboard at http://localhost:3000.

7. Build & Run via Docker

# Build container image
make docker-build

# Run microservice container
make docker-run

πŸ“Š Quality & FinOps Benchmarks

Metric Target / Measured Status
Mypy Strict Coverage 100% βœ… Passing
Schema Compliance Rate 100% (Zero Schema Drift) βœ… Passing
Prompt Injection Resilience 100% Pass Rate βœ… Passing
Network Retry Recovery (Tier 1) Up to 3 attempts (Tenacity backoff) βœ… Passing
Schema Repair Rate (Tier 2) Max 2 correction attempts βœ… Passing
Docker Container Size < 250 MB βœ… Passing

πŸ“„ License & Contributing

Distributed under the MIT License. See LICENSE for more information.

About

Automated Structured Content Generator

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages