File size: 2,823 Bytes
e92d49a
 
a82b536
e92d49a
 
d35bd88
 
e92d49a
 
72c963b
d35bd88
 
e92d49a
a82b536
 
f9ad9a3
a82b536
f9ad9a3
a82b536
 
 
 
 
 
 
f9ad9a3
a82b536
 
e92d49a
 
 
f9ad9a3
e92d49a
 
 
 
7f32096
e92d49a
 
 
 
 
 
 
 
d35bd88
 
e92d49a
d35bd88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from __future__ import annotations

from contextlib import asynccontextmanager
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel, Field
import logging
from google.genai.errors import ClientError, ServerError
from rag.chain import aanswer

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


@asynccontextmanager
async def lifespan(app: FastAPI):
    logger.info("FAQ RAG Chatbot starting up")
    yield
    logger.info("FAQ RAG Chatbot shutting down")


app = FastAPI(
    title="FAQ RAG Chatbot",
    version="1.0.0",
    description="RAG-based chatbot for samagama internship portal - CSFAQ Project",
    openapi_url="/openapi.json",
    docs_url="/",
    lifespan=lifespan,
)


class ChatRequest(BaseModel):
    question: str = Field(..., min_length=1, description="User Query..")

class ChatResponse(BaseModel):
    question: str
    answer: str
    sources: list[str] = Field(default_factory=list)

@app.get("/health")
async def health() -> dict[str, str]:
    return {"status": "ok"}


@app.post("/chat", response_model=ChatResponse)
async def chat(payload: ChatRequest) -> ChatResponse:
    question = payload.question.strip()

    try:
        response = await aanswer(question)
    except ClientError as error:
        message = str(error).lower()
        if error.code == 429 or "quota" in message or "rate limit" in message:
            raise HTTPException(
                status_code=429,
                detail=(
                    "The language model quota has been exceeded. "
                    "Please try again later."
                ),
            ) from None
        raise HTTPException(
            status_code=400,
            detail=(
                "Unable to process your request right now. "
                "Please try again later."
            ),
        ) from None
    except ServerError as error:
        if error.code == 503 or "unavailable" in str(error).lower():
            raise HTTPException(
                status_code=503,
                detail=(
                    "The language service is temporarily unavailable. "
                    "Please try again later."
                ),
            ) from None
        raise HTTPException(
            status_code=502,
            detail=(
                "Unable to process your request right now. "
                "Please try again later."
            ),
        ) from None
    except Exception:
        logger.exception("Chat handler error")
        raise HTTPException(
            status_code=502,
            detail=(
                "Unable to process your request right now. "
                "Please try again later."
            ),
        )

    return ChatResponse(
        question=question,
        answer=response.answer,
        sources=response.sources,
    )