75 lines
2.1 KiB
Python
75 lines
2.1 KiB
Python
"""Provider-neutral REST request and response schemas for the Demo API."""
|
|
|
|
from datetime import datetime
|
|
from uuid import UUID
|
|
|
|
from pydantic import BaseModel, ConfigDict, Field
|
|
|
|
from ekp_core.search import SearchResult
|
|
from ekp_domain import Document, DocumentStatus
|
|
|
|
|
|
class DocumentResponse(BaseModel):
|
|
"""Public document metadata without internal storage or provider fields."""
|
|
|
|
model_config = ConfigDict(from_attributes=True)
|
|
|
|
id: UUID
|
|
filename: str
|
|
media_type: str
|
|
size_bytes: int
|
|
status: DocumentStatus
|
|
error_message: str | None
|
|
created_at: datetime
|
|
updated_at: datetime
|
|
|
|
@classmethod
|
|
def from_document(cls, document: Document) -> "DocumentResponse":
|
|
"""Serialize a detached canonical model through the explicit API shape."""
|
|
return cls.model_validate(document)
|
|
|
|
|
|
class SearchRequest(BaseModel):
|
|
"""Validated public search input; normalization remains in the core service."""
|
|
|
|
query: str = Field(min_length=1, max_length=1600)
|
|
top_k: int = Field(default=5, ge=1, le=64, strict=True)
|
|
|
|
|
|
class SourceResponse(BaseModel):
|
|
"""Traceable source returned after dense retrieval and reranking."""
|
|
|
|
document_id: UUID
|
|
filename: str
|
|
page: int | None
|
|
section: str | None
|
|
chunk_id: UUID
|
|
content: str
|
|
score: float
|
|
rerank_score: float
|
|
|
|
|
|
class SearchResponse(BaseModel):
|
|
"""Minimal response contract consumed by the DEMO-010 Search Lab."""
|
|
|
|
results: list[SourceResponse]
|
|
|
|
@classmethod
|
|
def from_result(cls, result: SearchResult) -> "SearchResponse":
|
|
"""Hide index-profile internals while preserving every required source field."""
|
|
return cls(
|
|
results=[
|
|
SourceResponse(
|
|
document_id=source.document_id,
|
|
filename=source.filename,
|
|
page=source.page,
|
|
section=source.section,
|
|
chunk_id=source.chunk_id,
|
|
content=source.content,
|
|
score=source.score,
|
|
rerank_score=source.rerank_score,
|
|
)
|
|
for source in result.results
|
|
]
|
|
)
|