
    hC                         d Z ddlZddlZddlmZmZmZmZmZm	Z	 ddl
Z
ddl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mZ ddlmZ d	d
lmZ d	dl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e      Z'y)z9
Handles embedding calls to Bedrock's `/invoke` endpoint
    N)AnyCallableListOptionalTupleUnion)	embedding)AsyncHTTPHandlerHTTPHandler_get_httpx_clientget_async_httpx_client)
get_secret)AmazonEmbeddingRequestCohereEmbeddingRequest)EmbeddingResponse   )
BaseAWSLLM)BedrockError   )AmazonTitanG1Config)&AmazonTitanMultimodalEmbeddingG1Config)AmazonTitanV2Config)BedrockCohereEmbeddingConfigc                   2   e Zd Zdedeeef   fdZd Zde	e
   de	eeej                  f      deded	edefd
Zde	e   de	eeej                  f      deded	edefdZ	 dde	e
   de	eeej                  f      dee   dede	e   dedededede	e   fdZ	 dde	e   de	eeej                  f      dee   dede	e   dedededede	e   fdZ	 ddedee   de	e   dedede	ee
ef      de	eeej                  f      de	e   de	e   dedede	e   defdZy)BedrockEmbeddingoptional_paramsreturnc                 L   	 ddl m} |j                  dd       }|j                  dd       }|j                  dd       }|j                  dd       }|j                  dd       }|j                  d	d       }|j                  d
d       }	|j                  dd       }
|j                  dd       }|Dt	        dd       }|t        |t              r|}t	        dd       }|t        |t              r|}|d}| j                  ||||||	||
|	      }||fS # t        $ r t        d      w xY w)Nr   )Credentialsz7Missing boto3 to call bedrock. Run 'pip install boto3'.aws_secret_access_keyaws_access_key_idaws_session_tokenaws_region_nameaws_role_nameaws_session_nameaws_profile_nameaws_web_identity_tokenaws_sts_endpointAWS_REGION_NAME
AWS_REGIONz	us-west-2)	r!   r    r"   r#   r%   r&   r$   r'   r(   )botocore.credentialsr   ImportErrorpopr   
isinstancestrget_credentials)selfr   r   r    r!   r"   r#   r$   r%   r&   r'   r(   litellm_aws_region_namestandard_aws_region_namecredentialss                  `/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/llms/bedrock/embed/embedding.py_load_credentialsz"BedrockEmbedding._load_credentials"   s{   	Y8
 !0 3 34KT R+//0CTJ+//0CTJ)--.?F'++OTB*../A4H*../A4H!0!4!45Mt!T*../A4H "&01BD&I#&2z'8 #:'1,'E$'3
(#9 #;&"-#'#7#7/"7/+--'#9- $8 
$
 O++Y  	YWXX	Ys   D D#c                    K   y wN )r1   s    r5   async_embeddingsz!BedrockEmbedding.async_embeddingsV   s	     s   clienttimeoutapi_baseheadersdatac                 0   |t        |t              sJi }|:t        |t              st        |t              rt	        j
                  |      }||d<   t        |      }n|}	 |j                  ||t        j                  |            }|j                          |j                         S # t        j                  $ r<}|j                  j                  }	t        |	|j                  j                        d }~wt        j                   $ r t        dd      w xY w)Nr<   urlr>   r?   status_codemessage  Timeout error occurred.)r.   r   floatinthttpxTimeoutr   postjsondumpsraise_for_statusHTTPStatusErrorresponserD   r   textTimeoutException
r1   r;   r<   r=   r>   r?   _paramsrQ   err
error_codes
             r5   _make_sync_callz BedrockEmbedding._make_sync_callY   s     >FK!@G"gu-GS1I#mmG4G%,	"&w/FF	S{{xtzzRVGW{XH%%' }} $$ 	R11J:s||?P?PQQ%% 	S38QRR	Ss    7B' 'D:7C11$Dc                   K   |t        |t              sdi }|:t        |t              st        |t              rt	        j
                  |      }||d<   t        |t        j                  j                        }n|}	 |j                  ||t        j                  |             d {   }|j                          |j                         S 7 $# t        j                  $ r<}|j                  j                   }	t#        |	|j                  j$                        d }~wt        j&                  $ r t#        dd      w xY ww)Nr<   )paramsllm_providerrA   rC   rF   rG   )r.   r
   rH   rI   rJ   rK   r   litellmLlmProvidersBEDROCKrL   rM   rN   rO   rP   rQ   rD   r   rR   rS   rT   s
             r5   _make_async_callz!BedrockEmbedding._make_async_callu   s     >F4D!EG"gu-GS1I#mmG4G%,	"+W-A-A-I-IF F	S#[[XwTZZX\M][^^H%%' }} _$$ 	R11J:s||?P?PQQ%% 	S38QRR	Ss<   A9D><*C &C'C ;D>C D; 7D$D;;D>N
batch_datar4   extra_headersendpoint_urlr#   modellogging_objapi_keyc           
         g }|D ]  }ddi}|ddi|}| j                  ||||t        j                  |      ||
      }|	j                  |d||j                  |j
                  d       | j                  |||j                  |j
                  |      }|	j                  |d|d|i	       |j                  |        d }|d
k(  rt               j                  ||      }nA|dk(  rt               j                  ||      }n |dk(  rt               j                  ||      }|t        dj                  |            |S NContent-Typeapplication/jsonr4   r#   ra   rb   r?   r>   re    )complete_input_dictr=   r>   )inputre   additional_args)r;   r<   r=   r>   r?   rl   )rm   re   original_responsern   amazon.titan-embed-image-v1)response_listrc   amazon.titan-embed-text-v1amazon.titan-embed-text-v2:0z?Unable to map model response to known provider format. model={})get_request_headersrM   rN   pre_callrB   r>   rX   	post_callappendr   _transform_responser   r   	Exceptionformatr1   r;   r<   r`   r4   ra   rb   r#   rc   rd   re   	responsesr?   r>   preppedrQ   returned_responses                    r5   _single_func_embeddingsz(BedrockEmbedding._single_func_embeddings   s    !#	D%'9:G()+=OO..(!0,*jj&   / G   +/ '&! !  ++  , H !!"*!6 =	 "  X&S V :> 1168LL"+5 M  
 22 3 5 I I'u !J ! 44 3 5 I I'u !J ! $QXX  !     c           
        K   g }|D ]  }ddi}|ddi|}| j                  ||||t        j                  |      ||
      }|	j                  |d||j                  |j
                  d       | j                  |||j                  |j
                  |       d {   }|	j                  |d|d|i	       |j                  |        d }|d
k(  rt               j                  ||      }nA|dk(  rt               j                  ||      }n |dk(  rt               j                  ||      }|t        dj                  |            |S 7 wrg   )rt   rM   rN   ru   rB   r>   r_   rv   rw   r   rx   r   r   ry   rz   r{   s                    r5   _async_single_func_embeddingsz.BedrockEmbedding._async_single_func_embeddings   s     !#	D%'9:G()+=OO..(!0,*jj&   / G   +/ '&! !  "22  3  H !!"*!6 =	 "  X&S V :> 1168LL"+5 M  
 22 3 5 I I'u !J ! 44 3 5 I I'u !J ! $QXX  ! Ws   BEEB1Erm   model_responseprint_verbose
aembeddinglitellm_paramsc                    | j                  |      \  }}|j                  d      d   }t        j                  |      }|j	                         D ci c]$  \  }}|j                         | j                  vr||& }}}|j                  dd        |j                  dd       xs |}d }d }|dk(  rt               j                  |||      }n|dk(  r|dv rg }|D ]  }|d	k(  rt               j                  ||
      }n_|dk(  rt               j                  ||
      }n>|dk(  rt               j                  ||
      }nt        dj                  |g d            |j                  |        | j!                  ||j                  dd       |      \  }}| d| d}|`|
r/| j#                  |t%        |t&              r|nd |	||||||||
      S | j)                  |t%        |t*              r|nd |	||||||||
      S |t        d      ddi}|ddi|}| j-                  ||||t/        j0                  |      ||      }t3        ||||||||j4                  d |
|	||j6                        S c c}}w )N.r   usermodel_idcohere)rc   rm   inference_paramsamazon)rp   rr   rs   rp   )rm   r   rr   rs   z(Unmapped model. Received={}. Expected={}aws_bedrock_runtime_endpoint)r=   r   r#   z/model/z/invoke)
r;   r<   r`   r4   ra   rb   r#   rc   rd   re   z)Unable to map Bedrock request to providerrh   ri   rj   )rc   rm   r   rd   r   encodingr?   complete_api_basere   r   r<   r;   r>   )r6   splitcopydeepcopyitemsloweraws_authentication_paramsr-   r   _transform_requestr   r   r   ry   rz   rw   get_runtime_endpointr   r.   r
   r   r   rt   rM   rN   cohere_embeddingrB   r>   )r1   rc   rm   r=   r   r   r   rd   r;   r<   r   ra   r   r   re   r4   r#   providerr   kvmodelIdr?   r`   itransformed_requestrb   proxy_endpoint_urlr>   r}   s                                 r5   
embeddingszBedrockEmbedding.embeddings<  sd   " (,'='=o'N$_ ;;s#A&==9 )..0
1wwy > >> qD
 

 	D	
 
D1:U 	 26%)
x/1DD5;K E D !e 0
 '

 J99 ?@SS2B T  (
 ::*=*?*R*R2B +S +' <<*=*?*R*R2B +S +' $BII!	 	 !!"565 : ,0+D+D)8)<)<.* , ,E ,
(( 'wwiw?!99 "-*VEU2V !#) +"/!-$3 +# :    // )j.M %'+) /' 0    \GHH!#56$%'9K]KG**#+'%D! + 
  )#+%kk!OO
 	
e
s   )I/r8   )__name__
__module____qualname__dictr   r   r/   r6   r:   r   r   r   rH   rJ   rK   rX   r
   r_   r   r   r   r   r   boolr   r9   r   r5   r   r   !   s   2,2, 
sCx2,h% %u}} 456 	
   
8)* %u}} 456 	
   
T "&R!%R! %u}} 456R! J	R!
 R!  ~R! R! R! R! R! #R!~ "&R!)*R! %u}} 456R! J	R!
 R!  ~R! R! R! R! R! #R!F "&V
V
 CyV
 3-	V

 *V
  V
 {,<<=>V
 %u}} 456V
 TNV
  ~V
 V
 V
 #V
  
!V
r   r   )(__doc__r   rM   typingr   r   r   r   r   r   rJ   r\   !litellm.llms.cohere.embed.handlerr	   r   &litellm.llms.custom_httpx.http_handlerr
   r   r   r   litellm.secret_managers.mainr   litellm.types.llms.bedrockr   r   litellm.types.utilsr   base_aws_llmr   common_utilsr   amazon_titan_g1_transformationr   &amazon_titan_multimodal_transformationr   amazon_titan_v2_transformationr   cohere_transformationr   r   r9   r   r5   <module>r      s[      > >   K  4 U 1 % ' ? @ ?q
z q
r   