Repository: deepset-ai/haystack

Issue:
`PromptNode`: some Hugging Face models are not recognized
**Describe the bug**
Some Hugging Face models are not recognized and the Haystack chooses the (wrong) OpenAI invocation layer.

**Error message**
> OpenAIError: api_key None must be a valid OpenAI key. Visit https://openai.com/api/ to get one.

**To Reproduce**
```python
from haystack.nodes import PromptNode

prompt_node = PromptNode(model_name_or_path="ybelkada/flan-t5-xl-sharded-bf16")
```

**System:**
 - OS: Ubuntu 22.04, Colab
 - Haystack version (commit or version number): 1.18.1, 1.19.0rc0



Discussion:

Resolving patch:
diff --git a/haystack/nodes/prompt/invocation_layer/azure_open_ai.py b/haystack/nodes/prompt/invocation_layer/azure_open_ai.py
--- a/haystack/nodes/prompt/invocation_layer/azure_open_ai.py
+++ b/haystack/nodes/prompt/invocation_layer/azure_open_ai.py
@@ -42,5 +42,7 @@ def supports(cls, model_name_or_path: str, **kwargs) -> bool:
         Ensures Azure OpenAI Invocation Layer is selected when `azure_base_url` and `azure_deployment_name` are provided in
         addition to a list of supported models.
         """
-        valid_model = any(m for m in ["ada", "babbage", "davinci", "curie"] if m in model_name_or_path)
+        valid_model = model_name_or_path in ["ada", "babbage", "davinci", "curie"] or any(
+            m in model_name_or_path for m in ["-ada-", "-babbage-", "-davinci-", "-curie-"]
+        )
         return valid_model and has_azure_parameters(**kwargs)
diff --git a/haystack/nodes/prompt/invocation_layer/open_ai.py b/haystack/nodes/prompt/invocation_layer/open_ai.py
--- a/haystack/nodes/prompt/invocation_layer/open_ai.py
+++ b/haystack/nodes/prompt/invocation_layer/open_ai.py
@@ -231,5 +231,7 @@ def _ensure_token_limit(self, prompt: Union[str, List[Dict[str, str]]]) -> Union
 
     @classmethod
     def supports(cls, model_name_or_path: str, **kwargs) -> bool:
-        valid_model = any(m for m in ["ada", "babbage", "davinci", "curie"] if m in model_name_or_path)
+        valid_model = model_name_or_path in ["ada", "babbage", "davinci", "curie"] or any(
+            m in model_name_or_path for m in ["-ada-", "-babbage-", "-davinci-", "-curie-"]
+        )
         return valid_model and not has_azure_parameters(**kwargs)
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
@@ -1873,7 +1873,10 @@ def _preprocess_documents(self, docs: List[Document]) -> List[Document]:
 
     @staticmethod
     def _infer_model_format(model_name_or_path: str, use_auth_token: Optional[Union[str, bool]]) -> str:
-        if any(m in model_name_or_path for m in ["ada", "babbage", "davinci", "curie"]):
+        valid_openai_model_name = model_name_or_path in ["ada", "babbage", "davinci", "curie"] or any(
+            m in model_name_or_path for m in ["-ada-", "-babbage-", "-davinci-", "-curie-"]
+        )
+        if valid_openai_model_name:
             return "openai"
         if model_name_or_path in ["small", "medium", "large", "multilingual-22-12", "finance-sentiment"]:
             return "cohere"


Test patch:
diff --git a/test/prompt/invocation_layer/test_openai.py b/test/prompt/invocation_layer/test_openai.py
--- a/test/prompt/invocation_layer/test_openai.py
+++ b/test/prompt/invocation_layer/test_openai.py
@@ -120,3 +120,18 @@ def test_openai_organization(mock_open_ai_request, load_openai_tokenizer):
 
     invocation_layer.invoke(prompt="dummy_prompt")
     assert mock_open_ai_request.call_args.kwargs["headers"]["OpenAI-Organization"] == "fake_organization"
+
+
+@pytest.mark.unit
+def test_supports(load_openai_tokenizer):
+    layer = OpenAIInvocationLayer(api_key="some_fake_key")
+
+    assert layer.supports("ada")
+    assert layer.supports("babbage")
+    assert layer.supports("curie")
+    assert layer.supports("davinci")
+    assert layer.supports("text-ada-001")
+    assert layer.supports("text-davinci-002")
+
+    # the following model contains "ada" in the name, but it's not from OpenAI
+    assert not layer.supports("ybelkada/mpt-7b-bf16-sharded")