Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add default_headers for azure embeddings #8535

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions haystack/components/embedders/azure_document_embedder.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def __init__( # noqa: PLR0913 (too-many-arguments)
embedding_separator: str = "\n",
timeout: Optional[float] = None,
max_retries: Optional[int] = None,
default_headers: Optional[Dict[str, str]] = None,
):
"""
Creates an AzureOpenAIDocumentEmbedder component.
Expand Down Expand Up @@ -95,6 +96,7 @@ def __init__( # noqa: PLR0913 (too-many-arguments)
`OPENAI_TIMEOUT` environment variable, or 30 seconds.
:param max_retries: Maximum number of retries to contact AzureOpenAI after an internal error.
If not set, defaults to either the `OPENAI_MAX_RETRIES` environment variable or to 5 retries.
:param default_headers: Default headers to use for the AzureOpenAI client.
"""
# if not provided as a parameter, azure_endpoint is read from the env var AZURE_OPENAI_ENDPOINT
azure_endpoint = azure_endpoint or os.environ.get("AZURE_OPENAI_ENDPOINT")
Expand All @@ -119,6 +121,7 @@ def __init__( # noqa: PLR0913 (too-many-arguments)
self.embedding_separator = embedding_separator
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.default_headers = default_headers or {}

self._client = AzureOpenAI(
api_version=api_version,
Expand All @@ -129,6 +132,7 @@ def __init__( # noqa: PLR0913 (too-many-arguments)
organization=organization,
timeout=self.timeout,
max_retries=self.max_retries,
default_headers=self.default_headers,
)

def _get_telemetry_data(self) -> Dict[str, Any]:
Expand Down Expand Up @@ -161,6 +165,7 @@ def to_dict(self) -> Dict[str, Any]:
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,
default_headers=self.default_headers,
)

@classmethod
Expand Down
5 changes: 5 additions & 0 deletions haystack/components/embedders/azure_text_embedder.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def __init__(
max_retries: Optional[int] = None,
prefix: str = "",
suffix: str = "",
default_headers: Optional[Dict[str, str]] = None,
):
"""
Creates an AzureOpenAITextEmbedder component.
Expand Down Expand Up @@ -82,6 +83,7 @@ def __init__(
A string to add at the beginning of each text.
:param suffix:
A string to add at the end of each text.
:param default_headers: Default headers to use for the AzureOpenAI client.
"""
# Why is this here?
# AzureOpenAI init is forcing us to use an init method that takes either base_url or azure_endpoint as not
Expand All @@ -105,6 +107,7 @@ def __init__(
self.max_retries = max_retries or int(os.environ.get("OPENAI_MAX_RETRIES", 5))
self.prefix = prefix
self.suffix = suffix
self.default_headers = default_headers or {}

self._client = AzureOpenAI(
api_version=api_version,
Expand All @@ -115,6 +118,7 @@ def __init__(
organization=organization,
timeout=self.timeout,
max_retries=self.max_retries,
default_headers=self.default_headers,
)

def _get_telemetry_data(self) -> Dict[str, Any]:
Expand Down Expand Up @@ -143,6 +147,7 @@ def to_dict(self) -> Dict[str, Any]:
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,
default_headers=self.default_headers,
)

@classmethod
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
enhancements:
- |
Adds `default_headers` parameter to `AzureOpenAIDocumentEmbedder` and `AzureOpenAITextEmbedder`
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def test_to_dict(self, monkeypatch):
"embedding_separator": "\n",
"max_retries": 5,
"timeout": 30.0,
"default_headers": {},
},
}

Expand Down
1 change: 1 addition & 0 deletions test/components/embedders/test_azure_text_embedder.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def test_to_dict(self, monkeypatch):
"timeout": 30.0,
"prefix": "",
"suffix": "",
"default_headers": {},
},
}

Expand Down