File size: 6,985 Bytes
0828c2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
"""Fusion algorithms for combining multiple retrieval results."""

import logging
from collections import defaultdict
from typing import Any

from langchain_core.callbacks import CallbackManagerForRetrieverRun
from langchain_core.documents import Document
from langchain_core.retrievers import BaseRetriever

logger = logging.getLogger(__name__)


def reciprocal_rank_fusion(
    results_list: list[list[Document]],
    k: int = 60,
    weights: list[float] | None = None,
) -> list[Document]:
    """Combine multiple ranked lists using Reciprocal Rank Fusion (RRF).

    RRF is a simple but effective method for combining rankings from
    multiple retrieval systems. It's based on the formula:
        RRF(d) = sum(1 / (k + rank(d)))

    where k is a constant (typically 60) and rank(d) is the position
    of document d in each ranking (1-indexed).

    Args:
        results_list: List of ranked document lists from different retrievers
        k: RRF constant (higher = less aggressive re-ranking, default: 60)
        weights: Optional weights for each result list (must sum to 1.0)

    Returns:
        Combined and re-ranked list of documents

    Reference:
        Cormack, G. V., Clarke, C. L., & Buettcher, S. (2009).
        Reciprocal rank fusion outperforms condorcet and individual
        rank learning methods. SIGIR.
    """
    if not results_list:
        return []

    # Normalize weights if provided
    if weights is None:
        weights = [1.0] * len(results_list)
    else:
        if len(weights) != len(results_list):
            raise ValueError("Number of weights must match number of result lists")
        # Normalize weights to sum to 1
        total_weight = sum(weights)
        if total_weight > 0:
            weights = [w / total_weight for w in weights]

    # Calculate RRF scores
    # Use page_content as document identifier (could also use metadata)
    doc_scores: dict[str, float] = defaultdict(float)
    doc_map: dict[str, Document] = {}

    for result_idx, results in enumerate(results_list):
        weight = weights[result_idx]
        for rank, doc in enumerate(results, start=1):
            # Create a unique key for the document
            doc_key = doc.page_content

            # RRF formula with weight
            score = weight * (1.0 / (k + rank))
            doc_scores[doc_key] += score

            # Keep the document object (prefer first occurrence)
            if doc_key not in doc_map:
                doc_map[doc_key] = doc

    # Sort by RRF score (descending)
    sorted_keys = sorted(doc_scores.keys(), key=lambda x: doc_scores[x], reverse=True)

    # Return sorted documents
    return [doc_map[key] for key in sorted_keys]


def weighted_fusion(
    results_list: list[list[tuple[Document, float]]],
    weights: list[float] | None = None,
) -> list[Document]:
    """Combine multiple scored result lists using weighted scoring.

    This method is useful when retrievers provide relevance scores.
    Documents are combined by weighted sum of their scores.

    Args:
        results_list: List of (document, score) tuples from different retrievers
        weights: Weights for each result list (normalized internally)

    Returns:
        Combined and re-ranked list of documents
    """
    if not results_list:
        return []

    # Normalize weights
    if weights is None:
        weights = [1.0] * len(results_list)
    else:
        total_weight = sum(weights)
        if total_weight > 0:
            weights = [w / total_weight for w in weights]

    # Combine scores
    doc_scores: dict[str, float] = defaultdict(float)
    doc_map: dict[str, Document] = {}

    for result_idx, results in enumerate(results_list):
        weight = weights[result_idx]
        for doc, score in results:
            doc_key = doc.page_content
            doc_scores[doc_key] += weight * score
            if doc_key not in doc_map:
                doc_map[doc_key] = doc

    # Sort by combined score
    sorted_keys = sorted(doc_scores.keys(), key=lambda x: doc_scores[x], reverse=True)

    return [doc_map[key] for key in sorted_keys]


class FusionRetriever(BaseRetriever):
    """A retriever that combines results from multiple retrievers using fusion.

    This retriever implements the Ensemble pattern, allowing multiple
    retrieval strategies to be combined for better recall and precision.

    Attributes:
        retrievers: List of retrievers to combine
        weights: Weights for each retriever (optional)
        fusion_algorithm: Algorithm to use ('rrf' or 'weighted')
        rrf_k: Constant for RRF algorithm
        final_k: Number of documents to return after fusion
    """

    retrievers: list[BaseRetriever]
    weights: list[float] | None = None
    fusion_algorithm: str = "rrf"
    rrf_k: int = 60
    final_k: int = 4

    model_config = {"arbitrary_types_allowed": True}

    def _get_relevant_documents(
        self,
        query: str,
        *,
        run_manager: CallbackManagerForRetrieverRun | None = None,  # noqa: ARG002
    ) -> list[Document]:
        """Get documents from all retrievers and fuse results.

        Args:
            query: Query string to search for
            run_manager: Callback manager

        Returns:
            Fused and re-ranked list of documents
        """
        # Collect results from all retrievers
        all_results: list[list[Document]] = []

        for retriever in self.retrievers:
            try:
                results = retriever.invoke(query)
                all_results.append(results)
                logger.debug(f"Retriever returned {len(results)} documents")
            except Exception as e:
                logger.warning(f"Retriever failed: {e}")
                all_results.append([])

        # Apply fusion algorithm
        if self.fusion_algorithm == "rrf":
            fused = reciprocal_rank_fusion(
                all_results,
                k=self.rrf_k,
                weights=self.weights,
            )
        else:
            # For non-RRF, we don't have scores, so use RRF as fallback
            logger.warning(
                f"Fusion algorithm '{self.fusion_algorithm}' not supported "
                "for rank-only results, falling back to RRF"
            )
            fused = reciprocal_rank_fusion(all_results, k=self.rrf_k, weights=self.weights)

        # Return top k results
        result = fused[: self.final_k]
        logger.debug(f"Fusion returned {len(result)} documents from {len(fused)} total")

        return result

    def get_retriever_info(self) -> dict[str, Any]:
        """Get information about the fusion configuration.

        Returns:
            Dictionary with fusion configuration details
        """
        return {
            "num_retrievers": len(self.retrievers),
            "weights": self.weights,
            "fusion_algorithm": self.fusion_algorithm,
            "rrf_k": self.rrf_k,
            "final_k": self.final_k,
        }