Deploy Docker: app\api\endpoints.py
Browse files- app//api//endpoints.py +55 -0
app//api//endpoints.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
锘縡rom fastapi import APIRouter, Depends, HTTPException
|
| 2 |
+
from typing import List, Optional
|
| 3 |
+
|
| 4 |
+
from app.api.models import (
|
| 5 |
+
QueryRequest, QueryResponse, SearchRequest, SearchResponse,
|
| 6 |
+
CollectionStatsResponse, HealthResponse
|
| 7 |
+
)
|
| 8 |
+
from app.services.rag_service import RAGService
|
| 9 |
+
from app.core.config import settings
|
| 10 |
+
from app.core.security import get_current_active_user
|
| 11 |
+
from app.db.models import User
|
| 12 |
+
|
| 13 |
+
router = APIRouter()
|
| 14 |
+
|
| 15 |
+
_rag_service = None
|
| 16 |
+
def get_rag_service():
|
| 17 |
+
global _rag_service
|
| 18 |
+
if _rag_service is None:
|
| 19 |
+
_rag_service = RAGService()
|
| 20 |
+
return _rag_service
|
| 21 |
+
|
| 22 |
+
@router.post("/query", response_model=QueryResponse)
|
| 23 |
+
async def query_documents(
|
| 24 |
+
request: QueryRequest,
|
| 25 |
+
rag_service: RAGService = Depends(get_rag_service),
|
| 26 |
+
current_user: User = Depends(get_current_active_user) # Protege a rota
|
| 27 |
+
):
|
| 28 |
+
"""Query documents using RAG with conversation memory"""
|
| 29 |
+
try:
|
| 30 |
+
# Extrai os escopos do usu谩rio logado
|
| 31 |
+
user_scopes = current_user.scopes.split(',')
|
| 32 |
+
|
| 33 |
+
# Passa os escopos para a fun莽茫o de query (ser谩 implementado no pr贸ximo passo)
|
| 34 |
+
result = rag_service.query(
|
| 35 |
+
question=request.question,
|
| 36 |
+
chat_history=request.chat_history,
|
| 37 |
+
k=request.k,
|
| 38 |
+
allowed_scopes=user_scopes # NOVO
|
| 39 |
+
)
|
| 40 |
+
return QueryResponse(**result)
|
| 41 |
+
except Exception as e:
|
| 42 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 43 |
+
|
| 44 |
+
@router.get("/health", response_model=HealthResponse)
|
| 45 |
+
async def health_check(vector_store: RAGService = Depends(get_rag_service)):
|
| 46 |
+
try:
|
| 47 |
+
stats = vector_store.vector_store.get_collection_stats()
|
| 48 |
+
return HealthResponse(
|
| 49 |
+
status="healthy",
|
| 50 |
+
vector_store=bool(stats),
|
| 51 |
+
chroma_directory=settings.chroma_persist_directory,
|
| 52 |
+
embeddings_model=settings.embeddings_model
|
| 53 |
+
)
|
| 54 |
+
except Exception as e:
|
| 55 |
+
return HealthResponse(status=f"error: {e}", vector_store=False)
|