Spaces:
Running
Running
Update app/api/routes.py
Browse files- app/api/routes.py +55 -7
app/api/routes.py
CHANGED
|
@@ -303,7 +303,6 @@
|
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
-
|
| 307 |
|
| 308 |
|
| 309 |
# app/api/routes.py
|
|
@@ -320,14 +319,17 @@ from qdrant_client.http.models import Filter, FieldCondition, MatchValue
|
|
| 320 |
from app.core.llm_engine import ask_gemini
|
| 321 |
from app.core.mongo import conversations
|
| 322 |
from qdrant_client import QdrantClient
|
| 323 |
-
from app.core.config import QDRANT_URL
|
| 324 |
|
| 325 |
router = APIRouter()
|
| 326 |
UPLOAD_DIR = "uploads"
|
| 327 |
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 328 |
|
| 329 |
# Qdrant Client
|
| 330 |
-
qdrant_client = QdrantClient(
|
|
|
|
|
|
|
|
|
|
| 331 |
|
| 332 |
# In-memory (for fallback)
|
| 333 |
chat_histories = defaultdict(list)
|
|
@@ -427,6 +429,41 @@ async def get_all_chats():
|
|
| 427 |
# ---------------------------------------------------
|
| 428 |
# ✅ Delete Chat (Qdrant + Mongo + File)
|
| 429 |
# ---------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 430 |
|
| 431 |
@router.delete("/chat/{chat_id}")
|
| 432 |
async def delete_chat(chat_id: str):
|
|
@@ -486,14 +523,26 @@ async def query_pdf(doc_id: str = Form(...), question: str = Form(...)):
|
|
| 486 |
|
| 487 |
# ✅ Vector Search
|
| 488 |
question_vector = embedder.encode([question])[0].tolist()
|
| 489 |
-
hits = qdrant.search(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 490 |
collection_name=COLLECTION_NAME,
|
| 491 |
-
|
| 492 |
query_filter=Filter(
|
| 493 |
must=[FieldCondition(key="doc_id", match=MatchValue(value=doc_id))]
|
| 494 |
),
|
| 495 |
limit=5,
|
| 496 |
-
)
|
|
|
|
|
|
|
| 497 |
context = "\n".join([hit.payload["text"] for hit in hits])
|
| 498 |
full_context = f"{structured_history}\n\n{context}"
|
| 499 |
|
|
@@ -521,4 +570,3 @@ async def get_conversation(doc_id: str):
|
|
| 521 |
if not doc:
|
| 522 |
return {"history": []}
|
| 523 |
return {"history": doc.get("history", [])}
|
| 524 |
-
|
|
|
|
| 303 |
|
| 304 |
|
| 305 |
|
|
|
|
| 306 |
|
| 307 |
|
| 308 |
# app/api/routes.py
|
|
|
|
| 319 |
from app.core.llm_engine import ask_gemini
|
| 320 |
from app.core.mongo import conversations
|
| 321 |
from qdrant_client import QdrantClient
|
| 322 |
+
from app.core.config import QDRANT_URL, QDRANT_API_KEY
|
| 323 |
|
| 324 |
router = APIRouter()
|
| 325 |
UPLOAD_DIR = "uploads"
|
| 326 |
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 327 |
|
| 328 |
# Qdrant Client
|
| 329 |
+
qdrant_client = QdrantClient(
|
| 330 |
+
url=QDRANT_URL,
|
| 331 |
+
api_key=QDRANT_API_KEY,
|
| 332 |
+
check_compatibility=False)
|
| 333 |
|
| 334 |
# In-memory (for fallback)
|
| 335 |
chat_histories = defaultdict(list)
|
|
|
|
| 429 |
# ---------------------------------------------------
|
| 430 |
# ✅ Delete Chat (Qdrant + Mongo + File)
|
| 431 |
# ---------------------------------------------------
|
| 432 |
+
# @router.delete("/chat/{chat_id}")
|
| 433 |
+
# async def delete_chat(chat_id: str):
|
| 434 |
+
# try:
|
| 435 |
+
# chat = conversations.find_one({"_id": ObjectId(chat_id)})
|
| 436 |
+
# if not chat:
|
| 437 |
+
# raise HTTPException(status_code=404, detail="Chat not found")
|
| 438 |
+
|
| 439 |
+
# doc_id = chat.get("doc_id")
|
| 440 |
+
# qdrant_collection = chat.get("qdrant_collection")
|
| 441 |
+
|
| 442 |
+
# # ✅ Delete embeddings from Qdrant
|
| 443 |
+
# try:
|
| 444 |
+
# qdrant_client.delete(
|
| 445 |
+
# collection_name=qdrant_collection,
|
| 446 |
+
# points_selector=Filter(
|
| 447 |
+
# must=[FieldCondition(key="doc_id", match=MatchValue(value=doc_id))]
|
| 448 |
+
# ),
|
| 449 |
+
# )
|
| 450 |
+
# except Exception as e:
|
| 451 |
+
# print(f"⚠️ Qdrant delete failed: {e}")
|
| 452 |
+
|
| 453 |
+
# # ✅ Delete uploaded file
|
| 454 |
+
# file_path = chat.get("file_path")
|
| 455 |
+
# if file_path and os.path.exists(file_path):
|
| 456 |
+
# os.remove(file_path)
|
| 457 |
+
|
| 458 |
+
# # ✅ Delete from MongoDB
|
| 459 |
+
# conversations.delete_one({"_id": ObjectId(chat_id)})
|
| 460 |
+
|
| 461 |
+
# return {"status": "success", "message": "Chat deleted successfully"}
|
| 462 |
+
|
| 463 |
+
# except Exception as e:
|
| 464 |
+
# raise HTTPException(status_code=500, detail=str(e))
|
| 465 |
+
|
| 466 |
+
|
| 467 |
|
| 468 |
@router.delete("/chat/{chat_id}")
|
| 469 |
async def delete_chat(chat_id: str):
|
|
|
|
| 523 |
|
| 524 |
# ✅ Vector Search
|
| 525 |
question_vector = embedder.encode([question])[0].tolist()
|
| 526 |
+
# hits = qdrant.search(
|
| 527 |
+
# collection_name=COLLECTION_NAME,
|
| 528 |
+
# query_vector=question_vector,
|
| 529 |
+
# query_filter=Filter(
|
| 530 |
+
# must=[FieldCondition(key="doc_id", match=MatchValue(value=doc_id))]
|
| 531 |
+
# ),
|
| 532 |
+
# limit=5,
|
| 533 |
+
# )
|
| 534 |
+
|
| 535 |
+
|
| 536 |
+
hits = qdrant_client.query_points(
|
| 537 |
collection_name=COLLECTION_NAME,
|
| 538 |
+
query=question_vector,
|
| 539 |
query_filter=Filter(
|
| 540 |
must=[FieldCondition(key="doc_id", match=MatchValue(value=doc_id))]
|
| 541 |
),
|
| 542 |
limit=5,
|
| 543 |
+
).points
|
| 544 |
+
|
| 545 |
+
|
| 546 |
context = "\n".join([hit.payload["text"] for hit in hits])
|
| 547 |
full_context = f"{structured_history}\n\n{context}"
|
| 548 |
|
|
|
|
| 570 |
if not doc:
|
| 571 |
return {"history": []}
|
| 572 |
return {"history": doc.get("history", [])}
|
|
|