Delete documents from the FAISS index. Note: FAISS doesn't support direct deletion. We mark documents as deleted by removing them from docstore and mappings, but the index remains unchanged. For true deletion, we would need to rebuild the index. Args
(self, request: FaissDeleteRequest)
| 451 | ) |
| 452 | |
| 453 | async def delete_documents(self, request: FaissDeleteRequest) -> ActionResult: |
| 454 | """Delete documents from the FAISS index. |
| 455 | |
| 456 | Note: FAISS doesn't support direct deletion. We mark documents as deleted |
| 457 | by removing them from docstore and mappings, but the index remains unchanged. |
| 458 | For true deletion, we would need to rebuild the index. |
| 459 | |
| 460 | Args: |
| 461 | request: Delete request with document IDs |
| 462 | |
| 463 | Returns: |
| 464 | Action result with count and success status in extra |
| 465 | """ |
| 466 | if self.index is None: |
| 467 | return ActionResult( |
| 468 | success=False, |
| 469 | message="FAISS index not initialized", |
| 470 | extra={"error": "FAISS index not initialized"} |
| 471 | ) |
| 472 | |
| 473 | try: |
| 474 | deleted_count = 0 |
| 475 | for doc_id in request.ids: |
| 476 | if doc_id in self.docstore: |
| 477 | # Remove from docstore and mappings |
| 478 | idx = self.id_to_index.pop(doc_id, None) |
| 479 | if idx is not None: |
| 480 | self.index_to_id.pop(idx, None) |
| 481 | del self.docstore[doc_id] |
| 482 | deleted_count += 1 |
| 483 | |
| 484 | self._operation_count += 1 |
| 485 | await self._auto_save() |
| 486 | |
| 487 | logger.info(f"| 🗑️ Deleted {deleted_count} documents from FAISS index") |
| 488 | return ActionResult( |
| 489 | success=True, |
| 490 | message=f"Deleted {deleted_count} documents from FAISS index", |
| 491 | extra={ |
| 492 | "deleted_count": deleted_count, |
| 493 | "requested_ids": request.ids, |
| 494 | "total_requested": len(request.ids) |
| 495 | } |
| 496 | ) |
| 497 | |
| 498 | except Exception as e: |
| 499 | return ActionResult( |
| 500 | success=False, |
| 501 | message=f"Failed to delete documents: {str(e)}", |
| 502 | extra={"error": str(e), "requested_ids": request.ids} |
| 503 | ) |
| 504 | |
| 505 | async def get_index_info(self) -> ActionResult: |
| 506 | """Get information about the FAISS index. |