1"""Reranking strategy registry for RAG document reordering.
2
3Handler-based registry that stores handler instances and dispatches
4to them based on strategy name via can_handle().
5"""
6
7from __future__ import annotations
8
9
10class RerankingStrategyRegistry:
11 """Registry of reranking strategy handlers.
12
13 Reranking strategies reorder documents after initial retrieval
14 using cross-encoders, LLM-based scoring, or fusion techniques.
15
16 Uses a handler-based dispatch pattern where handlers implement
17 can_handle(strategy: str) and create_and_rerank() methods.
18
19 Usage::
20
21 registry = RerankingStrategyRegistry()
22 registry.register(FlashRankStrategyHandler())
23 handler = registry.get("flashrank")
24 result = await handler.create_and_rerank(strategy="flashrank", ...)
25 """
26
27 def __init__(self) -> None:
28 """Initialize an empty handler registry."""
29 self._handlers: list = []
30
31 @classmethod
32 def with_defaults(cls) -> RerankingStrategyRegistry:
33 """Create a RerankingStrategyRegistry with default handlers.
34
35 Returns:
36 A RerankingStrategyRegistry with no default handlers.
37 Handlers are registered conditionally by the provider.
38 """
39 return cls()
40
41 def register(self, handler: object) -> None:
42 """Register a handler instance.
43
44 Args:
45 handler: A handler instance with can_handle(strategy) method.
46 """
47 self._handlers.append(handler)
48
49 def get(self, strategy: str) -> object | None:
50 """Get a handler that can handle the given strategy.
51
52 Args:
53 strategy: Strategy name to look up.
54
55 Returns:
56 First handler where can_handle(strategy) is True, or None.
57 """
58 for handler in self._handlers:
59 if handler.can_handle(strategy):
60 return handler
61 return None
62
63
64__all__ = ["RerankingStrategyRegistry"]