o
    ưi!                     @   s   d Z ddlmZmZ ddlZddlmZmZ ddlZddl	m
Z
mZmZ ddlmZ ddlmZ dd	lmZ d
dlmZ G dd dZdS )z!
OpenAI Image Variations Handler
    )CallableOptionalN)AsyncOpenAIOpenAI)	FileTypesImageResponseLlmProviders)ProviderConfigManager   )BaseImageVariationConfig)LiteLLMLoggingObj   )OpenAIErrorc                    @   s   e Zd Zdee defddZdee dedefddZde	d	e	d
ee	 dee dededee	 dee
 dedededededededef ddZ					ddede	d	e	dee	 dedee
 de	dedededee d
ee	 dee defddZdS )OpenAIImageVariationsHandlerclientinit_client_paramsc                 C   "   |d u rt di |}|S |}|S N )r   selfr   r   Zopenai_clientr   r   c/home/app/Keep/.python/lib/python3.10/site-packages/litellm/llms/openai/image_variations/handler.pyget_sync_client   s   z,OpenAIImageVariationsHandler.get_sync_clientreturnc                 C   r   r   )r   r   r   r   r   get_async_client!   s   z-OpenAIImageVariationsHandler.get_async_clientapi_keyapi_baseorganizationdataheadersmodeltimeoutmax_retrieslogging_objmodel_responseoptional_paramslitellm_paramsimageprovider_configc                    s  zP||t j||	|d}| j||d}|jjjdi |I d H }| }| }|
j||||dd |j	|t
di |tjdtjdddd	|
||||d |d

W S  ty } z-t|dd}t|dd }t|dt|}t|dd }|d u r|rt|dd }t|||dd }~ww )Nr   base_urlhttp_clientr!   r"   r   r   r   r   r   r   original_responseadditional_args   GEThttps://litellm.aimethodurlstatus_coderequest
r    r$   raw_responser#   Zrequest_datar'   r%   r&   encodingr   r8     r   textresponser8   messager   r   )litellmclient_sessionr   imageswith_raw_responsecreate_variationparse
model_dump	post_call"transform_response_image_variationr   httpxResponseRequest	Exceptiongetattrstrr   )r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r   r;   r?   response_jsoner8   error_headers
error_texterror_responser   r   r   async_image_variations,   sf   	
z3OpenAIImageVariationsHandler.async_image_variationsNcustom_llm_providerprint_verbosec                 C   s  zt j|pdtjd}|d u rtd| d|	dd}|j|||	|p%i d}|d}|s6td	| |jd||||d
d |
ddr_| j	|||pOi |||||||||||	|
dW S ||t
j|||d}| j||d}|jjjdi |}| }| }|j||||dd |j|tdi |tjdtjdddd||||	|
d |d
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    providerz$image variation provider not found: .r"   r   )r    r'   r%   r   r   z9data field is required, for openai image variations. Got=)r   r   Zcomplete_input_dict)inputr   r0   Z
async_callF)r   r   r   r$   r   r#   r    r!   r"   r   r   r(   r'   r%   r&   r)   r,   r-   r.   r1   r2   r3   r4   r7   r:   r8   r=   r   r>   r?   r@   r   )r	   Z#get_provider_image_variation_configr   ZOPENAI
ValueErrorpopZ!transform_request_image_variationgetZpre_callrV   rB   rC   r   rD   rE   rF   rG   rH   rI   rJ   r   rK   rL   rM   rN   rO   rP   r   )r   r$   r   r   r    r'   r!   rW   r#   r%   r&   rX   Z	logger_fnr   r   r   r(   r"   r   Z	json_datar   r;   r?   rQ   rR   r8   rS   rT   rU   r   r   r   image_variationsw   s   

		
z-OpenAIImageVariationsHandler.image_variations)NNNNN)__name__
__module____qualname__r   r   dictr   r   r   rP   floatintr   r   r   r   rV   r   r`   r   r   r   r   r      s    

	

W	
r   )__doc__typingr   r   rK   Zopenair   r   rB   Zlitellm.types.utilsr   r   r   Zlitellm.utilsr	   Z(base_llm.image_variations.transformationr   Zcustom_httpx.llm_http_handlerr   Zcommon_utilsr   r   r   r   r   r   <module>   s    