o
    ưik.                     @   s   d dl Z d dl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m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 G dd deZdS )    N)CallableListOptionalUnion)AsyncOpenAIOpenAI)Logging)CustomStreamWrapper)BaseLLM)AllMessageValuesOpenAITextCompletionUserMessage)LlmProvidersModelResponseTextCompletionResponse)ProviderConfigManager   )BaseOpenAILLMOpenAIError   )OpenAITextCompletionConfigc                       s>  e Zd Ze Zd fddZdd Z								d ded	ed
ede	e
e e
e f dededededee dee dedee dee fddZ		d!dedededed	ed
edededee fddZ				d"d	edededed
ededee fddZ			d#d	edededed
edededee fddZ  ZS )$OpenAITextCompletionreturnNc                    s   t    d S )N)super__init__)self	__class__ ]/home/app/Keep/.python/lib/python3.10/site-packages/litellm/llms/openai/completion/handler.pyr      s   zOpenAITextCompletion.__init__c                 C   s   ddi}|rd| |d< |S )Nzcontent-typezapplication/jsonzBearer Authorizationr   )r   api_keyheadersr   r   r   validate_environment   s
   z)OpenAITextCompletion.validate_environmentFmodel_responser    modelmessagestimeoutcustom_llm_providerlogging_objoptional_paramsprint_verboseapi_baseacompletionorganizationr!   c                 C   s  z|d u r| j |d}|d u s|d u rtdddtj|t|d}|j||||d}|dd}|j||||
|d	d
 |du ri|ddrX| j	||
|||||||||dW S | j
|
||||||||||dW S |ddr| j||
|||||||||dW S |d u rt||
tj|||d}n|}|jjjdi |}| }| }|j||||
dd tdi |W S  ty } z-t|dd}t|dd }t|dt|}t|dd }|d u r|rt|dd }t|||dd }~ww )N)r    i  zMissing model or messages)status_codemessage)r$   provider)r$   r%   r)   r!   max_retriesr   )r!   r+   Zcomplete_input_dict)inputr    additional_argsTstreamF)r(   r+   r    datar!   r#   r$   r&   r1   clientr-   )r+   r5   r!   r#   r    r(   r$   r&   r1   r-   r6   r    base_urlhttp_clientr&   r1   r-   r!   r+   r    original_responser3   r.     r!   textresponser.   r/   r!   r   )r"   r   r   Z#get_provider_text_completion_configr   Z!transform_text_completion_requestpopZpre_callgetasync_streamingr,   	streamingr   litellmclient_sessioncompletionswith_raw_responsecreateparse
model_dump	post_callr   	Exceptiongetattrstr)r   r#   r    r$   r%   r&   r'   r(   r)   r*   r+   r,   Zlitellm_paramsZ	logger_fnr6   r-   r!   Zprovider_configr5   r1   openai_clientraw_responser?   response_jsoner.   error_headers
error_texterror_responser   r   r   
completion    s   	"	
zOpenAITextCompletion.completionr5   r1   c              
      s   zD|d u rt ||t ||	|
d}n|}|jjjd
i |I d H }| }| }|j||||dd t	d
i |}t
||j_|W S  ty~ } z-t|dd}t|dd }t|dt|}t|dd }|d u rs|rst|dd }t|||d	d }~ww )Nr7   r:   r;   r.   r=   r!   r>   r?   r@   r   )r   r   Z_get_async_http_clientrG   rH   rI   rJ   rK   rL   r   jsondumpsZ_hidden_paramsr<   rM   rN   rO   r   )r   r(   r+   r5   r!   r#   r    r$   r&   r1   r-   r6   Zopenai_aclientrQ   r?   rR   Zresponse_objrS   r.   rT   rU   rV   r   r   r   r,      sN   		z OpenAITextCompletion.acompletionc              
   c   s^   |
d u rt ||tj||	|d}n|
}z|jjjdi |}| }W n9 ty\ } z-t|dd}t|dd }t|dt	|}t|dd }|d u rQ|rQt|dd }t
|||dd }~ww t||d||d	d d
}z|D ]}|V  qmW d S  ty } z-t|dd}t|dd }t|dt	|}t|dd }|d u r|rt|dd }t
|||dd }~ww )Nr7   r.   r=   r!   r>   r?   r@   text-completion-openaistream_optionsZcompletion_streamr$   r'   r(   r[   r   )r   rE   rF   rG   rH   rI   rJ   rM   rN   rO   r   r	   rB   )r   r(   r    r5   r!   r#   r$   r&   r+   r1   r6   r-   rP   rQ   r?   rS   r.   rT   rU   rV   streamwrapperchunkr   r   r   rD      sb   	

zOpenAITextCompletion.streamingc              
   C  s   |
d u rt ||	tj|||d}n|
}|jjjdi |I d H }| }t||d||dd d}z|2 z	3 d H W }|V  q46 W d S  t	yz } z-t
|dd}t
|dd }t
|dt|}t
|d	d }|d u ro|rot
|dd }t|||d
d }~ww )Nr7   rZ   r[   r\   r.   r=   r!   r>   r?   r@   r   )r   rE   Zaclient_sessionrG   rH   rI   rJ   r	   rB   rM   rN   rO   r   )r   r(   r    r5   r!   r#   r$   r&   r1   r+   r6   r-   rP   rQ   r?   r]   Ztransformed_chunkrS   r.   rT   rU   rV   r   r   r   rC     sH   	
z$OpenAITextCompletion.async_streaming)r   N)NNFNNNNN)NN)NNNN)NNN)__name__
__module____qualname__r   Z$openai_text_completion_global_configr   r"   r   rO   r   r   r   r   floatLiteLLMLoggingObjdictr   r   boolrW   intr,   rD   rC   __classcell__r   r   r   r   r      s    	

 	

B	
G	
r   ) rX   typingr   r   r   r   Zopenair   r   rE   Z*litellm.litellm_core_utils.litellm_loggingr   rc   Z,litellm.litellm_core_utils.streaming_handlerr	   Zlitellm.llms.baser
   Zlitellm.types.llms.openair   r   Zlitellm.types.utilsr   r   r   Zlitellm.utilsr   Zcommon_utilsr   r   Ztransformationr   r   r   r   r   r   <module>   s    