SAAHMATHWORKS
ready for hugging face space
f37bf1d
from fastapi import APIRouter, HTTPException
from fastapi.responses import StreamingResponse
from api.models import ChatRequest, ChatResponse
from api.services import ChatService, StreamService
from api.config import settings
from uuid import uuid4
router = APIRouter(prefix="/chat", tags=["Chat"])
@router.post("", response_model=ChatResponse)
async def chat_simple(request: ChatRequest):
"""
Simple non-streaming chat endpoint.
Best for handling interrupts - returns complete response.
"""
if not settings.system_initialized or not settings.chat_manager:
raise HTTPException(status_code=503, detail="System initializing...")
try:
return await ChatService.process_message(
message=request.message,
session_id=request.session_id
)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.post("/stream")
async def chat_stream(request: ChatRequest):
"""
Streaming chat endpoint using Server-Sent Events (SSE).
Streams tokens in real-time and handles interrupts.
"""
if not settings.system_initialized or not settings.chat_manager:
raise HTTPException(status_code=503, detail="System initializing...")
session_id = request.session_id or f"api_{uuid4()}"
return StreamingResponse(
StreamService.stream_chat_response(request.message, session_id),
media_type="text/event-stream",
headers={
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"X-Accel-Buffering": "no"
}
)