Sign inSign up

amankhan1009/daily-hadith-ai-agent

By amankhan1009

β€’Updated about 9 hours ago

Production-grade fail-closed AI agent delivering verified daily Hadith notifications via Telegram.

Image
0

1.4K

amankhan1009/daily-hadith-ai-agent repository overview

⁠Daily Hadith AI Agent

A production-grade, fail-closed AI system and publicly accessible Telegram bot delivering verified daily Hadith notifications to subscribers, orchestrated with LangGraph, verified deterministically with Pydantic, persisted on Neon PostgreSQL, and powered by Groq.

πŸ“š Deep Dive Architectural Guide: For an exhaustive, step-by-step technical explanation, interview cheatsheet, and full system diagrams, read docs/PROJECT_WORKFLOW.md⁠.

CI/CD Pipeline Docker Image Python 3.14 LangGraph PostgreSQL License: MIT Tests Passing Coverage Code Style: Ruff


⁠Core Problem & Engineering Philosophy

Generative AI models are probabilistic by natureβ€”they hallucinate, paraphrase without permission, and confabulate citations. When building software around sacred or canonical domain texts (such as Hadith literature, legal statutes, or clinical guidelines), probabilistic generation without deterministic boundaries is unacceptable.

A naive implementation might ask an LLM to "send a daily Hadith from Sahih al-Bukhari". This frequently results in invented chains of narration (Isnad), paraphrased translations, or fabricated references.

Daily Hadith AI Agent solves this through architectural rigor:

  • Canonical texts are never generated or modified by AI. The English translation is retrieved directly from an approved primary source (HadeethEnc⁠) via its official API and preserved with byte-for-byte fidelity.
  • Deterministic verification gates run before any LLM is invoked. If references, metadata, or English/Arabic cross-matching fail, the pipeline terminates immediately (fail-closed).
  • Generative AI is strictly isolated to supplementary educational reflections, clearly demarcated as non-canonical commentary.

⁠System Architecture

flowchart TD
    subgraph Ingestion ["1. Deterministic Ingestion Pipeline"]
        API["HadeethEnc API (GET)"] --> Fetch["Candidate Ingestion (EN + AR)"]
        Fetch --> Match["Metadata Cross-Match & Provenance"]
        Match --> Gate{"Passes Deterministic Gate?"}
        Gate -- "No" --> Abort["Persist Audit Log & Abort"]
        Gate -- "Yes" --> DB[("Neon PostgreSQL Candidate Queue")]
    end

    subgraph Webhook ["2. Inbound Webhook & Multi-User Router"]
        Users["Public Telegram Users"] -->|"/start, /today, /stop, /help"| WH["FastAPI POST /telegram/webhook"]
        WH --> Sec{"Verify Secret Token"}
        Sec -- "Invalid" --> Reject["403 Forbidden"]
        Sec -- "Valid" --> Router["Command Router"]
        Router -->|"/start (Idempotent)"| SubDB[("Telegram Subscribers")]
        Router -->|"/today"| InstantSend["Dispatch Today's Hadith"]
        Router -->|"/stop"| Deact["Deactivate (is_active = false)"]
    end

    subgraph Daily ["3. Daily Orchestration & Fan-Out Broadcast"]
        Sched["Daily Trigger: 10:00 AM IST"] --> LG["LangGraph Daily StateGraph"]
        LG --> Idemp{"Idempotency Check"}
        Idemp -- "Already Sent" --> Skip["Skip Execution"]
        Idemp -- "Fresh Run" --> LLM["Groq LLM Reflection (Segregated)"]
        LLM --> Guard{"Guardrails AI Gate"}
        Guard -- "Violation" --> Fallback["Omit Reflection (Fail-Safe)"]
        Guard -- "Valid" --> Format["Format Canonical Notification"]
        Fallback --> Format
        Format --> Broadcaster["Multi-User Fan-Out Broadcaster"]
        SubDB -->|"Active Subscribers"| Broadcaster
        Broadcaster -->|"30 msg/sec Throttling"| TG["Telegram Bot API"]
        TG -->|"403 Blocked"| AutoDeact["Auto-Deactivate Blocked User"]
        TG -->|"Delivered"| Record["Record Per-Subscriber Delivery Log"]
    end

⁠Repository Structure & Package Guides

Every folder in this codebase includes its own dedicated README guide:

DirectoryPackage / ComponentDocumentation Guide
docs/⁠Technical Specifications & Handover Recordsdocs/README.md⁠
src/⁠Application Source Root & Architecture Overviewsrc/README.md⁠
src/database/⁠Neon PostgreSQL Persistence, Models & Migrationssrc/database/README.md⁠
src/hadeethenc/⁠HadeethEnc API Client, Schemas & Invariant Verifiersrc/hadeethenc/README.md⁠
src/ingestion/⁠Candidate Ingestion Service & Deduplicationsrc/ingestion/README.md⁠
src/workflow/⁠LangGraph StateGraph Orchestration & Routingsrc/workflow/README.md⁠
src/llm/⁠Groq LLM Reflections, Prompts & Graceful Fallbacksrc/llm/README.md⁠
src/guardrails/⁠Deterministic Hallucination & Theological Guardrailssrc/guardrails/README.md⁠
src/formatter/⁠Telegram Notification Formatter & HTML Payload Assemblysrc/formatter/README.md⁠
src/delivery/⁠Telegram Bot API Client, Webhook Router & Multi-User Broadcastersrc/delivery/README.md⁠
tests/⁠Automated Test Suite (99 unit & integration tests across 13 modules)tests/README.md⁠
scripts/⁠Standalone Operational & Live E2E Verification Scriptsscripts/README.md⁠
alembic/⁠Database Schema Migration Versionsalembic/README.md⁠

⁠Key Reliability Principles

  1. Deterministic Content Gates: Every candidate record undergoes Pydantic schema validation, reference consistency checks, and English/Arabic pair matching.
  2. Fail-Closed Safety: If external APIs are unresponsive, references are ambiguous, or metadata is incomplete, the system rejects the candidate rather than dispatching unverified religious material.
  3. Exact Source Attribution: Every notification includes the authentic collection title, verified reference number, canonical HadeethEnc URL, and translation-version metadata.
  4. Physical Idempotency: Neon PostgreSQL enforces a UNIQUE constraint on DailyDeliveryLog.scheduled_date, ensuring at most one delivery per calendar day.
  5. Clear AI Separation: Any LLM-generated reflection is explicitly labeled: AI-generated reflection β€” not part of the original Hadith.
  6. No Web Scraping: Only official REST API endpoints are utilized.

⁠Project Status & Roadmap

The project is actively developed following a 20-milestone engineering roadmap:

MilestoneStatusKey Deliverable
01 β€” Project FoundationVerifiedGit repo, environment audit, documentation scaffolding
02 β€” Source Validation SpecVerifiedHadeethEnc API contracts & Arabic reference regex grammar
03 β€” Configuration & SecretsVerifiedPydantic Settings, secret masking, timezone validation
04 β€” Database FoundationVerifiedNeon PostgreSQL connection, SQLAlchemy models, Alembic migrations
05 β€” HadeethEnc IntegrationVerifiedTyped HTTP client, Pydantic schemas, invariant verifier
06 β€” Source Catalogue AuditVerifiedEmpirical audit of 2,776+ Hadiths, acceptance policy
07 β€” Deterministic EngineVerifiedIngestion service, queue persistence, live cloud verification
08 β€” LangGraph WorkflowVerifiedStateGraph state machine with conditional routing & live idempotency
09 β€” Groq IntegrationVerifiedLLM reflection generation with strict segregation
10 β€” Guardrails AIVerifiedContent validation & hallucination boundary enforcement
11 β€” Final Content ValidationVerifiedAssembled notification payloads & HTML escaping checks
12 β€” Telegram Integration & Public Subscription EngineVerifiedPublic bot webhook, subscriber registration, commands & rate-limited broadcast
13 β€” Scheduling & Daily Multi-User BroadcastVerifiedHeadless runner CLI, APScheduler (10 AM IST, 1hr misfire grace), Cloud Scheduler cron endpoint
14 β€” History & Per-Subscriber Daily IdempotencyVerifiedComposite idempotency unique constraints, mid-run recovery queries & per-user delivery logs
15 β€” Failure HandlingVerifiedFault tolerance, retry exponential backoff & fail-closed resilience stress tests (19 tests)
16 β€” Automated Testing & Code QualityVerifiedAutomated test suite (99 tests, 89% coverage), Ruff linting & PEP 8 code formatting
17 β€” Docker Containerization & GitHub Actions CI/CDVerifiedMulti-stage Docker containerization (non-root appuser, ~160MB), automated test-gated CI/CD publishing to Docker Hub
18 β€” Cloud Deployment & Public Production BotVerifiedDeployed on Render Free Web Service with automated daily 10:00 AM IST scheduler on cron-job.org and live Telegram subscribers
19 β€” Production Readiness & System AuditVerified20/20 Definition of Done verified, production runbook, and defensive security audit

For the complete 20-milestone breakdown, see docs/PROJECT_ROADMAP.md⁠.


⁠Local Development Setup

⁠Prerequisites
  • Python 3.14 or 3.13
  • Git
⁠Installation
# Clone the repository
git clone https://github.com/Amankhan1009/daily-hadith-ai-agent.git
cd daily-hadith-ai-agent

# Create and activate virtual environment
python3 -m venv .venv
source .venv/bin/activate

# Install dependencies
pip install -r requirements.txt
⁠Running the Test Suite
.venv/bin/pytest -v

⁠License & Attribution

  • Source Code: MIT License.
  • Hadith Content: Retrieved from HadeethEnc⁠ and attributed under their open non-commercial educational reuse guidelines.

Tag summary

Content type

Image

Digest

sha256:fa549b7b3…

Size

99.3 MB

Last updated

about 9 hours ago

docker pull amankhan1009/daily-hadith-ai-agent