File size: 1,272 Bytes
e46d575
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from fastapi import APIRouter, Depends, HTTPException

from backend.core.auth import get_current_user
from backend.models.practice import PracticeRequest, PracticeResponse, PracticeProblem
from backend.services import claude_service, prompt_templates

router = APIRouter(prefix="/api", tags=["practice"])


@router.post("/practice", response_model=PracticeResponse)
async def generate_practice(
    body: PracticeRequest,
    user: dict = Depends(get_current_user),
):
    """Generate 3 adaptive practice problems for the given topic and grade."""
    system = prompt_templates.practice_system(body.grade)
    user_msg = prompt_templates.practice_user(body.topic, body.count)

    try:
        raw = await claude_service.complete_json(system, user_msg)
    except ValueError as e:
        raise HTTPException(status_code=502, detail=str(e))

    problems = []
    for item in (raw if isinstance(raw, list) else []):
        problems.append(
            PracticeProblem(
                question=item.get("question", ""),
                answer=item.get("answer", ""),
                hint=item.get("hint", ""),
                difficulty=item.get("difficulty", "medium"),
            )
        )

    return PracticeResponse(problems=problems, session_id=body.session_id)