Repository: deepset-ai/haystack

Issue:
Wrong encoder instance for a sbert-based retriever?
**Question**
Hi! When I instance a retriever in this way:

```python
from haystack.nodes import EmbeddingRetriever

retriever = EmbeddingRetriever(
    document_store=document_store, 
    embedding_model="sentence-transformers/paraphrase-multilingual-mpnet-base-v2"
)
```

I've noticed that the `model_format` property of the `EmbeddingRetriever` instance is always set to `farm`. So an instance of `_DefaultEmbeddingEncoder` is created instead of an instance of `_SentenceTransformersEmbeddingEncoder`.

This implies that `inference_from_dicts` is called when calling `document_store.update_embeddings()` but it I guess it should be not needed for Sentence Transformers model embeddings (it takes a lot more time).

To make things run faster, one should do:

```python
from haystack.nodes import EmbeddingRetriever

retriever = EmbeddingRetriever(
    document_store=document_store,
    embedding_model="sentence-transformers/paraphrase-multilingual-mpnet-base-v2",
    model_format="sentence_transformers"
)
```

This is fine, but shouldn't `model_format` be automatically inferred from model id/name? 



Discussion:
Hi @mpangrazzi,

this is a very valid feature idea. One thing to consider: it's not always possible to infer the `model_format` from the model name, e.g. if it is a local path. So automatic inference would not work. But, in a lot of cases, sentence transformers models are named `sentence_transformers/ ...` .
We'd suggest to implement a **warning** if the param `embedding_model` starts with "sentence_transformers" and the param `model_format` is either not set or set to `farm`.

Would you like to contribute to Haystack by suggesting this change in a Pull Request? :)

Thanks @mkkuemmel. Sure, will do in the next few days :)
Hi @mpangrazzi! Are you still interested on raising this Pull Request? :) If not, we can put this into our backlog.

Resolving patch:
diff --git a/haystack/nodes/retriever/dense.py b/haystack/nodes/retriever/dense.py
--- a/haystack/nodes/retriever/dense.py
+++ b/haystack/nodes/retriever/dense.py
@@ -1143,6 +1143,13 @@ def __init__(
 
         if model_format not in _EMBEDDING_ENCODERS.keys():
             raise ValueError(f"Unknown retriever embedding model format {model_format}")
+
+        if self.embedding_model.startswith("sentence-transformers") and self.model_format != "sentence_transformers":
+            logger.warning(
+                f"You seem to be using a Sentence Transformer embedding model but 'model_format' is set to '{self.model_format}'."
+                f" You may need to set 'model_format='sentence_transformers' to ensure correct loading of model."
+            )
+
         self.embedding_encoder = _EMBEDDING_ENCODERS[model_format](self)
 
     def retrieve(


Test patch:
diff --git a/test/test_retriever.py b/test/test_retriever.py
--- a/test/test_retriever.py
+++ b/test/test_retriever.py
@@ -1,7 +1,9 @@
+import logging
 import time
 
 import numpy as np
 import pandas as pd
+from haystack.document_stores.memory import InMemoryDocumentStore
 import pytest
 from pathlib import Path
 from elasticsearch import Elasticsearch
@@ -11,7 +13,7 @@
 from haystack.document_stores.elasticsearch import ElasticsearchDocumentStore
 from haystack.document_stores.faiss import FAISSDocumentStore
 from haystack.document_stores import MilvusDocumentStore
-from haystack.nodes.retriever.dense import DensePassageRetriever, TableTextRetriever
+from haystack.nodes.retriever.dense import DensePassageRetriever, EmbeddingRetriever, TableTextRetriever
 from haystack.nodes.retriever.sparse import ElasticsearchRetriever, ElasticsearchFilterOnlyRetriever, TfidfRetriever
 from transformers import DPRContextEncoderTokenizerFast, DPRQuestionEncoderTokenizerFast
 
@@ -123,7 +125,7 @@ def test_elasticsearch_custom_query():
         document_store=document_store,
         custom_query="""
             {
-                "size": 10, 
+                "size": 10,
                 "query": {
                     "bool": {
                         "should": [{
@@ -138,7 +140,7 @@ def test_elasticsearch_custom_query():
         document_store=document_store,
         custom_query="""
                 {
-                    "size": 10, 
+                    "size": 10,
                     "query": {
                         "bool": {
                             "should": [{
@@ -549,3 +551,17 @@ def test_elasticsearch_all_terms_must_match():
     results_w_all_terms_must_match = doc_store.query(query="drink green tea", all_terms_must_match=True)
     assert len(results_w_all_terms_must_match) == 1
     doc_store.delete_index(index)
+
+
+def test_embeddings_encoder_of_embedding_retriever_should_warn_about_model_format(caplog):
+    document_store = InMemoryDocumentStore()
+
+    with caplog.at_level(logging.WARNING):
+        EmbeddingRetriever(
+            document_store=document_store, embedding_model="sentence-transformers/paraphrase-multilingual-mpnet-base-v2"
+        )
+
+        assert (
+            "You may need to set 'model_format='sentence_transformers' to ensure correct loading of model."
+            in caplog.text
+        )