• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

deepset-ai / haystack / 18592817487

17 Oct 2025 12:33PM UTC coverage: 92.2% (+0.1%) from 92.062%
18592817487

Pull #9859

github

web-flow
Merge f20ff2b98 into a43c47b63
Pull Request #9859: feat: Add FallbackChatGenerator

13346 of 14475 relevant lines covered (92.2%)

0.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.84
haystack/components/generators/azure.py
1
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
2
#
3
# SPDX-License-Identifier: Apache-2.0
4

5
import os
1✔
6
from typing import Any, Optional
1✔
7

8
from openai.lib.azure import AzureADTokenProvider, AzureOpenAI
1✔
9

10
from haystack import component, default_from_dict, default_to_dict
1✔
11
from haystack.components.generators import OpenAIGenerator
1✔
12
from haystack.dataclasses import StreamingCallbackT
1✔
13
from haystack.utils import Secret, deserialize_callable, deserialize_secrets_inplace, serialize_callable
1✔
14
from haystack.utils.http_client import init_http_client
1✔
15

16

17
@component
1✔
18
class AzureOpenAIGenerator(OpenAIGenerator):
1✔
19
    """
20
    Generates text using OpenAI's large language models (LLMs).
21

22
    It works with the gpt-4 - type models and supports streaming responses
23
    from OpenAI API.
24

25
    You can customize how the text is generated by passing parameters to the
26
    OpenAI API. Use the `**generation_kwargs` argument when you initialize
27
    the component or when you run it. Any parameter that works with
28
    `openai.ChatCompletion.create` will work here too.
29

30

31
    For details on OpenAI API parameters, see
32
    [OpenAI documentation](https://platform.openai.com/docs/api-reference/chat).
33

34

35
    ### Usage example
36

37
    ```python
38
    from haystack.components.generators import AzureOpenAIGenerator
39
    from haystack.utils import Secret
40
    client = AzureOpenAIGenerator(
41
        azure_endpoint="<Your Azure endpoint e.g. `https://your-company.azure.openai.com/>",
42
        api_key=Secret.from_token("<your-api-key>"),
43
        azure_deployment="<this a model name, e.g.  gpt-4o-mini>")
44
    response = client.run("What's Natural Language Processing? Be brief.")
45
    print(response)
46
    ```
47

48
    ```
49
    >> {'replies': ['Natural Language Processing (NLP) is a branch of artificial intelligence that focuses on
50
    >> the interaction between computers and human language. It involves enabling computers to understand, interpret,
51
    >> and respond to natural human language in a way that is both meaningful and useful.'], 'meta': [{'model':
52
    >> 'gpt-4o-mini', 'index': 0, 'finish_reason': 'stop', 'usage': {'prompt_tokens': 16,
53
    >> 'completion_tokens': 49, 'total_tokens': 65}}]}
54
    ```
55
    """
56

57
    # pylint: disable=super-init-not-called
58
    def __init__(  # pylint: disable=too-many-positional-arguments  # noqa: PLR0913
1✔
59
        self,
60
        azure_endpoint: Optional[str] = None,
61
        api_version: Optional[str] = "2023-05-15",
62
        azure_deployment: Optional[str] = "gpt-4o-mini",
63
        api_key: Optional[Secret] = Secret.from_env_var("AZURE_OPENAI_API_KEY", strict=False),
64
        azure_ad_token: Optional[Secret] = Secret.from_env_var("AZURE_OPENAI_AD_TOKEN", strict=False),
65
        organization: Optional[str] = None,
66
        streaming_callback: Optional[StreamingCallbackT] = None,
67
        system_prompt: Optional[str] = None,
68
        timeout: Optional[float] = None,
69
        max_retries: Optional[int] = None,
70
        http_client_kwargs: Optional[dict[str, Any]] = None,
71
        generation_kwargs: Optional[dict[str, Any]] = None,
72
        default_headers: Optional[dict[str, str]] = None,
73
        *,
74
        azure_ad_token_provider: Optional[AzureADTokenProvider] = None,
75
    ):
76
        """
77
        Initialize the Azure OpenAI Generator.
78

79
        :param azure_endpoint: The endpoint of the deployed model, for example `https://example-resource.azure.openai.com/`.
80
        :param api_version: The version of the API to use. Defaults to 2023-05-15.
81
        :param azure_deployment: The deployment of the model, usually the model name.
82
        :param api_key: The API key to use for authentication.
83
        :param azure_ad_token: [Azure Active Directory token](https://www.microsoft.com/en-us/security/business/identity-access/microsoft-entra-id).
84
        :param organization: Your organization ID, defaults to `None`. For help, see
85
        [Setting up your organization](https://platform.openai.com/docs/guides/production-best-practices/setting-up-your-organization).
86
        :param streaming_callback: A callback function called when a new token is received from the stream.
87
            It accepts [StreamingChunk](https://docs.haystack.deepset.ai/docs/data-classes#streamingchunk)
88
            as an argument.
89
        :param system_prompt: The system prompt to use for text generation. If not provided, the Generator
90
        omits the system prompt and uses the default system prompt.
91
        :param timeout: Timeout for AzureOpenAI client. If not set, it is inferred from the
92
            `OPENAI_TIMEOUT` environment variable or set to 30.
93
        :param max_retries: Maximum retries to establish contact with AzureOpenAI if it returns an internal error.
94
            If not set, it is inferred from the `OPENAI_MAX_RETRIES` environment variable or set to 5.
95
        :param http_client_kwargs:
96
            A dictionary of keyword arguments to configure a custom `httpx.Client`or `httpx.AsyncClient`.
97
            For more information, see the [HTTPX documentation](https://www.python-httpx.org/api/#client).
98
        :param generation_kwargs: Other parameters to use for the model, sent directly to
99
            the OpenAI endpoint. See [OpenAI documentation](https://platform.openai.com/docs/api-reference/chat) for
100
            more details.
101
            Some of the supported parameters:
102
            - `max_completion_tokens`: An upper bound for the number of tokens that can be generated for a completion,
103
                including visible output tokens and reasoning tokens.
104
            - `temperature`: The sampling temperature to use. Higher values mean the model takes more risks.
105
                Try 0.9 for more creative applications and 0 (argmax sampling) for ones with a well-defined answer.
106
            - `top_p`: An alternative to sampling with temperature, called nucleus sampling, where the model
107
                considers the results of the tokens with top_p probability mass. For example, 0.1 means only the tokens
108
                comprising the top 10% probability mass are considered.
109
            - `n`: The number of completions to generate for each prompt. For example, with 3 prompts and n=2,
110
                the LLM will generate two completions per prompt, resulting in 6 completions total.
111
            - `stop`: One or more sequences after which the LLM should stop generating tokens.
112
            - `presence_penalty`: The penalty applied if a token is already present.
113
                Higher values make the model less likely to repeat the token.
114
            - `frequency_penalty`: Penalty applied if a token has already been generated.
115
                Higher values make the model less likely to repeat the token.
116
            - `logit_bias`: Adds a logit bias to specific tokens. The keys of the dictionary are tokens, and the
117
                values are the bias to add to that token.
118
        :param default_headers: Default headers to use for the AzureOpenAI client.
119
        :param azure_ad_token_provider: A function that returns an Azure Active Directory token, will be invoked on
120
            every request.
121
        """
122
        # We intentionally do not call super().__init__ here because we only need to instantiate the client to interact
123
        # with the API.
124

125
        # Why is this here?
126
        # AzureOpenAI init is forcing us to use an init method that takes either base_url or azure_endpoint as not
127
        # None init parameters. This way we accommodate the use case where env var AZURE_OPENAI_ENDPOINT is set instead
128
        # of passing it as a parameter.
129
        azure_endpoint = azure_endpoint or os.environ.get("AZURE_OPENAI_ENDPOINT")
1✔
130
        if not azure_endpoint:
1✔
131
            raise ValueError("Please provide an Azure endpoint or set the environment variable AZURE_OPENAI_ENDPOINT.")
×
132

133
        if api_key is None and azure_ad_token is None:
1✔
134
            raise ValueError("Please provide an API key or an Azure Active Directory token.")
×
135

136
        # The check above makes mypy incorrectly infer that api_key is never None,
137
        # which propagates the incorrect type.
138
        self.api_key = api_key  # type: ignore
1✔
139
        self.azure_ad_token = azure_ad_token
1✔
140
        self.generation_kwargs = generation_kwargs or {}
1✔
141
        self.system_prompt = system_prompt
1✔
142
        self.streaming_callback = streaming_callback
1✔
143
        self.api_version = api_version
1✔
144
        self.azure_endpoint = azure_endpoint
1✔
145
        self.azure_deployment = azure_deployment
1✔
146
        self.organization = organization
1✔
147
        self.model: str = azure_deployment or "gpt-4o-mini"
1✔
148
        self.timeout = timeout if timeout is not None else float(os.environ.get("OPENAI_TIMEOUT", "30.0"))
1✔
149
        self.max_retries = max_retries if max_retries is not None else int(os.environ.get("OPENAI_MAX_RETRIES", "5"))
1✔
150
        self.http_client_kwargs = http_client_kwargs
1✔
151
        self.default_headers = default_headers or {}
1✔
152
        self.azure_ad_token_provider = azure_ad_token_provider
1✔
153

154
        self.client = AzureOpenAI(
1✔
155
            api_version=api_version,
156
            azure_endpoint=azure_endpoint,
157
            azure_deployment=azure_deployment,
158
            azure_ad_token_provider=azure_ad_token_provider,
159
            api_key=api_key.resolve_value() if api_key is not None else None,
160
            azure_ad_token=azure_ad_token.resolve_value() if azure_ad_token is not None else None,
161
            organization=organization,
162
            timeout=self.timeout,
163
            max_retries=self.max_retries,
164
            http_client=init_http_client(self.http_client_kwargs, async_client=False),
165
            default_headers=self.default_headers,
166
        )
167

168
    def to_dict(self) -> dict[str, Any]:
1✔
169
        """
170
        Serialize this component to a dictionary.
171

172
        :returns:
173
            The serialized component as a dictionary.
174
        """
175
        callback_name = serialize_callable(self.streaming_callback) if self.streaming_callback else None
1✔
176
        azure_ad_token_provider_name = None
1✔
177
        if self.azure_ad_token_provider:
1✔
178
            azure_ad_token_provider_name = serialize_callable(self.azure_ad_token_provider)
1✔
179
        return default_to_dict(
1✔
180
            self,
181
            azure_endpoint=self.azure_endpoint,
182
            azure_deployment=self.azure_deployment,
183
            organization=self.organization,
184
            api_version=self.api_version,
185
            streaming_callback=callback_name,
186
            generation_kwargs=self.generation_kwargs,
187
            system_prompt=self.system_prompt,
188
            api_key=self.api_key.to_dict() if self.api_key is not None else None,
189
            azure_ad_token=self.azure_ad_token.to_dict() if self.azure_ad_token is not None else None,
190
            timeout=self.timeout,
191
            max_retries=self.max_retries,
192
            http_client_kwargs=self.http_client_kwargs,
193
            default_headers=self.default_headers,
194
            azure_ad_token_provider=azure_ad_token_provider_name,
195
        )
196

197
    @classmethod
1✔
198
    def from_dict(cls, data: dict[str, Any]) -> "AzureOpenAIGenerator":
1✔
199
        """
200
        Deserialize this component from a dictionary.
201

202
        :param data:
203
            The dictionary representation of this component.
204
        :returns:
205
            The deserialized component instance.
206
        """
207
        deserialize_secrets_inplace(data["init_parameters"], keys=["api_key", "azure_ad_token"])
1✔
208
        init_params = data.get("init_parameters", {})
1✔
209
        serialized_callback_handler = init_params.get("streaming_callback")
1✔
210
        if serialized_callback_handler:
1✔
211
            data["init_parameters"]["streaming_callback"] = deserialize_callable(serialized_callback_handler)
×
212
        serialized_azure_ad_token_provider = init_params.get("azure_ad_token_provider")
1✔
213
        if serialized_azure_ad_token_provider:
1✔
214
            data["init_parameters"]["azure_ad_token_provider"] = deserialize_callable(
×
215
                serialized_azure_ad_token_provider
216
            )
217
        return default_from_dict(cls, data)
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc