
    {h                         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y)    )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y)OCIAuthTypez'OCI authentication types as enumerator.            N)__name__
__module____qualname____doc__API_KEYSECURITY_TOKENINSTANCE_PRINCIPALRESOURCE_PRINCIPAL     j/var/www/html/engine/venv/lib/python3.12/site-packages/langchain_community/embeddings/oci_generative_ai.pyr   r      s    1GNr   r   c                   t   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
   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y)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.
    If a specific config file location is used, you must pass
    the file location where profile name configs present
    through auth_file_location

    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_profilez~/.oci/configauth_file_location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(  r9j
                  j                  |d	   |d
         |d<   |j                  dd       n|d   t        d      j                  k(  r;fd}j
                  j                  |d	   |d
         |d<    ||d         |d<   n|d   t        d      j                  k(  r(j                  j                  j                         |d<   nN|d   t        d      j                  k(  r(j                  j                  j                         |d<   nt        d       j                  j                  di ||d<   |S # t        $ r}t        d      |d}~wt         $ r}t        d|      |d}~ww xY w)zBValidate that OCI config and python package exists in environment.r!   Nr   r)   )
      )configsignerr)   retry_strategytimeoutr#   r   r&   r%   )file_locationprofile_namer7   r8   r   c                 (   j                   j                  | j                  d      d       }t        | j                  d      d      5 }|j	                         }d d d        j
                  j                  j                  |      S # 1 sw Y   /xY w)Nkey_filesecurity_token_filezutf-8)encoding)r8   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_signer}   s    >>"z2DB "'<= -$%FFH	- 88++??	2NN	- -s   	BB)rH   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.
                If INSTANCE_PRINCIPAL or RESOURCE_PRINCIPAL is used,
                please check the specified
                auth_profile, auth_file_location and auth_type are valid.r   )rL   retryDEFAULT_RETRY_STRATEGYr   namer7   	from_filepoprE   rF   %InstancePrincipalsSecurityTokenSignerget_resource_principals_signer
ValueErrorgenerative_ai_inferenceGenerativeAiInferenceClientImportError	Exception)clsr2   client_kwargsrM   exerL   s         @r   validate_environmentz'OCIGenAIEmbeddings.validate_environmentb   s   
 ('M?	 $*+=$>"%))"B"B$M k"k!n&9&99*-***>*>"()=">!'!7 +? +h' !!(D1$A(;(;;O +.***>*>"()=">!'!7 +? +h' +E,X6+h' $A(;(;;HH$$JJL h' $A(;(;;HH$$CCE h' !!JKKVs::VV   F8$   	G   	M  	s$   E=F	 		F<FF<*F77F<c                 .    | j                   xs 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                  j	                  t
              r|j                   j                        }n|j                   j                        }g }dt        t        t              f fd} |       D ]k  }|j                  | j                   j                  |      } j                  j                  |      }|j!                  |j"                  j$                         m |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'   r3   c               3      K   t        dt              j                        D ]  } | | j                  z      y w)Nr   )rangelenr.   )ir`   rc   s    r   split_textsz7OCIGenAIEmbeddings.embed_documents.<locals>.split_texts   s=     1c%j$//: 5ADOO 3445s   ;>)serving_moder*   r,   inputs)oci.generative_ai_inferencere   r'   rU   
startswithCUSTOM_ENDPOINT_PREFIXDedicatedServingModeOnDemandServingModer   r   strEmbedTextDetailsr*   r,   r!   
embed_textextenddata
embeddings)	r`   rc   re   rl   rx   rk   chunkinvocation_objresponses	   ``       r   embed_documentsz"OCIGenAIEmbeddings.embed_documents   s     	7}}FGG==##$:;!664==6QL!55t}}5ML
	5Xd3i0 	5 !] 	8E#44)#22	 5 N {{--n=Hhmm667	8 r   textc                 ,    | j                  |g      d   S )zCall out to OCIGenAI's embedding endpoint.

        Args:
            text: The text to embed.

        Returns:
            Embeddings for the text.
        r   )r|   )r`   r}   s     r   embed_queryzOCIGenAIEmbeddings.embed_query   s     ##TF+A..r   ) r   r   r   r   r!   r   __annotations__r"   r#   r	   rs   r%   r&   r'   r(   r   r)   r*   r,   r.   intr   model_configr   r^   propertyr   rb   r   floatr|   r   r   r   r   r    r       sQ   > FCNC(Ix}( #,L(3-+ )87 #Hhsm"H#'L(4.'0&*hsm*$(NHSM(#Hhsm#XJ H2FLH$ H4 H HT 
WS#X%6 
 
#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   rp   r   r    r   r   r   <module>r      s9     ? ? 0 ) *5 $ O/J O/r   