|
|
@@ -1,8 +1,11 @@
|
|
|
import json
|
|
|
from collections import defaultdict
|
|
|
from typing import Any, Optional
|
|
|
+import threading
|
|
|
+import logging
|
|
|
|
|
|
from pydantic import BaseModel
|
|
|
+from flask import Flask, current_app
|
|
|
|
|
|
from configs import dify_config
|
|
|
from core.rag.datasource.keyword.jieba.jieba_keyword_table_handler import JiebaKeywordTableHandler
|
|
|
@@ -23,21 +26,51 @@ class Jieba(BaseKeyword):
|
|
|
super().__init__(dataset)
|
|
|
self._config = KeywordTableConfig()
|
|
|
|
|
|
- def create(self, texts: list[Document], **kwargs) -> BaseKeyword:
|
|
|
+ def create(self, tenant_id, texts: list[Document], **kwargs) -> BaseKeyword:
|
|
|
lock_name = "keyword_indexing_lock_{}".format(self.dataset.id)
|
|
|
with redis_client.lock(lock_name, timeout=600):
|
|
|
+
|
|
|
keyword_table_handler = JiebaKeywordTableHandler()
|
|
|
keyword_table = self._get_dataset_keyword_table()
|
|
|
- for text in texts:
|
|
|
- keywords = keyword_table_handler.extract_keywords(
|
|
|
- text.page_content, self._config.max_keywords_per_chunk
|
|
|
- )
|
|
|
- self._update_segment_keywords(self.dataset.id, text.metadata["doc_id"], list(keywords))
|
|
|
- keyword_table = self._add_text_to_keyword_table(keyword_table, text.metadata["doc_id"], list(keywords))
|
|
|
+
|
|
|
+ # for text in texts:
|
|
|
+ # keywords = keyword_table_handler.extract_keywords(
|
|
|
+ # tenant_id, text.page_content, self._config.max_keywords_per_chunk
|
|
|
+ # )
|
|
|
+ # self._update_segment_keywords(self.dataset.id, text.metadata["doc_id"], list(keywords))
|
|
|
+ # keyword_table = self._add_text_to_keyword_table(keyword_table, text.metadata["doc_id"], list(keywords))
|
|
|
+
|
|
|
+ for i in range(0, len(texts), 10):
|
|
|
+ threads = []
|
|
|
+ sub_texts = texts[i : i + 10]
|
|
|
+ for text in sub_texts:
|
|
|
+ extract_keywords_thread = threading.Thread(
|
|
|
+ target=self.extract_keywords,
|
|
|
+ kwargs={
|
|
|
+ "flask_app": current_app._get_current_object(),
|
|
|
+ "tenant_id": tenant_id,
|
|
|
+ "keyword_table_handler": keyword_table_handler,
|
|
|
+ "keyword_table": keyword_table,
|
|
|
+ "text": text,
|
|
|
+ },
|
|
|
+ )
|
|
|
+ threads.append(extract_keywords_thread)
|
|
|
+ extract_keywords_thread.start()
|
|
|
+ for thread in threads:
|
|
|
+ thread.join()
|
|
|
|
|
|
+ logging.info(f"keyword_table: {keyword_table}")
|
|
|
self._save_dataset_keyword_table(keyword_table)
|
|
|
|
|
|
return self
|
|
|
+
|
|
|
+ def extract_keywords(self, flask_app: Flask, tenant_id, keyword_table_handler, keyword_table, text: list[Document]):
|
|
|
+ with flask_app.app_context():
|
|
|
+ keywords = keyword_table_handler.extract_keywords(
|
|
|
+ tenant_id, text.page_content, self._config.max_keywords_per_chunk
|
|
|
+ )
|
|
|
+ self._update_segment_keywords(self.dataset.id, text.metadata["doc_id"], list(keywords))
|
|
|
+ keyword_table = self._add_text_to_keyword_table(keyword_table, text.metadata["doc_id"], list(keywords))
|
|
|
|
|
|
def add_texts(self, texts: list[Document], **kwargs):
|
|
|
lock_name = "keyword_indexing_lock_{}".format(self.dataset.id)
|