jira877832 commited on
Commit
3cd23b1
·
verified ·
1 Parent(s): e5952ef

Create reranker.py

Browse files
Files changed (1) hide show
  1. reranker.py +11 -0
reranker.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sentence_transformers import CrossEncoder
2
+
3
+ class CrossEncoderRanker:
4
+ def __init__(self, model_name="BAAI/bge-reranker-large"):
5
+ self.model = CrossEncoder(model_name)
6
+
7
+ def rank(self, question: str, chunks: list, top_k: int = 5):
8
+ pairs = [(question, chunk) for chunk in chunks]
9
+ scores = self.model.predict(pairs)
10
+ ranked = sorted(zip(chunks, scores), key=lambda x: x[1], reverse=True)
11
+ return ranked[:top_k] # [(chunk, score), ...]