08. Weighted Hybrid Strategies
RRF treats all strategies equally, but domain knowledge often suggests certain strategies should contribute more.
Adaptive weighting adjusts strategy weights based on query characteristics. Queries with rare terms benefit from sparse retrieval dominance; conceptual queries benefit from dense retrieval.
Manual weighting assigns fixed weights based on offline evaluation. If dense retrieval consistently outperforms sparse on your benchmark, assign higher weight.
from typing import List, Dict
def weighted_hybrid_fusion(
results_by_strategy: Dict[str, List[dict]],
weights: Dict[str, float],
k: int = 60
) -> List[dict]:
"""
Weighted RRF fusion across strategies.
Args:
results_by_strategy: {strategy_name: [results]}
weights: {strategy_name: weight} - weights sum to 1
k: RRF constant
"""
# Normalize weights
total_weight = sum(weights.values())
normalized_weights = {k: v / total_weight for k, v in weights.items()}
doc_scores = defaultdict(float)
doc_metadata = {}
for strategy_name, strategy_results in results_by_strategy.items():
weight = normalized_weights.get(strategy_name, 0)
for rank, result in enumerate(strategy_results, start=1):
doc_id = result.get('doc_id', result.get('index', rank))
# Weighted RRF contribution
doc_scores[doc_id] += weight * (1 / (k + rank))
if doc_id not in doc_metadata:
doc_metadata[doc_id] = result.copy()
sorted_docs = sorted(doc_scores.items(), key=lambda x: x[1], reverse=True)
return [
{**doc_metadata[doc_id], 'weighted_rrf_score': score, 'doc_id': doc_id}
for doc_id, score in sorted_docs
]
# Example: weight dense retrieval higher
fused_results = weighted_hybrid_fusion(
results_by_strategy={
'dense': dense_search(query, 100),
'sparse': bm25_search(query, 100),
'keyword': keyword_search(query, 50)
},
weights={
'dense': 0.5,
'sparse': 0.35,
'keyword': 0.15
}
)
Local verification checkpoint
Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.
Local verification checkpoint
Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.
Run a grid search over weight combinations on your benchmark. Report the weights that maximize MRR@10.