o
    Zh                     @   s~   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 deZG d	d
 d
ee
ZdS )    )Enum)AnyDictIteratorListMappingOptional)
Embeddings)pre_init)	BaseModel
ConfigDictzocid1.generativeaiendpointc                   @   s    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_KEYZSECURITY_TOKENZINSTANCE_PRINCIPALZRESOURCE_PRINCIPAL r   r   g/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/embeddings/oci_generative_ai.pyr      s    r   c                   @   s8  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dZededefddZedee
ef fddZdee
 deee  fddZde
dee fd 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.
    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_idZENDtruncate`   
batch_sizeZforbidr   )extraZprotected_namespacesvaluesreturnc              
      sl  |d dur|S zddl  i d|d  jjdd}|d tdjkr7 jj|d	 |d
 d|d< |dd nO|d tdjkr^ fdd} jj|d	 |d
 d|d< ||d d|d< n(|d tdjkrp jj	
 |d< n|d tdjkr jj	 |d< ntd jjdi ||d< W |S  ty } ztd|d}~w ty } ztd||d}~ww )zBValidate that OCI config and python package exists in environment.r   Nr   r"   )
      )configsignerr"   Zretry_strategytimeoutr   r   r   r   )Zfile_locationZprofile_namer,   r-   r   c                    s^    j | dd }t| ddd}| }W d    n1 s"w   Y   jj||S )Nkey_fileZsecurity_token_filezutf-8)encoding)r-   Zload_private_key_from_filegetopenreadauthsignersZSecurityTokenSigner)
oci_configpkfZ	st_stringocir   r   make_security_token_signer}   s   


zKOCIGenAIEmbeddings.validate_environment.<locals>.make_security_token_signer)r6   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   )r:   retryZDEFAULT_RETRY_STRATEGYr   namer,   	from_filepopr4   r5   Z%InstancePrincipalsSecurityTokenSignerZget_resource_principals_signer
ValueErrorZgenerative_ai_inferenceZGenerativeAiInferenceClientImportError	Exception)clsr(   Zclient_kwargsr;   exer   r9   r   validate_environmentb   sh   





z'OCIGenAIEmbeddings.validate_environmentc                 C   s   | j pi }i d|iS )zGet the identifying parameters.r!   )r!   )selfZ_model_kwargsr   r   r   _identifying_params   s   
z&OCIGenAIEmbeddings._identifying_paramstextsc           	         s   ddl m}  jstd jtr|j jd}n|j jd}g }dtt	t
  f fdd}| D ]}|j| j j|d	} j|}||jj q5|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)Zendpoint_id)r    r)   c                  3   s2    t dt jD ]} | |  j  V  q
d S )Nr   )rangelenr&   )irG   rI   r   r   split_texts   s   z7OCIGenAIEmbeddings.embed_documents.<locals>.split_texts)serving_moder#   r$   Zinputs)Zoci.generative_ai_inferencerJ   r    r@   
startswithCUSTOM_ENDPOINT_PREFIXZDedicatedServingModeZOnDemandServingModer   r   strZEmbedTextDetailsr#   r$   r   Z
embed_textextenddata
embeddings)	rG   rI   rJ   rP   rV   rO   chunkZinvocation_objresponser   rN   r   embed_documents   s$   	
z"OCIGenAIEmbeddings.embed_documentstextc                 C   s   |  |gd S )zCall out to OCIGenAI's embedding endpoint.

        Args:
            text: The text to embed.

        Returns:
            Embeddings for the text.
        r   )rY   )rG   rZ   r   r   r   embed_query   s   	zOCIGenAIEmbeddings.embed_query) r   r   r   r   r   r   __annotations__r   r   r   rS   r   r   r    r!   r   r"   r#   r$   r&   intr   Zmodel_configr
   rF   propertyr   rH   r   floatrY   r[   r   r   r   r   r      s:   
 
J%r   N)enumr   typingr   r   r   r   r   r   Zlangchain_core.embeddingsr	   Zlangchain_core.utilsr
   Zpydanticr   r   rR   r   r   r   r   r   r   <module>   s     	