
    dh                         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Jr  S SKJr  S SKJrJrJr  S SKJr  \" SS	S
S9 " S S\\5      5       rg)    N)AnyDictListOptional)
deprecated)
Embeddings)run_in_executor)	BaseModel
ConfigDictmodel_validator)Selfz0.2.11z1.0zlangchain_aws.BedrockEmbeddings)sinceremovalalternative_importc                      \ rS rSr% Sr Sr\\S'    Sr\	\
   \S'    Sr\	\
   \S'    Sr\
\S'    Sr\	\   \S	'    Sr\	\
   \S
'    Sr\\S'    \" SSS9r\" SS9S\4S j5       rS\
S\\   4S jrS\\   S\\   4S jrS\\
   S\\\      4S jrS\
S\\   4S jrS\
S\\   4S jrS\\
   S\\\      4S jrSrg)BedrockEmbeddings   a  Bedrock embedding models.

To authenticate, the AWS client uses the following methods to
automatically load credentials:
https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html

If a specific credential profile should be used, you must pass
the name of the profile from the ~/.aws/credentials file that is to be used.

Make sure the credentials / roles used have the required policies to
access the Bedrock service.
Nclientregion_namecredentials_profile_namezamazon.titan-embed-text-v1model_idmodel_kwargsendpoint_urlF	normalizeforbid )extraprotected_namespacesafter)modereturnc                    U R                   b  U $  SSKnU R                  b  UR                  U R                  S9nOUR                  5       n0 nU R                  (       a  U R                  US'   U R
                  (       a  U R
                  US'   UR                   " S0 UD6U l         U $ ! [         a    [        S5      e[         a  n[        SU 35      UeSnAff = f)	zJValidate that AWS credentials to and python package exists in environment.Nr   )profile_namer   r   zRCould not import boto3 python package. Please install it with `pip install boto3`.zCould not load credentials to authenticate with AWS client. Please check that credentials in the specified profile name are valid. Bedrock error: )zbedrock-runtime)	r   boto3r   Sessionr   r   ImportError	Exception
ValueError)selfr$   sessionclient_paramses        ^/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/embeddings/bedrock.pyvalidate_environment&BedrockEmbeddings.validate_environmentP   s     ;;"K	,,8--T5R5R-S  --/M/3/?/?m,  040A0An-!..LmLDK   	>   	::;> 		s   BB' 'CCCtextc                 z   UR                  [        R                  S5      nU R                  R	                  S5      S   nU R
                  =(       d    0 n0 UEnUS:X  a   SUR                  5       ;  a  SUS'   U/US'   OXS'   [        R                  " U5      n U R                  R                  UU R                  S	S	S
9n[        R                  " UR                  S5      R                  5       5      nUS:X  a  UR                  S5      S   $ UR                  S5      $ ! [         a  n[        SU 35      eSnAff = f)z'Call out to Bedrock embedding endpoint. .r   cohere
input_typesearch_documenttexts	inputTextzapplication/json)bodymodelIdacceptcontentTyper9   
embeddings	embeddingz$Error raised by inference endpoint: N)replaceoslinesepr   splitr   keysjsondumpsr   invoke_modelloadsgetreadr'   r(   )	r)   r0   provider_model_kwargs
input_bodyr9   responseresponse_bodyr,   s	            r-   _embedding_func!BedrockEmbeddings._embedding_funcw   s9    ||BJJ, ==&&s+A.))/R&&
x:??#44+<
<(#'&Jw '+{#zz*%	I{{//).	 0 H !JJx||F';'@'@'BCM8#$((6q99 %((55 	ICA3GHH	Is   A2D D 
D:'D55D:r=   c                     [         R                  " U5      nU[         R                  R                  U5      -  nUR	                  5       $ )z)Normalize the embedding to a unit vector.)nparraylinalgnormtolist)r)   r=   embnorm_embs       r-   _normalize_vector#BedrockEmbeddings._normalize_vector   s4    hhz",,      r7   c                     / nU HG  nU R                  U5      nU R                  (       a  U R                  U5      nUR                  U5        MI     U$ )zCompute doc embeddings using a Bedrock model.

Args:
    texts: The list of texts to embed

Returns:
    List of embeddings, one for each text.
)rO   r   rY   append)r)   r7   resultsr0   rM   s        r-   embed_documents!BedrockEmbeddings.embed_documents   sM     D++D1H~~11(;NN8$  r[   c                 l    U R                  U5      nU R                  (       a  U R                  U5      $ U$ )z{Compute query embeddings using a Bedrock model.

Args:
    text: The text to embed.

Returns:
    Embeddings for the text.
)rO   r   rY   )r)   r0   r>   s      r-   embed_queryBedrockEmbeddings.embed_query   s2     ((.	>>)))44r[   c                 L   #    [        SU R                  U5      I Sh  vN $  N7f)zAsynchronous compute query embeddings using a Bedrock model.

Args:
    text: The text to embed.

Returns:
    Embeddings for the text.
N)r	   rb   )r)   r0   s     r-   aembed_queryBedrockEmbeddings.aembed_query   s"      %T4+;+;TBBBBs   $"$c                    #    [         R                  " U Vs/ sH  o R                  U5      PM     sn6 I Sh  vN n[        U5      $ s  snf  N7f)zAsynchronous compute doc embeddings using a Bedrock model.

Args:
    texts: The list of texts to embed

Returns:
    List of embeddings, one for each text.
N)asynciogatherre   list)r)   r7   r0   results       r-   aembed_documents"BedrockEmbeddings.aembed_documents   sC      ~~E'RED(9(9$(?E'RSSF| (SSs   AAAAA)r   ) __name__
__module____qualname____firstlineno____doc__r   r   __annotations__r   r   strr   r   r   r   r   r   boolr   model_configr   r   r.   r   floatrO   rY   r_   rb   re   rl   __static_attributes__r   r[   r-   r   r      sY   " FC!%K#% /3hsm2 1Hc0L $(L(4.'1"&L(3-&CItEH2FL'"$d $ #$L#IC #IDK #IJ!DK !DK !T#Y 4U3D ( U  
Cs 
CtE{ 
CDI $tE{:K r[   r   )rh   rD   r@   typingr   r   r   r   numpyrR   langchain_core._api.deprecationr   langchain_core.embeddingsr   langchain_core.runnables.configr	   pydanticr
   r   r   typing_extensionsr   r   r   r[   r-   <module>r      sW      	 , ,  6 0 ; ; ; " 
8
K	: K
Kr[   