IlyasMoutawwakil HF staff commited on
Commit
4ab96ab
1 Parent(s): 73e7897

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +19 -4
handler.py CHANGED
@@ -1,5 +1,6 @@
1
  from typing import Any, Dict, List
2
 
 
3
  from fastrag.retrievers import QuantizedBiEncoderRetriever
4
 
5
 
@@ -9,9 +10,23 @@ class EndpointHandler:
9
  self.retriever = QuantizedBiEncoderRetriever(embedding_model=model_id)
10
 
11
  def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
12
- queries = data.get("queries")
 
13
 
14
- assert isinstance(queries, list), "Expected a list of queries"
15
- assert all(isinstance(query, str) for query in queries), "Expected a list of strings"
 
 
 
16
 
17
- return self.retriever.embed_queries(queries=queries)
 
 
 
 
 
 
 
 
 
 
 
1
  from typing import Any, Dict, List
2
 
3
+ from haystack.schema import Document
4
  from fastrag.retrievers import QuantizedBiEncoderRetriever
5
 
6
 
 
10
  self.retriever = QuantizedBiEncoderRetriever(embedding_model=model_id)
11
 
12
  def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
13
+ queries = data.get("queries", None)
14
+ documents = data.get("documents", None)
15
 
16
+ if queries is not None:
17
+ assert isinstance(queries, list), "Expected queries to be a list"
18
+ assert all(
19
+ isinstance(query, str) for query in queries
20
+ ), "Expected each query in queries to be a string"
21
 
22
+ return self.retriever.embed_queries(queries=queries)
23
+ elif documents is not None:
24
+ assert isinstance(documents, list), "Expected documents to be a list"
25
+ assert all(
26
+ isinstance(document, dict) for document in documents
27
+ ), "Expected each document in documents to be a dictionary"
28
+
29
+ documents = [Document.from_dict(document) for document in documents]
30
+ return self.retriever.embed_documents(documents=documents)
31
+ else:
32
+ raise ValueError("Expected either queries or documents")