Upload app.py
Browse files
app.py
CHANGED
|
@@ -878,7 +878,11 @@ async def process_api(
|
|
| 878 |
|
| 879 |
|
| 880 |
@app.post("/search/")
|
| 881 |
-
async def search_api(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 882 |
"""
|
| 883 |
ENDPOINT DE BUSCA (RAG Híbrido) com Monitoramento de Latência
|
| 884 |
"""
|
|
@@ -887,6 +891,11 @@ async def search_api(query: str = Form(...), job_id: str = Form(...)):
|
|
| 887 |
raise HTTPException(status_code=404, detail="Job ID não encontrado.")
|
| 888 |
|
| 889 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 890 |
model = load_retriever()
|
| 891 |
reranker = load_reranker()
|
| 892 |
|
|
@@ -896,9 +905,12 @@ async def search_api(query: str = Form(...), job_id: str = Form(...)):
|
|
| 896 |
bm25_index = cached_data.get("bm25_index")
|
| 897 |
|
| 898 |
# ==================================================================
|
| 899 |
-
# FASE 0: QUERY EXPANSION (Melhora recall)
|
| 900 |
# ==================================================================
|
| 901 |
-
|
|
|
|
|
|
|
|
|
|
| 902 |
|
| 903 |
# ==================================================================
|
| 904 |
# FASE 1: HYBRID SEARCH (FAISS Semântico + BM25 Lexical)
|
|
@@ -907,7 +919,8 @@ async def search_api(query: str = Form(...), job_id: str = Form(...)):
|
|
| 907 |
query_embedding = model.encode([expanded_query], convert_to_numpy=True)
|
| 908 |
query_normalized = query_embedding / np.linalg.norm(query_embedding, axis=1, keepdims=True)
|
| 909 |
|
| 910 |
-
|
|
|
|
| 911 |
|
| 912 |
# --- 1A: Busca Semântica (FAISS) ---
|
| 913 |
semantic_scores = {}
|
|
@@ -998,7 +1011,7 @@ async def search_api(query: str = Form(...), job_id: str = Form(...)):
|
|
| 998 |
reverse=True
|
| 999 |
)
|
| 1000 |
|
| 1001 |
-
final_top_k =
|
| 1002 |
final_results = []
|
| 1003 |
context_parts = []
|
| 1004 |
|
|
|
|
| 878 |
|
| 879 |
|
| 880 |
@app.post("/search/")
|
| 881 |
+
async def search_api(
|
| 882 |
+
query: str = Form(...),
|
| 883 |
+
job_id: str = Form(...),
|
| 884 |
+
turbo_mode: str = Form("false") # Modo rápido: skip query expansion, menos candidatos
|
| 885 |
+
):
|
| 886 |
"""
|
| 887 |
ENDPOINT DE BUSCA (RAG Híbrido) com Monitoramento de Latência
|
| 888 |
"""
|
|
|
|
| 891 |
raise HTTPException(status_code=404, detail="Job ID não encontrado.")
|
| 892 |
|
| 893 |
try:
|
| 894 |
+
# Parse turbo_mode
|
| 895 |
+
turbo_mode_bool = turbo_mode.lower() in ("true", "1", "yes")
|
| 896 |
+
if turbo_mode_bool:
|
| 897 |
+
logging.info("TURBO MODE ATIVADO - Skip query expansion, menos candidatos")
|
| 898 |
+
|
| 899 |
model = load_retriever()
|
| 900 |
reranker = load_reranker()
|
| 901 |
|
|
|
|
| 905 |
bm25_index = cached_data.get("bm25_index")
|
| 906 |
|
| 907 |
# ==================================================================
|
| 908 |
+
# FASE 0: QUERY EXPANSION (Melhora recall) - Skip em turbo mode
|
| 909 |
# ==================================================================
|
| 910 |
+
if turbo_mode_bool:
|
| 911 |
+
expanded_query = query # Turbo: usa query original (economiza ~5-10s)
|
| 912 |
+
else:
|
| 913 |
+
expanded_query = expand_query(query) # Qualidade: expande query
|
| 914 |
|
| 915 |
# ==================================================================
|
| 916 |
# FASE 1: HYBRID SEARCH (FAISS Semântico + BM25 Lexical)
|
|
|
|
| 919 |
query_embedding = model.encode([expanded_query], convert_to_numpy=True)
|
| 920 |
query_normalized = query_embedding / np.linalg.norm(query_embedding, axis=1, keepdims=True)
|
| 921 |
|
| 922 |
+
# Turbo: menos candidatos = reranking mais rápido
|
| 923 |
+
top_k_retrieval = min(30 if turbo_mode_bool else 100, len(df))
|
| 924 |
|
| 925 |
# --- 1A: Busca Semântica (FAISS) ---
|
| 926 |
semantic_scores = {}
|
|
|
|
| 1011 |
reverse=True
|
| 1012 |
)
|
| 1013 |
|
| 1014 |
+
final_top_k = 5 if turbo_mode_bool else 10 # Turbo: menos contexto
|
| 1015 |
final_results = []
|
| 1016 |
context_parts = []
|
| 1017 |
|