您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

disable_annotation_reply_task.py 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import logging
  2. import time
  3. import click
  4. from celery import shared_task
  5. from core.rag.datasource.vdb.vector_factory import Vector
  6. from extensions.ext_database import db
  7. from extensions.ext_redis import redis_client
  8. from models.dataset import Dataset
  9. from models.model import App, AppAnnotationSetting, MessageAnnotation
  10. @shared_task(queue="dataset")
  11. def disable_annotation_reply_task(job_id: str, app_id: str, tenant_id: str):
  12. """
  13. Async enable annotation reply task
  14. """
  15. logging.info(click.style(f"Start delete app annotations index: {app_id}", fg="green"))
  16. start_at = time.perf_counter()
  17. # get app info
  18. app = db.session.query(App).where(App.id == app_id, App.tenant_id == tenant_id, App.status == "normal").first()
  19. annotations_count = db.session.query(MessageAnnotation).where(MessageAnnotation.app_id == app_id).count()
  20. if not app:
  21. logging.info(click.style(f"App not found: {app_id}", fg="red"))
  22. db.session.close()
  23. return
  24. app_annotation_setting = db.session.query(AppAnnotationSetting).where(AppAnnotationSetting.app_id == app_id).first()
  25. if not app_annotation_setting:
  26. logging.info(click.style(f"App annotation setting not found: {app_id}", fg="red"))
  27. db.session.close()
  28. return
  29. disable_app_annotation_key = f"disable_app_annotation_{str(app_id)}"
  30. disable_app_annotation_job_key = f"disable_app_annotation_job_{str(job_id)}"
  31. try:
  32. dataset = Dataset(
  33. id=app_id,
  34. tenant_id=tenant_id,
  35. indexing_technique="high_quality",
  36. collection_binding_id=app_annotation_setting.collection_binding_id,
  37. )
  38. try:
  39. if annotations_count > 0:
  40. vector = Vector(dataset, attributes=["doc_id", "annotation_id", "app_id"])
  41. vector.delete()
  42. except Exception:
  43. logging.exception("Delete annotation index failed when annotation deleted.")
  44. redis_client.setex(disable_app_annotation_job_key, 600, "completed")
  45. # delete annotation setting
  46. db.session.delete(app_annotation_setting)
  47. db.session.commit()
  48. end_at = time.perf_counter()
  49. logging.info(click.style(f"App annotations index deleted : {app_id} latency: {end_at - start_at}", fg="green"))
  50. except Exception as e:
  51. logging.exception("Annotation batch deleted index failed")
  52. redis_client.setex(disable_app_annotation_job_key, 600, "error")
  53. disable_app_annotation_error_key = f"disable_app_annotation_error_{str(job_id)}"
  54. redis_client.setex(disable_app_annotation_error_key, 600, str(e))
  55. finally:
  56. redis_client.delete(disable_app_annotation_key)
  57. db.session.close()