Team Ai
Apppublic

Underground-Digital/Workflow-Engine

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
disable_annotation_reply_task.py69 linesDownload Raw Back to annotation
1import logging2import time3 4import click5from celery import shared_task6from werkzeug.exceptions import NotFound7 8from core.rag.datasource.vdb.vector_factory import Vector9from extensions.ext_database import db10from extensions.ext_redis import redis_client11from models.dataset import Dataset12from models.model import App, AppAnnotationSetting, MessageAnnotation13 14 15@shared_task(queue="dataset")16def disable_annotation_reply_task(job_id: str, app_id: str, tenant_id: str):17    """18    Async enable annotation reply task19    """20    logging.info(click.style("Start delete app annotations index: {}".format(app_id), fg="green"))21    start_at = time.perf_counter()22    # get app info23    app = db.session.query(App).filter(App.id == app_id, App.tenant_id == tenant_id, App.status == "normal").first()24    annotations_count = db.session.query(MessageAnnotation).filter(MessageAnnotation.app_id == app_id).count()25    if not app:26        raise NotFound("App not found")27 28    app_annotation_setting = (29        db.session.query(AppAnnotationSetting).filter(AppAnnotationSetting.app_id == app_id).first()30    )31 32    if not app_annotation_setting:33        raise NotFound("App annotation setting not found")34 35    disable_app_annotation_key = "disable_app_annotation_{}".format(str(app_id))36    disable_app_annotation_job_key = "disable_app_annotation_job_{}".format(str(job_id))37 38    try:39        dataset = Dataset(40            id=app_id,41            tenant_id=tenant_id,42            indexing_technique="high_quality",43            collection_binding_id=app_annotation_setting.collection_binding_id,44        )45 46        try:47            if annotations_count > 0:48                vector = Vector(dataset, attributes=["doc_id", "annotation_id", "app_id"])49                vector.delete_by_metadata_field("app_id", app_id)50        except Exception:51            logging.exception("Delete annotation index failed when annotation deleted.")52        redis_client.setex(disable_app_annotation_job_key, 600, "completed")53 54        # delete annotation setting55        db.session.delete(app_annotation_setting)56        db.session.commit()57 58        end_at = time.perf_counter()59        logging.info(60            click.style("App annotations index deleted : {} latency: {}".format(app_id, end_at - start_at), fg="green")61        )62    except Exception as e:63        logging.exception("Annotation batch deleted index failed:{}".format(str(e)))64        redis_client.setex(disable_app_annotation_job_key, 600, "error")65        disable_app_annotation_error_key = "disable_app_annotation_error_{}".format(str(job_id))66        redis_client.setex(disable_app_annotation_error_key, 600, str(e))67    finally:68        redis_client.delete(disable_app_annotation_key)69