2023-06-01 23:19:36 +08:00
|
|
|
import datetime
|
|
|
|
import logging
|
|
|
|
import time
|
|
|
|
|
|
|
|
import click
|
|
|
|
from celery import shared_task
|
2024-02-06 13:21:13 +08:00
|
|
|
from werkzeug.exceptions import NotFound
|
|
|
|
|
2024-01-12 12:34:01 +08:00
|
|
|
from core.indexing_runner import DocumentIsPausedException, IndexingRunner
|
2024-02-22 23:31:57 +08:00
|
|
|
from core.rag.index_processor.index_processor_factory import IndexProcessorFactory
|
2023-06-01 23:19:36 +08:00
|
|
|
from extensions.ext_database import db
|
2024-01-12 12:34:01 +08:00
|
|
|
from models.dataset import Dataset, Document, DocumentSegment
|
2023-06-01 23:19:36 +08:00
|
|
|
|
|
|
|
|
2024-08-26 13:38:37 +08:00
|
|
|
@shared_task(queue="dataset")
|
2023-06-01 23:19:36 +08:00
|
|
|
def document_indexing_update_task(dataset_id: str, document_id: str):
|
|
|
|
"""
|
|
|
|
Async update document
|
|
|
|
:param dataset_id:
|
|
|
|
:param document_id:
|
|
|
|
|
|
|
|
Usage: document_indexing_update_task.delay(dataset_id, document_id)
|
|
|
|
"""
|
2024-08-26 13:38:37 +08:00
|
|
|
logging.info(click.style("Start update document: {}".format(document_id), fg="green"))
|
2023-06-01 23:19:36 +08:00
|
|
|
start_at = time.perf_counter()
|
|
|
|
|
2024-08-26 13:38:37 +08:00
|
|
|
document = db.session.query(Document).filter(Document.id == document_id, Document.dataset_id == dataset_id).first()
|
2023-06-01 23:19:36 +08:00
|
|
|
|
|
|
|
if not document:
|
2024-08-26 13:38:37 +08:00
|
|
|
raise NotFound("Document not found")
|
2023-06-01 23:19:36 +08:00
|
|
|
|
2024-08-26 13:38:37 +08:00
|
|
|
document.indexing_status = "parsing"
|
2024-04-12 16:22:24 +08:00
|
|
|
document.processing_started_at = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None)
|
2023-06-01 23:19:36 +08:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# delete all document segment and index
|
|
|
|
try:
|
|
|
|
dataset = db.session.query(Dataset).filter(Dataset.id == dataset_id).first()
|
|
|
|
if not dataset:
|
2024-08-26 13:38:37 +08:00
|
|
|
raise Exception("Dataset not found")
|
2023-06-01 23:19:36 +08:00
|
|
|
|
2024-02-22 23:31:57 +08:00
|
|
|
index_type = document.doc_form
|
|
|
|
index_processor = IndexProcessorFactory(index_type).init_index_processor()
|
2023-06-01 23:19:36 +08:00
|
|
|
|
|
|
|
segments = db.session.query(DocumentSegment).filter(DocumentSegment.document_id == document_id).all()
|
2024-07-17 20:38:32 +08:00
|
|
|
if segments:
|
|
|
|
index_node_ids = [segment.index_node_id for segment in segments]
|
2023-06-01 23:19:36 +08:00
|
|
|
|
2024-07-17 20:38:32 +08:00
|
|
|
# delete from vector index
|
|
|
|
index_processor.clean(dataset, index_node_ids)
|
2023-06-01 23:19:36 +08:00
|
|
|
|
2024-07-17 20:38:32 +08:00
|
|
|
for segment in segments:
|
|
|
|
db.session.delete(segment)
|
|
|
|
db.session.commit()
|
2023-06-01 23:19:36 +08:00
|
|
|
end_at = time.perf_counter()
|
|
|
|
logging.info(
|
2024-08-26 13:38:37 +08:00
|
|
|
click.style(
|
|
|
|
"Cleaned document when document update data source or process rule: {} latency: {}".format(
|
|
|
|
document_id, end_at - start_at
|
|
|
|
),
|
|
|
|
fg="green",
|
|
|
|
)
|
|
|
|
)
|
2023-06-01 23:19:36 +08:00
|
|
|
except Exception:
|
|
|
|
logging.exception("Cleaned document when document update data source or process rule failed")
|
2023-06-25 16:49:14 +08:00
|
|
|
|
2023-06-01 23:19:36 +08:00
|
|
|
try:
|
|
|
|
indexing_runner = IndexingRunner()
|
2023-06-16 21:47:51 +08:00
|
|
|
indexing_runner.run([document])
|
2023-06-01 23:19:36 +08:00
|
|
|
end_at = time.perf_counter()
|
2024-08-26 13:38:37 +08:00
|
|
|
logging.info(click.style("update document: {} latency: {}".format(document.id, end_at - start_at), fg="green"))
|
2023-06-25 16:49:14 +08:00
|
|
|
except DocumentIsPausedException as ex:
|
2024-08-26 13:38:37 +08:00
|
|
|
logging.info(click.style(str(ex), fg="yellow"))
|
2023-06-25 16:49:14 +08:00
|
|
|
except Exception:
|
|
|
|
pass
|