2023-05-15 08:51:32 +08:00
|
|
|
import logging
|
|
|
|
import time
|
|
|
|
|
2024-02-22 23:31:57 +08:00
|
|
|
from core.rag.datasource.retrieval_service import RetrievalService
|
|
|
|
from core.rag.models.document import Document
|
2024-09-08 13:14:11 +08:00
|
|
|
from core.rag.retrieval.retrieval_methods import RetrievalMethod
|
2023-05-15 08:51:32 +08:00
|
|
|
from extensions.ext_database import db
|
|
|
|
from models.account import Account
|
2024-01-12 12:34:01 +08:00
|
|
|
from models.dataset import Dataset, DatasetQuery, DocumentSegment
|
2023-11-17 22:13:37 +08:00
|
|
|
|
|
|
|
default_retrieval_model = {
|
2024-08-26 13:43:57 +08:00
|
|
|
"search_method": RetrievalMethod.SEMANTIC_SEARCH.value,
|
|
|
|
"reranking_enable": False,
|
|
|
|
"reranking_model": {"reranking_provider_name": "", "reranking_model_name": ""},
|
|
|
|
"top_k": 2,
|
|
|
|
"score_threshold_enabled": False,
|
2023-11-17 22:13:37 +08:00
|
|
|
}
|
2023-05-15 08:51:32 +08:00
|
|
|
|
2024-02-22 23:31:57 +08:00
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
class HitTestingService:
|
|
|
|
@classmethod
|
2024-09-30 15:38:43 +08:00
|
|
|
def retrieve(
|
|
|
|
cls,
|
|
|
|
dataset: Dataset,
|
|
|
|
query: str,
|
|
|
|
account: Account,
|
|
|
|
retrieval_model: dict,
|
|
|
|
external_retrieval_model: dict,
|
|
|
|
limit: int = 10,
|
|
|
|
) -> dict:
|
2023-08-23 19:57:27 +08:00
|
|
|
if dataset.available_document_count == 0 or dataset.available_segment_count == 0:
|
2023-06-25 16:49:14 +08:00
|
|
|
return {
|
|
|
|
"query": {
|
|
|
|
"content": query,
|
2024-08-26 13:43:57 +08:00
|
|
|
"tsne_position": {"x": 0, "y": 0},
|
2023-06-25 16:49:14 +08:00
|
|
|
},
|
2024-08-26 13:43:57 +08:00
|
|
|
"records": [],
|
2023-06-25 16:49:14 +08:00
|
|
|
}
|
2023-05-15 08:51:32 +08:00
|
|
|
|
2023-11-17 22:13:37 +08:00
|
|
|
start = time.perf_counter()
|
|
|
|
|
|
|
|
# get retrieval model , if the model is not setting , using default
|
|
|
|
if not retrieval_model:
|
2024-09-12 15:50:49 +08:00
|
|
|
retrieval_model = dataset.retrieval_model or default_retrieval_model
|
2023-11-17 22:13:37 +08:00
|
|
|
|
2024-08-26 13:43:57 +08:00
|
|
|
all_documents = RetrievalService.retrieve(
|
2024-09-08 13:14:11 +08:00
|
|
|
retrieval_method=retrieval_model.get("search_method", "semantic_search"),
|
2024-08-26 13:43:57 +08:00
|
|
|
dataset_id=dataset.id,
|
|
|
|
query=cls.escape_query_for_search(query),
|
|
|
|
top_k=retrieval_model.get("top_k", 2),
|
|
|
|
score_threshold=retrieval_model.get("score_threshold", 0.0)
|
|
|
|
if retrieval_model["score_threshold_enabled"]
|
2024-09-13 10:21:58 +08:00
|
|
|
else 0.0,
|
2024-08-26 13:43:57 +08:00
|
|
|
reranking_model=retrieval_model.get("reranking_model", None)
|
|
|
|
if retrieval_model["reranking_enable"]
|
|
|
|
else None,
|
2024-09-12 15:50:49 +08:00
|
|
|
reranking_mode=retrieval_model.get("reranking_mode") or "reranking_model",
|
2024-08-26 13:43:57 +08:00
|
|
|
weights=retrieval_model.get("weights", None),
|
|
|
|
)
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
end = time.perf_counter()
|
|
|
|
logging.debug(f"Hit testing retrieve in {end - start:0.4f} seconds")
|
|
|
|
|
|
|
|
dataset_query = DatasetQuery(
|
2024-08-26 13:43:57 +08:00
|
|
|
dataset_id=dataset.id, content=query, source="hit_testing", created_by_role="account", created_by=account.id
|
2023-05-15 08:51:32 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
db.session.add(dataset_query)
|
|
|
|
db.session.commit()
|
|
|
|
|
2024-07-02 17:57:42 +08:00
|
|
|
return cls.compact_retrieve_response(dataset, query, all_documents)
|
2023-05-15 08:51:32 +08:00
|
|
|
|
2024-09-30 15:38:43 +08:00
|
|
|
@classmethod
|
|
|
|
def external_retrieve(
|
|
|
|
cls,
|
|
|
|
dataset: Dataset,
|
|
|
|
query: str,
|
|
|
|
account: Account,
|
|
|
|
external_retrieval_model: dict,
|
|
|
|
) -> dict:
|
|
|
|
if dataset.provider != "external":
|
|
|
|
return {
|
|
|
|
"query": {"content": query},
|
|
|
|
"records": [],
|
|
|
|
}
|
|
|
|
|
|
|
|
start = time.perf_counter()
|
|
|
|
|
|
|
|
all_documents = RetrievalService.external_retrieve(
|
|
|
|
dataset_id=dataset.id,
|
|
|
|
query=cls.escape_query_for_search(query),
|
|
|
|
external_retrieval_model=external_retrieval_model,
|
|
|
|
)
|
|
|
|
|
|
|
|
end = time.perf_counter()
|
|
|
|
logging.debug(f"External knowledge hit testing retrieve in {end - start:0.4f} seconds")
|
|
|
|
|
|
|
|
dataset_query = DatasetQuery(
|
|
|
|
dataset_id=dataset.id, content=query, source="hit_testing", created_by_role="account", created_by=account.id
|
|
|
|
)
|
|
|
|
|
|
|
|
db.session.add(dataset_query)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return cls.compact_external_retrieve_response(dataset, query, all_documents)
|
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
@classmethod
|
2024-07-02 17:57:42 +08:00
|
|
|
def compact_retrieve_response(cls, dataset: Dataset, query: str, documents: list[Document]):
|
2023-05-15 08:51:32 +08:00
|
|
|
records = []
|
2024-09-30 15:38:43 +08:00
|
|
|
|
2023-06-25 16:49:14 +08:00
|
|
|
for document in documents:
|
2024-08-26 13:43:57 +08:00
|
|
|
index_node_id = document.metadata["doc_id"]
|
|
|
|
|
|
|
|
segment = (
|
|
|
|
db.session.query(DocumentSegment)
|
|
|
|
.filter(
|
|
|
|
DocumentSegment.dataset_id == dataset.id,
|
|
|
|
DocumentSegment.enabled == True,
|
|
|
|
DocumentSegment.status == "completed",
|
|
|
|
DocumentSegment.index_node_id == index_node_id,
|
|
|
|
)
|
|
|
|
.first()
|
|
|
|
)
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
if not segment:
|
|
|
|
continue
|
|
|
|
|
|
|
|
record = {
|
|
|
|
"segment": segment,
|
2024-08-26 13:43:57 +08:00
|
|
|
"score": document.metadata.get("score", None),
|
2023-05-15 08:51:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
records.append(record)
|
|
|
|
|
|
|
|
return {
|
|
|
|
"query": {
|
2023-06-25 16:49:14 +08:00
|
|
|
"content": query,
|
2023-05-15 08:51:32 +08:00
|
|
|
},
|
2024-08-26 13:43:57 +08:00
|
|
|
"records": records,
|
2023-05-15 08:51:32 +08:00
|
|
|
}
|
|
|
|
|
2024-09-30 15:38:43 +08:00
|
|
|
@classmethod
|
|
|
|
def compact_external_retrieve_response(cls, dataset: Dataset, query: str, documents: list):
|
|
|
|
records = []
|
|
|
|
if dataset.provider == "external":
|
|
|
|
for document in documents:
|
|
|
|
record = {
|
|
|
|
"content": document.get("content", None),
|
|
|
|
"title": document.get("title", None),
|
|
|
|
"score": document.get("score", None),
|
|
|
|
"metadata": document.get("metadata", None),
|
|
|
|
}
|
|
|
|
records.append(record)
|
|
|
|
return {
|
|
|
|
"query": {
|
|
|
|
"content": query,
|
|
|
|
},
|
|
|
|
"records": records,
|
|
|
|
}
|
|
|
|
|
2023-11-17 22:13:37 +08:00
|
|
|
@classmethod
|
|
|
|
def hit_testing_args_check(cls, args):
|
2024-08-26 13:43:57 +08:00
|
|
|
query = args["query"]
|
2023-11-17 22:13:37 +08:00
|
|
|
|
|
|
|
if not query or len(query) > 250:
|
2024-08-26 13:43:57 +08:00
|
|
|
raise ValueError("Query is required and cannot exceed 250 characters")
|
2024-07-23 15:02:25 +08:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def escape_query_for_search(query: str) -> str:
|
|
|
|
return query.replace('"', '\\"')
|