File size: 1,336 Bytes
95183e4
 
4ab96ab
95183e4
 
 
73e7897
95183e4
 
 
 
 
4ab96ab
 
95183e4
4ab96ab
 
 
 
 
95183e4
4ab96ab
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from typing import Any, Dict, List

from haystack.schema import Document
from fastrag.retrievers import QuantizedBiEncoderRetriever


class EndpointHandler:
    def __init__(self, path=""):
        model_id = "Intel/bge-small-en-v1.5-rag-int8-static"
        self.retriever = QuantizedBiEncoderRetriever(embedding_model=model_id)

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

        if queries is not None:
            assert isinstance(queries, list), "Expected queries to be a list"
            assert all(
                isinstance(query, str) for query in queries
            ), "Expected each query in queries to be a string"

            return self.retriever.embed_queries(queries=queries)
        elif documents is not None:
            assert isinstance(documents, list), "Expected documents to be a list"
            assert all(
                isinstance(document, dict) for document in documents
            ), "Expected each document in documents to be a dictionary"

            documents = [Document.from_dict(document) for document in documents]
            return self.retriever.embed_documents(documents=documents)
        else:
            raise ValueError("Expected either queries or documents")