mirror of
https://github.com/langgenius/dify.git
synced 2026-04-23 08:26:28 +08:00
refactor(api): type hit testing retrieve responses with TypedDict (#34484)
This commit is contained in:
parent
e178451d04
commit
71d299d0d3
@ -1,7 +1,7 @@
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import time
|
import time
|
||||||
from typing import Any
|
from typing import Any, TypedDict
|
||||||
|
|
||||||
from graphon.model_runtime.entities import LLMMode
|
from graphon.model_runtime.entities import LLMMode
|
||||||
|
|
||||||
@ -18,6 +18,16 @@ from models.enums import CreatorUserRole, DatasetQuerySource
|
|||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class QueryDict(TypedDict):
|
||||||
|
content: str
|
||||||
|
|
||||||
|
|
||||||
|
class RetrieveResponseDict(TypedDict):
|
||||||
|
query: QueryDict
|
||||||
|
records: list[dict[str, Any]]
|
||||||
|
|
||||||
|
|
||||||
default_retrieval_model = {
|
default_retrieval_model = {
|
||||||
"search_method": RetrievalMethod.SEMANTIC_SEARCH,
|
"search_method": RetrievalMethod.SEMANTIC_SEARCH,
|
||||||
"reranking_enable": False,
|
"reranking_enable": False,
|
||||||
@ -150,7 +160,7 @@ class HitTestingService:
|
|||||||
return dict(cls.compact_external_retrieve_response(dataset, query, all_documents))
|
return dict(cls.compact_external_retrieve_response(dataset, query, all_documents))
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def compact_retrieve_response(cls, query: str, documents: list[Document]) -> dict[Any, Any]:
|
def compact_retrieve_response(cls, query: str, documents: list[Document]) -> RetrieveResponseDict:
|
||||||
records = RetrievalService.format_retrieval_documents(documents)
|
records = RetrievalService.format_retrieval_documents(documents)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@ -161,7 +171,7 @@ class HitTestingService:
|
|||||||
}
|
}
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def compact_external_retrieve_response(cls, dataset: Dataset, query: str, documents: list) -> dict[Any, Any]:
|
def compact_external_retrieve_response(cls, dataset: Dataset, query: str, documents: list) -> RetrieveResponseDict:
|
||||||
records = []
|
records = []
|
||||||
if dataset.provider == "external":
|
if dataset.provider == "external":
|
||||||
for document in documents:
|
for document in documents:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user