study / backend /reranker.py
Michael Bernovskiy
reranker added
1268b93
raw
history blame
408 Bytes
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]]