a
    bg                     @   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   ~/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/embeddings/oci_generative_ai.pyr      s
   r   c                   @   s   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eedddZeee
ef dddZee
 eee  dddZe
ee d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              
      s  |d dur|S zddl  i d|d  jjdd}|d tdjkrp jj|d	 |d
 d|d< |dd n|d tdjkr fdd} jj|d	 |d
 d|d< ||d d|d< nR|d tdjkr jj	
 |d< n.|d tdjkr jj	 |d< ntd jjf i ||d< W n` tyX } ztd|W Y d}~n:d}~0  ty } ztd||W Y d}~n
d}~0 0 |S )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 sD0    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:   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   sb    





z'OCIGenAIEmbeddings.validate_environmentr)   c                 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)textsr)   c           	         s   ddl m}  jstd jtr6|j jd}n|j jd}g }tt	t
  d fdd}| D ]4}|j| j j|d	} j|}||jj qj|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    rG   c                  3   s0   t dt jD ]} | |  j  V  qd S )Nr   )rangelenr&   )irH   rJ   r   r   split_texts   s    z7OCIGenAIEmbeddings.embed_documents.<locals>.split_texts)serving_moder#   r$   inputs)Zoci.generative_ai_inferencerK   r    r@   
startswithCUSTOM_ENDPOINT_PREFIXZDedicatedServingModeZOnDemandServingModer   r   strZEmbedTextDetailsr#   r$   r   Z
embed_textextenddata
embeddings)	rH   rJ   rK   rQ   rX   rP   chunkZinvocation_objresponser   rO   r   embed_documents   s$    	
z"OCIGenAIEmbeddings.embed_documents)textr)   c                 C   s   |  |gd S )zCall out to OCIGenAI's embedding endpoint.

        Args:
            text: The text to embed.

        Returns:
            Embeddings for the text.
        r   )r[   )rH   r\   r   r   r   embed_query   s    	zOCIGenAIEmbeddings.embed_query) r   r   r   r   r   r   __annotations__r   r   r   rU   r   r   r    r!   r   r"   r#   r$   r&   intr   Zmodel_configr
   rF   propertyr   rI   r   floatr[   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   rT   r   r   r   r   r   r   <module>   s    	