diff --git a/smart-kiosk-assistant/rag-service/api/custom_endpoints.py b/smart-kiosk-assistant/rag-service/api/custom_endpoints.py index 4b6beefe..e0f1f05e 100644 --- a/smart-kiosk-assistant/rag-service/api/custom_endpoints.py +++ b/smart-kiosk-assistant/rag-service/api/custom_endpoints.py @@ -173,7 +173,7 @@ async def ingest_context_file(request: Request) -> BatchIngestResponse: (e.g. ``file`` or ``files``) so the endpoint is resilient to client changes. """ form = await request.form() - uploads = [value for value in form.values() if isinstance(value, StarletteUploadFile)] + uploads = [value for key, value in form.multi_items() if isinstance(value, StarletteUploadFile)] if not uploads: raise HTTPException( status_code=422, @@ -199,8 +199,14 @@ def context_stats(): @router.delete("/api/v1/context") def clear_context(): - get_shared_pipeline().clear_context() - return JSONResponse(content={"status": "cleared"}, status_code=200) + try: + get_shared_pipeline().clear_context() + return JSONResponse(content={"status": "cleared"}, status_code=200) + except Exception as exc: + return JSONResponse( + content={"status": "failed", "detail": str(exc)}, + status_code=500 + ) @router.post("/api/v1/query") diff --git a/smart-kiosk-assistant/rag-service/pipeline.py b/smart-kiosk-assistant/rag-service/pipeline.py index 75d2e612..5e0dcaf4 100644 --- a/smart-kiosk-assistant/rag-service/pipeline.py +++ b/smart-kiosk-assistant/rag-service/pipeline.py @@ -146,16 +146,17 @@ def ingest_text(self, text: str, source: str = "api", metadata: dict | None = No return self._ingestion.ingest_text(text, source=source, metadata=metadata) def clear_context(self) -> None: - client = getattr(self.vectorstore, "_client", None) - if client is None: - raise RuntimeError("Vector store client is not available") + collection = getattr(self.vectorstore, "_collection", None) + if collection is None: + logger.info("No collection to clear") + return try: - client.delete_collection(self.collection_name) - except Exception: # noqa: BLE001 - logger.info( - "Collection %s did not exist yet during clear_context", self.collection_name, - ) - self.vectorstore = self._build_vectorstore() + all_ids = collection.get(include=[]) + if all_ids["ids"]: + collection.delete(ids=all_ids["ids"]) + logger.info("Deleted %d documents from collection", len(all_ids["ids"])) + except Exception as exc: # noqa: BLE001 + raise RuntimeError(f"Failed to clear context: {exc}") from exc def get_stats(self) -> dict: collection = getattr(self.vectorstore, "_collection", None)