|
|
|
@ -23,31 +23,29 @@ default_retrieval_model = {
|
|
|
|
|
|
|
|
|
|
|
|
class RetrievalService:
|
|
|
|
class RetrievalService:
|
|
|
|
@classmethod
|
|
|
|
@classmethod
|
|
|
|
def retrieve(cls, retrival_method: str, dataset_id: str, query: str,
|
|
|
|
def retrieve(cls,
|
|
|
|
top_k: int, score_threshold: Optional[float] = .0,
|
|
|
|
retrieval_method: str,
|
|
|
|
reranking_model: Optional[dict] = None, reranking_mode: Optional[str] = 'reranking_model',
|
|
|
|
dataset_id: str,
|
|
|
|
weights: Optional[dict] = None, provider: Optional[str] = None,
|
|
|
|
query: str,
|
|
|
|
external_retrieval_model: Optional[dict] = None):
|
|
|
|
top_k: int,
|
|
|
|
|
|
|
|
score_threshold: Optional[float] = .0,
|
|
|
|
|
|
|
|
reranking_model: Optional[dict] = None,
|
|
|
|
|
|
|
|
reranking_mode: Optional[str] = 'reranking_model',
|
|
|
|
|
|
|
|
weights: Optional[dict] = None
|
|
|
|
|
|
|
|
):
|
|
|
|
dataset = db.session.query(Dataset).filter(
|
|
|
|
dataset = db.session.query(Dataset).filter(
|
|
|
|
Dataset.id == dataset_id
|
|
|
|
Dataset.id == dataset_id
|
|
|
|
).first()
|
|
|
|
).first()
|
|
|
|
if not dataset:
|
|
|
|
if not dataset:
|
|
|
|
return []
|
|
|
|
return []
|
|
|
|
if provider == 'external':
|
|
|
|
|
|
|
|
all_documents = ExternalDatasetService.fetch_external_knowledge_retrival(
|
|
|
|
|
|
|
|
dataset.tenant_id,
|
|
|
|
|
|
|
|
dataset_id,
|
|
|
|
|
|
|
|
query,
|
|
|
|
|
|
|
|
external_retrieval_model
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
if not dataset or dataset.available_document_count == 0 or dataset.available_segment_count == 0:
|
|
|
|
if not dataset or dataset.available_document_count == 0 or dataset.available_segment_count == 0:
|
|
|
|
return []
|
|
|
|
return []
|
|
|
|
all_documents = []
|
|
|
|
all_documents = []
|
|
|
|
threads = []
|
|
|
|
threads = []
|
|
|
|
exceptions = []
|
|
|
|
exceptions = []
|
|
|
|
# retrieval_model source with keyword
|
|
|
|
# retrieval_model source with keyword
|
|
|
|
if retrival_method == 'keyword_search':
|
|
|
|
if retrieval_method == 'keyword_search':
|
|
|
|
keyword_thread = threading.Thread(target=RetrievalService.keyword_search, kwargs={
|
|
|
|
keyword_thread = threading.Thread(target=RetrievalService.keyword_search, kwargs={
|
|
|
|
'flask_app': current_app._get_current_object(),
|
|
|
|
'flask_app': current_app._get_current_object(),
|
|
|
|
'dataset_id': dataset_id,
|
|
|
|
'dataset_id': dataset_id,
|
|
|
|
@ -59,7 +57,7 @@ class RetrievalService:
|
|
|
|
threads.append(keyword_thread)
|
|
|
|
threads.append(keyword_thread)
|
|
|
|
keyword_thread.start()
|
|
|
|
keyword_thread.start()
|
|
|
|
# retrieval_model source with semantic
|
|
|
|
# retrieval_model source with semantic
|
|
|
|
if RetrievalMethod.is_support_semantic_search(retrival_method):
|
|
|
|
if RetrievalMethod.is_support_semantic_search(retrieval_method):
|
|
|
|
embedding_thread = threading.Thread(target=RetrievalService.embedding_search, kwargs={
|
|
|
|
embedding_thread = threading.Thread(target=RetrievalService.embedding_search, kwargs={
|
|
|
|
'flask_app': current_app._get_current_object(),
|
|
|
|
'flask_app': current_app._get_current_object(),
|
|
|
|
'dataset_id': dataset_id,
|
|
|
|
'dataset_id': dataset_id,
|
|
|
|
@ -68,19 +66,19 @@ class RetrievalService:
|
|
|
|
'score_threshold': score_threshold,
|
|
|
|
'score_threshold': score_threshold,
|
|
|
|
'reranking_model': reranking_model,
|
|
|
|
'reranking_model': reranking_model,
|
|
|
|
'all_documents': all_documents,
|
|
|
|
'all_documents': all_documents,
|
|
|
|
'retrival_method': retrival_method,
|
|
|
|
'retrieval_method': retrieval_method,
|
|
|
|
'exceptions': exceptions,
|
|
|
|
'exceptions': exceptions,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
threads.append(embedding_thread)
|
|
|
|
threads.append(embedding_thread)
|
|
|
|
embedding_thread.start()
|
|
|
|
embedding_thread.start()
|
|
|
|
|
|
|
|
|
|
|
|
# retrieval source with full text
|
|
|
|
# retrieval source with full text
|
|
|
|
if RetrievalMethod.is_support_fulltext_search(retrival_method):
|
|
|
|
if RetrievalMethod.is_support_fulltext_search(retrieval_method):
|
|
|
|
full_text_index_thread = threading.Thread(target=RetrievalService.full_text_index_search, kwargs={
|
|
|
|
full_text_index_thread = threading.Thread(target=RetrievalService.full_text_index_search, kwargs={
|
|
|
|
'flask_app': current_app._get_current_object(),
|
|
|
|
'flask_app': current_app._get_current_object(),
|
|
|
|
'dataset_id': dataset_id,
|
|
|
|
'dataset_id': dataset_id,
|
|
|
|
'query': query,
|
|
|
|
'query': query,
|
|
|
|
'retrival_method': retrival_method,
|
|
|
|
'retrieval_method': retrieval_method,
|
|
|
|
'score_threshold': score_threshold,
|
|
|
|
'score_threshold': score_threshold,
|
|
|
|
'top_k': top_k,
|
|
|
|
'top_k': top_k,
|
|
|
|
'reranking_model': reranking_model,
|
|
|
|
'reranking_model': reranking_model,
|
|
|
|
@ -97,7 +95,7 @@ class RetrievalService:
|
|
|
|
exception_message = ';\n'.join(exceptions)
|
|
|
|
exception_message = ';\n'.join(exceptions)
|
|
|
|
raise Exception(exception_message)
|
|
|
|
raise Exception(exception_message)
|
|
|
|
|
|
|
|
|
|
|
|
if retrival_method == RetrievalMethod.HYBRID_SEARCH.value:
|
|
|
|
if retrieval_method == RetrievalMethod.HYBRID_SEARCH.value:
|
|
|
|
data_post_processor = DataPostProcessor(str(dataset.tenant_id), reranking_mode,
|
|
|
|
data_post_processor = DataPostProcessor(str(dataset.tenant_id), reranking_mode,
|
|
|
|
reranking_model, weights, False)
|
|
|
|
reranking_model, weights, False)
|
|
|
|
all_documents = data_post_processor.invoke(
|
|
|
|
all_documents = data_post_processor.invoke(
|
|
|
|
@ -108,6 +106,24 @@ class RetrievalService:
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return all_documents
|
|
|
|
return all_documents
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
|
|
|
def external_retrieve(cls,
|
|
|
|
|
|
|
|
dataset_id: str,
|
|
|
|
|
|
|
|
query: str,
|
|
|
|
|
|
|
|
external_retrieval_model: Optional[dict] = None):
|
|
|
|
|
|
|
|
dataset = db.session.query(Dataset).filter(
|
|
|
|
|
|
|
|
Dataset.id == dataset_id
|
|
|
|
|
|
|
|
).first()
|
|
|
|
|
|
|
|
if not dataset:
|
|
|
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
all_documents = ExternalDatasetService.fetch_external_knowledge_retrieval(
|
|
|
|
|
|
|
|
dataset.tenant_id,
|
|
|
|
|
|
|
|
dataset_id,
|
|
|
|
|
|
|
|
query,
|
|
|
|
|
|
|
|
external_retrieval_model
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
return all_documents
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
@classmethod
|
|
|
|
def keyword_search(
|
|
|
|
def keyword_search(
|
|
|
|
cls, flask_app: Flask, dataset_id: str, query: str, top_k: int, all_documents: list, exceptions: list
|
|
|
|
cls, flask_app: Flask, dataset_id: str, query: str, top_k: int, all_documents: list, exceptions: list
|
|
|
|
|