from typing import Dict, List, Any from FlagEmbedding import BGEM3FlagModel class EndpointHandler(): def __init__(self, path=""): self.model = BGEM3FlagModel('BAAI/bge-m3', use_fp16=True) def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]: """ data args: inputs (:`list`: `str`) kwargs Return: A :obj:`list` | `dict`: will be serialized and returned """ results = [] inputs = data.pop("inputs",data) for i in inputs: output = self.model.encode(i, return_dense=False, return_sparse=True, return_colbert_vecs=False) results.append(self._toJsonSerialisableFormat(self.model.convert_id_to_token(output['lexical_weights']))) return results def _toJsonSerialisableFormat(self, model_output): # convert the numpy float16 to a Python float # so it can be serialised as JSON return {key:float(value) for (key,value) in model_output.items()}