
    NgE                         d dl mZ d dlmZmZmZmZmZmZ d dl	m
Z
 d dlmZ d dlmZmZ dZ G d de          Z G d	 d
ee
          ZdS )    )Enum)AnyDictIteratorListMappingOptional)
Embeddings)pre_init)	BaseModel
ConfigDictzocid1.generativeaiendpointc                   "    e Zd ZdZdZdZdZdZdS )OCIAuthTypez'OCI authentication types as enumerator.            N)__name__
__module____qualname____doc__API_KEYSECURITY_TOKENINSTANCE_PRINCIPALRESOURCE_PRINCIPAL     l/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/embeddings/oci_generative_ai.pyr   r      s,        11GNr   r   c                      e Zd ZU dZdZeed<   dZeed<   dZe	e
         ed<   	 dZe	e
         ed<   	 dZe	e
         ed	<   	 dZe	e         ed
<   	 dZe	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edefd            Zedee
ef         fd            Zdee
         deee                  fdZde
dee         fdZdS )OCIGenAIEmbeddingsa*  OCI embedding models.

    To authenticate, the OCI client uses the methods described in
    https://docs.oracle.com/en-us/iaas/Content/API/Concepts/sdk_authentication_methods.htm

    The authentifcation method is passed through auth_type and should be one of:
    API_KEY (default), SECURITY_TOKEN, INSTANCE_PRINCIPLE, RESOURCE_PRINCIPLE

    Make sure you have the required policies (profile/roles) to
    access the OCI Generative AI service. If a specific config profile is used,
    you must pass the name of the profile (~/.oci/config) through auth_profile.

    To use, you must provide the compartment id
    along with the endpoint url, and model id
    as named parameters to the constructor.

    Example:
        .. code-block:: python

            from langchain.embeddings import OCIGenAIEmbeddings

            embeddings = OCIGenAIEmbeddings(
                model_id="MY_EMBEDDING_MODEL",
                service_endpoint="https://inference.generativeai.us-chicago-1.oci.oraclecloud.com",
                compartment_id="MY_OCID"
            )
    Nclientservice_modelsr   	auth_typeDEFAULTauth_profilemodel_idmodel_kwargsservice_endpointcompartment_idENDtruncate`   
batch_sizeforbidr   )extraprotected_namespacesvaluesreturnc                    |d         |S 	 ddl i d|d         j        j        dd}|d         t          d          j        k    r;j                            |d	         
          |d<   |                    dd           n|d         t          d          j        k    r?fd}j                            |d	         
          |d<    ||d                   |d<   n|d         t          d          j        k    r"j        j	        
                                |d<   nO|d         t          d          j        k    r"j        j	                                        |d<   nt          d           j        j        di ||d<   n?# t          $ r}t          d          |d}~wt           $ r}t          d          |d}~ww xY w|S )zBValidate that OCI config and python package exists in environment.r!   Nr   r(   )
      )configsignerr(   retry_strategytimeoutr#   r   r%   )profile_namer6   r7   r   c                 @   j                             |                     d          d           }t          |                     d          d          5 }|                                }d d d            n# 1 swxY w Y   j        j                            ||          S )Nkey_filesecurity_token_filezutf-8)encoding)r7   load_private_key_from_filegetopenreadauthsignersSecurityTokenSigner)
oci_configpkf	st_stringocis       r   make_security_token_signerzKOCIGenAIEmbeddings.validate_environment.<locals>.make_security_token_signert   s    >>"z22D B "'<==   -$%FFHH	- - - - - - - - - - - - - - - 8+??	2NNNs   A55A9<A9)rF   r   r   z'Please provide valid value to auth_typezYCould not import oci python package. Please make sure you have the oci package installed.zCould not authenticate with OCI client. Please check if ~/.oci/config exists. If INSTANCE_PRINCIPLE or RESOURCE_PRINCIPLE is used, Please check the specified auth_profile and auth_type are valid.r   )rJ   retryDEFAULT_RETRY_STRATEGYr   namer6   	from_filepoprC   rD   %InstancePrincipalsSecurityTokenSignerget_resource_principals_signer
ValueErrorgenerative_ai_inferenceGenerativeAiInferenceClientImportError	Exception)clsr1   client_kwargsrK   exerJ   s         @r   validate_environmentz'OCIGenAIEmbeddings.validate_environmentZ   sd   
 ('M=	JJJ $*+=$>"%)"B$ M k"k!nn&999*-**>*>!'!7 +? + +h' !!(D1111$A(;;;O O O O O +.**>*>!'!7 +? + +h' +E*D,X6+ + +h'' $A(;;;H$JJLL h'' $A(;;;H$CCEE h'' !!JKKKVs:V       F8  	 	 	G    	 	 	8  	 s$   E6F 
G FG +F;;G c                      | j         pi }i d|iS )zGet the identifying parameters.r'   )r'   )self_model_kwargss     r   _identifying_paramsz&OCIGenAIEmbeddings._identifying_params   s(     )/R
}-
 	
r   textsc                     ddl m}  j        st          d           j                            t
                    r|                     j                  }n|                     j                  }g }dt          t          t                            f fd} |            D ]^}|                    | j         j        |          } j                            |          }|                    |j        j                   _|S )	zCall out to OCIGenAI's embedding endpoint.

        Args:
            texts: The list of texts to embed.

        Returns:
            List of embeddings, one for each text.
        r   )modelsz'Model ID is required to embed documents)endpoint_id)r&   r2   c               3      K   t          dt                    j                  D ]} | | j        z            V  d S )Nr   )rangelenr-   )ir^   ra   s    r   split_textsz7OCIGenAIEmbeddings.embed_documents.<locals>.split_texts   sT      1c%jj$/:: 5 5ADO 33444445 5r   )serving_moder)   r+   inputs)oci.generative_ai_inferencerc   r&   rS   
startswithCUSTOM_ENDPOINT_PREFIXDedicatedServingModeOnDemandServingModer   r   strEmbedTextDetailsr)   r+   r!   
embed_textextenddata
embeddings)	r^   ra   rc   rj   rv   ri   chunkinvocation_objresponses	   ``       r   embed_documentsz"OCIGenAIEmbeddings.embed_documents   s1    	766666} 	HFGGG=##$:;; 	N!664=6QQLL!55t}5MML
	5Xd3i0 	5 	5 	5 	5 	5 	5 	5 ![]] 	8 	8E#44)#2	 5  N {--n==Hhm67777r   textc                 :    |                      |g          d         S )zCall out to OCIGenAI's embedding endpoint.

        Args:
            text: The text to embed.

        Returns:
            Embeddings for the text.
        r   )rz   )r^   r{   s     r   embed_queryzOCIGenAIEmbeddings.embed_query   s     ##TF++A..r   )r   r   r   r   r!   r   __annotations__r"   r#   r	   rq   r%   r&   r'   r   r(   r)   r+   r-   intr   model_configr   r\   propertyr   r`   r   floatrz   r}   r   r   r   r    r       s         8 FCNC(Ix}((( #,L(3-+++ #Hhsm"""H#'L(4.'''0&*hsm***$(NHSM(((#Hhsm###XJ :H2FFFLF$ F4 F F F XFP 
WS#X%6 
 
 
 X
#T#Y #4U3D # # # #J	/ 	/U 	/ 	/ 	/ 	/ 	/ 	/r   r    N)enumr   typingr   r   r   r   r   r	   langchain_core.embeddingsr
   langchain_core.utilsr   pydanticr   r   rn   r   r    r   r   r   <module>r      s          ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 0 0 0 0 0 0 ) ) ) ) ) ) * * * * * * * *5     $   E/ E/ E/ E/ E/J E/ E/ E/ E/ E/r   