
    g.                         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 ddlmZ dd	lmZmZmZmZ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)ModelResponse   )UnsupportedParamsError)AllMessageValues%ChatCompletionToolChoiceFunctionParam#ChatCompletionToolChoiceObjectParamChatCompletionToolParam$ChatCompletionToolParamFunctionChunk   )
BaseConfig   AzureOpenAIError)Loggingc                       e Zd ZdZ	 	 	 	 	 	 	 	 	 	 d,d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-d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	 	 d.d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	 	 d.dededee   ded ee   d*ee   defd+Z xZ S )/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                 _/var/www/openai/venv/lib/python3.12/site-packages/litellm/llms/azure/chat/gpt_transformation.py__init__zAzureOpenAIConfig.__init__:   sA     (--/!--/JCf}!2U3 *    c                      t         |          S )N)super
get_config)clsr.   s    r2   r7   zAzureOpenAIConfig.get_configL   s    w!##r4   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 )r)   r9   s     r2   get_supported_openai_paramsz-AzureOpenAIConfig.get_supported_openai_paramsP   s    
 	
r4   non_default_paramsoptional_paramsdrop_paramsapi_versionc                    | j                  |      }|j                  d      }|d   }|d   }	|d   }
|j                         D ]6  \  }}|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(  rt        |t              r}d }d}d|v r|d   }d}nd|v r|d   d   }|d   d   }	 |O|dk  r|	dk  sd|vrAt        dt        |             }t        dt        ||!             }|g|d"<   ||d<   d	|d#<   %||d<   ,||v s2|||<   9 |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-completionsrC    response_schemajson_tool_calljson_schemaschemaname08zgpt-4ofunction)r]   )typer_   )r]   
parametersr>   	json_mode)rH   splitr,   litellmrK   r   
isinstancedictr   r   r   r   )r)   rI   rJ   r9   rK   rL   supported_openai_paramsapi_version_timesapi_version_yearapi_version_monthapi_version_dayparamr1   r[   schema_name_tool_choice_tools                    r2   map_openai_paramsz#AzureOpenAIConfig.map_openai_paramsl   sV    #'"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.2#%$-"'(9":K"2K"e+"'"6x"@K"'"6v">K *%/4E4Lu,#F'!F!,"$L 4'!E!,"E 16wOG,5AOM237OK09>O$5611).&c 7f r4   messageslitellm_paramsheadersc                 &    t        |      }||d|S )N)r9   rq   r
   )r)   r9   rq   rJ   rr   rs   s         r2   transform_requestz#AzureOpenAIConfig.transform_request   s(     4H= 
 
 	
r4   raw_responsemodel_responselogging_objrequest_dataencodingapi_keyrb   c                     t        d      )Nz^Azure OpenAI handler.py has custom logic for transforming response, as it uses the OpenAI SDK.NotImplementedError)r)   r9   rv   rw   rx   ry   rq   rJ   rr   rz   r{   rb   s               r2   transform_responsez$AzureOpenAIConfig.transform_response   s     "l
 	
r4   c                 
    ddiS Ntokenazure_ad_tokenrG   r)   s    r2   get_mapped_special_auth_paramsz0AzureOpenAIConfig.get_mapped_special_auth_params   s    )**r4   c                 L    |j                         D ]  \  }}|dk(  s||d<    |S r   )r,   )r)   rI   rJ   rl   r1   s        r2   map_special_auth_paramsz)AzureOpenAIConfig.map_special_auth_params   s4    .446LE549 01 7 r4   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ukrG   r   s    r2   get_eu_regionsz AzureOpenAIConfig.get_eu_regions   s     CBr4   c                 
    g dS )r   )
useastuseastus2eastus2euapeastus3southcentraluswestuswestus2westus3westus4rG   r   s    r2   get_us_regionsz AzureOpenAIConfig.get_us_regions   s    
 	
r4   error_messagerS   c                     t        |||      S )N)rT   rS   rs   r   )r)   r   rS   rs   s       r2   get_error_classz!AzureOpenAIConfig.get_error_class  s      !{G
 	
r4   api_basec                     t        d      )NzTAzure OpenAI has custom logic for validating environment, as it uses the OpenAI SDK.r}   )r)   rs   r9   rq   rJ   r{   r   s          r2   validate_environmentz&AzureOpenAIConfig.validate_environment  s     "b
 	
r4   )
NNNNNNNNNN)rX   )NN)!__name__
__module____qualname____doc__r   intr   strrf   listr3   classmethodr7   r   rH   boolrp   r   ru   r	   r   LoggingClassr   r   r   r   r   r   r   r   r   r   __classcell__)r.   s   @r2   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 
D a a a 	a
 a a 
aF

 '(
 	

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

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

 
 #
 3-
 

r4   r   )!typingr   r   r   r   r   httpx._modelsr   r	   rd   3litellm.litellm_core_utils.prompt_templates.factoryr   )litellm.llms.base_llm.chat.transformationr   litellm.types.utilsr   
exceptionsr   types.llms.openair   r   r   r   r   base_llm.chat.transformationr   common_utilsr   *litellm.litellm_core_utils.litellm_loggingr   LiteLLMLoggingObjr   r   rG   r4   r2   <module>r      sR    < < +  G - 1  7 +W$LLC

 C
r4   