
    h1                         d dl mZmZmZmZmZ d dlmZmZ d dl	Z	d dl
mZ d dlmZ d dlmZmZ d dlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ er	d dlmZ eZneZ G d de      Zy)    )TYPE_CHECKINGAnyListOptionalUnion)HeadersResponseN convert_to_azure_openai_messages)BaseLLMException)+API_VERSION_MONTH_SUPPORTED_RESPONSE_FORMAT*API_VERSION_YEAR_SUPPORTED_RESPONSE_FORMAT)ModelResponse   )UnsupportedParamsError)AllMessageValues   )
BaseConfig   AzureOpenAIError)Loggingc                   <    e Zd ZdZ	 	 	 	 	 	 	 	 	 	 d0dee   deeeef      dee	   dee   dee   dee   d	ee   d
eeee	f      dee   dee   ddfdZ
e fd       Zdedee   fdZdedefdZdededefdZ	 d1dedededededefdZdedee   dedededefdZ	 	 d2deded ed!ed"edee   deded#ed$ee   d%ee   defd&Zdefd'Zdedefd(Zdee   fd)Zdee   fd*Zd+ed,edeeef   defd-Z 	 	 d2dededee   deded$ee   d.ee   defd/Z! xZ"S )3AzureOpenAIConfigaR  
    Reference: https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#chat-completions

    The class `AzureOpenAIConfig` provides configuration for the OpenAI's Chat API interface, for use with Azure. Below are the parameters::

    - `frequency_penalty` (number or null): Defaults to 0. Allows a value between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, thereby minimizing repetition.

    - `function_call` (string or object): This optional parameter controls how the model calls functions.

    - `functions` (array): An optional parameter. It is a list of functions for which the model may generate JSON inputs.

    - `logit_bias` (map): This optional parameter modifies the likelihood of specified tokens appearing in the completion.

    - `max_tokens` (integer or null): This optional parameter helps to set the maximum number of tokens to generate in the chat completion.

    - `n` (integer or null): This optional parameter helps to set how many chat completion choices to generate for each input message.

    - `presence_penalty` (number or null): Defaults to 0. It penalizes new tokens based on if they appear in the text so far, hence increasing the model's likelihood to talk about new topics.

    - `stop` (string / array / null): Specifies up to 4 sequences where the API will stop generating further tokens.

    - `temperature` (number or null): Defines the sampling temperature to use, varying between 0 and 2.

    - `top_p` (number or null): An alternative to sampling with temperature, used for nucleus sampling.
    Nfrequency_penaltyfunction_call	functions
logit_bias
max_tokensnpresence_penaltystoptemperaturetop_preturnc                     t               j                         }|j                         D ]%  \  }}|dk7  s|t        | j                  ||       ' y )Nself)localscopyitemssetattr	__class__)r'   r   r   r   r   r   r    r!   r"   r#   r$   locals_keyvalues                 f/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/llms/azure/chat/gpt_transformation.py__init__zAzureOpenAIConfig.__init__8   sA     (--/!--/JCf}!2U3 *    c                      t         |          S )N)super
get_config)clsr,   s    r0   r5   zAzureOpenAIConfig.get_configJ   s    w!##r2   modelc                 
    g dS )N)r#   r    streamstream_optionsr"   r   max_completion_tokenstoolstool_choicer!   r   r   userr   r   r<   r=   r$   logprobstop_logprobsresponse_formatseedextra_headersparallel_tool_calls
prediction
modalitiesaudioweb_search_options )r'   r7   s     r0   get_supported_openai_paramsz-AzureOpenAIConfig.get_supported_openai_paramsN   s    
 	
r2   c                 D    ddl }|j                  dd|      }d|v sd|v ryy)	aj  
        Determines if the model supports response_format.
        - Handles Azure deployment names (e.g., azure/gpt-4.1-suffix)
        - Normalizes model names (e.g., gpt-4-1 -> gpt-4.1)
        - Strips deployment-specific suffixes
        - Passes provider to supports_response_schema
        - Backwards compatible with previous model name patterns
        r   Nz	(\d)-(\d)z\1.\2zgpt-3.5zgpt-35FT)resub)r'   r7   rL   normalized_models       r0   #_is_response_format_supported_modelz5AzureOpenAIConfig._is_response_format_supported_modeln   s2     	 66,%@((H,=r2   api_version_yearapi_version_monthc                     t        |      }t        |      }t        t              }t        t              }||kD  ry||k  ry||k\  S )z
        - check if api_version is supported for response_format
        - returns True if the API version is equal to or newer than the supported version
        TF)intr   r   )r'   rP   rQ   api_year	api_monthsupported_yearsupported_months          r0   )_is_response_format_supported_api_versionz;AzureOpenAIConfig._is_response_format_supported_api_version   sU     '()*	GHIJ n$& //r2   non_default_paramsoptional_paramsdrop_paramsapi_versionc                    | j                  |      }|j                  d      }|d   }|d   }	|d   }
|j                         D ]4  \  }}|dk(  r	 |dk  s|dk(  r|	dk  s|dk(  r4|	dk(  r/|
dk  r*t        j                  d	u s||d	u rDt        d
d| d      |dk(  r:|dk(  r5|	dk  r0t        j                  d	u s||d	u r}t        d
d| d| d| d      ||d<   |dk(  rNt        |t              r>| j                  |      }| j                  ||	      }|xr |}| j                  |||      }|dk(  r8t        |t              r(|j                  dg        |d   j                  |       *||v s0|||<   7 |S )N-r      r   r=   20231201Ti  z6Azure does not support 'tool_choice', for api_version=z. Bump your API version to '2023-12-01-preview' or later. This parameter requires 'api_version="2023-12-01-preview"' or later. Azure API Reference: https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#chat-completions)status_codemessagerequired202405zAzure does not support 'z' as a z param, for api_version=a  . To drop 'tool_choice=required' for calls with this Azure API version, set `litellm.drop_params=True` or for proxy:

`litellm_settings:
 drop_params: true`
Azure API Reference: https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#chat-completionsrA   )rZ   r/   is_response_format_supportedr<   )rJ   splitr*   litellmr[   r   
isinstancedictrO   rX   _add_response_format_to_toolslist
setdefaultextend)r'   rY   rZ   r7   r[   r\   supported_openai_paramsapi_version_timesrP   rQ   api_version_dayparamr/   rO   (is_response_format_supported_api_versionrh   s                   r0   map_openai_paramsz#AzureOpenAIConfig.map_openai_params   s    #'"B"B5"I'--c2,Q/-a0+A..446LE5% %v-(F27H47O(F2-5+d2 **d2#/K44G4(+(^_j^k  lU  %X  j($.3D3L**d2#/K44G4(+&>ugWUGSklwkx  yD  %E 
 6;OM2++
5$0G<<UC 4
 BB(*; 9 = <; -
 #'"D"D$31M #E #
 '!j&=**7B7(//611).&A 7D r2   messageslitellm_paramsheadersc                 &    t        |      }||d|S )N)r7   rw   r
   )r'   r7   rw   rZ   rx   ry   s         r0   transform_requestz#AzureOpenAIConfig.transform_request   s(     4H= 
 
 	
r2   raw_responsemodel_responselogging_objrequest_dataencodingapi_key	json_modec                     t        d      )Nz^Azure OpenAI handler.py has custom logic for transforming response, as it uses the OpenAI SDK.NotImplementedError)r'   r7   r|   r}   r~   r   rw   rZ   rx   r   r   r   s               r0   transform_responsez$AzureOpenAIConfig.transform_response   s     "l
 	
r2   c                 
    ddiS Ntokenazure_ad_tokenrI   r'   s    r0   get_mapped_special_auth_paramsz0AzureOpenAIConfig.get_mapped_special_auth_params
  s    )**r2   c                 L    |j                         D ]  \  }}|dk(  s||d<    |S r   )r*   )r'   rY   rZ   rt   r/   s        r0   map_special_auth_paramsz)AzureOpenAIConfig.map_special_auth_params  s4    .446LE549 01 7 r2   c                 
    g dS )
        Source: https://learn.microsoft.com/en-us/azure/ai-services/openai/concepts/models#gpt-4-and-gpt-4-turbo-model-availability
        )europeswedenswitzerlandfranceukrI   r   s    r0   get_eu_regionsz AzureOpenAIConfig.get_eu_regions  s     CBr2   c                 
    g dS )r   )
useastuseastus2eastus2euapeastus3southcentraluswestuswestus2westus3westus4rI   r   s    r0   get_us_regionsz AzureOpenAIConfig.get_us_regions  s    
 	
r2   error_messagerc   c                     t        |||      S )N)rd   rc   ry   r   )r'   r   rc   ry   s       r0   get_error_classz!AzureOpenAIConfig.get_error_class*  s      !{G
 	
r2   api_basec                     t        d      )NzTAzure OpenAI has custom logic for validating environment, as it uses the OpenAI SDK.r   )r'   ry   r7   rw   rZ   rx   r   r   s           r0   validate_environmentz&AzureOpenAIConfig.validate_environment1  s     "b
 	
r2   )
NNNNNNNNNN) )NN)#__name__
__module____qualname____doc__r   rS   r   strrl   rn   r1   classmethodr5   r   rJ   boolrO   rX   rv   r   r{   r	   r   LoggingClassr   r   r   r   r   r   r   r   r   r   __classcell__)r,   s   @r0   r   r      s,   8 ,048$(%)$(*.+/%)#4#C=4  c4i 014 D>	4
 TN4 SM4 C=4 #3-4 uS$Y'(4 c]4 }4 
4$ $ $
 
c 
@  &0 #08;0	08 P P P 	P
 P P 
Pd

 '(
 	

 
 
 

4 "&$(

 
 &	

 "
 
 '(
 
 
 
 #
 D>
 

$+ +$ QU CS	 C
S	 
"
 
/2
=B4==Q
	
 "&"&

 
 '(	

 
 
 #
 3-
 

r2   r   ) typingr   r   r   r   r   httpx._modelsr   r	   rj   3litellm.litellm_core_utils.prompt_templates.factoryr   )litellm.llms.base_llm.chat.transformationr   litellm.types.llms.azurer   r   litellm.types.utilsr   
exceptionsr   types.llms.openair   base_llm.chat.transformationr   common_utilsr   *litellm.litellm_core_utils.litellm_loggingr   LiteLLMLoggingObjr   r   rI   r2   r0   <module>r      sP    < < +  G . 1 2 6 +W$LL`

 `
r2   