
    gZ$                         d Z ddlmZmZmZmZmZmZ ddlZddl	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mZ d	d
lmZ er	ddlmZ eZneZ G d dee      Zy)z
Support for gpt model family 
    )TYPE_CHECKINGAnyListOptionalUnioncastN)BaseLLMModelInfo)
BaseConfigBaseLLMException)get_secret_str)AllMessageValues)ModelInfoBaseModelResponse   )OpenAIError)Loggingc                   p    e Zd ZU dZdZee   ed<   dZee	e
ef      ed<   dZee   ed<   dZee   ed<   dZee   ed<   dZee   ed<   dZee   ed	<   dZee	e
ef      ed
<   dZee   ed<   dZee   ed<   dZee   ed<   	 	 	 	 	 	 	 	 	 	 	 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ee   ddfdZe fd       Zde
defdZdedede
dedef
dZdedede
dedef
dZdee   de
de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!jD                  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
d&ede	ee!jN                  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*	 d.d"ee
   d(ee
   dee
   fd*Z+	 d/de
d+ee,   de,fd,Z- xZ.S )0OpenAIGPTConfiga  
    Reference: https://platform.openai.com/docs/api-reference/chat/create

    The class `OpenAIConfig` provides configuration for the OpenAI's Chat API interface. 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response_format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   r   locals_keyvalues                  `/var/www/openai/venv/lib/python3.12/site-packages/litellm/llms/openai/chat/gpt_transformation.py__init__zOpenAIGPTConfig.__init__A   sA     (--/!--/JCf}!2U3 *    c                      t         |          S N)super
get_config)clsr'   s    r+   r1   zOpenAIGPTConfig.get_configT   s    w!##r-   modelc                     g d}g }|dk7  r|dk7  r|j                  d       |t        j                  v s|t        j                  v r|j                  d       ||z   S )N)r   r   logprobstop_logprobsr   max_completion_tokens
modalities
predictionr   r   seedr   streamstream_optionsr   r   toolstool_choicer   r   max_retriesextra_headersparallel_tool_callszgpt-3.5-turbo-16kzgpt-4r   user)appendlitellmopen_ai_chat_completion_modelsopen_ai_text_completion_models)r"   r3   base_paramsmodel_specific_paramss       r+   get_supported_openai_paramsz+OpenAIGPTConfig.get_supported_openai_paramsX   sm    
4 !#((Ug-=!(():; W;;;g<<<!(( 222r-   non_default_paramsoptional_paramsdrop_paramsc                 l    | j                  |      }|j                         D ]  \  }}||v s|||<    |S )a  
        If any supported_openai_params are in non_default_params, add them to optional_params, so they are use in API call

        Args:
            non_default_params (dict): Non-default parameters to filter.
            optional_params (dict): Optional parameters to update.
            model (str): Model name for parameter support check.

        Returns:
            dict: Updated optional_params with supported non-default parameters.
        )rI   r%   )r"   rJ   rK   r3   rL   supported_openai_paramsparamr*   s           r+   _map_openai_paramsz"OpenAIGPTConfig._map_openai_params   sG    $ #'"B"B5"I.446LE5//).& 7 r-   c                 ,    | j                  ||||      S )N)rJ   rK   r3   rL   )rP   )r"   rJ   rK   r3   rL   s        r+   map_openai_paramsz!OpenAIGPTConfig.map_openai_params   s(     &&1+#	 ' 
 	
r-   messagesc                     |S r/    )r"   rS   r3   s      r+   _transform_messagesz#OpenAIGPTConfig._transform_messages   s	     r-   litellm_paramsheadersc                 6    | j                  ||      }||d|S )z
        Transform the overall request to be sent to the API.

        Returns:
            dict: The transformed request. Sent as the body of the API call.
        )rS   r3   )r3   rS   )rV   )r"   r3   rS   rK   rW   rX   s         r+   transform_requestz!OpenAIGPTConfig.transform_request   s3     ++XU+K 
 
 	
r-   raw_responsemodel_responselogging_objrequest_dataencodingapi_key	json_modec                     t         )zt
        Transform the response from the API.

        Returns:
            dict: The transformed response.
        NotImplementedError)r"   r3   r[   r\   r]   r^   rS   rK   rW   r_   r`   ra   s               r+   transform_responsez"OpenAIGPTConfig.transform_response   s
    ( "!r-   error_messagestatus_codec                 N    t        ||t        t        j                  |            S )N)rg   messagerX   )r   r   httpxHeaders)r"   rf   rg   rX   s       r+   get_error_classzOpenAIGPTConfig.get_error_class   s%     #!0
 	
r-   api_basec                     t         r/   rc   )r"   rX   r3   rS   rK   r`   rm   s          r+   validate_environmentz$OpenAIGPTConfig.validate_environment   s
     "!r-   c                    |d}|t        d      }t        j                  j                  | ddd| i      }|j                  dk7  rt        d|j                         |j                         d	   }|D cg c]  }|d
   	 c}S c c}w )zV
        Calls OpenAI's `/v1/models` endpoint and returns the list of models.
        zhttps://api.openai.comOPENAI_API_KEYz
/v1/modelsAuthorizationzBearer )urlrX      zFailed to get models: dataid)r   rD   module_level_clientgetrg   	Exceptiontextjson)r"   r`   rm   responsemodelsr3   s         r+   
get_modelszOpenAIGPTConfig.get_models   s     /H?$%56G..22*J'$y&9: 3 

 3&4X]]ODEE()/0d000s   9Bexisting_model_infoc           
      0    ||S t        |ddddd d d       S )Nopenaichatg        )r)   litellm_providermodeinput_cost_per_tokenoutput_cost_per_tokenr   max_input_tokensmax_output_tokens)r   )r"   r3   r   s      r+   get_model_infozOpenAIGPTConfig.get_model_info  s5     *&&%!$"%!"	
 		
r-   )NNNNNNNNNNN)NNr/   )/__name__
__module____qualname____doc__r   r   int__annotations__r   r   strdictr   listr   r   r   r   r   r   r   r   r,   classmethodr1   rI   boolrP   rR   r   r   rV   rZ   rj   Responser   LiteLLMLoggingObjr   re   rk   r   rl   ro   r~   r   r   __classcell__)r'   s   @r+   r   r      s   4 (,x}+04M8E#t),-4 $Ix~$!%J% $J$Ax}&*hsm*'+D(5d#
$+!%K#%E8C=&*OXd^* ,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 
4& $ $'3 '3 '3R   	
  
0
 
 
 	

 
 

-.7:		


 '(
 	

 
 
 

@ "&$("" nn" &	"
 '" " '(" " " " #" D>" 
",
 
/2
=B4CV=W
	
 "&"&	"	" 	" '(		"
 	" #	" 3-	" 
	" HL1}17?}1	c10 JN

/7/F
	
r-   r   )r   typingr   r   r   r   r   r   rj   rD    litellm.llms.base_llm.base_utilsr	   )litellm.llms.base_llm.chat.transformationr
   r   litellm.secret_managers.mainr   litellm.types.llms.openair   litellm.types.utilsr   r   common_utilsr   *litellm.litellm_core_utils.litellm_loggingr   _LiteLLMLoggingObjr   r   rU   r-   r+   <module>r      sN    C B   = R 7 6 < &X*w
&
 w
r-   