Spaces:
Sleeping
Sleeping
File size: 1,785 Bytes
409c17a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
"""
Domain Layer - Retriever Interface
Defines contract for document retrieval implementations.
"""
from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import List, Optional
@dataclass
class RetrievalResult:
"""Single retrieval result"""
content: str
score: float
document_id: str
chunk_index: int
metadata: dict
class IRetriever(ABC):
"""Interface for document retrieval"""
@abstractmethod
async def search(
self,
query: str,
top_k: int = 10,
filters: Optional[dict] = None,
min_score: float = 0.0,
) -> List[RetrievalResult]:
"""Search for relevant documents"""
pass
@abstractmethod
async def hybrid_search(
self,
query: str,
top_k: int = 10,
alpha: float = 0.5,
filters: Optional[dict] = None,
) -> List[RetrievalResult]:
"""Hybrid search (semantic + keyword)"""
pass
class IReranker(ABC):
"""Interface for result reranking"""
@abstractmethod
async def rerank(
self, query: str, results: List[RetrievalResult], top_k: int = 10
) -> List[RetrievalResult]:
"""Rerank retrieval results"""
pass
class IEmbedder(ABC):
"""Interface for text embedding"""
@abstractmethod
async def embed_text(self, text: str) -> List[float]:
"""Generate embedding for single text"""
pass
@abstractmethod
async def embed_texts(self, texts: List[str]) -> List[List[float]]:
"""Generate embeddings for multiple texts"""
pass
@abstractmethod
def get_dimension(self) -> int:
"""Get embedding dimension"""
pass
|