File size: 2,305 Bytes
5e1a30c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
77
78
79
80
81
82
83
84
85
86
"""
Base interface for Reranker sub-components.

This module defines the abstract base class for all reranker implementations
in the modular retriever architecture.
"""

from abc import ABC, abstractmethod
from typing import List, Dict, Any, Tuple

from src.core.interfaces import Document


class Reranker(ABC):
    """
    Abstract base class for reranker implementations.
    
    This interface defines the contract for all reranker sub-components
    in the modular retriever architecture. All implementations are direct
    as they handle model inference without external API dependencies.
    """
    
    @abstractmethod
    def __init__(self, config: Dict[str, Any]):
        """
        Initialize the reranker.
        
        Args:
            config: Configuration dictionary specific to the reranker type
        """
        pass
    
    @abstractmethod
    def rerank(
        self, 
        query: str, 
        documents: List[Document], 
        initial_scores: List[float]
    ) -> List[Tuple[int, float]]:
        """
        Rerank documents based on query relevance.
        
        Args:
            query: The search query
            documents: List of candidate documents
            initial_scores: Initial relevance scores from fusion
            
        Returns:
            List of (document_index, reranked_score) tuples sorted by score
        """
        pass
    
    @abstractmethod
    def is_enabled(self) -> bool:
        """
        Check if reranking is enabled.
        
        Returns:
            True if reranking should be performed
        """
        pass
    
    @abstractmethod
    def get_reranker_info(self) -> Dict[str, Any]:
        """
        Get information about the reranker.
        
        Returns:
            Dictionary with reranker configuration and statistics
        """
        pass
    
    def get_component_info(self) -> Dict[str, Any]:
        """
        Get component information for logging and debugging.
        
        Returns:
            Dictionary with component details
        """
        return {
            "type": "reranker",
            "class": self.__class__.__name__,
            "module": self.__class__.__module__,
            "enabled": self.is_enabled(),
            **self.get_reranker_info()
        }