Download src/retrieval/reranker.py from Estarcode/campusdesk-ai: direct link, hf CLI and curl.
- Browser
- Download file 1.87 kB
-
https://huggingface.co/Estarcode/campusdesk-ai/resolve/main/src/retrieval/reranker.py
- Command line
-
hf download hf://Estarcode/campusdesk-ai/src/retrieval/reranker.py
-
curl -L -o reranker.py https://huggingface.co/Estarcode/campusdesk-ai/resolve/main/src/retrieval/reranker.py
1.87 kB
| """Multi-subquery hybrid score fusion and soft intent reranker.""" | |
| from typing import List, Optional, Tuple | |
| import numpy as np | |
| from src.schemas import PassageRecord, RetrievedPassage | |
| def rerank_passages( | |
| passages: List[PassageRecord], | |
| dense_scores_per_subquery: List[np.ndarray], | |
| bm25_scores_per_subquery: List[np.ndarray], | |
| predicted_intent: Optional[str] = None, | |
| intent_bonus_weight: float = 0.08, | |
| top_k: int = 3, | |
| ) -> List[RetrievedPassage]: | |
| """ | |
| Fuse full-query and sub-query relevance with a soft intent prior. | |
| Uses soft boosting rather than hard filtering so multi-part questions | |
| can retrieve evidence across all notices. | |
| """ | |
| dense_full = dense_scores_per_subquery[0] | |
| dense_sub_max = np.max(np.vstack(dense_scores_per_subquery), axis=0) | |
| dense_combined = 0.45 * dense_full + 0.55 * dense_sub_max | |
| bm25_full = bm25_scores_per_subquery[0] | |
| bm25_sub_max = np.max(np.vstack(bm25_scores_per_subquery), axis=0) | |
| bm25_combined = 0.45 * bm25_full + 0.55 * bm25_sub_max | |
| scored: List[Tuple[float, float, float, float, PassageRecord]] = [] | |
| for idx, p in enumerate(passages): | |
| d_val = float(dense_combined[idx]) | |
| b_val = float(bm25_combined[idx]) | |
| i_bonus = ( | |
| intent_bonus_weight if (predicted_intent and predicted_intent in p.intent_hints) else 0.0 | |
| ) | |
| total = 0.62 * d_val + 0.38 * b_val + i_bonus | |
| scored.append((total, d_val, b_val, i_bonus, p)) | |
| scored.sort(key=lambda x: x[0], reverse=True) | |
| return [ | |
| RetrievedPassage( | |
| passage=item[4], | |
| score=round(item[0], 4), | |
| dense_score=round(item[1], 4), | |
| lexical_score=round(item[2], 4), | |
| intent_bonus=round(item[3], 4), | |
| rank=rank_idx, | |
| ) | |
| for rank_idx, item in enumerate(scored[:top_k], start=1) | |
| ] | |