Skip to content

Latest commit

 

History

25 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🧠 Smart-Educator — Unified Architecture & Developer Guide

Production-Ready, Streamlined Educational Assessment & Adaptive Learning Engine
Project Version: v0.1.0 (Streamlined Production)
Architecture: FastAPI (Async) + LangGraph + Google Gemini + PostgreSQL + Redis + ChromaDB


📋 Table of Contents

  1. Project Overview & Core Capabilities
  2. Architectural Overview & Microservices
  3. End-to-End System & Data Flow
  4. Complete API Surface & Endpoint Reference
  5. Database Schema & Migrations (Alembic)
  6. Caching & Vector DB Infrastructure
  7. The 4 Backend Optimization Pillars
  8. Clean Codebase Directory Sitemap
  9. Step-by-Step Setup & How-to-Run Guide
  10. Automated Testing & Health Probes

🚀 1. Project Overview & Core Capabilities

Smart-Educator is an automated, AI-driven educational platform designed to turn raw educational documents (PDFs, text passages, curricula) into structured, high-quality assessment datasets and provide an interactive adaptive learning loop.

Core Capabilities:

  1. Document Ingestion & Chunking: Streams and parses PDF/TXT documents asynchronously with overlapping character windows to preserve semantic context across chunk boundaries.
  2. Automated Learning Outcome (LO) Extraction: Uses Google Gemini to analyze context and extract explicit, verifiable Learning Outcomes (text, concept, source_evidence).
  3. Multi-Type Question Generation: Generates Multiple-Choice Questions (MCQs), True/False, and Short Answer questions aligned with specific LOs and Bloom's difficulty distributions.
  4. High-Dimensional Semantic Question-to-LO Linking: Uses 3072-dimensional Gemini vector embeddings and cosine similarity to score question-to-outcome alignment with confidence scores and reasoning.
  5. 8-Criteria LLM-as-Judge Evaluation: Scores generated questions across 8 strict criteria (grounding, clarity, answer correctness, explanation correctness, LO alignment, difficulty, validity, choices) and categorizes items as accepted, needs_review, or rejected.
  6. Interactive Web Studio & Adaptive Exam: Built-in browser studio with real-time pipeline visualizer, question inspector, and an adaptive quiz engine that diagnoses student weaknesses and pulls targeted practice questions from ChromaDB.
  7. Clean Dataset Export: Filters approved assessment data for downstream usage with one-click JSON export.
  8. Training Pair Construction & Fine-Tuning: Generates positive, negative, and Jaccard-based hard-negative pairs to fine-tune embedding models.
  9. Vector Similarity Recommendation: Uses ChromaDB and Gemini embeddings to perform semantic search and recommend relevant questions for user queries.

🏗️ 2. Architectural Overview & Microservices

The system is organized into decoupled layers running on an async event loop:

                                    ┌────────────────────────────────────────────────────────┐
                                    │               FastAPI + Web Studio UI Layer            │
                                    └───────────────────────────┬────────────────────────────┘
                                                                │
                      ┌─────────────────────────────────────────┴─────────────────────────────────────────┐
                      ▼                                                                                   ▼
┌───────────────────────────────────────────┐                               ┌───────────────────────────────────────────┐
│     API, File Ingestion & CRUD Layer      │                               │      AI Pipeline (LangGraph & Gemini)     │
│  - FastAPI Routes & Request Schemas       │                               │  - 5-Node State Machine Pipeline          │
│  - PyMuPDF / Direct Text Chunking         │                               │  - Google Gemini Structured Output        │
│  - PostgreSQL Async Engine & Alembic      │                               │  - High-Dimensional Vector LO Linker      │
│  - Async CRUD Layer & Dataset Export      │                               │  - 8-Criteria LLM-as-Judge Evaluator      │
└─────────────────────┬─────────────────────┘                               └─────────────────────┬─────────────────────┘
                      │                                                                           │
                      └─────────────────────────────────────────┬─────────────────────────────────┘
                                                                ▼
                                    ┌────────────────────────────────────────────────────────┐
                                    │            Infrastructure, Vector DB & Cache           │
                                    │  - Docker Compose (Postgres, Redis, ChromaDB)          │
                                    │  - Redis Caching Layer (Idempotency & Embeddings)      │
                                    │  - VectorStoreService (ChromaDB + Gemini Embeddings)   │
                                    │  - Pair Generator & Offline Fine-Tuning Script         │
                                    └────────────────────────────────────────────────────────┘
Layer Primary Responsibilities Key Files / Modules
API & Data Endpoints (/health, /data/*, /dataset/export), File Ingestion, PostgreSQL ORM, Async CRUD routes/, crud/, models/, helpers/db.py, services/file_service.py
AI Pipeline LangGraph Orchestration, Gemini API Wrapper, LO Extraction, Question Generation, Semantic Linker, LLM-as-Judge Evaluator graph/graph.py, services/gemini_client.py, services/lo_extraction.py, services/question_generator.py, services/lo_linker.py, services/evaluator.py
Infra & Search Docker Stack, Redis Cache, ChromaDB Vector DB, Training Pair Generator, Embedding Services docker/docker-compose.yml, helpers/redis_client.py, helpers/hashing.py, services/embedding_service.py, services/vector_store.py, services/pair_generator.py

🔄 3. End-to-End System & Data Flow

🌊 1. Ingestion & Processing Flow (/api/v1/data/*)

  1. User uploads a PDF or TXT file to POST /api/v1/data/upload/{project_id}.
  2. file_service.py validates format and file size limit, generates a unique filename, and writes chunks asynchronously using aiofiles.
  3. User triggers POST /api/v1/data/process/{project_id}. file_service.py extracts text via PyMuPDF/UTF-8 and splits text into structured chunks with overlap.

⚡ 2. Generation & Evaluation Flow (POST /api/v1/dataset/generate)

flowchart TD
    A[EducationalContext Payload] --> B[Compute Context Hash: sha256 passage + config]
    B --> C{Check Redis Cache}
    C -->|Cache Hit| D[Return Cached JSON Instantly - 2ms]
    C -->|Cache Miss| E[LangGraph 5-Node State Machine]
    
    E --> E1[Node 1: parse_node]
    E1 --> E2[Node 2: extract_outcomes_node via Gemini]
    E2 --> E3[Node 3: generate_questions_node via Gemini]
    E3 --> E4[Node 4: link_outcomes_node via Gemini Embeddings]
    E4 --> E5[Node 5: evaluate_node via LLM-as-Judge]
    
    E5 --> F{Evaluate Batch Status}
    F -->|All Rejected| G[log_and_discard_node: Log & Skip DB Write]
    F -->|Accepted / Needs Review| H[store_node: Save LOs, Questions, Links, Evals to PostgreSQL]
    
    H --> H1[Auto-index Kept Questions into ChromaDB Vector Store]
    H1 --> I[Store Result JSON in Redis - TTL 24h]
    I --> J[Return Response Summary JSON]
Loading

🎯 3. Adaptive Testing & Vector Recommendation Flow

  1. Interactive Quiz (/app or #tab-quiz): Student answers generated/stored questions.
  2. Diagnostic Evaluation: System calculates score percentage and pinpoints failed Learning Outcomes.
  3. Adaptive Weakness Practice: Single-click query against ChromaDB retrieves targeted reinforcement questions matching the student's weak concepts.
  4. Pair Building (POST /training-pairs/build): Exports positive, negative, and hard-negative pairs to assets/training_pairs.jsonl.
  5. Semantic Search (POST /recommendations/questions): Converts user search query to a 3072-dim embedding, queries ChromaDB, and returns top $K$ matching questions.

🔌 4. Complete API Surface & Endpoint Reference

Method Path Summary Inputs Output
GET / or /app Web Studio User Interface None HTML Web Application
GET /api/v1/health High-level health status None {"status": "ok", "app": "smart-educator"}
GET /api/v1/health/live Process liveness probe None {"status": "alive"}
GET /api/v1/health/ready Deep readiness probe (Postgres, Redis, ChromaDB) None {"status": "ready", "dependencies": {...}}
POST /api/v1/data/upload/{project_id} Upload passage file file: UploadFile {"signal": "...", "file_id": "..."}
POST /api/v1/data/process/{project_id} Chunk uploaded file ProcessRequest(file_id, chunk_size, overlap_size) Array of text chunk objects
POST /api/v1/dataset/generate Run full AI pipeline EducationalContext JSON payload Summary counts & status breakdown
POST /api/v1/dataset/evaluate Re-evaluate DB questions EvaluateRequest(question_ids: Optional[list]) {"questions_evaluated": N, "status_breakdown": {...}}
GET /api/v1/dataset/export Export clean dataset Query param: difficulty (optional) Array of flat dataset records
POST /api/v1/training-pairs/build Generate training pairs None {"total_pairs": N, "positive_pairs": X, ...}
POST /api/v1/recommendations/questions Vector similarity search RecommendationRequest(query, top_k, difficulty) {"query": "...", "recommendations": [...]}

🗄️ 5. Database Schema & Migrations (Alembic)

Database schema is version-controlled via Alembic migrations with cascade deletes (ondelete="CASCADE") and indexes on foreign keys and status columns.

❓ Why Alembic? (Why not simple Base.metadata.create_all?)

  • Zero Data Loss on Schema Evolution: create_all() can only create tables if they don't exist; it cannot alter existing tables. If a column or index is added later, create_all() fails to update PostgreSQL unless you drop the entire database (destroying all student records). Alembic safely runs incremental ALTER TABLE migrations without deleting existing data.
  • Auditability & Version Control: Schema changes are tracked as code in migrations/versions/ alongside Git commits, making database state reproducible across dev, staging, and production.
  • Multi-Instance Race-Condition Safety: Decouples schema execution from FastAPI container boot so multiple Uvicorn/Gunicorn worker processes don't fight to create/lock tables on startup.

                    ┌─────────────────────────┐
                    │    learning_outcomes    │
                    ├─────────────────────────┤
                    │ id (UUID, PK)           │
                    │ text (TEXT)             │
                    │ concept (VARCHAR)       │
                    │ source_evidence (TEXT)  │
                    │ context_hash (Indexed)  │
                    └────────────▲────────────┘
                                 │
                                 │ (via QuestionLOLink)
                                 │
                    ┌────────────┴────────────┐
                    │    question_lo_links    │
                    ├─────────────────────────┤
                    │ id (UUID, PK)           │
                    │ question_id (FK, Index) ┼──────────┐
                    │ lo_id (FK, Index)       │          │
                    │ confidence (FLOAT)      │          │
                    │ reason (TEXT)           │          │
                    └─────────────────────────┘          │
                                                         │
                                                         ▼
┌─────────────────────────┐                 ┌─────────────────────────┐
│   evaluation_results    │                 │        questions        │
├─────────────────────────┤                 ├─────────────────────────┤
│ id (UUID, PK)           │                 │ id (UUID, PK)           │
│ question_id (FK, Index) ┼─────────────────┤ question_text (TEXT)    │
│ 8 Criteria Scores (0-1) │                 │ question_type (VARCHAR) │
│ overall_score (FLOAT)   │                 │ choices (JSONB)         │
│ status (Indexed)        │                 │ correct_answer (TEXT)   │
└─────────────────────────┘                 │ explanation (TEXT)      │
                                            │ difficulty (VARCHAR)    │
                                            │ estimated_time_minutes  │
                                            │ related_LO_ids (JSONB)  │
                                            │ source_evidence (TEXT)  │
                                            └─────────────────────────┘

⚡ 6. Caching & Vector DB Infrastructure

🔴 Redis Caching Layer (redis_client.py)

  • Pipeline Cache (dataset_generate:<sha256_hash>): Caches complete /dataset/generate output payloads for 24 hours (REDIS_TTL=86400).
  • Embedding Cache (embedding:<sha256_text_model>): Caches Gemini vectors in Redis to eliminate redundant embedding API calls.

🔷 ChromaDB Vector DB (vector_store.py)

  • Stores question text vectors in collection "questions" (mapped to port 8001 on host).
  • Lazy client initialization on first call prevents startup blocking.

⚡ 7. Performance & Optimization Architecture (Streamlined Upgrades)

To ensure high throughput, minimal memory usage, and instant startup times, the architecture underwent a comprehensive performance streamlining:

📊 Performance Benchmarks (Before vs. After)

Metric Before Streamlining After Streamlining Concrete Benefit
RAM Footprint ~2,200 MB (2.2 GB) ~220 MB 📉 90% reduction in memory usage
Server Startup / Reload 12 – 18 seconds < 0.5 seconds 40x faster startup & hot-reloading
Docker Image Size ~4,500 MB (4.5 GB) ~190 MB 📦 95% smaller container image
Docker Build Time 8 – 10 minutes ~15 – 20 seconds ⏱️ 30x faster CI/CD and deployment
Cached Request Latency 12,000 ms 2 ms (Redis cache) 🚀 6,000x faster response on repeat inputs
Semantic Dimensions 768 dims (SBERT 2020) 3,072 dims (Gemini) 🎯 4x more semantic nuance for Arabic & STEM

🛠️ Optimization Details by Component

1. High-Dimensional Vector Semantic Linking (Replaced Heavy PyTorch)

  • Location: src/services/lo_linker.py & src/services/embedding_service.py
  • Type of Update: Machine Learning / Vector Linking Optimization
  • What Changed: Replaced local SentenceTransformer("paraphrase-multilingual-mpnet-base-v2") with Google's cloud-based Gemini Embeddings API (3072 dims) and a fast C-level NumPy cosine similarity function:
    def _cosine_similarity(vec_a, vec_b) -> float:
        a, b = np.array(vec_a, dtype=np.float32), np.array(vec_b, dtype=np.float32)
        norm = np.linalg.norm(a) * np.linalg.norm(b)
        return float(np.dot(a, b) / norm) if norm != 0 else 0.0
  • Why: Eliminates the 1.1 GB model weight download and the ~1.8 GB PyTorch RAM overhead, running linking calculations in microseconds.

2. Functional Document Service (Removed OOP Boilerplate)

  • Location: src/services/file_service.py & src/routes/data.py
  • Type of Update: Code Architecture & File I/O Streamlining
  • What Changed: Deleted 4 legacy OOP controller classes (BaseController, ProjectController, DataController, ProcessController) in favor of pure, testable functions and direct pymupdf (fitz) page extraction.
  • Why: Replaces deprecated LangChain wrapper overhead with direct asynchronous streaming (aiofiles) in 512KB non-blocking chunks.

3. Single-Stage Production Containerization

  • Location: Dockerfile & src/requirements.txt
  • Type of Update: DevOps & Container Infrastructure
  • What Changed: Pruned torch, transformers, sentence-transformers, and heavy CUDA dependencies from requirements.txt. Simplified the Dockerfile into a clean single-stage Python 3.11-slim container.
  • Why: Shrinks Docker builds from 10 minutes down to ~15 seconds, and cuts image size by 95% (from 4.5 GB to 190 MB).

4. SHA-256 Context Fingerprinting & 2ms Redis Caching

  • Location: src/helpers/hashing.py & src/routes/dataset.py
  • Type of Update: Caching & Cost Optimization
  • What Changed: Generates deterministic SHA-256 hashes of subject + grade_level + passage and checks Redis before invoking the LangGraph pipeline or LLMs.
  • Why: Repeated requests for identical educational passages return in 2 milliseconds with $0 in Gemini API costs.

5. Connection Pooling & Database Readiness Probes

  • Location: src/helpers/db.py, src/routes/base.py, migrations/
  • Type of Update: Database Reliability & Concurrency
  • What Changed: Configured async connection pooling (pool_size=10, max_overflow=20, pool_pre_ping=True, pool_recycle=3600), version-controlled Alembic migrations, and real-time dependency readiness checks.
  • Why: Prevents connection saturation under concurrent user traffic and eliminates dead database socket errors.

📂 8. Clean Codebase Directory Sitemap

Smart-Educator/
├── assets/                    # Shared assets (sample texts, training pairs)
├── docker/                    # Container orchestration
│   └── docker-compose.yml     # Multi-service stack (API, Postgres, Redis, ChromaDB)
├── migrations/                # Alembic database migrations
│   ├── env.py                 # Async migration runner
│   └── versions/              # Migration versions (001_initial_schema.py)
├── tests/                     # Automated testing suite (28 tests)
│   ├── conftest.py            # Test engine fixtures with NullPool
│   ├── test_api_endpoints.py  # Route and probe tests
│   ├── test_evaluator.py      # 8-criteria scoring tests
│   ├── test_multi_context.py  # Chunking and context isolation tests
│   ├── test_pair_generator.py # Jaccard similarity & triplet pair tests
│   └── test_schemas.py        # Pydantic validation tests
├── src/                       # Lean application source (7 clean modules)
│   ├── crud/                  # Async SQLAlchemy CRUD operations
│   │   ├── evaluation_results.py
│   │   ├── learning_outcomes.py
│   │   ├── question_lo_links.py
│   │   └── questions.py
│   ├── graph/                 # LangGraph state machine workflow
│   │   └── graph.py
│   ├── helpers/               # Core infrastructure utilities
│   │   ├── config.py          # BaseSettings schema & env parsing
│   │   ├── db.py              # Async connection pool & engine
│   │   ├── hashing.py         # SHA-256 context hashing
│   │   ├── logger.py          # Correlation ID (X-Request-ID) middleware
│   │   ├── redis_client.py    # Async Redis cache client
│   │   ├── security.py        # API key verification dependency
│   │   └── storage.py         # Storage provider interface
│   ├── models/                # SQLAlchemy ORM database models
│   │   ├── base.py
│   │   ├── enums.py
│   │   ├── evaluationResult.py
│   │   ├── learningOutcome.py
│   │   ├── questionL0Link.py
│   │   └── questions.py
│   ├── routes/                # FastAPI endpoint routers & schemas
│   │   ├── base.py            # /health, /health/live, /health/ready
│   │   ├── data.py            # /api/v1/data (upload & chunking)
│   │   ├── dataset.py         # /api/v1/dataset (generate, evaluate, export)
│   │   ├── recommendations_questions.py # /api/v1/recommendations
│   │   ├── training_pairs.py  # /api/v1/training-pairs/build
│   │   └── schemes/           # Pydantic request/response schemas
│   ├── services/              # Core business & AI logic
│   │   ├── document_service.py# PDF & text file processing
│   │   ├── embedding_service.py# Gemini Embeddings API + Redis vector cache
│   │   ├── evaluator.py       # LLM-as-Judge 8-criteria evaluator
│   │   ├── file_service.py    # File storage, validation & chunking
│   │   ├── fine_tune.py       # Offline embedding fine-tuning script
│   │   ├── gemini_client.py   # Gemini API client wrapper
│   │   ├── lo_extraction.py   # Learning outcome extraction
│   │   ├── lo_linker.py       # Gemini embedding cosine similarity linker
│   │   ├── pair_generator.py  # Contrastive pair generator with Jaccard metrics
│   │   ├── question_generator.py # Question generator
│   │   └── vector_store.py    # ChromaDB vector store client
│   ├── static/                # Web Studio User Interface
│   │   └── index.html         # Interactive dashboard, generator & quiz studio
│   ├── main.py                # Application entrypoint & middleware registration
│   └── requirements.txt       # Streamlined production dependencies
├── .env                       # Environment configuration
├── Dockerfile                 # Single-stage fast production Docker image
├── alembic.ini                # Alembic migration configuration
├── PROJECT_BLUEPRINT.md       # Master system blueprint
└── README.md                  # Developer guide & architecture overview (this file)

💻 9. Step-by-Step Setup & How-to-Run Guide

Step 1: Environment Configuration

Create or verify .env in the project root:

APP_NAME="smart-educator"
APP_VERSION="0.1"
GEMINI_API_KEY="your_actual_gemini_api_key"

DATABASE_URL="postgresql+asyncpg://postgres:postgres@localhost:5432/smart_educator"

REDIS_HOST="localhost"
REDIS_PORT=6379
CHROMA_HOST="localhost"
CHROMA_PORT=8001
REDIS_TTL=86400

Step 2: Spin Up Infrastructure Containers

docker compose -f docker/docker-compose.yml up -d postgres redis chromadb

Step 3: Run Database Migrations

source .venv/bin/activate
alembic upgrade head

Step 4: Run the Development Server

uvicorn main:app --app-dir src --reload --port 8000

🧪 10. Automated Testing & Health Probes

Run the full automated test suite (28 tests covering schemas, 8-criteria evaluations, multi-context chunking, pair generation, and API endpoints):

source .venv/bin/activate
pytest -v tests/

Check live service readiness probe (actively testing PostgreSQL, Redis, and ChromaDB):

curl http://localhost:8000/api/v1/health/ready

Output:

{
  "status": "ready",
  "response_time_ms": 12.4,
  "dependencies": {
    "database": { "status": "healthy" },
    "redis": { "status": "healthy" },
    "chromadb": { "status": "healthy" }
  }
}

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages