You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
1.2 KiB
Python
32 lines
1.2 KiB
Python
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.exc import SQLAlchemyError
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.api.routes.dependencies import db_error_detail, get_db
|
|
from app.api.schemas import ChatRequest, ChatResponse
|
|
from app.services.orquestrador_service import OrquestradorService
|
|
|
|
router = APIRouter(tags=["Chat"])
|
|
|
|
|
|
@router.post("/chat", response_model=ChatResponse)
|
|
async def chat(request: ChatRequest, db: Session = Depends(get_db)):
|
|
"""Processa mensagem do usuario via orquestrador e retorna resposta do chat."""
|
|
try:
|
|
service = OrquestradorService(db)
|
|
result = await service.handle_message(
|
|
message=request.message,
|
|
user_id=request.user_id,
|
|
)
|
|
return ChatResponse(response=result)
|
|
except SQLAlchemyError as exc:
|
|
raise HTTPException(
|
|
status_code=503,
|
|
detail=db_error_detail(exc),
|
|
)
|
|
except ValueError as exc:
|
|
# Erros de configuracao de Vertex (regiao/projeto/modelo)
|
|
raise HTTPException(status_code=500, detail=f"Configuracao invalida do Vertex AI: {exc}")
|
|
except RuntimeError as exc:
|
|
raise HTTPException(status_code=503, detail=f"Falha temporaria no LLM/Vertex AI: {exc}")
|