IlyasMoutawwakil HF staff commited on
Commit
575fb42
1 Parent(s): 2d8aeb1

Create handler.py

Browse files
Files changed (1) hide show
  1. handler.py +38 -0
handler.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Any, Dict, List
2
+
3
+ from fastrag.rankers import QuantizedBiEncoderRanker
4
+ from fastrag.retrievers import QuantizedBiEncoderRetriever
5
+ from haystack import Pipeline
6
+ from haystack.document_stores import InMemoryDocumentStore
7
+ from haystack.schema import Document
8
+
9
+
10
+ class EndpointHandler:
11
+ def __init__(self, path=""):
12
+ EXAMPLES = [
13
+ "There is a blue house on Oxford Street.",
14
+ "Paris is the capital of France.",
15
+ "The first commit in fastRAG was in 2022",
16
+ ]
17
+ document_store = InMemoryDocumentStore(use_gpu=False, use_bm25=False, embedding_dim=384, return_embedding=True)
18
+
19
+ documents = []
20
+ for i, d in enumerate(EXAMPLES):
21
+ documents.append(Document(content=d, id=i))
22
+
23
+ document_store.write_documents(documents)
24
+
25
+ model_id = "Intel/bge-small-en-v1.5-rag-int8-static"
26
+ retriever = QuantizedBiEncoderRetriever(document_store=document_store, embedding_model=model_id)
27
+ document_store.update_embeddings(retriever=retriever)
28
+
29
+ ranker = QuantizedBiEncoderRanker("Intel/bge-large-en-v1.5-rag-int8-static")
30
+
31
+ self.pipe = Pipeline()
32
+ self.pipe.add_node(component=retriever, name="retriever", inputs=["Query"])
33
+ self.pipe.add_node(component=ranker, name="ranker", inputs=["retriever"])
34
+
35
+ def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
36
+ query = data.pop("inputs", data)
37
+ results = self.pipe.run(query=query)
38
+ return results