
    ^h)                         S SK r S SKrS SKrS SKJrJrJrJr  S SKrS SK	J
r
  S SKrS SKJr  S SKJr  S SKJrJr  S SKJr  SS	KJr  SS
KJr  \(       a  S SKJr  O\r " S S\
5      r " S S\5      rg)    N)TYPE_CHECKINGAnyOptionalUnion)	BaseModel)verbose_logger)Logging)_get_httpx_clientget_async_httpx_client)ImageResponse   )
BaseAWSLLM)BedrockError)AWSPreparedRequestc                   B    \ rS rSr% Sr\\S'   \\S'   \\S'   \	\S'   Sr
g)	BedrockImagePreparedRequest   zN
Internal/Helper class for preparing the request for bedrock image generation
endpoint_urlpreppedbodydata N)__name__
__module____qualname____firstlineno____doc__str__annotations__r   bytesdict__static_attributes__r       b/home/james-whalen/.local/lib/python3.13/site-packages/litellm/llms/bedrock/image/image_handler.pyr   r      s     
K
Jr#   r   c                   P   \ rS rSrSr   SS\S\S\S\S\S	\	\
\\R                  4      S
\S\	\   S\	\   4S jjrS\S	\	\
\\R                  4      S\S\S\S\S\4S jrS\S\S\	\   S\	\   S\S\S\4S jrS\S\S\S\4S jrS\S\S\S\S\R(                  S\S\4S jrSrg)BedrockImageGeneration&   z"
Bedrock Image Generation handler
Nmodelpromptmodel_responseoptional_paramslogging_objtimeoutaimg_generationapi_baseextra_headersc
           	         U R                  UUUU	UUS9n
USL a  U R                  U
UUUUUS9$ [        5       n UR                  U
R                  U
R
                  R                  U
R                  S9nUR                  5         U R!                  UUUUUU
R"                  S9nU$ ! [        R                   a8  nUR                  R                  n[        XR                  R                  S9eS nAf[        R                   a    [        SSS9ef = f)	N)r(   r+   r/   r0   r,   r)   T)prepared_requestr-   r(   r,   r)   r*   urlheadersr   status_codemessage  Timeout error occurred.)r*   r(   r,   r)   responser   )_prepare_requestasync_image_generationr
   postr   r   r5   r   raise_for_statushttpxHTTPStatusErrorr;   r7   r   textTimeoutException+_transform_response_dict_to_openai_responser   )selfr(   r)   r*   r+   r,   r-   r.   r/   r0   r2   clientr;   err
error_codes                  r$   image_generation'BedrockImageGeneration.image_generation+   sM     00+'# 1 
 d"..!1'- /   #$	S{{'7'D'DN^NfNfNnNn  vF  vK  vK{  LH%%' II)#!&& J 
  $$ 	R11J:||?P?PQQ%% 	S38QRR	Ss   A	B" "D63C))"Dr2   returnc           	        #    [        [        R                  R                  SU0S9n UR	                  UR
                  UR                  R                  UR                  S9I Sh  vN nUR                  5         U R#                  UUUUUR$                  US9nU$  N4! [        R                   a8  n	U	R                  R                  n
[        XR                  R                  S9eSn	A	f[        R                    a    [        SSS9ef = f7f)	zs
Asynchronous handler for bedrock image generation

Awaits the response from the bedrock image generation endpoint
r-   )llm_providerparamsr3   Nr6   r9   r:   )r(   r,   r)   r;   r   r*   )r   litellmLlmProvidersBEDROCKr>   r   r   r5   r   r?   r@   rA   r;   r7   r   rB   rC   rD   r   )rE   r2   r-   r(   r,   r)   r*   async_clientr;   rG   rH   s              r$   r=   -BedrockImageGeneration.async_image_generation^   s     . --55w'

	S)..3C3P3PZjZrZrZzZz  BR  BW  BW.  X  XH%%' II#!&&) J 
 # X$$ 	R11J:||?P?PQQ%% 	S38QRR	Ss:   %D<B $B%B 9 DB D/3C"""DDc                 p    SSK Jn  SSKJn  U R                  U5      n	Un
U R                  UU	R                  U	R                  S9u  pU SU
 S3nU" U	R                  SU	R                  5      nU R                  XUS	9n[        R                  " U5      R                  S
5      nSS0nUb  SS0UEnU" SXUS9nUR                  U5        Ub  SU;   a  US   UR                  S'   UR!                  5       nUR#                  USUUUR                  S.S9  [%        UUUUS9$ ! [         a    [	        S5      ef = f)a  
Prepare the request body, headers, and endpoint URL for the Bedrock Image Generation API

Args:
    model (str): The model to use for the image generation
    optional_params (dict): The optional parameters for the image generation
    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
    prompt (str): The prompt to use for the image generation
Returns:
    BedrockImagePreparedRequest: The prepared request object

The BedrockImagePreparedRequest contains:
    endpoint_url (str): The endpoint URL for the Bedrock Image Generation API
    prepped (httpx.Request): The prepared request object
    body (bytes): The request body
r   )	SigV4Auth)
AWSRequestz7Missing boto3 to call bedrock. Run 'pip install boto3'.)r/   aws_bedrock_runtime_endpointaws_region_namez/model/z/invokebedrock)r(   r)   r+   zutf-8zContent-Typezapplication/jsonPOST)methodr4   r   r5   Authorization )complete_input_dictr/   r5   )inputapi_keyadditional_args)r   r   r   r   )botocore.authrU   botocore.awsrequestrV   ImportError*_get_boto_credentials_from_optional_paramsget_runtime_endpointrW   rX   credentials_get_request_bodyjsondumpsencodeadd_authr5   preparepre_callr   )rE   r(   r+   r/   r0   r,   r)   rU   rV   boto3_credentials_infomodelId_proxy_endpoint_urlsigv4r   r   r5   requestr   s                      r$   r<   'BedrockImageGeneration._prepare_request   s   6	Y/6 "&!P!P"

  $ 9 9)?)\)\2BB !: !

 !3377)7K".."22
 %% & 

 zz$&&w/!#56$%'9K]KG1g
 	w%/]*J/<_/MGOOO,//# 	'+."?? 	 	
 ++	
 	
e  	YWXX	Ys   D D5c                    UR                  S5      S   n[        R                  " U5      nUR                  SS5        0 nUS:X  a  [        R
                  R                  U5      (       a(  [        R
                  R                  X#S9n[        U5      $ UR                  [        R                  S5      n[        R                  R                  5       nUR                  5        H  u  pX;  d  M  XU	'   M     SUS	S
./0UEn U$ [        SSU S3S9e)z
Get the request body for the Bedrock Image Generation API

Checks the model/provider and transforms the request body accordingly

Returns:
    dict: The request body to use for the Bedrock Image Generation API
.r   userN	stability)r)   r+    text_prompts   )rB   weighti  zUnsupported model=z, passed inr6   )splitcopydeepcopypoprO   AmazonStability3Config_is_stability_3_modeltransform_request_bodyr!   replaceoslinesepAmazonStabilityConfig
get_configitemsr   )rE   r(   r)   r+   providerinference_paramsr   request_bodyconfigkvs              r$   rh   (BedrockImageGeneration._get_request_body   s    ;;s#A&==9D	
 {"--CCEJJ&==TT!  U   L))

C8 66AAC"LLNDA1./+	 + #f%B$C&  +=eWK)P r#   r;   r   c                 j   Ub  UR                  USUR                  SU0S9  [        R                  " SUR                  5        UR	                  5       nUc  [        S5      e[        R                  R                  US9(       a  [        R                  O[        R                  nUR                  UUS9  U$ )zH
Transforms the Image Generation response from Bedrock to OpenAI format
r]   r^   )r_   r`   original_responsera   zraw model_response: %sz)Error in response object format, got None)r(   )r*   response_dict)	post_callrB   r   debugri   
ValueErrorrO   r   r   r   *transform_response_dict_to_openai_response)	rE   r*   r(   r,   r)   r;   r   r   config_classs	            r$   rD   BBedrockImageGeneration._transform_response_dict_to_openai_response	  s     "!!"*--!6 =	 "  	5x}}E  HII --CC%CP **.. 	
 	??)' 	@ 	

 r#   r   )FNN)r   r   r   r   r   r   r   r!   LitellmLoggingr   r   floatr@   TimeoutboolrI   r   r=   r<   rh   ResponserD   r"   r   r#   r$   r&   r&   &   s    !&"&(,11 1 &	1
 1 $1 %u}} 4561 1 3-1  ~1f%5% %u}} 456% 	%
 $% % &% 
%NU
U
 U
 3-	U

  ~U
 $U
 U
 
%U
n++ + 	+
 
+Z$%$ $ $	$
 $ ..$ $ 
$r#   r&   )r   ri   r   typingr   r   r   r   r@   pydanticr   rO   litellm._loggingr   *litellm.litellm_core_utils.litellm_loggingr	   r   &litellm.llms.custom_httpx.http_handlerr
   r   litellm.types.utilsr   base_aws_llmr   common_utilsr   rc   r   r   r&   r   r#   r$   <module>r      sY      	 6 6    + P . % '6) GZ Gr#   