Ë
    uPêj7  ã                   óè   — d dl mZmZmZ d dlmZmZ d dl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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Zd dlm Z! e!Z"neZ" G d„ de«      Z#y)é    )ÚTYPE_CHECKINGÚAnyÚFinal)ÚHeadersÚResponseN)Ú,drop_tool_reference_parts_from_tool_messagesÚhoist_images_from_tool_messages)Ú convert_to_azure_openai_messages)ÚBaseLLMException)ÚGPT_REASONING_SERIES_MARKERS)Ú+API_VERSION_MONTH_SUPPORTED_RESPONSE_FORMATÚ*API_VERSION_YEAR_SUPPORTED_RESPONSE_FORMAT)ÚModelResponseé   )ÚUnsupportedParamsError)ÚAllMessageValuesé   )Ú
BaseConfigé   ©ÚAzureOpenAIError)ÚLoggingc                   óJ  ‡ — e Zd ZdZ	 	 	 	 	 	 	 	 	 	 d2dedz  deez  dz  dedz  dedz  dedz  dedz  d	edz  d
eez  dz  dedz  dedz  ddfd„Ze	ˆ fd„«       Z
dedee   fd„Ze	dedefd„«       Zdedefd„Zdededefd„Z	 d3d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	 	 d4ded ed!ed"ed#edee   deded$d%d&edz  d'edz  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z  defd/„Z	 	 d4dededee   deded&edz  d0edz  defd1„Zˆ xZS )5Ú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                 úo/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/llms/azure/chat/gpt_transformation.pyÚ__init__zAzureOpenAIConfig.__init__?   sF   € ô  ›Ÿ™›ˆØ!Ÿ-™-›/ò 	4‰JˆC�Ø�f‹} Ñ!2Ü˜Ÿ™¨¨UÕ3ñ	4ó    c                 ó    •— t         ‰| �  «       S ©N)ÚsuperÚ
get_config)Úclsr,   s    €r0   r6   zAzureOpenAIConfig.get_configQ   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Úprompt_cache_keyÚstore© )r'   r8   s     r0   Úget_supported_openai_paramsz-AzureOpenAIConfig.get_supported_openai_paramsU   s   € ò
ð 	
r2   c                 ó@   ‡— t        ˆfd„t        D «       «      xs d‰v S )ac  Whether Azure rejects the legacy ``max_tokens`` key for this deployment.

        Deliberately wider than ``AzureOpenAIGPT5Config.is_model_gpt_5_model``: the whole gpt-5
        name family needs the rename, including the ``gpt-5-chat*`` models that are excluded from
        the reasoning path by https://github.com/BerriAI/litellm/issues/13781.
        c              3   ó&   •K  — | ]  }|‰v –— Œ
 y ­wr4   rL   )Ú.0Úmarkerr8   s     €r0   ú	<genexpr>zCAzureOpenAIConfig.requires_max_completion_tokens.<locals>.<genexpr>   s   øè ø€ ÒN v�6˜U”?ÑNùs   ƒÚgpt5_series)Úanyr   )r7   r8   s    `r0   Úrequires_max_completion_tokensz0AzureOpenAIConfig.requires_max_completion_tokensw   s#   ø€ ô ÓNÔ1MÔNÓNÒhÐR_ÐchÐRhÐhr2   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'   r8   rW   Únormalized_models       r0   Ú#_is_response_format_supported_modelz5AzureOpenAIConfig._is_response_format_supported_model�   s2   € ó 	ð #%§&¡&¨°xÀÓ"GÐàÐ(Ñ(¨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'   r[   r\   Úapi_yearÚ	api_monthÚsupported_yearÚsupported_months          r0   Ú)_is_response_format_supported_api_versionz;AzureOpenAIConfig._is_response_format_supported_api_version”   sU   € ô
 Ð.Ó/ˆÜÐ0Ó1ˆ	Ü #Ô$NÓ OˆÜ!$Ô%PÓ!Qˆð �nÒ$Øà˜Ò&Øð  Ñ/Ð/r2   Únon_default_paramsÚoptional_paramsÚdrop_paramsÚapi_versionc                 ó   — | j                  |«      }| j                  |«      }|j                  d«      }t        |«      dk\  r|d   }	|d   }
|d   }nd }	d }
d }|j	                  «       D �]b  \  }}|dk(  r|r|j                  d|«       Œ!|dk(  rš	 |	�|
�|€||d<   Œ3|	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Œjt        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(  rVt        |t        «      rF| j                  |«      }|	�|
€d}n| j                  |	|
«      }|xr |}| j                  |||¬«      }�Œ|dk(  r8t        |t        «      r(|j                  dg «       |d   j                  |«       �ŒX||v s�Œ^|||<   �Œe |S )Nú-r   r   é   r   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-completionsrB   )re   r/   Úis_response_format_supportedr=   )rM   rU   ÚsplitÚlenr*   Ú
setdefaultÚlitellmrf   r   Ú
isinstanceÚdictrZ   rc   Ú_add_response_format_to_toolsÚlistÚextend)r'   rd   re   r8   rf   rg   Úsupported_openai_paramsÚrenames_max_tokensÚapi_version_timesr[   r\   Úapi_version_dayÚparamr/   rZ   Ú(is_response_format_supported_api_versionrs   s                    r0   Úmap_openai_paramsz#AzureOpenAIConfig.map_openai_params¨   s�  € ð *.×)IÑ)IÈ%Ó)PÐØ$(×$GÑ$GÈÓ$NÐØ#.×#4Ñ#4°SÓ#9ÐäÐ Ó! QÒ&Ø0°Ñ3ÐØ 1°!Ñ 4ÐØ/°Ñ2‰Oà#ÐØ $ÐØ"ˆOà.×4Ñ4Ó6ó ;	/‰LˆE�5Ø˜Ò$Ñ);Ø×*Ñ*Ð+BÀEÕJØ˜-Ò'ðð $Ð+Ð/@Ð/HÈOÐLcØ5:�O MÒ2ð )¨6Ò1Ø,°Ò6Ð;LÈtÒ;SØ,°Ò6Ð;LÐPTÒ;TÐYhÐkoÒYoä"×.Ñ.°$Ñ6¸;Ð;RÐWbÐfjÑWjØ ä"8Ø,/Ø,bÐcnÐboð  pYð  )\ô#ð ð  *Ò,Ø(¨FÒ2Ð7HÈDÒ7Pä"×.Ñ.°$Ñ6¸;Ð;RÐWbÐfjÑWjØ ä"8Ø,/Ø*BÀ5À'ÈÐQVÐPWÐWoÐp{Ðo|ð  }Hð  )Iô#ð ð
 :?˜¨Ò6ØÐ+Ò+´
¸5Ä$Ô0GØ6:×6^Ñ6^Ð_dÓ6eÐ3à#Ð+Ð/@Ð/HØ?CÑ<à?C×?mÑ?mØ(Ð*;ó@Ð<ð =ÒdÐAdð -ð #'×"DÑ"DØ$3ØØ1Mð #Eó #’ð
 ˜'Ò!¤j°¼Ô&=Ø×*Ñ*¨7°BÔ7Ø Ñ(×/Ñ/°Ö6ØÐ1Ó1Ø).� Ó&ðw;	/ðx Ðr2   ÚmessagesÚlitellm_paramsÚheadersc                 óN   — t        |«      }t        t        |«      «      }||dœ|¥S )N)r8   r„   )r   r
   r	   )r'   r8   r„   re   r…   r†   Ústripped_messagesÚazure_messagess           r0   Útransform_requestz#AzureOpenAIConfig.transform_requestû   s<   € ô $PÐPXÓ#YÐÜ @ÔA`ÐarÓAsÓ tˆàØ&ñ
ð ð
ð 	
r2   Úraw_responseÚmodel_responseÚlogging_objÚrequest_dataÚencodingztiktoken.Encoding | NoneÚ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'   r8   r‹   rŒ   r�   rŽ   r„   re   r…   r�   r�   r‘   s               r0   Útransform_responsez$AzureOpenAIConfig.transform_response  s   € ô "Øló
ð 	
r2   c                 ó
   — ddiS ©NÚtokenÚazure_ad_tokenrL   ©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'   rd   re   r�   r/   s        r0   Úmap_special_auth_paramsz)AzureOpenAIConfig.map_special_auth_params   s9   € Ø.×4Ñ4Ó6ò 	:‰LˆE�5Ø˜ÓØ49�Ð 0Ò1ð	:ð Ð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ÚukrL   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Úwestus4rL   rš   s    r0   Úget_us_regionsz AzureOpenAIConfig.get_us_regions,  s   € ò
ð 	
r2   Úerror_messagern   c                 ó   — t        |||¬«      S )N)ro   rn   r†   r   )r'   r²   rn   r†   s       r0   Úget_error_classz!AzureOpenAIConfig.get_error_class=  s   € Ü¨À;ÐX_Ô`Ð`r2   Úapi_basec                 ó   — t        d«      ‚)NzTAzure OpenAI has custom logic for validating environment, as it uses the OpenAI SDK.r“   )r'   r†   r8   r„   re   r…   r�   rµ   s           r0   Úvalidate_environmentz&AzureOpenAIConfig.validate_environment@  s   € ô "Øbó
ð 	
r2   )
NNNNNNNNNN)Ú )NN) Ú__name__Ú
__module__Ú__qualname__Ú__doc__r^   Ústrry   r{   r1   Úclassmethodr6   rM   ÚboolrU   rZ   rc   rƒ   r   rŠ   r   r   ÚLoggingClassr•   r›   r�   r¥   r±   r   r   r´   r·   Ú__classcell__)r,   s   @r0   r   r   $   sE  ø„ ñð8 )-Ø+/Ø!%Ø"&Ø!%ØØ'+Ø"&Ø"&Ø ñ4à ™:ð4ð ˜T‘z DÑ(ð4ð ˜$‘;ð	4ð
 ˜4‘Kð4ð ˜$‘Jð4ð �‰:ð4ð  ™*ð4ð �D‰j˜4Ñð4ð ˜4‘Zð4ð �T‰zð4ð 
ó4ð$ ó$ó ð$ð 
°ð  
¸¸c¹ó  
ðD ði°3ð i¸4ò ió ðið¸ð Àó ð&0È#ð 0Ðbeð 0Ðjnó 0ð4 ñQà ðQð ðQð ð	Qð
 ðQð ðQð 
óQðf
àð
ð Ð'Ñ(ð
ð ð	
ð
 ð
ð ð
ð 
ó
ð6 #Ø!%ñ
àð
ð ð
ð &ð	
ð
 "ð
ð ð
ð Ð'Ñ(ð
ð ð
ð ð
ð -ð
ð �t‘ð
ð ˜$‘;ð
ð 
ó
ð$+°ó +ð¸$ð ÐQUó ðC  S¡	ó Cð
  S¡	ó 
ð"a¨Sð a¸sð aÈTÐT[É^ð aÐ`pó að #Ø#ñ
àð
ð ð
ð Ð'Ñ(ð	
ð
 ð
ð ð
ð �t‘ð
ð ˜‘*ð
ð 
÷
r2   r   )$Útypingr   r   r   Úhttpx._modelsr   r   rw   Ú8litellm.litellm_core_utils.prompt_templates.common_utilsr   r	   Ú3litellm.litellm_core_utils.prompt_templates.factoryr
   Ú)litellm.llms.base_llm.chat.transformationr   Ú-litellm.llms.openai.chat.gpt_5_transformationr   Úlitellm.types.llms.azurer   r   Úlitellm.types.utilsr   Ú
exceptionsr   Útypes.llms.openair   Úbase_llm.chat.transformationr   Úcommon_utilsr   ÚtiktokenÚ*litellm.litellm_core_utils.litellm_loggingr   ÚLiteLLMLoggingObjrÀ   r   rL   r2   r0   ú<module>rÑ      s[   ðß ,Ñ ,ç +ã ÷õõ GÝ V÷õ .å 1Ý 2Ý 6Ý +áÛåWà$�Là€Lôh
˜
õ h
r2   