Ë
    µŒjä  ã                   ó´   — d dl Z d dl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mZ d dlmZ d dlmZmZmZ d dlmZ  edd	d
¬«       G d„ dee«      «       Zy)é    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                   óv  — e Zd ZU dZ	 dZeed<   	 dZee	   ed<   	 dZ
ee	   ed<   	 dZe	ed<   	 dZee   ed<   	 dZee	   ed	<   	 d
Zeed<   	  edd¬«      Z ed¬«      defd„«       Zde	dee   fd„Zdee   dee   fd„Zdee	   deee      fd„Zde	dee   fd„Zde	dee   fd„Zdee	   deee      fd„Zy)ÚBedrockEmbeddingsa×  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                 ó¢  — | j                   �| S 	 ddl}| j                  �|j                  | j                  ¬«      }n|j                  «       }i }| j                  r| j                  |d<   | j
                  r| j
                  |d<    |j                   di |¤Ž| _         | S # t        $ r t        d«      ‚t        $ r}t        d|› �«      |‚d}~ww xY w)	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`.z’Could 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        úp/var/www/html/Fitness-lenito-AI-main/venv/lib/python3.12/site-packages/langchain_community/embeddings/bedrock.pyÚvalidate_environmentz&BedrockEmbeddings.validate_environmentP   sì   € ð �;‰;Ð"ØˆKð	Ûà×,Ñ,Ð8ØŸ-™-°T×5RÑ5R˜-ÓS‘ð  Ÿ-™-›/�àˆMØ×ÒØ/3×/?Ñ/?�˜mÑ,à× Ò Ø04×0AÑ0A�˜nÑ-à(˜'Ÿ.™.ÑL¸mÑLˆDŒKð ˆøô ò 	Üð>óð ô ò 	Üð:à:;¸ð>óð ð	ûð	ús   �BB ÂCÂ:C	Ã	CÚtextc                 ój  — |j                  t        j                  d«      }| j                  j	                  d«      d   }| j
                  xs i }i |¥}|dk(  rd|j                  «       vrd|d<   |g|d<   n||d<   t        j                  |«      }	 | j                  j                  || j                  d	d	¬
«      }t        j                  |j                  d«      j                  «       «      }|dk(  r|j                  d«      d   S |j                  d«      S # t        $ r}t        d|› �«      ‚d}~ww xY w)z'Call out to Bedrock embedding endpoint.Ú Ú.r   ÚcohereÚ
input_typeÚsearch_documentÚtextsÚ	inputTextzapplication/json)ÚbodyÚmodelIdÚacceptÚcontentTyper7   Ú
embeddingsÚ	embeddingz$Error raised by inference endpoint: N)ÚreplaceÚosÚlinesepr   Úsplitr   ÚkeysÚjsonÚdumpsr   Úinvoke_modelÚloadsÚgetÚreadr&   r'   )	r(   r.   ÚproviderÚ_model_kwargsÚ
input_bodyr7   ÚresponseÚresponse_bodyr+   s	            r,   Ú_embedding_funcz!BedrockEmbeddings._embedding_funcw   s;  € ð �|‰|œBŸJ™J¨Ó,ˆð —=‘=×&Ñ& sÓ+¨AÑ.ˆØ×)Ñ)Ò/¨RˆØ&˜Ð&ˆ
Ø�xÒØ :§?¡?Ó#4Ñ4Ø+<�
˜<Ñ(Ø#' &ˆJ�wÒð '+ˆJ�{Ñ#Ü�z‰z˜*Ó%ˆð	Ià—{‘{×/Ñ/ØØŸ™Ø)Ø.ð	 0ó ˆHô !ŸJ™J x§|¡|°FÓ';×'@Ñ'@Ó'BÓCˆMØ˜8Ò#Ø$×(Ñ(¨Ó6°qÑ9Ð9ð %×(Ñ(¨Ó5Ð5øÜò 	IÜÐCÀAÀ3ÐGÓHÐHûð	Iús   ÂA3D ÄD Ä	D2ÄD-Ä-D2r;   c                 ó�   — t        j                  |«      }|t         j                  j                  |«      z  }|j	                  «       S )z)Normalize the embedding to a unit vector.)ÚnpÚarrayÚlinalgÚnormÚtolist)r(   r;   ÚembÚnorm_embs       r,   Ú_normalize_vectorz#BedrockEmbeddings._normalize_vectorœ   s4   € ä�h‰h�zÓ"ˆØœŸ™Ÿ™¨Ó,Ñ,ˆØ�‰Ó Ð ó    r5   c                 ó–   — g }|D ]A  }| j                  |«      }| j                  r| j                  |«      }|j                  |«       ŒC |S )z¸Compute doc embeddings using a Bedrock model.

        Args:
            texts: The list of texts to embed

        Returns:
            List of embeddings, one for each text.
        )rM   r   rV   Úappend)r(   r5   Úresultsr.   rK   s        r,   Úembed_documentsz!BedrockEmbeddings.embed_documents¢   sM   € ð ˆÛˆDØ×+Ñ+¨DÓ1ˆHà�~Š~Ø×1Ñ1°(Ó;�à�N‰N˜8Õ$ð ð ˆrW   c                 ób   — | j                  |«      }| j                  r| j                  |«      S |S )z£Compute query embeddings using a Bedrock model.

        Args:
            text: The text to embed.

        Returns:
            Embeddings for the text.
        )rM   r   rV   )r(   r.   r<   s      r,   Úembed_queryzBedrockEmbeddings.embed_query¶   s2   € ð ×(Ñ(¨Ó.ˆ	à�>Š>Ø×)Ñ)¨)Ó4Ð4àÐrW   c              ƒ   óL   K  — t        d| j                  |«      ƒ d{  –—† S 7 Œ­w)z°Asynchronous compute query embeddings using a Bedrock model.

        Args:
            text: The text to embed.

        Returns:
            Embeddings for the text.
        N)r	   r]   )r(   r.   s     r,   Úaembed_queryzBedrockEmbeddings.aembed_queryÆ   s#   è ø€ ô % T¨4×+;Ñ+;¸TÓB×BÐBÐBús   ‚$�"ž$c              ƒ   óœ   K  — t        j                  |D �cg c]  }| j                  |«      ‘Œ c}Ž ƒ d{  –—† }t        |«      S c c}w 7 Œ­w)zÅAsynchronous 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Úgatherr_   Úlist)r(   r5   r.   Úresults       r,   Úaembed_documentsz"BedrockEmbeddings.aembed_documentsÒ   sF   è ø€ ô —~‘~ÉEÓ'RÉEÀD¨×(9Ñ(9¸$Õ(?ÈEÑ'RÐS×Sˆä�F‹|Ðùò (SÐSús   ‚A–A®AµA
¶A)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Ú__annotations__r   r   Ústrr   r   r   r   r   r   Úboolr   Úmodel_configr   r   r-   r   ÚfloatrM   rV   r[   r]   r_   re   r   rW   r,   r   r      sY  … ñðð" €FˆCÓØØ!%€K�˜#‘Ó%ðð /3Ð˜h s™mÓ2ðð 1€HˆcÓ0ðLð $(€L�(˜4‘.Ó'Ø1à"&€L�(˜3‘-Ó&ØCà€IˆtÓØEá HÀ2ÔF€Lá˜'Ô"ð$ dò $ó #ð$ðL#I Cð #I¨D°©Kó #IðJ!¨D°©Kð !¸DÀ¹Kó !ð T¨#¡Yð °4¸¸U¹Ñ3Dó ð( ð ¨¨U©ó ð 
C sð 
C¨t°E©{ó 
Cð¨D°©Ið ¸$¸tÀE¹{Ñ:Kô rW   r   )ra   rB   r>   Útypingr   r   r   r   ÚnumpyrO   Úlangchain_core._api.deprecationr   Úlangchain_core.embeddingsr   Úlangchain_core.runnables.configr	   Úpydanticr
   r   r   Útyping_extensionsr   r   r   rW   r,   Ú<module>rv      sW   ðÛ Û Û 	ß ,Ó ,ã Ý 6Ý 0Ý ;ß ;Ñ ;Ý "ñ Ø
ØØ8ôô
K˜	 :ó Kóñ
KrW   