From 48c2168dffe30ac16786b6a9e21a83be7e53332a Mon Sep 17 00:00:00 2001 From: Jyong <76649700+JohnJyong@users.noreply.github.com> Date: Thu, 3 Apr 2025 13:03:22 +0800 Subject: [PATCH] fix annotation failed when embedding model and dimension changed (#17347) --- .../disable_annotation_reply_task.py | 2 +- .../enable_annotation_reply_task.py | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/api/tasks/annotation/disable_annotation_reply_task.py b/api/tasks/annotation/disable_annotation_reply_task.py index 26bf1c7c9f..504fe5921b 100644 --- a/api/tasks/annotation/disable_annotation_reply_task.py +++ b/api/tasks/annotation/disable_annotation_reply_task.py @@ -46,7 +46,7 @@ def disable_annotation_reply_task(job_id: str, app_id: str, tenant_id: str): try: if annotations_count > 0: vector = Vector(dataset, attributes=["doc_id", "annotation_id", "app_id"]) - vector.delete_by_metadata_field("app_id", app_id) + vector.delete() except Exception: logging.exception("Delete annotation index failed when annotation deleted.") redis_client.setex(disable_app_annotation_job_key, 600, "completed") diff --git a/api/tasks/annotation/enable_annotation_reply_task.py b/api/tasks/annotation/enable_annotation_reply_task.py index b42af0c7fa..3dc10534df 100644 --- a/api/tasks/annotation/enable_annotation_reply_task.py +++ b/api/tasks/annotation/enable_annotation_reply_task.py @@ -49,6 +49,27 @@ def enable_annotation_reply_task( db.session.query(AppAnnotationSetting).filter(AppAnnotationSetting.app_id == app_id).first() ) if annotation_setting: + if dataset_collection_binding.id != annotation_setting.collection_binding_id: + old_dataset_collection_binding = ( + DatasetCollectionBindingService.get_dataset_collection_binding_by_id_and_type( + annotation_setting.collection_binding_id, "annotation" + ) + ) + if old_dataset_collection_binding and annotations: + old_dataset = Dataset( + id=app_id, + tenant_id=tenant_id, + indexing_technique="high_quality", + embedding_model_provider=old_dataset_collection_binding.provider_name, + embedding_model=old_dataset_collection_binding.model_name, + collection_binding_id=old_dataset_collection_binding.id, + ) + + old_vector = Vector(old_dataset, attributes=["doc_id", "annotation_id", "app_id"]) + try: + old_vector.delete() + except Exception as e: + logging.info(click.style("Delete annotation index error: {}".format(str(e)), fg="red")) annotation_setting.score_threshold = score_threshold annotation_setting.collection_binding_id = dataset_collection_binding.id annotation_setting.updated_user_id = user_id