o
    ưi(                     @  s   d Z ddlmZ ddlZddlmZmZmZmZ ddl	Z	ddl
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mZmZ dd
lmZ ddlmZ ddlmZ er_ddlm Z  neZ G dd deZ!G dd deZ"dS )zW
Bedrock Image Edit Handler

Handles image edit requests for Bedrock stability models.
    )annotationsN)TYPE_CHECKINGAnyOptionalUnion)	BaseModel)verbose_logger)Logging)BedrockStabilityImageEditConfig)AsyncHTTPHandlerHTTPHandler_get_httpx_clientget_async_httpx_client)ImageResponse   )
BaseAWSLLM)BedrockError)AWSPreparedRequestc                   @  s2   e Zd ZU dZded< ded< ded< ded	< d
S )BedrockImageEditPreparedRequestzP
    Internal/Helper class for preparing the request for bedrock image edit
    strendpoint_urlr   preppedbytesbodydictdataN)__name__
__module____qualname____doc____annotations__ r!   r!   ^/home/app/Keep/.python/lib/python3.10/site-packages/litellm/llms/bedrock/image_edit/handler.pyr   &   s   
 r   c                   @  s`   e Zd ZdZed.ddZ					d/d0ddZ	d1d2d#d$Zd3d%d&Zd4d'd(Z	d5d,d-Z
dS )6BedrockImageEditz$
    Bedrock Image Edit handler
    model
str | Nonec                 C  s   t |rt S td| )Nz*Unsupported model for bedrock image edit: )r
   Z_is_stability_edit_model
ValueError)clsr$   r!   r!   r"   get_config_class6   s   
z!BedrockImageEdit.get_config_classFNr   imagelistpromptOptional[str]model_responser   optional_paramsr   logging_objLitellmLoggingtimeout%Optional[Union[float, httpx.Timeout]]aimage_editboolapi_baseextra_headersOptional[dict]client.Optional[Union[HTTPHandler, AsyncHTTPHandler]]api_keyc              
   C  s   | j |||||	|
||d}|du r*| j|||||||d ur&t|tr&|dS d dS |d u s3t|ts6t }z|j|j|jj	|j
d}|  W n& tjya } z|jj}t||jjdd }~w tjyn   tdddw | j||||||jd}|S )	N)r$   r)   r+   r.   r5   r6   r/   r:   T)prepared_requestr1   r$   r/   r+   r-   r8   urlheadersr   status_codemessage  Timeout error occurred.)r-   r$   r/   r+   responser   )_prepare_requestasync_image_edit
isinstancer   r   r   postr   r   r>   r   raise_for_statushttpxHTTPStatusErrorrD   r@   r   textTimeoutException+_transform_response_dict_to_openai_responser   )selfr$   r)   r+   r-   r.   r/   r1   r3   r5   r6   r8   r:   r;   rD   err
error_coder!   r!   r"   
image_edit=   sX   
zBedrockImageEdit.image_editr;   r   Optional[AsyncHTTPHandler]returnc              
     s   |pt tjjd|id}z|j|j|jj|jdI dH }	|		  W n& t
jy; }
 z|
jj}t||
jjdd}
~
w t
jyH   tdddw | j||||	|j|d}|S )	z=
        Asynchronous handler for bedrock image edit
        r1   )Zllm_providerparamsr<   Nr?   rB   rC   )r$   r/   r+   rD   r   r-   )r   litellmZLlmProvidersZBEDROCKrH   r   r   r>   r   rI   rJ   rK   rD   r@   r   rL   rM   rN   r   )rO   r;   r1   r$   r/   r+   r-   r8   Zasync_clientrD   rP   rQ   r!   r!   r"   rF   |   s0   z!BedrockImageEdit.async_image_editc	              	   C  s   |  ||}	| |}
| j||
|d}| j||	j|	jd\}}| d| d}| j||||d}t|	d}ddi}|d	urFddi|}| j
|	j|	j|||||d
}|j|d|||jdd t||||dS )a  
        Prepare the request body, headers, and endpoint URL for the Bedrock Image Edit API

        Args:
            model (str): The model to use for the image edit
            image (list): The images to edit
            prompt (Optional[str]): The prompt for the edit
            optional_params (dict): The optional parameters for the image edit
            api_base (Optional[str]): The base URL for the Bedrock API
            extra_headers (Optional[dict]): The extra headers to include in the request
            logging_obj (LitellmLogging): The logging object to use for logging
            api_key (Optional[str]): The API key to use

        Returns:
            BedrockImageEditPreparedRequest: The prepared request object
        )r$   providerr.   )r5   aws_bedrock_runtime_endpointaws_region_namez/model/z/invoke)r$   r)   r+   r.   zutf-8zContent-Typezapplication/jsonN)credentialsrY   r6   r   r   r>   r:    )complete_input_dictr5   r>   )inputr:   additional_args)r   r   r   r   )Z*_get_boto_credentials_from_optional_paramsZget_bedrock_invoke_providerZget_bedrock_model_idZget_runtime_endpointrX   rY   _get_request_bodyjsondumpsencodeZget_request_headersrZ   Zpre_callr>   r   )rO   r$   r)   r+   r.   r5   r6   r/   r:   Zboto3_credentials_infoZbedrock_providerZmodelId_Zproxy_endpoint_urlr   r   r>   r   r!   r!   r"   rE      s`   

	z!BedrockImageEdit._prepare_requestc           	      C  s@   | j |d}| }|j|||r|d nd|i i d\}}t|S )z
        Get the request body for the Bedrock Image Edit API

        Checks the model/provider and transforms the request body accordingly

        Returns:
            dict: The request body to use for the Bedrock Image Edit API
        r$   r   N)r$   r+   r)   Z"image_edit_optional_request_paramsZlitellm_paramsr>   )r(   Ztransform_image_edit_requestr   )	rO   r$   r)   r+   r.   config_classconfig_instancerequest_bodyrc   r!   r!   r"   r_      s   
z"BedrockImageEdit._get_request_bodyrD   httpx.Responser   c           
      C  sl   |dur|j |d|jd|id td|j | }|du r#td| j|d}| }	|	j|||d}|S )	zR
        Transforms the Image Edit response from Bedrock to OpenAI format
        Nr[   r\   )r]   r:   original_responser^   zraw model_response: %sz)Error in response object format, got Nonerd   )r$   Zraw_responser/   )Z	post_callrL   r   debugr`   r&   r(   Ztransform_image_edit_response)
rO   r-   r$   r/   r+   rD   r   Zresponse_dictre   rf   r!   r!   r"   rN     s&   z<BedrockImageEdit._transform_response_dict_to_openai_response)r$   r%   )FNNNN)r$   r   r)   r*   r+   r,   r-   r   r.   r   r/   r0   r1   r2   r3   r4   r5   r,   r6   r7   r8   r9   r:   r,   )N)r;   r   r1   r2   r$   r   r/   r0   r+   r,   r-   r   r8   rS   rT   r   )r$   r   r)   r*   r+   r,   r.   r   r5   r,   r6   r7   r/   r0   r:   r,   rT   r   )
r$   r   r)   r*   r+   r,   r.   r   rT   r   )r-   r   r$   r   r/   r0   r+   r,   rD   rh   r   r   rT   r   )r   r   r   r   classmethodr(   rR   rF   rE   r_   rN   r!   r!   r!   r"   r#   1   s    G
&
Ur#   )#r   
__future__r   r`   typingr   r   r   r   rJ   Zpydanticr   rV   Zlitellm._loggingr   Z*litellm.litellm_core_utils.litellm_loggingr	   r0   Z8litellm.llms.bedrock.image_edit.stability_transformationr
   Z&litellm.llms.custom_httpx.http_handlerr   r   r   r   Zlitellm.types.utilsr   Zbase_aws_llmr   Zcommon_utilsr   Zbotocore.awsrequestr   r   r#   r!   r!   r!   r"   <module>   s&    