Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

document_indexing_task.py 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import logging
  2. import time
  3. import click
  4. from celery import shared_task # type: ignore
  5. from configs import dify_config
  6. from core.indexing_runner import DocumentIsPausedError, IndexingRunner
  7. from extensions.ext_database import db
  8. from libs.datetime_utils import naive_utc_now
  9. from models.dataset import Dataset, Document
  10. from services.feature_service import FeatureService
  11. @shared_task(queue="dataset")
  12. def document_indexing_task(dataset_id: str, document_ids: list):
  13. """
  14. Async process document
  15. :param dataset_id:
  16. :param document_ids:
  17. Usage: document_indexing_task.delay(dataset_id, document_ids)
  18. """
  19. documents = []
  20. start_at = time.perf_counter()
  21. dataset = db.session.query(Dataset).filter(Dataset.id == dataset_id).first()
  22. if not dataset:
  23. logging.info(click.style("Dataset is not found: {}".format(dataset_id), fg="yellow"))
  24. db.session.close()
  25. return
  26. # check document limit
  27. features = FeatureService.get_features(dataset.tenant_id)
  28. try:
  29. if features.billing.enabled:
  30. vector_space = features.vector_space
  31. count = len(document_ids)
  32. batch_upload_limit = int(dify_config.BATCH_UPLOAD_LIMIT)
  33. if features.billing.subscription.plan == "sandbox" and count > 1:
  34. raise ValueError("Your current plan does not support batch upload, please upgrade your plan.")
  35. if count > batch_upload_limit:
  36. raise ValueError(f"You have reached the batch upload limit of {batch_upload_limit}.")
  37. if 0 < vector_space.limit <= vector_space.size:
  38. raise ValueError(
  39. "Your total number of documents plus the number of uploads have over the limit of "
  40. "your subscription."
  41. )
  42. except Exception as e:
  43. for document_id in document_ids:
  44. document = (
  45. db.session.query(Document).filter(Document.id == document_id, Document.dataset_id == dataset_id).first()
  46. )
  47. if document:
  48. document.indexing_status = "error"
  49. document.error = str(e)
  50. document.stopped_at = naive_utc_now()
  51. db.session.add(document)
  52. db.session.commit()
  53. db.session.close()
  54. return
  55. for document_id in document_ids:
  56. logging.info(click.style("Start process document: {}".format(document_id), fg="green"))
  57. document = (
  58. db.session.query(Document).filter(Document.id == document_id, Document.dataset_id == dataset_id).first()
  59. )
  60. if document:
  61. document.indexing_status = "parsing"
  62. document.processing_started_at = naive_utc_now()
  63. documents.append(document)
  64. db.session.add(document)
  65. db.session.commit()
  66. try:
  67. indexing_runner = IndexingRunner()
  68. indexing_runner.run(documents)
  69. end_at = time.perf_counter()
  70. logging.info(click.style("Processed dataset: {} latency: {}".format(dataset_id, end_at - start_at), fg="green"))
  71. except DocumentIsPausedError as ex:
  72. logging.info(click.style(str(ex), fg="yellow"))
  73. except Exception:
  74. logging.exception("Document indexing task failed, dataset_id: {}".format(dataset_id))
  75. finally:
  76. db.session.close()