Repository: deepset-ai/haystack

Issue:
Update warning for dot_product when using sentence transformers
**Is your feature request related to a problem? Please describe.**

Is there any reason why we always recommend using cosine similarity when using sentence transformer models?
The warning is even issued when using a [model](https://huggingface.co/sentence-transformers/multi-qa-mpnet-base-dot-v1) that recommends usage of dot product in the model card:
```
WARNING:haystack.nodes.retriever._embedding_encoder: You are using a Sentence Transformer with the dot_product function. We recommend using cosine instead. This can be set when initializing the DocumentStore
```

![image](https://user-images.githubusercontent.com/1563902/196897741-4267520f-d203-4186-8c9d-508a5c6e4501.png)

**Describe the solution you'd like**
Double-check if there is a reason for this generic warning or if not, either remove it OR make it model-dependent (e.g. for sentence transformers the metric is often part of the model name)



Discussion:

Resolving patch:
diff --git a/haystack/nodes/retriever/_embedding_encoder.py b/haystack/nodes/retriever/_embedding_encoder.py
--- a/haystack/nodes/retriever/_embedding_encoder.py
+++ b/haystack/nodes/retriever/_embedding_encoder.py
@@ -89,6 +89,33 @@ def save(self, save_dir: Union[Path, str]):
         """
         pass
 
+    def _check_docstore_similarity_function(self, retriever: "EmbeddingRetriever"):
+        """
+        Check that document_store uses a similarity function
+        compatible with the embedding model
+        """
+        docstore_similarity = retriever.document_store.similarity
+        model_name = retriever.embedding_model
+
+        if "sentence-transformers" in model_name.lower():
+            model_similarity = None
+            if "-cos-" in model_name.lower():
+                model_similarity = "cosine"
+            elif "-dot-" in model_name.lower():
+                model_similarity = "dot_product"
+
+            if model_similarity is not None and docstore_similarity != model_similarity:
+                logger.warning(
+                    f"You seem to be using {model_name} model with the {docstore_similarity} function instead of the recommended {model_similarity}. "
+                    f"This can be set when initializing the DocumentStore"
+                )
+        elif "dpr" in model_name.lower() and docstore_similarity != "dot_product":
+            logger.warning(
+                f"You seem to be using a DPR model with the {docstore_similarity} function. "
+                f"We recommend using dot_product instead. "
+                f"This can be set when initializing the DocumentStore"
+            )
+
 
 class _DefaultEmbeddingEncoder(_BaseEmbeddingEncoder):
     def __init__(self, retriever: "EmbeddingRetriever"):
@@ -105,21 +132,7 @@ def __init__(self, retriever: "EmbeddingRetriever"):
             num_processes=0,
             use_auth_token=retriever.use_auth_token,
         )
-        # Check that document_store has the right similarity function
-        similarity = retriever.document_store.similarity
-        # If we are using a sentence transformer model
-        if "sentence" in retriever.embedding_model.lower() and similarity != "cosine":
-            logger.warning(
-                f"You seem to be using a Sentence Transformer with the {similarity} function. "
-                f"We recommend using cosine instead. "
-                f"This can be set when initializing the DocumentStore"
-            )
-        elif "dpr" in retriever.embedding_model.lower() and similarity != "dot_product":
-            logger.warning(
-                f"You seem to be using a DPR model with the {similarity} function. "
-                f"We recommend using dot_product instead. "
-                f"This can be set when initializing the DocumentStore"
-            )
+        self._check_docstore_similarity_function(retriever)
 
     def embed(self, texts: Union[List[List[str]], List[str], str]) -> np.ndarray:
         # TODO: FARM's `sample_to_features_text` need to fix following warning -
@@ -182,13 +195,7 @@ def __init__(self, retriever: "EmbeddingRetriever"):
         self.batch_size = retriever.batch_size
         self.embedding_model.max_seq_length = retriever.max_seq_len
         self.show_progress_bar = retriever.progress_bar
-        document_store = retriever.document_store
-        if document_store.similarity != "cosine":
-            logger.warning(
-                f"You are using a Sentence Transformer with the {document_store.similarity} function. "
-                f"We recommend using cosine instead. "
-                f"This can be set when initializing the DocumentStore"
-            )
+        self._check_docstore_similarity_function(retriever)
 
     def embed(self, texts: Union[List[List[str]], List[str], str]) -> np.ndarray:
         # texts can be a list of strings or a list of [title, text]


Test patch: