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.

duplicate_document_indexing_task.py 3.8KB

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