
    hP                         d dl mZ d dlmZ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 erd dlZdZ G d d	e      Z G d
 dee      Zy)    )Enum)TYPE_CHECKINGAnyDictIteratorListMappingOptional)
Embeddings)pre_init)	BaseModel
ConfigDictNz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     n/var/www/html/eduruby.in/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(  rQdt        t        t        f   ddf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_namer8   r9   r   
oci_configr4   z$oci.auth.signers.SecurityTokenSignerc                 :   j                   j                  | j                  d      d       }t        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)	r9   load_private_key_from_filegetopenstrreadauthsignersSecurityTokenSigner)r>   pkf	st_stringocis       r   make_security_token_signerzKOCIGenAIEmbeddings.validate_environment.<locals>.make_security_token_signer   s     >>"z2DB JNN+@ABW -$%FFH	- 88++??	2NN	- -s   BB)r>   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   )rN   retryDEFAULT_RETRY_STRATEGYr   namer8   	from_filepopdictrF   r   rH   rI   %InstancePrincipalsSecurityTokenSignerget_resource_principals_signer
ValueErrorgenerative_ai_inferenceGenerativeAiInferenceClientImportError	Exception)clsr3   client_kwargsrO   exerN   s         @r   validate_environmentz'OCIGenAIEmbeddings.validate_environmente   s    
 ('MA	 $*+=$>"%))"B"B$M k"k!n&9&99*-***>*>"()=">!'!7 +? +h' !!(D1$A(;(;;
O $S#X
O;
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$   FF 	G(F44G GG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(   r4   c               3      K   t        dt              j                        D ]  } | | j                  z      y w)Nr   )rangelenr/   )irc   rf   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_inferencerh   r(   rX   
startswithCUSTOM_ENDPOINT_PREFIXDedicatedServingModeOnDemandServingModer   r   rF   EmbedTextDetailsr+   r-   r"   
embed_textextenddata
embeddings)	rc   rf   rh   ro   rz   rn   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~   )rc   r   s     r   embed_queryzOCIGenAIEmbeddings.embed_query   s     ##TF+A..r   ) r   r   r   r   r"   r   __annotations__r#   r$   r
   rF   r&   r'   r(   r)   r   r*   r+   r-   r/   intr   model_configr   ra   propertyr	   re   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J$ J4 J JX 
WS#X%6 
 
#T#Y #4U3D #J	/ 	/U 	/r   r!   )enumr   typingr   r   r   r   r   r	   r
   langchain_core.embeddingsr   langchain_core.utilsr   pydanticr   r   rN   rs   r   r!   r   r   r   <module>r      sB     N N N 0 ) *5 $ Q/J Q/r   