# Buggy code
def __init__(
    self,
    document_store: Type[BaseDocumentStore],
    embedding_model: str,
    gpu: bool = True,
    model_format: str = "farm",
    pooling_strategy: str = "reduce_mean",
    emb_extraction_layer: int = -1,
):
    """
    TODO
    :param document_store:
    :param embedding_model:
    :param gpu:
    :param model_format:
    """
    self.document_store = document_store
    self.model_format = model_format
    self.embedding_model = embedding_model
    self.pooling_strategy = pooling_strategy
    self.emb_extraction_layer = emb_extraction_layer

    logger.info(f"Init retriever using embeddings of model {embedding_model}")
    if model_format == "farm" or model_format == "transformers":
        self.embedding_model = Inferencer.load(
            embedding_model,
            task_type="embeddings",
            extraction_strategy=self.pooling_strategy,
            extraction_layer=self.emb_extraction_layer,
            gpu=gpu,
            batch_size=4,
            max_seq_len=512,
            num_processes=0,
        )

    elif model_format == "sentence_transformers":
        from sentence_transformers import SentenceTransformer

        # pretrained embedding models coming from: https://github.com/UKPLab/sentence-transformers#pretrained-models
        # e.g. 'roberta-base-nli-stsb-mean-tokens'
        self.embedding_model = SentenceTransformer(embedding_model)
    else:
        raise NotImplementedError


# Traceback (TypeError)
Traceback (most recent call last):
File "/home/pedro/.local/lib/python3.8/site-packages/elasticsearch/serializer.py", line 50, in dumps
return json.dumps(
File "/home/pedro/.local/lib/python3.8/site-packages/simplejson/__init__.py", line 398, in dumps
return cls(
File "/home/pedro/.local/lib/python3.8/site-packages/simplejson/encoder.py", line 296, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/home/pedro/.local/lib/python3.8/site-packages/simplejson/encoder.py", line 378, in iterencode
return _iterencode(o, 0)
File "/home/pedro/.local/lib/python3.8/site-packages/elasticsearch/serializer.py", line 36, in default
raise TypeError("Unable to serialize %r (type: %s)" % (data, type(data)))
TypeError: Unable to serialize -0.065520875 (type: <class 'numpy.float32'>)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "sdk/accuracy_retriever.py", line 75, in <module>
top_docs = haystack_retriever(q, doc_store)
File "/mnt/Documents/Projets/BotPress/R_D/R_D_q_a/sdk/retrievers.py", line 113, in haystack_retriever
top_docs = retriever.retrieve(query=q, top_k=10)
File "/mnt/Documents/Projets/git_clones/haystack/haystack/retriever/elasticsearch.py", line 92, in retrieve
documents = self.document_store.query_by_embedding(query_emb[0], top_k, candidate_doc_ids)
File "/mnt/Documents/Projets/git_clones/haystack/haystack/database/elasticsearch.py", line 184, in query_by_embedding
result = self.client.search(index=self.index, body=body)["hits"]["hits"]
File "/home/pedro/.local/lib/python3.8/site-packages/elasticsearch/client/utils.py", line 92, in _wrapped
return func(*args, params=params, headers=headers, **kwargs)
File "/home/pedro/.local/lib/python3.8/site-packages/elasticsearch/client/__init__.py", line 1622, in search
return self.transport.perform_request(
File "/home/pedro/.local/lib/python3.8/site-packages/elasticsearch/transport.py", line 321, in perform_request
body = self.serializer.dumps(body)
File "/home/pedro/.local/lib/python3.8/site-packages/elasticsearch/serializer.py", line 54, in dumps
raise SerializationError(data, e)
elasticsearch.exceptions.SerializationError: ({'size': 10, 'query': {'script_score': {'query': {'match_all': {}}, 'script': {'source': "cosineSimilarity(params.query_vector,doc['question_emb']) + 1.0", 'params': {'query_vector': [-0.065520875, 0.023728848, ... lot of numbers ..., 0.047961414]}}}}, '_source': {'excludes': ['question_emb']}}, TypeError("Unable to serialize -0.065520875 (type: <class 'numpy.float32'>)"))

# Fixed code
def __init__(
    self,
    document_store: Type[BaseDocumentStore],
    embedding_model: str,
    gpu: bool = True,
    model_format: str = "farm",
    pooling_strategy: str = "reduce_mean",
    emb_extraction_layer: int = -1,
):
    """
    TODO
    :param document_store:
    :param embedding_model:
    :param gpu:
    :param model_format:
    """
    self.document_store = document_store
    self.model_format = model_format
    self.embedding_model = embedding_model
    self.pooling_strategy = pooling_strategy
    self.emb_extraction_layer = emb_extraction_layer

    logger.info(f"Init retriever using embeddings of model {embedding_model}")
    if model_format == "farm" or model_format == "transformers":
        self.embedding_model = Inferencer.load(
            embedding_model,
            task_type="embeddings",
            extraction_strategy=self.pooling_strategy,
            extraction_layer=self.emb_extraction_layer,
            gpu=gpu,
            batch_size=4,
            max_seq_len=512,
            num_processes=0,
        )

    elif model_format == "sentence_transformers":
        from sentence_transformers import SentenceTransformer

        # pretrained embedding models coming from: https://github.com/UKPLab/sentence-transformers#pretrained-models
        # e.g. 'roberta-base-nli-stsb-mean-tokens'
        if gpu:
            device = "gpu"
        else:
            device = "cpu"
        self.embedding_model = SentenceTransformer(embedding_model, device=device)
    else:
        raise NotImplementedError