
    i(                        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e      Z! G d de      Z"y)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                  :    e Zd ZU dZded<   ded<   ded<   ded	<   y
)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__     x/Users/manta/Documents/Projects/TheRoad-I1/.venv/lib/python3.12/site-packages/litellm/llms/bedrock/image_edit/handler.pyr   r   &   s     
K
Jr$   r   c                      e Zd ZdZed	d       Z	 	 	 	 	 d
	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 	 	 ddZ		 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ
y)BedrockImageEditz$
    Bedrock Image Edit handler
    c                T    t        j                  |      rt         S t        d|       )Nz*Unsupported model for bedrock image edit: )r   _is_stability_edit_model
ValueError)clsmodels     r%   get_config_classz!BedrockImageEdit.get_config_class6   s)    *CCEJ22I%QRRr$   Nc                   | j                  |||||	|
||      }|du r1| j                  |||||||t        |t              r|      S d       S |t        |t              s
t               }	 |j                  |j                  |j                  j                  |j                        }|j                          | j'                  ||||||j(                        }|S # t        j                  $ r<}|j                  j                  }t!        ||j                  j"                        d }~wt        j$                  $ r t!        dd      w xY w)	N)r,   imagepromptoptional_paramsapi_baseextra_headerslogging_objapi_keyT)prepared_requesttimeoutr,   r4   r0   model_responseclienturlheadersr   status_codemessage  Timeout error occurred.)r8   r,   r4   r0   responser   )_prepare_requestasync_image_edit
isinstancer   r   r   postr   r   r<   r   raise_for_statushttpxHTTPStatusErrorrB   r>   r   textTimeoutException+_transform_response_dict_to_openai_responser   )selfr,   r/   r0   r8   r1   r4   r7   aimage_editr2   r3   r9   r5   r6   rB   err
error_codes                    r%   
image_editzBedrockImageEdit.image_edit=   s     00+'# 1 	
 $((!1'- )jAQ.R  )    )   >FK!@&(F	S{{'7'D'DN^NfNfNnNn  vF  vK  vK{  LH%%' II)#!&& J 
  $$ 	R11J:s||?P?PQQ%% 	S38QRR	Ss   ,AC E	.7D%%$E	c                6  K   |xs' t        t        j                  j                  d|i      }	 |j	                  |j
                  |j                  j                  |j                         d{   }	|	j                          | j#                  ||||	|j$                  |      }|S 7 7# t        j                  $ r<}
|
j                  j                  }t        ||
j                  j                        d}
~
wt        j                   $ r t        dd      w xY ww)	z=
        Asynchronous handler for bedrock image edit
        r7   )llm_providerparamsr:   Nr=   r@   rA   )r,   r4   r0   rB   r   r8   )r   litellmLlmProvidersBEDROCKrF   r   r   r<   r   rG   rH   rI   rB   r>   r   rJ   rK   rL   r   )rM   r6   r7   r,   r4   r0   r8   r9   async_clientrB   rO   rP   s               r%   rD   z!BedrockImageEdit.async_image_edit|   s"      
!7 --55w'"

	S)..3C3P3PZjZrZrZzZz  BR  BW  BW.  X  XH%%' II#!&&) J 
 # X$$ 	R11J:s||?P?PQQ%% 	S38QRR	Ss:   ,D?B( .B&/B( #D&B( (D;7C22$DDc	           	        | j                  ||      }	| j                  |      }
| j                  ||
|      }| j                  ||	j                  |	j
                        \  }}| d| d}| j                  ||||      }t        j                  |      j                  d      }ddi}|ddi|}| j                  |	j                  |	j
                  |||||	      }|j                  |d
|||j                  d       t        ||||      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,   providerr1   )r2   aws_bedrock_runtime_endpointaws_region_namez/model/z/invoke)r,   r/   r0   r1   zutf-8zContent-Typezapplication/json)credentialsr\   r3   r   r   r<   r5    )complete_input_dictr2   r<   )inputr5   additional_args)r   r   r   r   )*_get_boto_credentials_from_optional_paramsget_bedrock_invoke_providerget_bedrock_model_idget_runtime_endpointr[   r\   _get_request_bodyjsondumpsencodeget_request_headersr]   pre_callr<   r   )rM   r,   r/   r0   r1   r2   r3   r4   r5   boto3_credentials_infobedrock_providermodelId_proxy_endpoint_urlr   r   r<   r   s                     r%   rC   z!BedrockImageEdit._prepare_request   sn   6 "&!P!PU"

  ;;EB++%+ , 

 !% 9 9)?)\)\2BB !: !

 !3377)7K%%+	 & 
 zz$&&w/!#56$%'9K]KG**.::2BB'+ + 
 	'+."?? 	 	
 /+	
 	
r$   c                    | j                  |      } |       }|j                  |||r|d   nd|i i       \  }}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,   r0   r/   "image_edit_optional_request_paramslitellm_paramsr<   )r-   transform_image_edit_requestr   )	rM   r,   r/   r0   r1   config_classconfig_instancerequest_bodyro   s	            r%   rf   z"BedrockImageEdit._get_request_body   s_     ,,5,9&.)FF#%(/> G 
a L!!r$   c                    |!|j                  |d|j                  d|i       t        j                  d|j                         |j	                         }|t        d      | j                  |      } |       }	|	j                  |||      }|S )zR
        Transforms the Image Edit response from Bedrock to OpenAI format
        r^   r_   )r`   r5   original_responsera   zraw model_response: %sz)Error in response object format, got Nonerr   )r,   raw_responser4   )	post_callrJ   r	   debugrg   r*   r-   transform_image_edit_response)
rM   r8   r,   r4   r0   rB   r   response_dictrv   rw   s
             r%   rL   z<BedrockImageEdit._transform_response_dict_to_openai_response  s     "!!"*--!6 =	 "  	5x}}E  HII,,5,9&.(FF!# G 
 r$   )r,   z
str | None)FNNNN)r,   r   r/   listr0   Optional[str]r8   r   r1   r   r4   LitellmLoggingr7   %Optional[Union[float, httpx.Timeout]]rN   boolr2   r   r3   Optional[dict]r9   z.Optional[Union[HTTPHandler, AsyncHTTPHandler]]r5   r   )N)r6   r   r7   r   r,   r   r4   r   r0   r   r8   r   r9   zOptional[AsyncHTTPHandler]returnr   )r,   r   r/   r   r0   r   r1   r   r2   r   r3   r   r4   r   r5   r   r   r   )
r,   r   r/   r   r0   r   r1   r   r   r   )r8   r   r,   r   r4   r   r0   r   rB   zhttpx.Responser   r   r   r   )r   r   r    r!   classmethodr-   rQ   rD   rC   rf   rL   r#   r$   r%   r'   r'   1   s    S S ""&(,AE!%== = 	=
 &= = $= 7= =  = &= ?= =N .2$9$ 7$ 	$
 $$ $ &$ +$ 
$LS
S
 S
 	S

 S
  S
 &S
 $S
 S
 
)S
j"" " 	"
 " 
"6#%# # $	#
 # !# # 
#r$   r'   )#r!   
__future__r   rg   typingr   r   r   r   rH   pydanticr   rU   litellm._loggingr	   *litellm.litellm_core_utils.litellm_loggingr
   r   8litellm.llms.bedrock.image_edit.stability_transformationr   &litellm.llms.custom_httpx.http_handlerr   r   r   r   litellm.types.utilsr   base_aws_llmr   common_utilsr   botocore.awsrequestr   r   r'   r#   r$   r%   <module>r      sg    #  6 6    + P  . % '6i Dz Dr$   