Skip to content

Backend service powering GrepGuru's AI platform. Provides intelligent assessment, analytics, and automation capabilities through RESTful APIs and event-driven architecture.

Notifications You must be signed in to change notification settings

HumbleBee14/grepguru-ai-engine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

9 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

AI Teaching Assistant - Backend

A modern, scalable backend for an AI-powered teaching assistant that automates grading, provides intelligent feedback, and enables Socratic teaching methods.

πŸ—οΈ Architecture Overview

The backend is built with FastAPI and follows modern Python async patterns with a clean, layered architecture:

β”œβ”€β”€ app/                      # Main application code
β”‚   β”œβ”€β”€ api/                  # API route handlers
β”‚   β”‚   β”œβ”€β”€ analytics.py      # Analytics & reporting endpoints
β”‚   β”‚   β”œβ”€β”€ assignments.py    # Assignment management API
β”‚   β”‚   β”œβ”€β”€ auth.py          # Authentication endpoints
β”‚   β”‚   β”œβ”€β”€ files.py         # File upload/download API
β”‚   β”‚   β”œβ”€β”€ grading.py       # AI grading operations
β”‚   β”‚   β”œβ”€β”€ health.py        # Health check endpoints
β”‚   β”‚   β”œβ”€β”€ notifications.py # Notification management
β”‚   β”‚   β”œβ”€β”€ socratic.py      # Socratic teaching API
β”‚   β”‚   β”œβ”€β”€ submissions.py   # Submission handling
β”‚   β”‚   β”œβ”€β”€ users.py         # User management
β”‚   β”‚   └── websocket.py     # Real-time WebSocket API
β”‚   β”œβ”€β”€ core/                # Core functionality & utilities
β”‚   β”‚   β”œβ”€β”€ auth.py          # Authentication & authorization logic
β”‚   β”‚   β”œβ”€β”€ config.py        # Application configuration
β”‚   β”‚   β”œβ”€β”€ events.py        # Application lifecycle events
β”‚   β”‚   β”œβ”€β”€ exceptions.py    # Custom exception classes
β”‚   β”‚   β”œβ”€β”€ logging.py       # Structured logging setup
β”‚   β”‚   β”œβ”€β”€ middleware.py    # HTTP middleware (CORS, logging, etc.)
β”‚   β”‚   β”œβ”€β”€ monitoring.py    # Prometheus metrics & monitoring
β”‚   β”‚   └── security.py      # Security utilities (password hashing, JWT)
β”‚   β”œβ”€β”€ db/                  # Database layer
β”‚   β”‚   β”œβ”€β”€ models/          # SQLAlchemy ORM models
β”‚   β”‚   β”‚   β”œβ”€β”€ assignment.py    # Assignment model
β”‚   β”‚   β”‚   β”œβ”€β”€ base.py          # Base model class
β”‚   β”‚   β”‚   β”œβ”€β”€ notification.py  # Notification model
β”‚   β”‚   β”‚   β”œβ”€β”€ socratic.py      # Socratic session models
β”‚   β”‚   β”‚   β”œβ”€β”€ submission.py    # Submission model
β”‚   β”‚   β”‚   └── user.py          # User model with roles
β”‚   β”‚   β”œβ”€β”€ migrations/      # Alembic database migrations
β”‚   β”‚   β”œβ”€β”€ repositories/    # Data access layer (Repository pattern)
β”‚   β”‚   β”‚   β”œβ”€β”€ assignment.py    # Assignment repository
β”‚   β”‚   β”‚   β”œβ”€β”€ base.py          # Base repository class
β”‚   β”‚   β”‚   β”œβ”€β”€ notification.py  # Notification repository
β”‚   β”‚   β”‚   β”œβ”€β”€ socratic.py      # Socratic repository
β”‚   β”‚   β”‚   └── user.py          # User repository
β”‚   β”‚   β”œβ”€β”€ init_db.py       # Database initialization & seed data
β”‚   β”‚   └── session.py       # Database session management
β”‚   β”œβ”€β”€ services/            # Business logic services
β”‚   β”‚   β”œβ”€β”€ document_parser.py   # PDF/document parsing
β”‚   β”‚   β”œβ”€β”€ grading_agent.py     # AI grading service using LangChain
β”‚   β”‚   β”œβ”€β”€ socratic.py          # Socratic teaching logic
β”‚   β”‚   β”œβ”€β”€ vector_store.py      # Vector embeddings & similarity search
β”‚   β”‚   └── websocket.py         # WebSocket connection manager
β”‚   β”œβ”€β”€ tasks/               # Background tasks (Celery)
β”‚   β”‚   β”œβ”€β”€ analytics.py     # Analytics data processing
β”‚   β”‚   β”œβ”€β”€ celery.py        # Celery configuration
β”‚   β”‚   β”œβ”€β”€ file_processing.py   # File processing tasks
β”‚   β”‚   β”œβ”€β”€ grading.py       # Async grading tasks
β”‚   β”‚   β”œβ”€β”€ monitoring.py    # System monitoring tasks
β”‚   β”‚   └── notifications.py # Email/notification tasks
β”‚   └── main.py              # FastAPI application entry point
β”œβ”€β”€ tests/                   # Test suite
β”œβ”€β”€ scripts/                 # Utility scripts
β”œβ”€β”€ data/                    # Data storage (assignments, submissions, etc.)
└── requirements-*.txt       # Dependencies organized by environment

πŸš€ Quick Start

Prerequisites

  • Python 3.10+
  • PostgreSQL 14+ (or use Docker)
  • Redis 6+ (or use Docker)
  • Elasticsearch 8+ (optional, for analytics)

1. Environment Setup

# Clone the repository
git clone <repository-url>
cd ai-ta-grading-agent/backend

# Create virtual environment
python -m venv .venv
source .venv/bin/activate  # On Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements-dev.txt  # For development
# OR
pip install -r requirements-core.txt  # For minimal setup

2. Configuration

# Copy environment template
cp .env.example .env

# Edit configuration (required settings)
# - DATABASE_URL: PostgreSQL connection string
# - SECRET_KEY: JWT secret key
# - OPENAI_API_KEY: OpenAI API key for AI grading
# - REDIS_URL: Redis connection string

3. Database Setup

# Initialize database with sample data
python -c "
import asyncio
from app.db.init_db import init_db
asyncio.run(init_db())
"

# Or run migrations manually
alembic upgrade head

4. Start the Application

# Development server (with auto-reload)
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000

# Production server
gunicorn app.main:app -w 4 -k uvicorn.workers.UvicornWorker

5. Verify Installation

Visit these URLs to confirm everything is working:

πŸ“‹ Requirements Structure

We use a modular requirements structure for different environments:

File Purpose Usage
requirements-core.txt 28 essential packages pip install -r requirements-core.txt
requirements-dev.txt Core + development tools pip install -r requirements-dev.txt
requirements-prod.txt Core + production extras pip install -r requirements-prod.txt
requirements-categorized.txt Detailed breakdown Reference documentation

Key Dependencies by Category:

  • 🌐 Web Framework: FastAPI, Uvicorn, Starlette, Pydantic
  • πŸ—„οΈ Database: SQLAlchemy, Alembic, Redis, Elasticsearch
  • πŸ€– AI/ML: LangChain, OpenAI, Hugging Face, NumPy, Pandas
  • πŸ“ Storage: MinIO, AioFiles
  • πŸ” Security: Passlib, Python-JOSE
  • πŸ“Š Monitoring: Prometheus, Structlog, Psutil
  • βœ‰οΈ Communication: AioSMTPLib, Aio-Pika

πŸ”§ Application Startup Flow

How the Application Starts

  1. app/main.py - Entry point that creates the FastAPI app
  2. create_application() - Configures the app with:
    • Middleware setup (CORS, logging, monitoring)
    • API router registration
    • Event handlers (startup/shutdown)
    • Exception handlers
  3. Startup Events - Initialize connections to:
    • Database (PostgreSQL)
    • Cache (Redis)
    • Search (Elasticsearch)
    • Message Queue (RabbitMQ)
  4. API Routers - Register all endpoint handlers from app/api/

Main Components Flow:

main.py β†’ create_application() β†’ middleware β†’ routers β†’ services β†’ repositories β†’ models

πŸ§ͺ Testing & Development

Running Tests

# Run all tests
pytest

# Run with coverage
pytest --cov=app tests/

# Run specific test categories
pytest tests/test_db.py          # Database tests
pytest tests/test_auth.py        # Authentication tests
pytest tests/test_health.py      # Health check tests

Local Development Configuration

For local testing, you can comment/uncomment these in your .env:

# Disable external services for local testing
# ELASTICSEARCH_URL=http://localhost:9200  # Comment out to disable analytics
# RABBITMQ_HOST=localhost                  # Comment out to disable task queue
# MINIO_URL=localhost:9000                 # Comment out to use local file storage

# Use simpler backends for development
DATABASE_URL=sqlite:///./test.db           # Use SQLite instead of PostgreSQL
REDIS_URL=redis://localhost:6379/0         # Use local Redis

πŸ“Š Monitoring & Health Checks

Available Endpoints

Endpoint Purpose Usage
/api/v1/health Application health Basic liveness check
/api/v1/health/detailed Detailed health Database, Redis, external services
/metrics Prometheus metrics System metrics for monitoring
/docs API Documentation Interactive Swagger UI
/redoc Alternative docs ReDoc interface

Health Check Response Example:

{
  "status": "healthy",
  "timestamp": "2024-01-20T10:30:00Z",
  "version": "0.1.0",
  "services": {
    "database": "healthy",
    "redis": "healthy",
    "elasticsearch": "healthy"
  }
}

Prometheus Metrics

The application exposes these metrics:

  • Request counts by endpoint and status code
  • Response times (latency histograms)
  • Active connections count
  • Database connection pool metrics
  • Custom business metrics (grading operations, user activity)

πŸ” Authentication & Authorization

User Roles

  • πŸ‘‘ Admin: Full system access
  • πŸ‘¨β€πŸ« Instructor: Manage assignments and grade submissions
  • πŸ‘¨β€πŸŽ“ TA: Grade submissions and assist students
  • πŸ‘€ Student: Submit assignments and view feedback

API Authentication

# Login to get JWT token
curl -X POST "/api/v1/auth/login" \
  -d "username=user@example.com&password=password"

# Use token in requests
curl -H "Authorization: Bearer <token>" "/api/v1/assignments"

🐳 Docker Deployment

# Build image
docker build -t ai-ta-backend .

# Run with docker-compose (includes PostgreSQL, Redis)
docker-compose up -d

πŸ” Troubleshooting

Common Issues

  1. Database Connection Error

    # Check PostgreSQL is running
    pg_isready -h localhost -p 5432
    
    # Verify connection string in .env
    DATABASE_URL=postgresql://user:password@localhost:5432/dbname
  2. OpenAI API Errors

    # Verify API key is set
    echo $OPENAI_API_KEY
    
    # Test API key
    curl -H "Authorization: Bearer $OPENAI_API_KEY" \
         https://api.openai.com/v1/models
  3. Import Errors

    # Ensure virtual environment is activated
    which python  # Should show venv path
    
    # Reinstall dependencies
    pip install -r requirements-dev.txt

Debug Mode

# Enable debug logging
export LOG_LEVEL=DEBUG

# Run with reload and debug
uvicorn app.main:app --reload --log-level debug

πŸ“ˆ Performance & Scaling

Current Capabilities

  • Async I/O: All database and external service calls are async
  • Connection Pooling: PostgreSQL and Redis connection pools
  • Caching: Multi-level caching strategy with Redis
  • Background Tasks: Celery for heavy operations (grading, analytics)

Scaling Options

  • Horizontal: Add more FastAPI workers/containers
  • Database: PostgreSQL read replicas
  • Cache: Redis cluster
  • Storage: S3/MinIO for file storage
  • Search: Elasticsearch cluster for analytics

🀝 Contributing

  1. Follow PEP 8 code style
  2. Add type hints to all functions
  3. Write tests for new features
  4. Update documentation for API changes
  5. Use conventional commit messages

πŸ“„ License

MIT License - see LICENSE file for details.


About

Backend service powering GrepGuru's AI platform. Provides intelligent assessment, analytics, and automation capabilities through RESTful APIs and event-driven architecture.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages