File size: 408 Bytes
1268b93
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
from FlagEmbedding import FlagReranker

reranker = FlagReranker('BAAI/bge-reranker-large',
                        use_fp16=True)


def rerank(query: str, documents: [str], k: int) -> [str]:
    scores = reranker.compute_score([(query, document) for document in documents])
    sorted_docs = sorted(enumerate(scores), key=lambda x: x[1], reverse=True)
    return [sorted_docs[i] for i, _ in sorted_docs[:k]]