File size: 1,166 Bytes
6fce4ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

from fastapi import APIRouter, HTTPException, status

from .models import ChatRequest, ChatResponse, ResetRequest, ResetResponse
from ..chat import ChatSession
from ..db import reset_history
from ..log import get_logger

router = APIRouter()
log = get_logger(__name__)


@router.post("/chat", response_model=ChatResponse, status_code=status.HTTP_200_OK)
async def chat_endpoint(payload: ChatRequest) -> ChatResponse:
    log.debug("chat request user=%s session=%s", payload.user, payload.session)
    async with ChatSession(user=payload.user, session=payload.session) as chat:
        try:
            reply = await chat.chat(payload.prompt)
        except Exception as exc:  # pragma: no cover - runtime errors
            log.exception("chat processing failed")
            raise HTTPException(status_code=500, detail=str(exc)) from exc
    return ChatResponse(reply=reply)


@router.post("/reset", response_model=ResetResponse, status_code=status.HTTP_200_OK)
async def reset_endpoint(payload: ResetRequest) -> ResetResponse:
    removed = reset_history(payload.user, payload.session)
    return ResetResponse(removed=removed)