File size: 2,647 Bytes
18b8b90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
468ea61
 
 
 
18b8b90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
468ea61
 
 
 
18b8b90
 
 
 
 
 
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
Feedback API — Endpoints for submitting and reviewing AI prediction corrections.
"""

import uuid
from fastapi import APIRouter, HTTPException
from pydantic import BaseModel, Field
from typing import Optional
import logging

from app.services.feedback.feedback_store import feedback_store

router = APIRouter()
logger = logging.getLogger(__name__)


class FeedbackRequest(BaseModel):
    """Payload for submitting a correction to an AI prediction."""
    prediction_id: str = Field(..., description="UUID of the original prediction")
    original_prediction: str = Field(..., description="The original AI prediction (e.g. 'Medium')")
    corrected_prediction: str = Field(..., description="The corrected value by the employee")
    corrected_by: Optional[str] = Field(None, description="Employee ID or name")
    reason: Optional[str] = Field(None, description="Reason for correction (optional)")


class FeedbackResponse(BaseModel):
    message: str
    feedback_id: str


@router.post("/feedback", response_model=FeedbackResponse)
async def submit_feedback(data: FeedbackRequest):
    """
    Ingests explicit user feedback validating or rejecting prior AI-driven predictions.
    Receives payloads mapping the original AI output alongside the human correction footprint.
    Persists evaluation data chronologically into the structured feedback reporting store.
    Returns a standard confirmation receipt bridging data gaps between AI logic and human oversight.
    """
    feedback_id = str(uuid.uuid4())
    try:
        feedback_store.save_feedback({
            "feedback_id": feedback_id,
            **data.model_dump(),
        })
    except Exception as e:
        logger.error("Feedback save failed: %s", e)
        raise HTTPException(status_code=500, detail="Failed to save feedback.")

    return FeedbackResponse(
        message="تم حفظ التصحيح بنجاح.",
        feedback_id=feedback_id,
    )


@router.get("/feedback/summary")
async def get_feedback_summary():
    """
    Aggregates accumulated AI prediction corrections into high-level evaluative statistics.
    Scans the feedback repository to compute drift metrics, common patterns, and accuracy rates.
    Designed primarily for platform administrators establishing continuous LLM refinement loops.
    Returns a structured dictionary mapping distinct models to their respective human correction summaries.
    """
    try:
        return feedback_store.get_summary()
    except Exception as e:
        logger.error("Failed to get feedback summary: %s", e)
        raise HTTPException(status_code=500, detail="Failed to retrieve feedback summary.")