lov2 / app /api /v2 /learning_path.py
work-sejal
Add knowledge graph and adaptive learning path features to HF Space
c045254
Raw
History Blame Contribute Delete
3.55 kB
"""Adaptive learning path API endpoints."""
import logging
from fastapi import APIRouter, Depends, HTTPException, Query
from app.core.exceptions import EntityNotFoundError, DatasetError
from app.schemas.learning_path import LearningPathRequest, LearningPathResponse
from app.services.adaptive_learning_path_service import AdaptiveLearningPathService
from app.api.v2.dependencies import get_adaptive_learning_path_service
logger = logging.getLogger(__name__)
router = APIRouter(prefix="/ai/v2/learning-path", tags=["learning-path"])
@router.get(
"/{student_id}",
response_model=LearningPathResponse,
summary="Generate adaptive learning path",
description="Generate a personalized learning path for a student to reach a target learning outcome"
)
async def generate_learning_path(
student_id: str,
target_lo_id: str = Query(..., description="Target learning outcome ID"),
max_steps: int = Query(default=10, ge=1, le=20, description="Maximum number of steps in path"),
include_mastered: bool = Query(default=False, description="Include already mastered LOs for review"),
difficulty_preference: str = Query(default="adaptive", description="Difficulty preference: easy, medium, hard, adaptive"),
learning_path_service: AdaptiveLearningPathService = Depends(get_adaptive_learning_path_service)
) -> LearningPathResponse:
"""Generate an adaptive learning path for a student."""
try:
request = LearningPathRequest(
student_id=student_id,
target_lo_id=target_lo_id,
max_steps=max_steps,
include_mastered=include_mastered,
difficulty_preference=difficulty_preference
)
return learning_path_service.generate_learning_path(request)
except EntityNotFoundError as exc:
logger.warning("Learning path generation failed - entity not found: %s", exc)
raise HTTPException(status_code=404, detail=str(exc)) from exc
except DatasetError as exc:
logger.error("Learning path generation failed - dataset error: %s", exc)
raise HTTPException(status_code=503, detail="Learning path service unavailable") from exc
except Exception as exc:
logger.error("Learning path generation failed - unexpected error: %s", exc)
raise HTTPException(status_code=500, detail="Internal server error") from exc
@router.post(
"/",
response_model=LearningPathResponse,
summary="Generate adaptive learning path (POST)",
description="Generate a personalized learning path using POST request body"
)
async def generate_learning_path_post(
request: LearningPathRequest,
learning_path_service: AdaptiveLearningPathService = Depends(get_adaptive_learning_path_service)
) -> LearningPathResponse:
"""Generate an adaptive learning path for a student using POST."""
try:
return learning_path_service.generate_learning_path(request)
except EntityNotFoundError as exc:
logger.warning("Learning path generation failed - entity not found: %s", exc)
raise HTTPException(status_code=404, detail=str(exc)) from exc
except DatasetError as exc:
logger.error("Learning path generation failed - dataset error: %s", exc)
raise HTTPException(status_code=503, detail="Learning path service unavailable") from exc
except Exception as exc:
logger.error("Learning path generation failed - unexpected error: %s", exc)
raise HTTPException(status_code=500, detail="Internal server error") from exc