File size: 8,120 Bytes
ed65693
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
"""Fusion strategies for combining sparse (BM25) and dense (FAISS) retrieval results.

Implements multiple fusion approaches for ablation comparison:
- Reciprocal Rank Fusion (RRF) — rank-based, score-agnostic baseline
- Linear combination — fixed α weighted sum
- Entropy-weighted fusion — adaptive α per query via calibrated entropy

All fusion methods produce a unified candidate list from two retriever outputs.
"""

from typing import Any

import numpy as np

from app.calibration import (
    CALIBRATION_METHODS,
    compute_alpha,
    compute_entropy,
)


def _merge_candidates(
    sparse_results: list[dict[str, Any]],
    dense_results: list[dict[str, Any]],
) -> dict[str, dict[str, Any]]:
    """Merge candidates from both retrievers, keyed by (source, chunk).

    Returns dict mapping key → {source, chunk, text, sparse_score, dense_score}.
    """
    merged: dict[str, dict[str, Any]] = {}

    for r in sparse_results:
        key = f"{r['source']}:{r['chunk']}"
        if key not in merged:
            merged[key] = {
                "source": r["source"],
                "chunk": r["chunk"],
                "text": r["text"],
                "sparse_score": r.get("score", 0.0),
                "dense_score": 0.0,
            }
        else:
            merged[key]["sparse_score"] = r.get("score", 0.0)

    for r in dense_results:
        key = f"{r['source']}:{r['chunk']}"
        if key not in merged:
            merged[key] = {
                "source": r["source"],
                "chunk": r["chunk"],
                "text": r["text"],
                "sparse_score": 0.0,
                "dense_score": r.get("score", 0.0),
            }
        else:
            merged[key]["dense_score"] = r.get("score", 0.0)

    return merged


def rrf_fuse(
    sparse_results: list[dict[str, Any]],
    dense_results: list[dict[str, Any]],
    k: int = 60,
    top_k: int | None = None,
) -> tuple[list[dict[str, Any]], dict[str, Any]]:
    """Reciprocal Rank Fusion.

    RRF(d) = Σ 1/(k + rank(d)) across retriever lists.
    Score-agnostic, operates on ranks only.

    Returns (fused_results, metadata).
    """
    rrf_scores: dict[str, float] = {}
    doc_data: dict[str, dict[str, Any]] = {}

    # Process sparse results
    for rank, r in enumerate(sparse_results, start=1):
        key = f"{r['source']}:{r['chunk']}"
        rrf_scores[key] = rrf_scores.get(key, 0.0) + 1.0 / (k + rank)
        if key not in doc_data:
            doc_data[key] = {
                "source": r["source"],
                "chunk": r["chunk"],
                "text": r["text"],
            }

    # Process dense results
    for rank, r in enumerate(dense_results, start=1):
        key = f"{r['source']}:{r['chunk']}"
        rrf_scores[key] = rrf_scores.get(key, 0.0) + 1.0 / (k + rank)
        if key not in doc_data:
            doc_data[key] = {
                "source": r["source"],
                "chunk": r["chunk"],
                "text": r["text"],
            }

    # Sort by RRF score
    sorted_keys = sorted(rrf_scores, key=rrf_scores.get, reverse=True)

    results = []
    for key in sorted_keys:
        r = doc_data[key]
        r["score"] = round(rrf_scores[key], 6)
        results.append(r)

    if top_k is not None:
        results = results[:top_k]

    metadata = {"fusion_method": "rrf", "rrf_k": k}
    return results, metadata


def linear_fuse(
    sparse_results: list[dict[str, Any]],
    dense_results: list[dict[str, Any]],
    alpha: float = 0.5,
    calibration: str = "minmax",
    corpus_cdfs: tuple[np.ndarray, np.ndarray] | None = None,
    top_k: int | None = None,
) -> tuple[list[dict[str, Any]], dict[str, Any]]:
    """Linear combination with fixed alpha.

    fused_score = α * sparse_calibrated + (1-α) * dense_calibrated

    Args:
        alpha: Weight for sparse retriever (0=all dense, 1=all sparse).
        calibration: Score calibration method ('raw', 'minmax', 'zscore', 'cdf').
        corpus_cdfs: (cdf_bm25, cdf_dense) required when calibration='cdf'.
    """
    merged = _merge_candidates(sparse_results, dense_results)
    if not merged:
        return [], {
            "fusion_method": "linear",
            "alpha": alpha,
            "calibration": calibration,
        }

    sparse_scores = np.array([m["sparse_score"] for m in merged.values()])
    dense_scores = np.array([m["dense_score"] for m in merged.values()])

    effective_calibration = calibration
    if calibration == "cdf" and corpus_cdfs is None:
        effective_calibration = "minmax"

    cal_fn = CALIBRATION_METHODS[effective_calibration]
    if effective_calibration == "cdf" and corpus_cdfs is not None:
        cal_sparse = cal_fn(sparse_scores, corpus_cdf=corpus_cdfs[0])
        cal_dense = cal_fn(dense_scores, corpus_cdf=corpus_cdfs[1])
    else:
        cal_sparse = cal_fn(sparse_scores)
        cal_dense = cal_fn(dense_scores)

    fused_scores = alpha * cal_sparse + (1 - alpha) * cal_dense

    results = []
    for (_key, data), score in zip(merged.items(), fused_scores, strict=True):
        results.append(
            {
                "source": data["source"],
                "chunk": data["chunk"],
                "text": data["text"],
                "score": round(float(score), 6),
            }
        )

    results.sort(key=lambda x: x["score"], reverse=True)
    if top_k is not None:
        results = results[:top_k]

    metadata = {
        "fusion_method": "linear",
        "alpha": alpha,
        "calibration": effective_calibration,
        "requested_calibration": calibration,
    }
    return results, metadata


def entropy_fuse(
    sparse_results: list[dict[str, Any]],
    dense_results: list[dict[str, Any]],
    calibration: str = "cdf",
    corpus_cdfs: tuple[np.ndarray, np.ndarray] | None = None,
    top_k: int | None = None,
) -> tuple[list[dict[str, Any]], dict[str, Any]]:
    """Entropy-weighted adaptive fusion.

    Computes per-query alpha from calibrated score distribution entropy:
    α = H_dense / (H_dense + H_sparse + ε)

    When dense has high entropy (low confidence), α is large → more weight on BM25.
    """
    merged = _merge_candidates(sparse_results, dense_results)
    if not merged:
        return [], {
            "fusion_method": "entropy_weighted",
            "alpha": 0.5,
            "h_sparse": 0.0,
            "h_dense": 0.0,
            "calibration": calibration,
        }

    sparse_scores = np.array([m["sparse_score"] for m in merged.values()])
    dense_scores = np.array([m["dense_score"] for m in merged.values()])

    effective_calibration = calibration
    if calibration == "cdf" and corpus_cdfs is None:
        effective_calibration = "minmax"

    cal_fn = CALIBRATION_METHODS[effective_calibration]
    if effective_calibration == "cdf" and corpus_cdfs is not None:
        cal_sparse = cal_fn(sparse_scores, corpus_cdf=corpus_cdfs[0])
        cal_dense = cal_fn(dense_scores, corpus_cdf=corpus_cdfs[1])
    else:
        cal_sparse = cal_fn(sparse_scores)
        cal_dense = cal_fn(dense_scores)

    # Compute entropy of each retriever's calibrated scores
    h_sparse = compute_entropy(cal_sparse)
    h_dense = compute_entropy(cal_dense)

    # Adaptive alpha
    alpha = compute_alpha(h_dense, h_sparse)

    # Fuse
    fused_scores = alpha * cal_sparse + (1 - alpha) * cal_dense

    results = []
    for (_key, data), score in zip(merged.items(), fused_scores, strict=True):
        results.append(
            {
                "source": data["source"],
                "chunk": data["chunk"],
                "text": data["text"],
                "score": round(float(score), 6),
            }
        )

    results.sort(key=lambda x: x["score"], reverse=True)
    if top_k is not None:
        results = results[:top_k]

    metadata = {
        "fusion_method": "entropy_weighted",
        "alpha": round(alpha, 6),
        "h_sparse": round(h_sparse, 6),
        "h_dense": round(h_dense, 6),
        "calibration": effective_calibration,
        "requested_calibration": calibration,
    }
    return results, metadata