1313# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
1414import copy
1515import os
16+ import warnings
1617from typing import Any , Callable , Dict , List , Optional , Type , Union
1718
1819from openai import AsyncAzureOpenAI , AsyncStream , AzureOpenAI , Stream
@@ -60,7 +61,8 @@ class AzureOpenAIModel(BaseModelBackend):
6061
6162 Args:
6263 model_type (Union[ModelType, str]): Model for which a backend is
63- created, one of GPT_* series.
64+ created, Should be the deployment name you chose when you deployed
65+ an azure model.
6466 model_config_dict (Optional[Dict[str, Any]], optional): A dictionary
6567 that will be fed into:obj:`openai.ChatCompletion.create()`. If
6668 :obj:`None`, :obj:`ChatGPTConfig().as_dict()` will be used.
@@ -71,8 +73,6 @@ class AzureOpenAIModel(BaseModelBackend):
7173 (default: :obj:`None`)
7274 api_version (Optional[str], optional): The api version for the model.
7375 (default: :obj:`None`)
74- azure_deployment_name (Optional[str], optional): The deployment name
75- you chose when you deployed an azure model. (default: :obj:`None`)
7676 azure_ad_token (Optional[str], optional): Your Azure Active Directory
7777 token, https://www.microsoft.com/en-us/security/business/
7878 identity-access/microsoft-entra-id. (default: :obj:`None`)
@@ -99,6 +99,10 @@ class AzureOpenAIModel(BaseModelBackend):
9999 AzureOpenAI client instance. If provided, this client will be
100100 used instead of creating a new one. The client should implement
101101 the AsyncAzureOpenAI client interface. (default: :obj:`None`)
102+ azure_deployment_name (Optional[str], optional): **Deprecated**.
103+ Use `model_type` parameter instead. This parameter is kept for
104+ backward compatibility and will be removed in a future version.
105+ (default: :obj:`None`)
102106 **kwargs (Any): Additional arguments to pass to the client
103107 initialization. Ignored if custom clients are provided.
104108
@@ -115,14 +119,35 @@ def __init__(
115119 timeout : Optional [float ] = None ,
116120 token_counter : Optional [BaseTokenCounter ] = None ,
117121 api_version : Optional [str ] = None ,
118- azure_deployment_name : Optional [str ] = None ,
119122 azure_ad_token_provider : Optional ["AzureADTokenProvider" ] = None ,
120123 azure_ad_token : Optional [str ] = None ,
121124 max_retries : int = 3 ,
122125 client : Optional [Any ] = None ,
123126 async_client : Optional [Any ] = None ,
127+ azure_deployment_name : Optional [str ] = None ,
124128 ** kwargs : Any ,
125129 ) -> None :
130+ # Handle deprecated azure_deployment_name parameter
131+ if azure_deployment_name is not None :
132+ warnings .warn (
133+ "The 'azure_deployment_name' parameter is deprecated. "
134+ "Please use 'model_type' parameter instead. "
135+ "The 'azure_deployment_name' parameter is being ignored." ,
136+ DeprecationWarning ,
137+ stacklevel = 2 ,
138+ )
139+
140+ # Handle deprecated AZURE_DEPLOYMENT_NAME environment variable
141+ if os .environ .get ("AZURE_DEPLOYMENT_NAME" ) is not None :
142+ warnings .warn (
143+ "The 'AZURE_DEPLOYMENT_NAME' environment variable is "
144+ "deprecated. Please use the 'model_type' parameter "
145+ "instead. The 'AZURE_DEPLOYMENT_NAME' environment "
146+ "variable is being ignored." ,
147+ DeprecationWarning ,
148+ stacklevel = 2 ,
149+ )
150+
126151 if model_config_dict is None :
127152 model_config_dict = ChatGPTConfig ().as_dict ()
128153 api_key = api_key or os .environ .get ("AZURE_OPENAI_API_KEY" )
@@ -133,9 +158,6 @@ def __init__(
133158 )
134159
135160 self .api_version = api_version or os .environ .get ("AZURE_API_VERSION" )
136- self ._azure_deployment_name = azure_deployment_name or os .environ .get (
137- "AZURE_DEPLOYMENT_NAME"
138- )
139161 self ._azure_ad_token = azure_ad_token or os .environ .get (
140162 "AZURE_AD_TOKEN"
141163 )
@@ -145,11 +167,6 @@ def __init__(
145167 "Must provide either the `api_version` argument "
146168 "or `AZURE_API_VERSION` environment variable."
147169 )
148- if self ._azure_deployment_name is None :
149- raise ValueError (
150- "Must provide either the `azure_deployment_name` argument "
151- "or `AZURE_DEPLOYMENT_NAME` environment variable."
152- )
153170
154171 # Use custom clients if provided, otherwise create new ones
155172 if client is not None :
@@ -162,7 +179,7 @@ def __init__(
162179
163180 self ._client = LangfuseOpenAI (
164181 azure_endpoint = str (self ._url ),
165- azure_deployment = self ._azure_deployment_name ,
182+ azure_deployment = str ( self .model_type ) ,
166183 api_version = self .api_version ,
167184 api_key = self ._api_key ,
168185 azure_ad_token = self ._azure_ad_token ,
@@ -174,7 +191,7 @@ def __init__(
174191 else :
175192 self ._client = AzureOpenAI (
176193 azure_endpoint = str (self ._url ),
177- azure_deployment = self ._azure_deployment_name ,
194+ azure_deployment = str ( self .model_type ) ,
178195 api_version = self .api_version ,
179196 api_key = self ._api_key ,
180197 azure_ad_token = self ._azure_ad_token ,
@@ -196,7 +213,7 @@ def __init__(
196213
197214 self ._async_client = LangfuseAsyncOpenAI (
198215 azure_endpoint = str (self ._url ),
199- azure_deployment = self ._azure_deployment_name ,
216+ azure_deployment = str ( self .model_type ) ,
200217 api_version = self .api_version ,
201218 api_key = self ._api_key ,
202219 azure_ad_token = self ._azure_ad_token ,
@@ -208,7 +225,7 @@ def __init__(
208225 else :
209226 self ._async_client = AsyncAzureOpenAI (
210227 azure_endpoint = str (self ._url ),
211- azure_deployment = self ._azure_deployment_name ,
228+ azure_deployment = str ( self .model_type ) ,
212229 api_version = self .api_version ,
213230 api_key = self ._api_key ,
214231 azure_ad_token = self ._azure_ad_token ,
@@ -359,7 +376,7 @@ def _request_chat_completion(
359376
360377 return self ._client .chat .completions .create (
361378 messages = messages ,
362- model = self ._azure_deployment_name , # type:ignore[arg-type]
379+ model = str ( self .model_type ),
363380 ** request_config ,
364381 )
365382
@@ -375,7 +392,7 @@ async def _arequest_chat_completion(
375392
376393 return await self ._async_client .chat .completions .create (
377394 messages = messages ,
378- model = self ._azure_deployment_name , # type:ignore[arg-type]
395+ model = str ( self .model_type ),
379396 ** request_config ,
380397 )
381398
@@ -396,7 +413,7 @@ def _request_parse(
396413
397414 return self ._client .beta .chat .completions .parse (
398415 messages = messages ,
399- model = self ._azure_deployment_name , # type:ignore[arg-type]
416+ model = str ( self .model_type ),
400417 ** request_config ,
401418 )
402419
@@ -417,7 +434,7 @@ async def _arequest_parse(
417434
418435 return await self ._async_client .beta .chat .completions .parse (
419436 messages = messages ,
420- model = self ._azure_deployment_name , # type:ignore[arg-type]
437+ model = str ( self .model_type ),
421438 ** request_config ,
422439 )
423440
@@ -443,7 +460,7 @@ def _request_stream_parse(
443460 # Use the beta streaming API for structured outputs
444461 return self ._client .beta .chat .completions .stream (
445462 messages = messages ,
446- model = self .model_type ,
463+ model = str ( self .model_type ) ,
447464 response_format = response_format ,
448465 ** request_config ,
449466 )
@@ -470,7 +487,7 @@ async def _arequest_stream_parse(
470487 # Use the beta streaming API for structured outputs
471488 return self ._async_client .beta .chat .completions .stream (
472489 messages = messages ,
473- model = self .model_type ,
490+ model = str ( self .model_type ) ,
474491 response_format = response_format ,
475492 ** request_config ,
476493 )
0 commit comments