Repository: deepset-ai/haystack

Issue:
Add `max_retries` and `timeout` params to all `AzureOpenAI` classes
**Is your feature request related to a problem? Please describe.**

Currently all `OpenAI` related classes (e.g. `OpenAIDocumentEmbedder`, `OpenAIChatGenerator`) can be initialised by setting `max_retries` and `timeout` params.

The corresponding `AzureOpenAI` don't always have the same params.

**Describe the solution you'd like**

It would be nice to have these params in the `AzureOpenAI` classes

**Describe alternatives you've considered**

Subclass `AzureOpenAI` and create custom components.

**Additional context**

cc @anakin87 :)


Discussion:
Thanks @EdoardoAbatiTR!

I'm also opening this for contributions. In case anyone is interested, let's discuss it here.
Hey, 

I am new here. Can I assign this to myself? 
@nvzard feel free to work on this. Take a look at the [guidelines for contributors](https://github.com/deepset-ai/haystack/blob/main/CONTRIBUTING.md).

I would suggest creating one PR for each component to be changed.
E.g., start with [`AzureOpenAIGenerator`](https://github.com/deepset-ai/haystack/blob/main/haystack/components/generators/azure.py) and use [`OpenAIGenerator`](https://github.com/deepset-ai/haystack/blob/main/haystack/components/generators/openai.py) as an inspiration.
@anakin87 thanks for the references 😄 

I've made the [first PR](https://github.com/deepset-ai/haystack/pull/7983) for `AzureOpenAIGenerator`, let me know if I missed anything. 

Pending: `AzureOpenAIDocumentEmbedder`, `AzureOpenAITextEmbedder`, `AzureOpenAIChatGenerator`
Hi all, thank you for working on this :) 

I think this got closed automatically because there was the "Fixes" keyword the previous PR, but it should stay open until the other classes are also updated
Ok np @EdoardoAbatiTR,  what other classes need to be updated?
@vblagoje `AzureOpenAIDocumentEmbedder, AzureOpenAITextEmbedder, AzureOpenAIChatGenerator` are left. I'll open a separate PR for each.

I already have a PR for `AzureOpenAIChatGenerator` https://github.com/deepset-ai/haystack/pull/7988 and will open one for the rest of the classes as well.
If it's okay for everyone. Can I take a couple of classes?
I'm new to open-source, and would love to contribute.
@kanjikinsmoke I already have started work for the other classes. I think there are other issues available to work on: https://github.com/deepset-ai/haystack/issues?q=is%3Aopen+is%3Aissue+label%3A%22Contributions+wanted%21%22
@kanjikinsmoke we just talked this morning internally about few easy issues that you could potentially work on. I'll ping @julian-risch to get back to you soon

Resolving patch:
diff --git a/haystack/components/embedders/azure_text_embedder.py b/haystack/components/embedders/azure_text_embedder.py
--- a/haystack/components/embedders/azure_text_embedder.py
+++ b/haystack/components/embedders/azure_text_embedder.py
@@ -41,6 +41,8 @@ def __init__(
         api_key: Optional[Secret] = Secret.from_env_var("AZURE_OPENAI_API_KEY", strict=False),
         azure_ad_token: Optional[Secret] = Secret.from_env_var("AZURE_OPENAI_AD_TOKEN", strict=False),
         organization: Optional[str] = None,
+        timeout: Optional[float] = None,
+        max_retries: Optional[int] = None,
         prefix: str = "",
         suffix: str = "",
     ):
@@ -67,6 +69,10 @@ def __init__(
             The Organization ID. See OpenAI's
             [production best practices](https://platform.openai.com/docs/guides/production-best-practices/setting-up-your-organization)
             for more information.
+        :param timeout: The timeout in seconds to be passed to the underlying `AzureOpenAI` client, if not set it is
+            inferred from the `OPENAI_TIMEOUT` environment variable or set to 30.
+        :param max_retries: Maximum retries to establish a connection with AzureOpenAI if it returns an internal error,
+            if not set it is inferred from the `OPENAI_MAX_RETRIES` environment variable or set to 5.
         :param prefix:
             A string to add at the beginning of each text.
         :param suffix:
@@ -90,6 +96,8 @@ def __init__(
         self.azure_deployment = azure_deployment
         self.dimensions = dimensions
         self.organization = organization
+        self.timeout = timeout or float(os.environ.get("OPENAI_TIMEOUT", 30.0))
+        self.max_retries = max_retries or int(os.environ.get("OPENAI_MAX_RETRIES", 5))
         self.prefix = prefix
         self.suffix = suffix
 
@@ -100,6 +108,8 @@ def __init__(
             api_key=api_key.resolve_value() if api_key is not None else None,
             azure_ad_token=azure_ad_token.resolve_value() if azure_ad_token is not None else None,
             organization=organization,
+            timeout=self.timeout,
+            max_retries=self.max_retries,
         )
 
     def _get_telemetry_data(self) -> Dict[str, Any]:
@@ -126,6 +136,8 @@ def to_dict(self) -> Dict[str, Any]:
             suffix=self.suffix,
             api_key=self.api_key.to_dict() if self.api_key is not None else None,
             azure_ad_token=self.azure_ad_token.to_dict() if self.azure_ad_token is not None else None,
+            timeout=self.timeout,
+            max_retries=self.max_retries,
         )
 
     @classmethod


Test patch:
diff --git a/test/components/embedders/test_azure_text_embedder.py b/test/components/embedders/test_azure_text_embedder.py
--- a/test/components/embedders/test_azure_text_embedder.py
+++ b/test/components/embedders/test_azure_text_embedder.py
@@ -34,6 +34,8 @@ def test_to_dict(self, monkeypatch):
                 "organization": None,
                 "azure_endpoint": "https://example-resource.azure.openai.com/",
                 "api_version": "2023-05-15",
+                "max_retries": 5,
+                "timeout": 30.0,
                 "prefix": "",
                 "suffix": "",
             },