o
    ưi                     @   sz   d Z ddlmZmZmZ ddlmZ ddlmZm	Z	 ddl
mZmZmZmZ ddlmZ dd	lmZ G d
d deeZdS )z
Support for gpt model family
    )ListOptionalUnion)BaseTextCompletionConfig)AllMessageValuesOpenAITextCompletionUserMessage)ChoicesMessageModelResponseTextCompletionResponse   )OpenAIGPTConfig   _transform_promptc                       s  e Zd ZU dZ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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 dee dee	 dee 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dZe fddZ		d!dee dee fddZdedefddZdedeee ee  f dededef
ddZ!  Z"S )"OpenAITextCompletionConfiga  
    Reference: https://platform.openai.com/docs/api-reference/completions/create

    The class `OpenAITextCompletionConfig` provides configuration for the OpenAI's text completion API interface. Below are the parameters:

    - `best_of` (integer or null): This optional parameter generates server-side completions and returns the one with the highest log probability per token.

    - `echo` (boolean or null): This optional parameter will echo back the prompt in addition to the completion.

    - `frequency_penalty` (number or null): Defaults to 0. It is a numbers from -2.0 to 2.0, where positive values decrease the model's likelihood to repeat the same line.

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

    - `logprobs` (integer or null): This optional parameter includes the log probabilities on the most likely tokens as well as the chosen tokens.

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

    - `n` (integer or null): This optional parameter sets how many completions to generate for each prompt.

    - `presence_penalty` (number or null): Defaults to 0 and can be between -2.0 and 2.0. Positive values increase 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.

    - `suffix` (string or null): Defines the suffix that comes after a completion of inserted text.

    - `temperature` (number or null): This optional parameter defines the sampling temperature to use.

    - `top_p` (number or null): An alternative to sampling with temperature, used for nucleus sampling.
    Nbest_ofechofrequency_penalty
logit_biaslogprobs
max_tokensnpresence_penaltystopsuffixtemperaturetop_preturnc                 C   s>   t   }| D ]\}}|dkr|d urt| j|| q	d S )Nself)localscopyitemssetattr	__class__)r   r   r   r   r   r   r   r   r   r   r   r   r   Zlocals_keyvalue r'   d/home/app/Keep/.python/lib/python3.10/site-packages/litellm/llms/openai/completion/transformation.py__init__9   s   
z#OpenAITextCompletionConfig.__init__c                    s
   t   S )N)super
get_config)clsr$   r'   r(   r+   M   s   
z%OpenAITextCompletionConfig.get_configresponse_objectmodel_response_objectc              
   C   s   z]|d u s	|d u rt dg }t|d D ]\}}t|d dd}t|d |||dd d}|| q||_d	|v rDt|d	|d	  d
|v rM|d
 |_d|v rV|d |_	||j
d< |W S  tyk } z|d }~ww )NzError in response object formatchoicestextZ	assistant)contentZrolefinish_reasonr   )r3   indexmessager   usageidmodeloriginal_response)
ValueError	enumerater	   r   getappendr0   r#   r7   r8   Z_hidden_params	Exception)r   r.   r/   Zchoice_listidxchoicer5   er'   r'   r(   %convert_to_chat_model_response_objectQ   s<   


z@OpenAITextCompletionConfig.convert_to_chat_model_response_objectr8   c                 C   s   g dS )N)Z	functionsZfunction_callr   r   r   streamZstream_optionsr   r   r   r   r   userZresponse_formatseedZtoolsZtool_choicemax_retriesr   Ztop_logprobsextra_headersr'   )r   r8   r'   r'   r(   get_supported_openai_paramsy   s   z6OpenAITextCompletionConfig.get_supported_openai_paramsmessagesoptional_paramsheadersc                 C   s   t |}||d|S )N)r8   promptr   )r   r8   rI   rJ   rK   rL   r'   r'   r(   !transform_text_completion_request   s   z<OpenAITextCompletionConfig.transform_text_completion_request)NNNNNNNNNNNN)NN)#__name__
__module____qualname____doc__r   r   int__annotations__r   boolr   r   dictr   r   r   r   r   r   strlistr   floatr)   classmethodr+   r   r
   rB   r   rH   r   r   rM   __classcell__r'   r'   r-   r(   r      s   
 	


(r   N)rQ   typingr   r   r   Z/litellm.llms.base_llm.completion.transformationr   Zlitellm.types.llms.openair   r   Zlitellm.types.utilsr   r	   r
   r   Zchat.gpt_transformationr   utilsr   r   r'   r'   r'   r(   <module>   s    