File size: 821 Bytes
138bca0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from typing import Any, Dict, List

from fastrag.rankers import QuantizedBiEncoderRanker


class RankerHandler:
    def __init__(self, path=""):
        model_id = "Intel/bge-large-en-v1.5-rag-int8-static"
        self.ranker = QuantizedBiEncoderRanker(model_name_or_path=model_id)

    def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
        query = data.get("query")
        documents = data.get("documents")

        top_k = data.get("top_k", None)

        assert isinstance(query, str), "Expected query to be a string"
        assert isinstance(documents, list), "Expected documents to be a list"
        assert all(isinstance(document, str) for document in documents), "Expected documents to be a list of strings"

        return self.ranker.predict(query=query, documents=documents, top_k=top_k)