2023-05-15 08:51:32 +08:00
|
|
|
import datetime
|
|
|
|
import json
|
2023-06-25 16:49:14 +08:00
|
|
|
import logging
|
2023-05-15 08:51:32 +08:00
|
|
|
import re
|
2023-07-28 20:47:15 +08:00
|
|
|
import threading
|
2023-05-15 08:51:32 +08:00
|
|
|
import time
|
2023-06-25 16:49:14 +08:00
|
|
|
import uuid
|
2024-01-12 12:34:01 +08:00
|
|
|
from typing import AbstractSet, Any, Collection, List, Literal, Optional, Type, Union, cast
|
2023-05-15 08:51:32 +08:00
|
|
|
|
2023-06-25 16:49:14 +08:00
|
|
|
from core.data_loader.file_extractor import FileExtractor
|
|
|
|
from core.data_loader.loader.notion import NotionLoader
|
2023-12-07 09:24:52 +08:00
|
|
|
from core.docstore.dataset_docstore import DatasetDocumentStore
|
2024-01-12 12:34:01 +08:00
|
|
|
from core.errors.error import ProviderTokenNotInitError
|
2023-07-28 20:47:15 +08:00
|
|
|
from core.generator.llm_generator import LLMGenerator
|
2023-06-25 16:49:14 +08:00
|
|
|
from core.index.index import IndexBuilder
|
2024-01-12 18:45:34 +08:00
|
|
|
from core.model_manager import ModelManager, ModelInstance
|
2024-01-02 23:42:00 +08:00
|
|
|
from core.model_runtime.entities.model_entities import ModelType, PriceType
|
|
|
|
from core.model_runtime.model_providers.__base.large_language_model import LargeLanguageModel
|
|
|
|
from core.model_runtime.model_providers.__base.text_embedding_model import TextEmbeddingModel
|
2024-01-03 13:02:56 +08:00
|
|
|
from core.model_runtime.model_providers.__base.tokenizers.gpt2_tokenzier import GPT2Tokenizer
|
2024-01-12 12:34:01 +08:00
|
|
|
from core.spiltter.fixed_text_splitter import EnhanceRecursiveCharacterTextSplitter, FixedRecursiveCharacterTextSplitter
|
2023-05-15 08:51:32 +08:00
|
|
|
from extensions.ext_database import db
|
|
|
|
from extensions.ext_redis import redis_client
|
|
|
|
from extensions.ext_storage import storage
|
2024-01-12 12:34:01 +08:00
|
|
|
from flask import Flask, current_app
|
|
|
|
from flask_login import current_user
|
|
|
|
from langchain.schema import Document
|
|
|
|
from langchain.text_splitter import TS, TextSplitter, TokenTextSplitter
|
2023-06-25 16:49:14 +08:00
|
|
|
from libs import helper
|
2024-01-12 12:34:01 +08:00
|
|
|
from models.dataset import Dataset, DatasetProcessRule
|
2023-06-25 16:49:14 +08:00
|
|
|
from models.dataset import Document as DatasetDocument
|
2024-01-12 12:34:01 +08:00
|
|
|
from models.dataset import DocumentSegment
|
2023-05-15 08:51:32 +08:00
|
|
|
from models.model import UploadFile
|
2023-06-16 21:47:51 +08:00
|
|
|
from models.source import DataSourceBinding
|
2024-01-12 12:34:01 +08:00
|
|
|
from sqlalchemy.orm.exc import ObjectDeletedError
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
class IndexingRunner:
|
|
|
|
|
2023-08-12 00:57:00 +08:00
|
|
|
def __init__(self):
|
2023-05-15 08:51:32 +08:00
|
|
|
self.storage = storage
|
2024-01-02 23:42:00 +08:00
|
|
|
self.model_manager = ModelManager()
|
2023-05-15 08:51:32 +08:00
|
|
|
|
2023-06-25 16:49:14 +08:00
|
|
|
def run(self, dataset_documents: List[DatasetDocument]):
|
2023-05-15 08:51:32 +08:00
|
|
|
"""Run the indexing process."""
|
2023-06-25 16:49:14 +08:00
|
|
|
for dataset_document in dataset_documents:
|
|
|
|
try:
|
|
|
|
# get dataset
|
|
|
|
dataset = Dataset.query.filter_by(
|
|
|
|
id=dataset_document.dataset_id
|
|
|
|
).first()
|
|
|
|
|
|
|
|
if not dataset:
|
|
|
|
raise ValueError("no dataset found")
|
|
|
|
|
|
|
|
# get the process rule
|
|
|
|
processing_rule = db.session.query(DatasetProcessRule). \
|
|
|
|
filter(DatasetProcessRule.id == dataset_document.dataset_process_rule_id). \
|
|
|
|
first()
|
|
|
|
|
2023-11-17 22:13:37 +08:00
|
|
|
# load file
|
2024-01-04 16:21:48 +08:00
|
|
|
text_docs = self._load_data(dataset_document, processing_rule.mode == 'automatic')
|
2023-11-17 22:13:37 +08:00
|
|
|
|
2024-01-12 18:45:34 +08:00
|
|
|
# get embedding model instance
|
|
|
|
embedding_model_instance = None
|
|
|
|
if dataset.indexing_technique == 'high_quality':
|
|
|
|
if dataset.embedding_model_provider:
|
|
|
|
embedding_model_instance = self.model_manager.get_model_instance(
|
|
|
|
tenant_id=dataset.tenant_id,
|
|
|
|
provider=dataset.embedding_model_provider,
|
|
|
|
model_type=ModelType.TEXT_EMBEDDING,
|
|
|
|
model=dataset.embedding_model
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
embedding_model_instance = self.model_manager.get_default_model_instance(
|
|
|
|
tenant_id=dataset.tenant_id,
|
|
|
|
model_type=ModelType.TEXT_EMBEDDING,
|
|
|
|
)
|
|
|
|
|
2023-06-25 16:49:14 +08:00
|
|
|
# get splitter
|
2024-01-12 18:45:34 +08:00
|
|
|
splitter = self._get_splitter(processing_rule, embedding_model_instance)
|
2023-06-25 16:49:14 +08:00
|
|
|
|
|
|
|
# split to documents
|
|
|
|
documents = self._step_split(
|
|
|
|
text_docs=text_docs,
|
|
|
|
splitter=splitter,
|
|
|
|
dataset=dataset,
|
|
|
|
dataset_document=dataset_document,
|
|
|
|
processing_rule=processing_rule
|
|
|
|
)
|
|
|
|
self._build_index(
|
|
|
|
dataset=dataset,
|
|
|
|
dataset_document=dataset_document,
|
|
|
|
documents=documents
|
|
|
|
)
|
|
|
|
except DocumentIsPausedException:
|
|
|
|
raise DocumentIsPausedException('Document paused, document id: {}'.format(dataset_document.id))
|
|
|
|
except ProviderTokenNotInitError as e:
|
|
|
|
dataset_document.indexing_status = 'error'
|
|
|
|
dataset_document.error = str(e.description)
|
|
|
|
dataset_document.stopped_at = datetime.datetime.utcnow()
|
|
|
|
db.session.commit()
|
2023-10-12 13:30:44 +08:00
|
|
|
except ObjectDeletedError:
|
|
|
|
logging.warning('Document deleted, document id: {}'.format(dataset_document.id))
|
2023-06-25 16:49:14 +08:00
|
|
|
except Exception as e:
|
|
|
|
logging.exception("consume document failed")
|
|
|
|
dataset_document.indexing_status = 'error'
|
|
|
|
dataset_document.error = str(e)
|
|
|
|
dataset_document.stopped_at = datetime.datetime.utcnow()
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
def run_in_splitting_status(self, dataset_document: DatasetDocument):
|
|
|
|
"""Run the indexing process when the index_status is splitting."""
|
|
|
|
try:
|
2023-06-16 21:47:51 +08:00
|
|
|
# get dataset
|
|
|
|
dataset = Dataset.query.filter_by(
|
2023-06-25 16:49:14 +08:00
|
|
|
id=dataset_document.dataset_id
|
2023-06-16 21:47:51 +08:00
|
|
|
).first()
|
|
|
|
|
|
|
|
if not dataset:
|
|
|
|
raise ValueError("no dataset found")
|
|
|
|
|
2023-06-25 16:49:14 +08:00
|
|
|
# get exist document_segment list and delete
|
|
|
|
document_segments = DocumentSegment.query.filter_by(
|
|
|
|
dataset_id=dataset.id,
|
|
|
|
document_id=dataset_document.id
|
|
|
|
).all()
|
|
|
|
|
2023-12-07 09:24:39 +08:00
|
|
|
for document_segment in document_segments:
|
|
|
|
db.session.delete(document_segment)
|
2023-06-25 16:49:14 +08:00
|
|
|
db.session.commit()
|
2023-06-16 21:47:51 +08:00
|
|
|
# get the process rule
|
|
|
|
processing_rule = db.session.query(DatasetProcessRule). \
|
2023-06-25 16:49:14 +08:00
|
|
|
filter(DatasetProcessRule.id == dataset_document.dataset_process_rule_id). \
|
2023-06-16 21:47:51 +08:00
|
|
|
first()
|
|
|
|
|
2024-01-04 16:21:48 +08:00
|
|
|
# load file
|
|
|
|
text_docs = self._load_data(dataset_document, processing_rule.mode == 'automatic')
|
|
|
|
|
2024-01-12 18:45:34 +08:00
|
|
|
# get embedding model instance
|
|
|
|
embedding_model_instance = None
|
|
|
|
if dataset.indexing_technique == 'high_quality':
|
|
|
|
if dataset.embedding_model_provider:
|
|
|
|
embedding_model_instance = self.model_manager.get_model_instance(
|
|
|
|
tenant_id=dataset.tenant_id,
|
|
|
|
provider=dataset.embedding_model_provider,
|
|
|
|
model_type=ModelType.TEXT_EMBEDDING,
|
|
|
|
model=dataset.embedding_model
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
embedding_model_instance = self.model_manager.get_default_model_instance(
|
|
|
|
tenant_id=dataset.tenant_id,
|
|
|
|
model_type=ModelType.TEXT_EMBEDDING,
|
|
|
|
)
|
|
|
|
|
2023-06-25 16:49:14 +08:00
|
|
|
# get splitter
|
2024-01-12 18:45:34 +08:00
|
|
|
splitter = self._get_splitter(processing_rule, embedding_model_instance)
|
2023-06-16 21:47:51 +08:00
|
|
|
|
2023-06-25 16:49:14 +08:00
|
|
|
# split to documents
|
|
|
|
documents = self._step_split(
|
2023-06-16 21:47:51 +08:00
|
|
|
text_docs=text_docs,
|
2023-06-25 16:49:14 +08:00
|
|
|
splitter=splitter,
|
2023-06-16 21:47:51 +08:00
|
|
|
dataset=dataset,
|
2023-06-25 16:49:14 +08:00
|
|
|
dataset_document=dataset_document,
|
2023-06-16 21:47:51 +08:00
|
|
|
processing_rule=processing_rule
|
|
|
|
)
|
2023-05-15 08:51:32 +08:00
|
|
|
|
2023-06-16 21:47:51 +08:00
|
|
|
# build index
|
|
|
|
self._build_index(
|
|
|
|
dataset=dataset,
|
2023-06-25 16:49:14 +08:00
|
|
|
dataset_document=dataset_document,
|
|
|
|
documents=documents
|
2023-06-16 21:47:51 +08:00
|
|
|
)
|
2023-06-25 16:49:14 +08:00
|
|
|
except DocumentIsPausedException:
|
|
|
|
raise DocumentIsPausedException('Document paused, document id: {}'.format(dataset_document.id))
|
|
|
|
except ProviderTokenNotInitError as e:
|
|
|
|
dataset_document.indexing_status = 'error'
|
|
|
|
dataset_document.error = str(e.description)
|
|
|
|
dataset_document.stopped_at = datetime.datetime.utcnow()
|
|
|
|
db.session.commit()
|
|
|
|
except Exception as e:
|
|
|
|
logging.exception("consume document failed")
|
|
|
|
dataset_document.indexing_status = 'error'
|
|
|
|
dataset_document.error = str(e)
|
|
|
|
dataset_document.stopped_at = datetime.datetime.utcnow()
|
|
|
|
db.session.commit()
|
2023-05-15 08:51:32 +08:00
|
|
|
|
2023-06-25 16:49:14 +08:00
|
|
|
def run_in_indexing_status(self, dataset_document: DatasetDocument):
|
|
|
|
"""Run the indexing process when the index_status is indexing."""
|
|
|
|
try:
|
|
|
|
# get dataset
|
|
|
|
dataset = Dataset.query.filter_by(
|
|
|
|
id=dataset_document.dataset_id
|
|
|
|
).first()
|
2023-05-15 08:51:32 +08:00
|
|
|
|
2023-06-25 16:49:14 +08:00
|
|
|
if not dataset:
|
|
|
|
raise ValueError("no dataset found")
|
2023-05-15 08:51:32 +08:00
|
|
|
|
2023-06-25 16:49:14 +08:00
|
|
|
# get exist document_segment list and delete
|
|
|
|
document_segments = DocumentSegment.query.filter_by(
|
|
|
|
dataset_id=dataset.id,
|
|
|
|
document_id=dataset_document.id
|
|
|
|
).all()
|
|
|
|
|
|
|
|
documents = []
|
|
|
|
if document_segments:
|
|
|
|
for document_segment in document_segments:
|
|
|
|
# transform segment to node
|
|
|
|
if document_segment.status != "completed":
|
|
|
|
document = Document(
|
|
|
|
page_content=document_segment.content,
|
|
|
|
metadata={
|
|
|
|
"doc_id": document_segment.index_node_id,
|
|
|
|
"doc_hash": document_segment.index_node_hash,
|
|
|
|
"document_id": document_segment.document_id,
|
|
|
|
"dataset_id": document_segment.dataset_id,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
documents.append(document)
|
2023-05-15 08:51:32 +08:00
|
|
|
|
2023-06-25 16:49:14 +08:00
|
|
|
# build index
|
|
|
|
self._build_index(
|
|
|
|
dataset=dataset,
|
|
|
|
dataset_document=dataset_document,
|
|
|
|
documents=documents
|
|
|
|
)
|
|
|
|
except DocumentIsPausedException:
|
|
|
|
raise DocumentIsPausedException('Document paused, document id: {}'.format(dataset_document.id))
|
|
|
|
except ProviderTokenNotInitError as e:
|
|
|
|
dataset_document.indexing_status = 'error'
|
|
|
|
dataset_document.error = str(e.description)
|
|
|
|
dataset_document.stopped_at = datetime.datetime.utcnow()
|
|
|
|
db.session.commit()
|
|
|
|
except Exception as e:
|
|
|
|
logging.exception("consume document failed")
|
|
|
|
dataset_document.indexing_status = 'error'
|
|
|
|
dataset_document.error = str(e)
|
|
|
|
dataset_document.stopped_at = datetime.datetime.utcnow()
|
|
|
|
db.session.commit()
|
2023-05-15 08:51:32 +08:00
|
|
|
|
2023-08-12 00:57:00 +08:00
|
|
|
def file_indexing_estimate(self, tenant_id: str, file_details: List[UploadFile], tmp_processing_rule: dict,
|
2023-08-29 03:37:45 +08:00
|
|
|
doc_form: str = None, doc_language: str = 'English', dataset_id: str = None,
|
|
|
|
indexing_technique: str = 'economy') -> dict:
|
2023-05-15 08:51:32 +08:00
|
|
|
"""
|
|
|
|
Estimate the indexing for the document.
|
|
|
|
"""
|
2024-01-02 23:42:00 +08:00
|
|
|
embedding_model_instance = None
|
2023-08-18 17:37:31 +08:00
|
|
|
if dataset_id:
|
|
|
|
dataset = Dataset.query.filter_by(
|
|
|
|
id=dataset_id
|
|
|
|
).first()
|
|
|
|
if not dataset:
|
|
|
|
raise ValueError('Dataset not found.')
|
2023-08-29 03:37:45 +08:00
|
|
|
if dataset.indexing_technique == 'high_quality' or indexing_technique == 'high_quality':
|
2024-01-10 20:48:16 +08:00
|
|
|
if dataset.embedding_model_provider:
|
|
|
|
embedding_model_instance = self.model_manager.get_model_instance(
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
provider=dataset.embedding_model_provider,
|
|
|
|
model_type=ModelType.TEXT_EMBEDDING,
|
|
|
|
model=dataset.embedding_model
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
embedding_model_instance = self.model_manager.get_default_model_instance(
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
model_type=ModelType.TEXT_EMBEDDING,
|
|
|
|
)
|
2023-08-18 17:37:31 +08:00
|
|
|
else:
|
2023-08-29 03:37:45 +08:00
|
|
|
if indexing_technique == 'high_quality':
|
2024-01-02 23:42:00 +08:00
|
|
|
embedding_model_instance = self.model_manager.get_default_model_instance(
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
model_type=ModelType.TEXT_EMBEDDING,
|
2023-08-29 03:37:45 +08:00
|
|
|
)
|
2023-06-16 21:47:51 +08:00
|
|
|
tokens = 0
|
|
|
|
preview_texts = []
|
|
|
|
total_segments = 0
|
2024-01-19 13:27:12 +08:00
|
|
|
total_price = 0
|
|
|
|
currency = 'USD'
|
2023-06-16 21:47:51 +08:00
|
|
|
for file_detail in file_details:
|
|
|
|
|
|
|
|
processing_rule = DatasetProcessRule(
|
|
|
|
mode=tmp_processing_rule["mode"],
|
|
|
|
rules=json.dumps(tmp_processing_rule["rules"])
|
|
|
|
)
|
2023-05-15 08:51:32 +08:00
|
|
|
|
2024-01-04 16:21:48 +08:00
|
|
|
# load data from file
|
|
|
|
text_docs = FileExtractor.load(file_detail, is_automatic=processing_rule.mode == 'automatic')
|
|
|
|
|
2023-06-25 16:49:14 +08:00
|
|
|
# get splitter
|
2024-01-12 18:45:34 +08:00
|
|
|
splitter = self._get_splitter(processing_rule, embedding_model_instance)
|
2023-05-15 08:51:32 +08:00
|
|
|
|
2023-06-25 16:49:14 +08:00
|
|
|
# split to documents
|
2023-07-28 20:47:15 +08:00
|
|
|
documents = self._split_to_documents_for_estimate(
|
2023-06-16 21:47:51 +08:00
|
|
|
text_docs=text_docs,
|
2023-06-25 16:49:14 +08:00
|
|
|
splitter=splitter,
|
2023-06-16 21:47:51 +08:00
|
|
|
processing_rule=processing_rule
|
|
|
|
)
|
2023-08-12 00:57:00 +08:00
|
|
|
|
2023-06-25 16:49:14 +08:00
|
|
|
total_segments += len(documents)
|
2023-08-12 00:57:00 +08:00
|
|
|
|
2023-06-25 16:49:14 +08:00
|
|
|
for document in documents:
|
2023-06-16 21:47:51 +08:00
|
|
|
if len(preview_texts) < 5:
|
2023-06-25 16:49:14 +08:00
|
|
|
preview_texts.append(document.page_content)
|
2024-01-02 23:42:00 +08:00
|
|
|
if indexing_technique == 'high_quality' or embedding_model_instance:
|
|
|
|
embedding_model_type_instance = embedding_model_instance.model_type_instance
|
|
|
|
embedding_model_type_instance = cast(TextEmbeddingModel, embedding_model_type_instance)
|
|
|
|
tokens += embedding_model_type_instance.get_num_tokens(
|
|
|
|
model=embedding_model_instance.model,
|
|
|
|
credentials=embedding_model_instance.credentials,
|
|
|
|
texts=[self.filter_string(document.page_content)]
|
|
|
|
)
|
2023-08-12 00:57:00 +08:00
|
|
|
|
2023-07-28 20:47:15 +08:00
|
|
|
if doc_form and doc_form == 'qa_model':
|
2024-01-02 23:42:00 +08:00
|
|
|
model_instance = self.model_manager.get_default_model_instance(
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
model_type=ModelType.LLM
|
2023-08-18 17:37:31 +08:00
|
|
|
)
|
2024-01-02 23:42:00 +08:00
|
|
|
|
|
|
|
model_type_instance = model_instance.model_type_instance
|
|
|
|
model_type_instance = cast(LargeLanguageModel, model_type_instance)
|
|
|
|
|
2023-07-28 20:47:15 +08:00
|
|
|
if len(preview_texts) > 0:
|
|
|
|
# qa model document
|
2023-10-12 13:30:44 +08:00
|
|
|
response = LLMGenerator.generate_qa_document(current_user.current_tenant_id, preview_texts[0],
|
|
|
|
doc_language)
|
2023-07-28 20:47:15 +08:00
|
|
|
document_qa_list = self.format_split_text(response)
|
2024-01-02 23:42:00 +08:00
|
|
|
price_info = model_type_instance.get_price(
|
|
|
|
model=model_instance.model,
|
|
|
|
credentials=model_instance.credentials,
|
|
|
|
price_type=PriceType.INPUT,
|
|
|
|
tokens=total_segments * 2000,
|
|
|
|
)
|
2023-07-28 20:47:15 +08:00
|
|
|
return {
|
|
|
|
"total_segments": total_segments * 20,
|
|
|
|
"tokens": total_segments * 2000,
|
2024-01-02 23:42:00 +08:00
|
|
|
"total_price": '{:f}'.format(price_info.total_amount),
|
|
|
|
"currency": price_info.currency,
|
2023-07-28 20:47:15 +08:00
|
|
|
"qa_preview": document_qa_list,
|
|
|
|
"preview": preview_texts
|
|
|
|
}
|
2024-01-02 23:42:00 +08:00
|
|
|
if embedding_model_instance:
|
|
|
|
embedding_model_type_instance = cast(TextEmbeddingModel, embedding_model_instance.model_type_instance)
|
|
|
|
embedding_price_info = embedding_model_type_instance.get_price(
|
|
|
|
model=embedding_model_instance.model,
|
|
|
|
credentials=embedding_model_instance.credentials,
|
|
|
|
price_type=PriceType.INPUT,
|
|
|
|
tokens=tokens
|
|
|
|
)
|
2024-01-19 13:27:12 +08:00
|
|
|
total_price = '{:f}'.format(embedding_price_info.total_amount)
|
|
|
|
currency = embedding_price_info.currency
|
2023-06-16 21:47:51 +08:00
|
|
|
return {
|
|
|
|
"total_segments": total_segments,
|
|
|
|
"tokens": tokens,
|
2024-01-19 13:27:12 +08:00
|
|
|
"total_price": total_price,
|
|
|
|
"currency": currency,
|
2023-06-16 21:47:51 +08:00
|
|
|
"preview": preview_texts
|
|
|
|
}
|
|
|
|
|
2023-08-18 17:37:31 +08:00
|
|
|
def notion_indexing_estimate(self, tenant_id: str, notion_info_list: list, tmp_processing_rule: dict,
|
2023-08-29 03:37:45 +08:00
|
|
|
doc_form: str = None, doc_language: str = 'English', dataset_id: str = None,
|
|
|
|
indexing_technique: str = 'economy') -> dict:
|
2023-06-16 21:47:51 +08:00
|
|
|
"""
|
|
|
|
Estimate the indexing for the document.
|
|
|
|
"""
|
2024-01-02 23:42:00 +08:00
|
|
|
embedding_model_instance = None
|
2023-08-18 17:37:31 +08:00
|
|
|
if dataset_id:
|
|
|
|
dataset = Dataset.query.filter_by(
|
|
|
|
id=dataset_id
|
|
|
|
).first()
|
|
|
|
if not dataset:
|
|
|
|
raise ValueError('Dataset not found.')
|
2023-08-29 03:37:45 +08:00
|
|
|
if dataset.indexing_technique == 'high_quality' or indexing_technique == 'high_quality':
|
2024-01-10 20:48:16 +08:00
|
|
|
if dataset.embedding_model_provider:
|
|
|
|
embedding_model_instance = self.model_manager.get_model_instance(
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
provider=dataset.embedding_model_provider,
|
|
|
|
model_type=ModelType.TEXT_EMBEDDING,
|
|
|
|
model=dataset.embedding_model
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
embedding_model_instance = self.model_manager.get_default_model_instance(
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
model_type=ModelType.TEXT_EMBEDDING,
|
|
|
|
)
|
2023-08-18 17:37:31 +08:00
|
|
|
else:
|
2023-08-29 03:37:45 +08:00
|
|
|
if indexing_technique == 'high_quality':
|
2024-01-02 23:42:00 +08:00
|
|
|
embedding_model_instance = self.model_manager.get_default_model_instance(
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
model_type=ModelType.TEXT_EMBEDDING
|
2023-08-29 03:37:45 +08:00
|
|
|
)
|
2023-06-16 21:47:51 +08:00
|
|
|
# load data from notion
|
2023-05-15 08:51:32 +08:00
|
|
|
tokens = 0
|
|
|
|
preview_texts = []
|
2023-06-16 21:47:51 +08:00
|
|
|
total_segments = 0
|
2024-01-19 13:27:12 +08:00
|
|
|
total_price = 0
|
|
|
|
currency = 'USD'
|
2023-06-16 21:47:51 +08:00
|
|
|
for notion_info in notion_info_list:
|
|
|
|
workspace_id = notion_info['workspace_id']
|
|
|
|
data_source_binding = DataSourceBinding.query.filter(
|
|
|
|
db.and_(
|
|
|
|
DataSourceBinding.tenant_id == current_user.current_tenant_id,
|
|
|
|
DataSourceBinding.provider == 'notion',
|
|
|
|
DataSourceBinding.disabled == False,
|
|
|
|
DataSourceBinding.source_info['workspace_id'] == f'"{workspace_id}"'
|
|
|
|
)
|
|
|
|
).first()
|
|
|
|
if not data_source_binding:
|
|
|
|
raise ValueError('Data source binding not found.')
|
2023-06-25 16:49:14 +08:00
|
|
|
|
2023-06-16 21:47:51 +08:00
|
|
|
for page in notion_info['pages']:
|
2023-06-25 16:49:14 +08:00
|
|
|
loader = NotionLoader(
|
|
|
|
notion_access_token=data_source_binding.access_token,
|
|
|
|
notion_workspace_id=workspace_id,
|
|
|
|
notion_obj_id=page['page_id'],
|
|
|
|
notion_page_type=page['type']
|
|
|
|
)
|
|
|
|
documents = loader.load()
|
|
|
|
|
2023-06-16 21:47:51 +08:00
|
|
|
processing_rule = DatasetProcessRule(
|
|
|
|
mode=tmp_processing_rule["mode"],
|
|
|
|
rules=json.dumps(tmp_processing_rule["rules"])
|
|
|
|
)
|
|
|
|
|
2023-06-25 16:49:14 +08:00
|
|
|
# get splitter
|
2024-01-12 18:45:34 +08:00
|
|
|
splitter = self._get_splitter(processing_rule, embedding_model_instance)
|
2023-06-16 21:47:51 +08:00
|
|
|
|
2023-06-25 16:49:14 +08:00
|
|
|
# split to documents
|
2023-07-28 20:47:15 +08:00
|
|
|
documents = self._split_to_documents_for_estimate(
|
2023-06-16 21:47:51 +08:00
|
|
|
text_docs=documents,
|
2023-06-25 16:49:14 +08:00
|
|
|
splitter=splitter,
|
2023-06-16 21:47:51 +08:00
|
|
|
processing_rule=processing_rule
|
|
|
|
)
|
2023-06-25 16:49:14 +08:00
|
|
|
total_segments += len(documents)
|
2024-01-02 23:42:00 +08:00
|
|
|
|
2024-01-04 13:28:52 +08:00
|
|
|
embedding_model_type_instance = None
|
|
|
|
if embedding_model_instance:
|
|
|
|
embedding_model_type_instance = embedding_model_instance.model_type_instance
|
|
|
|
embedding_model_type_instance = cast(TextEmbeddingModel, embedding_model_type_instance)
|
2024-01-02 23:42:00 +08:00
|
|
|
|
2023-06-25 16:49:14 +08:00
|
|
|
for document in documents:
|
2023-06-16 21:47:51 +08:00
|
|
|
if len(preview_texts) < 5:
|
2023-06-25 16:49:14 +08:00
|
|
|
preview_texts.append(document.page_content)
|
2024-01-04 13:28:52 +08:00
|
|
|
if indexing_technique == 'high_quality' and embedding_model_type_instance:
|
2024-01-02 23:42:00 +08:00
|
|
|
tokens += embedding_model_type_instance.get_num_tokens(
|
|
|
|
model=embedding_model_instance.model,
|
|
|
|
credentials=embedding_model_instance.credentials,
|
|
|
|
texts=[document.page_content]
|
|
|
|
)
|
2023-08-12 00:57:00 +08:00
|
|
|
|
2023-07-28 20:47:15 +08:00
|
|
|
if doc_form and doc_form == 'qa_model':
|
2024-01-02 23:42:00 +08:00
|
|
|
model_instance = self.model_manager.get_default_model_instance(
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
model_type=ModelType.LLM
|
2023-08-18 17:37:31 +08:00
|
|
|
)
|
2024-01-02 23:42:00 +08:00
|
|
|
|
|
|
|
model_type_instance = model_instance.model_type_instance
|
|
|
|
model_type_instance = cast(LargeLanguageModel, model_type_instance)
|
2023-07-28 20:47:15 +08:00
|
|
|
if len(preview_texts) > 0:
|
|
|
|
# qa model document
|
2023-10-12 13:30:44 +08:00
|
|
|
response = LLMGenerator.generate_qa_document(current_user.current_tenant_id, preview_texts[0],
|
|
|
|
doc_language)
|
2023-07-28 20:47:15 +08:00
|
|
|
document_qa_list = self.format_split_text(response)
|
2024-01-02 23:42:00 +08:00
|
|
|
|
|
|
|
price_info = model_type_instance.get_price(
|
|
|
|
model=model_instance.model,
|
|
|
|
credentials=model_instance.credentials,
|
|
|
|
price_type=PriceType.INPUT,
|
|
|
|
tokens=total_segments * 2000,
|
|
|
|
)
|
|
|
|
|
2023-07-28 20:47:15 +08:00
|
|
|
return {
|
|
|
|
"total_segments": total_segments * 20,
|
|
|
|
"tokens": total_segments * 2000,
|
2024-01-02 23:42:00 +08:00
|
|
|
"total_price": '{:f}'.format(price_info.total_amount),
|
|
|
|
"currency": price_info.currency,
|
2023-07-28 20:47:15 +08:00
|
|
|
"qa_preview": document_qa_list,
|
|
|
|
"preview": preview_texts
|
|
|
|
}
|
2024-01-19 13:27:12 +08:00
|
|
|
if embedding_model_instance:
|
|
|
|
embedding_model_type_instance = embedding_model_instance.model_type_instance
|
|
|
|
embedding_model_type_instance = cast(TextEmbeddingModel, embedding_model_type_instance)
|
|
|
|
embedding_price_info = embedding_model_type_instance.get_price(
|
|
|
|
model=embedding_model_instance.model,
|
|
|
|
credentials=embedding_model_instance.credentials,
|
|
|
|
price_type=PriceType.INPUT,
|
|
|
|
tokens=tokens
|
|
|
|
)
|
|
|
|
total_price = '{:f}'.format(embedding_price_info.total_amount)
|
|
|
|
currency = embedding_price_info.currency
|
2023-05-15 08:51:32 +08:00
|
|
|
return {
|
2023-06-16 21:47:51 +08:00
|
|
|
"total_segments": total_segments,
|
2023-05-15 08:51:32 +08:00
|
|
|
"tokens": tokens,
|
2024-01-19 13:27:12 +08:00
|
|
|
"total_price": total_price,
|
|
|
|
"currency": currency,
|
2023-05-15 08:51:32 +08:00
|
|
|
"preview": preview_texts
|
|
|
|
}
|
|
|
|
|
2023-11-17 22:13:37 +08:00
|
|
|
def _load_data(self, dataset_document: DatasetDocument, automatic: bool = False) -> List[Document]:
|
2023-05-15 08:51:32 +08:00
|
|
|
# load file
|
2023-06-25 16:49:14 +08:00
|
|
|
if dataset_document.data_source_type not in ["upload_file", "notion_import"]:
|
2023-05-15 08:51:32 +08:00
|
|
|
return []
|
|
|
|
|
2023-06-25 16:49:14 +08:00
|
|
|
data_source_info = dataset_document.data_source_info_dict
|
2023-06-16 21:47:51 +08:00
|
|
|
text_docs = []
|
2023-06-25 16:49:14 +08:00
|
|
|
if dataset_document.data_source_type == 'upload_file':
|
2023-06-16 21:47:51 +08:00
|
|
|
if not data_source_info or 'upload_file_id' not in data_source_info:
|
|
|
|
raise ValueError("no upload file found")
|
|
|
|
|
|
|
|
file_detail = db.session.query(UploadFile). \
|
|
|
|
filter(UploadFile.id == data_source_info['upload_file_id']). \
|
|
|
|
one_or_none()
|
|
|
|
|
2023-08-30 11:14:16 +08:00
|
|
|
if file_detail:
|
2024-01-04 16:21:48 +08:00
|
|
|
text_docs = FileExtractor.load(file_detail, is_automatic=automatic)
|
2023-06-25 16:49:14 +08:00
|
|
|
elif dataset_document.data_source_type == 'notion_import':
|
|
|
|
loader = NotionLoader.from_document(dataset_document)
|
|
|
|
text_docs = loader.load()
|
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
# update document status to splitting
|
|
|
|
self._update_document_index_status(
|
2023-06-25 16:49:14 +08:00
|
|
|
document_id=dataset_document.id,
|
2023-05-15 08:51:32 +08:00
|
|
|
after_indexing_status="splitting",
|
|
|
|
extra_update_params={
|
2023-06-25 16:49:14 +08:00
|
|
|
DatasetDocument.word_count: sum([len(text_doc.page_content) for text_doc in text_docs]),
|
|
|
|
DatasetDocument.parsing_completed_at: datetime.datetime.utcnow()
|
2023-05-15 08:51:32 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
# replace doc id to document model id
|
2023-06-25 16:49:14 +08:00
|
|
|
text_docs = cast(List[Document], text_docs)
|
2023-05-15 08:51:32 +08:00
|
|
|
for text_doc in text_docs:
|
|
|
|
# remove invalid symbol
|
2023-06-25 16:49:14 +08:00
|
|
|
text_doc.page_content = self.filter_string(text_doc.page_content)
|
|
|
|
text_doc.metadata['document_id'] = dataset_document.id
|
|
|
|
text_doc.metadata['dataset_id'] = dataset_document.dataset_id
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
return text_docs
|
|
|
|
|
|
|
|
def filter_string(self, text):
|
2023-06-28 14:58:40 +08:00
|
|
|
text = re.sub(r'<\|', '<', text)
|
|
|
|
text = re.sub(r'\|>', '>', text)
|
2024-01-15 16:52:18 +08:00
|
|
|
text = re.sub(r'[\x00-\x08\x0B\x0C\x0E-\x1F\x7F\xEF\xBF\xBE]', '', text)
|
|
|
|
# Unicode U+FFFE
|
|
|
|
text = re.sub(u'\uFFFE', '', text)
|
2023-06-28 14:58:40 +08:00
|
|
|
return text
|
2023-05-15 08:51:32 +08:00
|
|
|
|
2024-01-12 18:45:34 +08:00
|
|
|
def _get_splitter(self, processing_rule: DatasetProcessRule,
|
|
|
|
embedding_model_instance: Optional[ModelInstance]) -> TextSplitter:
|
2023-05-15 08:51:32 +08:00
|
|
|
"""
|
|
|
|
Get the NodeParser object according to the processing rule.
|
|
|
|
"""
|
|
|
|
if processing_rule.mode == "custom":
|
|
|
|
# The user-defined segmentation rule
|
|
|
|
rules = json.loads(processing_rule.rules)
|
|
|
|
segmentation = rules["segmentation"]
|
|
|
|
if segmentation["max_tokens"] < 50 or segmentation["max_tokens"] > 1000:
|
|
|
|
raise ValueError("Custom segment length should be between 50 and 1000.")
|
|
|
|
|
|
|
|
separator = segmentation["separator"]
|
2023-05-16 12:57:25 +08:00
|
|
|
if separator:
|
2023-05-15 08:51:32 +08:00
|
|
|
separator = separator.replace('\\n', '\n')
|
|
|
|
|
2024-01-12 18:45:34 +08:00
|
|
|
character_splitter = FixedRecursiveCharacterTextSplitter.from_encoder(
|
2023-05-15 08:51:32 +08:00
|
|
|
chunk_size=segmentation["max_tokens"],
|
2024-01-26 13:24:40 +08:00
|
|
|
chunk_overlap=segmentation.get('chunk_overlap', 0),
|
2023-05-16 12:57:25 +08:00
|
|
|
fixed_separator=separator,
|
2024-01-12 18:45:34 +08:00
|
|
|
separators=["\n\n", "。", ".", " ", ""],
|
|
|
|
embedding_model_instance=embedding_model_instance
|
2023-05-15 08:51:32 +08:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
# Automatic segmentation
|
2024-01-12 18:45:34 +08:00
|
|
|
character_splitter = EnhanceRecursiveCharacterTextSplitter.from_encoder(
|
2023-05-15 08:51:32 +08:00
|
|
|
chunk_size=DatasetProcessRule.AUTOMATIC_RULES['segmentation']['max_tokens'],
|
2024-01-26 13:24:40 +08:00
|
|
|
chunk_overlap=DatasetProcessRule.AUTOMATIC_RULES['segmentation']['chunk_overlap'],
|
2024-01-12 18:45:34 +08:00
|
|
|
separators=["\n\n", "。", ".", " ", ""],
|
|
|
|
embedding_model_instance=embedding_model_instance
|
2023-05-15 08:51:32 +08:00
|
|
|
)
|
|
|
|
|
2023-06-25 16:49:14 +08:00
|
|
|
return character_splitter
|
2023-05-15 08:51:32 +08:00
|
|
|
|
2023-06-25 16:49:14 +08:00
|
|
|
def _step_split(self, text_docs: List[Document], splitter: TextSplitter,
|
|
|
|
dataset: Dataset, dataset_document: DatasetDocument, processing_rule: DatasetProcessRule) \
|
|
|
|
-> List[Document]:
|
2023-05-15 08:51:32 +08:00
|
|
|
"""
|
2023-06-25 16:49:14 +08:00
|
|
|
Split the text documents into documents and save them to the document segment.
|
2023-05-15 08:51:32 +08:00
|
|
|
"""
|
2023-06-25 16:49:14 +08:00
|
|
|
documents = self._split_to_documents(
|
2023-05-15 08:51:32 +08:00
|
|
|
text_docs=text_docs,
|
2023-06-25 16:49:14 +08:00
|
|
|
splitter=splitter,
|
2023-07-28 20:47:15 +08:00
|
|
|
processing_rule=processing_rule,
|
|
|
|
tenant_id=dataset.tenant_id,
|
2023-08-18 17:37:31 +08:00
|
|
|
document_form=dataset_document.doc_form,
|
|
|
|
document_language=dataset_document.doc_language
|
2023-05-15 08:51:32 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
# save node to document segment
|
2023-12-07 09:24:52 +08:00
|
|
|
doc_store = DatasetDocumentStore(
|
2023-05-15 08:51:32 +08:00
|
|
|
dataset=dataset,
|
2023-06-25 16:49:14 +08:00
|
|
|
user_id=dataset_document.created_by,
|
|
|
|
document_id=dataset_document.id
|
2023-05-15 08:51:32 +08:00
|
|
|
)
|
2023-06-25 16:49:14 +08:00
|
|
|
|
2023-06-16 21:47:51 +08:00
|
|
|
# add document segments
|
2023-06-25 16:49:14 +08:00
|
|
|
doc_store.add_documents(documents)
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
# update document status to indexing
|
|
|
|
cur_time = datetime.datetime.utcnow()
|
|
|
|
self._update_document_index_status(
|
2023-06-25 16:49:14 +08:00
|
|
|
document_id=dataset_document.id,
|
2023-05-15 08:51:32 +08:00
|
|
|
after_indexing_status="indexing",
|
|
|
|
extra_update_params={
|
2023-06-25 16:49:14 +08:00
|
|
|
DatasetDocument.cleaning_completed_at: cur_time,
|
|
|
|
DatasetDocument.splitting_completed_at: cur_time,
|
2023-05-15 08:51:32 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
# update segment status to indexing
|
|
|
|
self._update_segments_by_document(
|
2023-06-25 16:49:14 +08:00
|
|
|
dataset_document_id=dataset_document.id,
|
2023-05-15 08:51:32 +08:00
|
|
|
update_params={
|
|
|
|
DocumentSegment.status: "indexing",
|
|
|
|
DocumentSegment.indexing_at: datetime.datetime.utcnow()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2023-06-25 16:49:14 +08:00
|
|
|
return documents
|
2023-05-15 08:51:32 +08:00
|
|
|
|
2023-06-25 16:49:14 +08:00
|
|
|
def _split_to_documents(self, text_docs: List[Document], splitter: TextSplitter,
|
2023-08-18 17:37:31 +08:00
|
|
|
processing_rule: DatasetProcessRule, tenant_id: str,
|
|
|
|
document_form: str, document_language: str) -> List[Document]:
|
2023-07-28 20:47:15 +08:00
|
|
|
"""
|
|
|
|
Split the text documents into nodes.
|
|
|
|
"""
|
|
|
|
all_documents = []
|
2023-07-29 17:49:18 +08:00
|
|
|
all_qa_documents = []
|
2023-07-28 20:47:15 +08:00
|
|
|
for text_doc in text_docs:
|
|
|
|
# document clean
|
|
|
|
document_text = self._document_clean(text_doc.page_content, processing_rule)
|
|
|
|
text_doc.page_content = document_text
|
|
|
|
|
|
|
|
# parse document to nodes
|
|
|
|
documents = splitter.split_documents([text_doc])
|
|
|
|
split_documents = []
|
2023-07-29 17:49:18 +08:00
|
|
|
for document_node in documents:
|
|
|
|
|
2023-08-25 15:50:29 +08:00
|
|
|
if document_node.page_content.strip():
|
|
|
|
doc_id = str(uuid.uuid4())
|
|
|
|
hash = helper.generate_text_hash(document_node.page_content)
|
|
|
|
document_node.metadata['doc_id'] = doc_id
|
|
|
|
document_node.metadata['doc_hash'] = hash
|
2023-12-19 18:11:27 +08:00
|
|
|
# delete Spliter character
|
|
|
|
page_content = document_node.page_content
|
|
|
|
if page_content.startswith(".") or page_content.startswith("。"):
|
|
|
|
page_content = page_content[1:]
|
|
|
|
else:
|
|
|
|
page_content = page_content
|
|
|
|
document_node.page_content = page_content
|
2024-01-25 13:59:18 +08:00
|
|
|
|
|
|
|
if document_node.page_content:
|
|
|
|
split_documents.append(document_node)
|
2023-07-29 17:49:18 +08:00
|
|
|
all_documents.extend(split_documents)
|
|
|
|
# processing qa document
|
|
|
|
if document_form == 'qa_model':
|
|
|
|
for i in range(0, len(all_documents), 10):
|
2023-07-29 17:00:21 +08:00
|
|
|
threads = []
|
2023-07-29 17:49:18 +08:00
|
|
|
sub_documents = all_documents[i:i + 10]
|
2023-07-29 17:00:21 +08:00
|
|
|
for doc in sub_documents:
|
2023-07-29 17:49:18 +08:00
|
|
|
document_format_thread = threading.Thread(target=self.format_qa_document, kwargs={
|
2023-08-18 17:37:31 +08:00
|
|
|
'flask_app': current_app._get_current_object(),
|
|
|
|
'tenant_id': tenant_id, 'document_node': doc, 'all_qa_documents': all_qa_documents,
|
|
|
|
'document_language': document_language})
|
2023-07-29 17:00:21 +08:00
|
|
|
threads.append(document_format_thread)
|
|
|
|
document_format_thread.start()
|
|
|
|
for thread in threads:
|
|
|
|
thread.join()
|
2023-07-29 17:49:18 +08:00
|
|
|
return all_qa_documents
|
2023-07-28 20:47:15 +08:00
|
|
|
return all_documents
|
|
|
|
|
2023-08-18 17:37:31 +08:00
|
|
|
def format_qa_document(self, flask_app: Flask, tenant_id: str, document_node, all_qa_documents, document_language):
|
2023-07-28 22:19:39 +08:00
|
|
|
format_documents = []
|
|
|
|
if document_node.page_content is None or not document_node.page_content.strip():
|
2023-07-29 17:49:18 +08:00
|
|
|
return
|
2023-08-16 15:39:31 +08:00
|
|
|
with flask_app.app_context():
|
|
|
|
try:
|
|
|
|
# qa model document
|
2023-08-18 17:37:31 +08:00
|
|
|
response = LLMGenerator.generate_qa_document(tenant_id, document_node.page_content, document_language)
|
2023-08-16 15:39:31 +08:00
|
|
|
document_qa_list = self.format_split_text(response)
|
|
|
|
qa_documents = []
|
|
|
|
for result in document_qa_list:
|
|
|
|
qa_document = Document(page_content=result['question'], metadata=document_node.metadata.copy())
|
|
|
|
doc_id = str(uuid.uuid4())
|
|
|
|
hash = helper.generate_text_hash(result['question'])
|
|
|
|
qa_document.metadata['answer'] = result['answer']
|
|
|
|
qa_document.metadata['doc_id'] = doc_id
|
|
|
|
qa_document.metadata['doc_hash'] = hash
|
|
|
|
qa_documents.append(qa_document)
|
|
|
|
format_documents.extend(qa_documents)
|
|
|
|
except Exception as e:
|
|
|
|
logging.exception(e)
|
2023-07-28 22:19:39 +08:00
|
|
|
|
2023-08-16 15:39:31 +08:00
|
|
|
all_qa_documents.extend(format_documents)
|
2023-07-28 22:19:39 +08:00
|
|
|
|
2023-07-28 20:47:15 +08:00
|
|
|
def _split_to_documents_for_estimate(self, text_docs: List[Document], splitter: TextSplitter,
|
|
|
|
processing_rule: DatasetProcessRule) -> List[Document]:
|
2023-05-15 08:51:32 +08:00
|
|
|
"""
|
|
|
|
Split the text documents into nodes.
|
|
|
|
"""
|
2023-06-25 16:49:14 +08:00
|
|
|
all_documents = []
|
2023-05-15 08:51:32 +08:00
|
|
|
for text_doc in text_docs:
|
|
|
|
# document clean
|
2023-06-25 16:49:14 +08:00
|
|
|
document_text = self._document_clean(text_doc.page_content, processing_rule)
|
|
|
|
text_doc.page_content = document_text
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
# parse document to nodes
|
2023-06-25 16:49:14 +08:00
|
|
|
documents = splitter.split_documents([text_doc])
|
|
|
|
|
|
|
|
split_documents = []
|
|
|
|
for document in documents:
|
|
|
|
if document.page_content is None or not document.page_content.strip():
|
|
|
|
continue
|
|
|
|
doc_id = str(uuid.uuid4())
|
|
|
|
hash = helper.generate_text_hash(document.page_content)
|
|
|
|
|
|
|
|
document.metadata['doc_id'] = doc_id
|
|
|
|
document.metadata['doc_hash'] = hash
|
|
|
|
|
|
|
|
split_documents.append(document)
|
|
|
|
|
|
|
|
all_documents.extend(split_documents)
|
2023-05-15 08:51:32 +08:00
|
|
|
|
2023-06-25 16:49:14 +08:00
|
|
|
return all_documents
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
def _document_clean(self, text: str, processing_rule: DatasetProcessRule) -> str:
|
|
|
|
"""
|
|
|
|
Clean the document text according to the processing rules.
|
|
|
|
"""
|
|
|
|
if processing_rule.mode == "automatic":
|
|
|
|
rules = DatasetProcessRule.AUTOMATIC_RULES
|
|
|
|
else:
|
|
|
|
rules = json.loads(processing_rule.rules) if processing_rule.rules else {}
|
|
|
|
|
|
|
|
if 'pre_processing_rules' in rules:
|
|
|
|
pre_processing_rules = rules["pre_processing_rules"]
|
|
|
|
for pre_processing_rule in pre_processing_rules:
|
|
|
|
if pre_processing_rule["id"] == "remove_extra_spaces" and pre_processing_rule["enabled"] is True:
|
|
|
|
# Remove extra spaces
|
|
|
|
pattern = r'\n{3,}'
|
|
|
|
text = re.sub(pattern, '\n\n', text)
|
|
|
|
pattern = r'[\t\f\r\x20\u00a0\u1680\u180e\u2000-\u200a\u202f\u205f\u3000]{2,}'
|
|
|
|
text = re.sub(pattern, ' ', text)
|
|
|
|
elif pre_processing_rule["id"] == "remove_urls_emails" and pre_processing_rule["enabled"] is True:
|
|
|
|
# Remove email
|
|
|
|
pattern = r'([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)'
|
|
|
|
text = re.sub(pattern, '', text)
|
|
|
|
|
|
|
|
# Remove URL
|
|
|
|
pattern = r'https?://[^\s]+'
|
|
|
|
text = re.sub(pattern, '', text)
|
|
|
|
|
|
|
|
return text
|
|
|
|
|
2023-07-28 20:47:15 +08:00
|
|
|
def format_split_text(self, text):
|
2024-01-12 18:45:34 +08:00
|
|
|
regex = r"Q\d+:\s*(.*?)\s*A\d+:\s*([\s\S]*?)(?=Q\d+:|$)"
|
2023-12-11 15:53:37 +08:00
|
|
|
matches = re.findall(regex, text, re.UNICODE)
|
2023-11-13 19:05:32 +08:00
|
|
|
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
"question": q,
|
|
|
|
"answer": re.sub(r"\n\s*", "\n", a.strip())
|
|
|
|
}
|
|
|
|
for q, a in matches if q and a
|
|
|
|
]
|
2023-07-28 20:47:15 +08:00
|
|
|
|
2023-06-25 16:49:14 +08:00
|
|
|
def _build_index(self, dataset: Dataset, dataset_document: DatasetDocument, documents: List[Document]) -> None:
|
2023-05-15 08:51:32 +08:00
|
|
|
"""
|
|
|
|
Build the index for the document.
|
|
|
|
"""
|
2023-06-25 16:49:14 +08:00
|
|
|
vector_index = IndexBuilder.get_index(dataset, 'high_quality')
|
|
|
|
keyword_table_index = IndexBuilder.get_index(dataset, 'economy')
|
2024-01-02 23:42:00 +08:00
|
|
|
embedding_model_instance = None
|
2023-08-29 03:37:45 +08:00
|
|
|
if dataset.indexing_technique == 'high_quality':
|
2024-01-02 23:42:00 +08:00
|
|
|
embedding_model_instance = self.model_manager.get_model_instance(
|
2023-08-29 03:37:45 +08:00
|
|
|
tenant_id=dataset.tenant_id,
|
2024-01-02 23:42:00 +08:00
|
|
|
provider=dataset.embedding_model_provider,
|
|
|
|
model_type=ModelType.TEXT_EMBEDDING,
|
|
|
|
model=dataset.embedding_model
|
2023-08-29 03:37:45 +08:00
|
|
|
)
|
2023-08-12 00:57:00 +08:00
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
# chunk nodes by chunk size
|
|
|
|
indexing_start_at = time.perf_counter()
|
|
|
|
tokens = 0
|
|
|
|
chunk_size = 100
|
2024-01-02 23:42:00 +08:00
|
|
|
|
|
|
|
embedding_model_type_instance = None
|
|
|
|
if embedding_model_instance:
|
|
|
|
embedding_model_type_instance = embedding_model_instance.model_type_instance
|
|
|
|
embedding_model_type_instance = cast(TextEmbeddingModel, embedding_model_type_instance)
|
|
|
|
|
2023-06-25 16:49:14 +08:00
|
|
|
for i in range(0, len(documents), chunk_size):
|
2023-05-15 08:51:32 +08:00
|
|
|
# check document is paused
|
2023-06-25 16:49:14 +08:00
|
|
|
self._check_document_paused_status(dataset_document.id)
|
|
|
|
chunk_documents = documents[i:i + chunk_size]
|
2024-01-02 23:42:00 +08:00
|
|
|
if dataset.indexing_technique == 'high_quality' or embedding_model_type_instance:
|
2023-08-29 03:37:45 +08:00
|
|
|
tokens += sum(
|
2024-01-02 23:42:00 +08:00
|
|
|
embedding_model_type_instance.get_num_tokens(
|
|
|
|
embedding_model_instance.model,
|
|
|
|
embedding_model_instance.credentials,
|
|
|
|
[document.page_content]
|
|
|
|
)
|
2023-08-29 03:37:45 +08:00
|
|
|
for document in chunk_documents
|
|
|
|
)
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
# save vector index
|
2023-06-25 16:49:14 +08:00
|
|
|
if vector_index:
|
|
|
|
vector_index.add_texts(chunk_documents)
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
# save keyword index
|
2023-06-25 16:49:14 +08:00
|
|
|
keyword_table_index.add_texts(chunk_documents)
|
2023-05-15 08:51:32 +08:00
|
|
|
|
2023-06-25 16:49:14 +08:00
|
|
|
document_ids = [document.metadata['doc_id'] for document in chunk_documents]
|
2023-05-15 08:51:32 +08:00
|
|
|
db.session.query(DocumentSegment).filter(
|
2023-06-25 16:49:14 +08:00
|
|
|
DocumentSegment.document_id == dataset_document.id,
|
|
|
|
DocumentSegment.index_node_id.in_(document_ids),
|
2023-05-15 08:51:32 +08:00
|
|
|
DocumentSegment.status == "indexing"
|
|
|
|
).update({
|
|
|
|
DocumentSegment.status: "completed",
|
2023-08-22 17:59:24 +08:00
|
|
|
DocumentSegment.enabled: True,
|
2023-05-15 08:51:32 +08:00
|
|
|
DocumentSegment.completed_at: datetime.datetime.utcnow()
|
|
|
|
})
|
|
|
|
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
indexing_end_at = time.perf_counter()
|
|
|
|
|
|
|
|
# update document status to completed
|
|
|
|
self._update_document_index_status(
|
2023-06-25 16:49:14 +08:00
|
|
|
document_id=dataset_document.id,
|
2023-05-15 08:51:32 +08:00
|
|
|
after_indexing_status="completed",
|
|
|
|
extra_update_params={
|
2023-06-25 16:49:14 +08:00
|
|
|
DatasetDocument.tokens: tokens,
|
|
|
|
DatasetDocument.completed_at: datetime.datetime.utcnow(),
|
|
|
|
DatasetDocument.indexing_latency: indexing_end_at - indexing_start_at,
|
2023-05-15 08:51:32 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
def _check_document_paused_status(self, document_id: str):
|
|
|
|
indexing_cache_key = 'document_{}_is_paused'.format(document_id)
|
|
|
|
result = redis_client.get(indexing_cache_key)
|
|
|
|
if result:
|
|
|
|
raise DocumentIsPausedException()
|
|
|
|
|
|
|
|
def _update_document_index_status(self, document_id: str, after_indexing_status: str,
|
|
|
|
extra_update_params: Optional[dict] = None) -> None:
|
|
|
|
"""
|
|
|
|
Update the document indexing status.
|
|
|
|
"""
|
2023-06-25 16:49:14 +08:00
|
|
|
count = DatasetDocument.query.filter_by(id=document_id, is_paused=True).count()
|
2023-05-15 08:51:32 +08:00
|
|
|
if count > 0:
|
|
|
|
raise DocumentIsPausedException()
|
2023-10-12 13:30:44 +08:00
|
|
|
document = DatasetDocument.query.filter_by(id=document_id).first()
|
|
|
|
if not document:
|
|
|
|
raise DocumentIsDeletedPausedException()
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
update_params = {
|
2023-06-25 16:49:14 +08:00
|
|
|
DatasetDocument.indexing_status: after_indexing_status
|
2023-05-15 08:51:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if extra_update_params:
|
|
|
|
update_params.update(extra_update_params)
|
|
|
|
|
2023-06-25 16:49:14 +08:00
|
|
|
DatasetDocument.query.filter_by(id=document_id).update(update_params)
|
2023-05-15 08:51:32 +08:00
|
|
|
db.session.commit()
|
|
|
|
|
2023-06-25 16:49:14 +08:00
|
|
|
def _update_segments_by_document(self, dataset_document_id: str, update_params: dict) -> None:
|
2023-05-15 08:51:32 +08:00
|
|
|
"""
|
|
|
|
Update the document segment by document id.
|
|
|
|
"""
|
2023-06-25 16:49:14 +08:00
|
|
|
DocumentSegment.query.filter_by(document_id=dataset_document_id).update(update_params)
|
2023-05-15 08:51:32 +08:00
|
|
|
db.session.commit()
|
|
|
|
|
2023-08-18 17:37:31 +08:00
|
|
|
def batch_add_segments(self, segments: List[DocumentSegment], dataset: Dataset):
|
|
|
|
"""
|
|
|
|
Batch add segments index processing
|
|
|
|
"""
|
|
|
|
documents = []
|
|
|
|
for segment in segments:
|
|
|
|
document = Document(
|
|
|
|
page_content=segment.content,
|
|
|
|
metadata={
|
|
|
|
"doc_id": segment.index_node_id,
|
|
|
|
"doc_hash": segment.index_node_hash,
|
|
|
|
"document_id": segment.document_id,
|
|
|
|
"dataset_id": segment.dataset_id,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
documents.append(document)
|
|
|
|
# save vector index
|
|
|
|
index = IndexBuilder.get_index(dataset, 'high_quality')
|
|
|
|
if index:
|
|
|
|
index.add_texts(documents, duplicate_check=True)
|
|
|
|
|
|
|
|
# save keyword index
|
|
|
|
index = IndexBuilder.get_index(dataset, 'economy')
|
|
|
|
if index:
|
|
|
|
index.add_texts(documents)
|
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
class DocumentIsPausedException(Exception):
|
|
|
|
pass
|
2023-10-12 13:30:44 +08:00
|
|
|
|
|
|
|
|
|
|
|
class DocumentIsDeletedPausedException(Exception):
|
|
|
|
pass
|