2026-05-27 15:51:42 +09:00
|
|
|
from datetime import datetime
|
2026-11-19 17:53:51 +08:00
|
|
|
from typing import Annotated, Any
|
2024-02-06 13:21:13 +08:00
|
|
|
|
2026-10-30 16:13:53 +08:00
|
|
|
from pydantic import BaseModel, ConfigDict, Discriminator, Field, Tag, field_validator
|
2023-09-27 16:06:32 +08:00
|
|
|
|
2026-05-27 15:51:42 +09:00
|
|
|
from fields.base import ResponseModel
|
|
|
|
|
from libs.helper import to_timestamp
|
2026-11-16 12:47:29 +08:00
|
|
|
from libs.uuid_utils import UUIDStr
|
2026-10-30 16:13:53 +08:00
|
|
|
from services.knowledge.entities.external import ExternalRetrievalConfig
|
|
|
|
|
from services.knowledge.entities.knowledge_entities import RetrievalModel
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class HitTestingExternalRetrievalModel(BaseModel):
|
|
|
|
|
"""Optional per-request overrides for an external knowledge provider."""
|
|
|
|
|
|
|
|
|
|
top_k: int | None = Field(default=None, description="Maximum number of results to return.")
|
|
|
|
|
score_threshold: float | None = Field(
|
|
|
|
|
default=None,
|
|
|
|
|
description="Minimum similarity score threshold for filtering results.",
|
|
|
|
|
)
|
|
|
|
|
score_threshold_enabled: bool | None = Field(
|
|
|
|
|
default=None,
|
|
|
|
|
description="Whether score threshold filtering is enabled.",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def to_domain(self) -> ExternalRetrievalConfig | None:
|
|
|
|
|
# An empty object has historically meant "use the dataset settings".
|
|
|
|
|
return ExternalRetrievalConfig.from_mapping(self.model_dump(exclude_unset=True))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class HitTestingPayload(BaseModel):
|
|
|
|
|
query: str = Field(description="Search query text.", max_length=250)
|
|
|
|
|
retrieval_model: RetrievalModel | None = Field(
|
|
|
|
|
default=None,
|
|
|
|
|
description=(
|
|
|
|
|
"Retrieval model configuration. Controls how chunks are searched and ranked when querying this "
|
|
|
|
|
"knowledge base."
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
external_retrieval_model: HitTestingExternalRetrievalModel | None = Field(
|
|
|
|
|
default=None,
|
|
|
|
|
description=(
|
|
|
|
|
"Optional retrieval overrides for an external knowledge base. Omit this field to use the settings "
|
|
|
|
|
"stored on the knowledge base."
|
|
|
|
|
),
|
|
|
|
|
)
|
2026-11-16 12:47:29 +08:00
|
|
|
attachment_ids: list[UUIDStr] | None = Field(
|
2026-10-30 16:13:53 +08:00
|
|
|
default=None,
|
|
|
|
|
description="List of attachment IDs to include in the retrieval context.",
|
|
|
|
|
)
|
2023-09-27 16:06:32 +08:00
|
|
|
|
|
|
|
|
|
2026-05-27 15:51:42 +09:00
|
|
|
class HitTestingQuery(ResponseModel):
|
|
|
|
|
content: str
|
2024-12-25 19:49:07 +08:00
|
|
|
|
2025-12-09 14:41:46 +08:00
|
|
|
|
2026-05-27 15:51:42 +09:00
|
|
|
class HitTestingDocument(ResponseModel):
|
|
|
|
|
id: str
|
|
|
|
|
data_source_type: str
|
|
|
|
|
name: str
|
|
|
|
|
doc_type: str | None
|
|
|
|
|
doc_metadata: Any | None
|
|
|
|
|
|
|
|
|
|
@field_validator("data_source_type", "doc_type", mode="before")
|
|
|
|
|
@classmethod
|
|
|
|
|
def _normalize_enum_fields(cls, value: Any) -> Any:
|
|
|
|
|
return _normalize_enum(value)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class HitTestingSegment(ResponseModel):
|
|
|
|
|
id: str
|
|
|
|
|
position: int
|
|
|
|
|
document_id: str
|
|
|
|
|
content: str
|
|
|
|
|
sign_content: str | None
|
|
|
|
|
answer: str | None
|
|
|
|
|
word_count: int
|
|
|
|
|
tokens: int
|
|
|
|
|
keywords: list[str]
|
|
|
|
|
index_node_id: str | None
|
|
|
|
|
index_node_hash: str | None
|
|
|
|
|
hit_count: int
|
|
|
|
|
enabled: bool
|
|
|
|
|
disabled_at: int | None
|
|
|
|
|
disabled_by: str | None
|
|
|
|
|
status: str
|
|
|
|
|
created_by: str
|
|
|
|
|
created_at: int
|
|
|
|
|
indexing_at: int | None
|
|
|
|
|
completed_at: int | None
|
|
|
|
|
error: str | None
|
|
|
|
|
stopped_at: int | None
|
2026-08-26 05:05:36 +00:00
|
|
|
document: HitTestingDocument = Field(description="Parent document information for the matched chunk.")
|
2026-05-27 15:51:42 +09:00
|
|
|
|
|
|
|
|
@field_validator("disabled_at", "created_at", "indexing_at", "completed_at", "stopped_at", mode="before")
|
|
|
|
|
@classmethod
|
|
|
|
|
def _normalize_timestamp(cls, value: datetime | int | None) -> int | None:
|
|
|
|
|
return to_timestamp(value)
|
|
|
|
|
|
|
|
|
|
@field_validator("status", mode="before")
|
|
|
|
|
@classmethod
|
|
|
|
|
def _normalize_enum_fields(cls, value: Any) -> Any:
|
|
|
|
|
return _normalize_enum(value)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class HitTestingChildChunk(ResponseModel):
|
|
|
|
|
id: str
|
|
|
|
|
content: str
|
|
|
|
|
position: int
|
|
|
|
|
score: float
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class HitTestingFile(ResponseModel):
|
|
|
|
|
id: str
|
|
|
|
|
name: str
|
|
|
|
|
size: int
|
|
|
|
|
extension: str
|
|
|
|
|
mime_type: str
|
|
|
|
|
source_url: str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class HitTestingRecord(ResponseModel):
|
2026-08-26 05:05:36 +00:00
|
|
|
segment: HitTestingSegment = Field(description="Matched chunk from the knowledge base.")
|
2026-05-27 15:51:42 +09:00
|
|
|
child_chunks: list[HitTestingChildChunk]
|
|
|
|
|
score: float | None
|
|
|
|
|
tsne_position: Any | None
|
|
|
|
|
files: list[HitTestingFile]
|
|
|
|
|
summary: str | None
|
|
|
|
|
|
|
|
|
|
|
2026-10-30 16:13:53 +08:00
|
|
|
class ExternalHitTestingQueryResponse(ResponseModel):
|
|
|
|
|
content: str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ExternalHitTestingRecordResponse(ResponseModel):
|
|
|
|
|
# Keep the two response branches disjoint: invalid internal records must not
|
|
|
|
|
# pass after Pydantic discards their segment-specific fields.
|
|
|
|
|
model_config = ConfigDict(extra="forbid")
|
|
|
|
|
|
|
|
|
|
content: str | None = None
|
|
|
|
|
title: str | None = None
|
|
|
|
|
score: float | None = None
|
|
|
|
|
metadata: dict[str, Any] | None = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ExternalHitTestingResponse(ResponseModel):
|
|
|
|
|
query: ExternalHitTestingQueryResponse
|
|
|
|
|
records: list[ExternalHitTestingRecordResponse]
|
|
|
|
|
|
|
|
|
|
|
2026-11-19 17:53:51 +08:00
|
|
|
def _record_source(record: Any) -> str:
|
2026-10-30 16:13:53 +08:00
|
|
|
if isinstance(record, HitTestingRecord) or (isinstance(record, dict) and "segment" in record):
|
|
|
|
|
return "internal"
|
|
|
|
|
return "external"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HitTestingResultRecord = Annotated[
|
|
|
|
|
Annotated[HitTestingRecord, Tag("internal")] | Annotated[ExternalHitTestingRecordResponse, Tag("external")],
|
|
|
|
|
Discriminator(_record_source),
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
2026-05-27 15:51:42 +09:00
|
|
|
class HitTestingResponse(ResponseModel):
|
2026-08-26 05:05:36 +00:00
|
|
|
query: HitTestingQuery = Field(description="The original query object.")
|
2026-10-30 16:13:53 +08:00
|
|
|
records: list[HitTestingResultRecord]
|
2026-05-27 15:51:42 +09:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def _normalize_enum(value: Any) -> Any:
|
|
|
|
|
if isinstance(value, str) or value is None:
|
|
|
|
|
return value
|
|
|
|
|
return getattr(value, "value", value)
|