| 
									
										
										
										
											2023-12-18 13:10:05 +08:00
										 |  |  | import logging | 
					
						
							|  |  |  | import time | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import click | 
					
						
							| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  | from celery import shared_task  # type: ignore | 
					
						
							| 
									
										
										
										
											2024-02-06 13:21:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-22 23:31:57 +08:00
										 |  |  | from core.rag.datasource.vdb.vector_factory import Vector | 
					
						
							| 
									
										
										
										
											2023-12-18 13:10:05 +08:00
										 |  |  | from models.dataset import Dataset | 
					
						
							|  |  |  | from services.dataset_service import DatasetCollectionBindingService | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-26 13:38:37 +08:00
										 |  |  | @shared_task(queue="dataset") | 
					
						
							|  |  |  | def delete_annotation_index_task(annotation_id: str, app_id: str, tenant_id: str, collection_binding_id: str): | 
					
						
							| 
									
										
										
										
											2023-12-18 13:10:05 +08:00
										 |  |  |     """
 | 
					
						
							|  |  |  |     Async delete annotation index task | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2024-08-26 13:38:37 +08:00
										 |  |  |     logging.info(click.style("Start delete app annotation index: {}".format(app_id), fg="green")) | 
					
						
							| 
									
										
										
										
											2023-12-18 13:10:05 +08:00
										 |  |  |     start_at = time.perf_counter() | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         dataset_collection_binding = DatasetCollectionBindingService.get_dataset_collection_binding_by_id_and_type( | 
					
						
							| 
									
										
										
										
											2024-08-26 13:38:37 +08:00
										 |  |  |             collection_binding_id, "annotation" | 
					
						
							| 
									
										
										
										
											2023-12-18 13:10:05 +08:00
										 |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         dataset = Dataset( | 
					
						
							|  |  |  |             id=app_id, | 
					
						
							|  |  |  |             tenant_id=tenant_id, | 
					
						
							| 
									
										
										
										
											2024-08-26 13:38:37 +08:00
										 |  |  |             indexing_technique="high_quality", | 
					
						
							|  |  |  |             collection_binding_id=dataset_collection_binding.id, | 
					
						
							| 
									
										
										
										
											2023-12-18 13:10:05 +08:00
										 |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-22 23:31:57 +08:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2024-08-26 13:38:37 +08:00
										 |  |  |             vector = Vector(dataset, attributes=["doc_id", "annotation_id", "app_id"]) | 
					
						
							|  |  |  |             vector.delete_by_metadata_field("annotation_id", annotation_id) | 
					
						
							| 
									
										
										
										
											2024-02-22 23:31:57 +08:00
										 |  |  |         except Exception: | 
					
						
							|  |  |  |             logging.exception("Delete annotation index failed when annotation deleted.") | 
					
						
							| 
									
										
										
										
											2023-12-18 13:10:05 +08:00
										 |  |  |         end_at = time.perf_counter() | 
					
						
							|  |  |  |         logging.info( | 
					
						
							| 
									
										
										
										
											2024-08-26 13:38:37 +08:00
										 |  |  |             click.style("App annotations index deleted : {} latency: {}".format(app_id, end_at - start_at), fg="green") | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2023-12-18 13:10:05 +08:00
										 |  |  |     except Exception as e: | 
					
						
							| 
									
										
										
										
											2024-11-15 15:41:40 +08:00
										 |  |  |         logging.exception("Annotation deleted index failed") |