File size: 1,201 Bytes
44a025a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import APIRouter, HTTPException, Query
from pydantic import BaseModel
from typing import Optional

from app.services.prediction_service import predict_answer

router = APIRouter()


class PredictionRequest(BaseModel):
    user_input: str


class PredictionResponse(BaseModel):
    status: str
    answer: Optional[str] = None
    score: Optional[str] = None
    message: Optional[str] = None


@router.post("/predict", response_model=PredictionResponse)
async def get_prediction(
    request: PredictionRequest,
    threshold_q: float = Query(0.7, description="Threshold for question matching"),
    threshold_a: float = Query(0.65, description="Threshold for answer matching"),
):
    """
    Predict an answer based on user input.

    - **user_input**: The user's question or input text
    - **threshold_q**: Threshold for question matching (default: 0.7)
    - **threshold_a**: Threshold for answer matching (default: 0.65)

    Returns the predicted answer and match type.
    """
    result = predict_answer(request.user_input, threshold_q, threshold_a)

    if result["status"] == "error":
        raise HTTPException(status_code=500, detail=result["message"])

    return result