a
    bga                     @  s   d dl mZ d dl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 dlmZmZ eeZG dd	 d	eeZd
dd	dddddZd	dddddddZdS )    )annotationsN)AnyCallableDictListOptionalUnion)
Embeddings)create_base_retry_decorator)get_from_dict_or_envpre_init)	BaseModel	SecretStrc                   @  s   e Zd ZU dZded< dZded< ded< d	Zd
ed< dZded< ded< edddddZ	dddddZ
dddddZdS )PremAIEmbeddingszPrem's Embedding APIsint
project_idNzOptional[SecretStr]premai_api_keystrmodelFboolshow_progress_bar   max_retriesr   clientr   )valuesreturnc              
   C  s   zddl m} W n. ty> } ztd|W Y d}~n
d}~0 0 zt|dd}||d|d< W n. ty } ztd	|W Y d}~n
d}~0 0 |S )
zFValidate that the package is installed and that the API token is validr   )PremzQCould not import Prem Python package.Please install it with: `pip install premai`Nr   ZPREMAI_API_KEY)Zapi_keyr   z,Your API Key is incorrect. Please try again.)premair   ImportErrorr   	Exception
ValueError)clsr   r   errorr    r#   s/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/embeddings/premai.pyvalidate_environments#   s      z&PremAIEmbeddings.validate_environmentszList[float])textr   c                 C  s    t | | j| j|d}|jd jS )zEmbed query textr   r   inputr   )embed_with_retryr   r   data	embedding)selfr&   
embeddingsr#   r#   r$   embed_query7   s    zPremAIEmbeddings.embed_queryz	List[str]zList[List[float]])textsr   c                 C  s$   t | | j| j|dj}dd |D S )Nr'   c                 S  s   g | ]
}|j qS r#   )r+   ).0r+   r#   r#   r$   
<listcomp>C       z4PremAIEmbeddings.embed_documents.<locals>.<listcomp>)r)   r   r   r*   )r,   r/   r-   r#   r#   r$   embed_documents>   s    z PremAIEmbeddings.embed_documents)__name__
__module____qualname____doc____annotations__r   r   r   r   r%   r.   r3   r#   r#   r#   r$   r      s   
r   r   r   r   zCallable[[Any], Any])embedderr   r   c                C  s~   ddl }|jjj|jjj|jjj|jjj	|jj
j|jjj|jjj|jjj|jjj|jjj|jjj|jjjg}t||dd}|S )zCreate a retry decorator for PremAIEmbeddings.

    Args:
        embedder (PremAIEmbeddings): The PremAIEmbeddings instance
        max_retries (int): The maximum number of retries

    Returns:
        Callable[[Any], Any]: The retry decorator
    r   N)Zerror_typesr   Zrun_manager)Zpremai.modelsmodelsZapi_response_validation_errorZAPIResponseValidationErrorZconflict_errorZConflictErrorZmodel_not_found_errorZModelNotFoundErrorZpermission_denied_errorZPermissionDeniedErrorZprovider_api_connection_errorZProviderAPIConnectionErrorZprovider_api_status_errorZProviderAPIStatusErrorZprovider_api_timeout_errorZProviderAPITimeoutErrorZprovider_internal_server_errorZProviderInternalServerErrorZprovider_not_found_errorZProviderNotFoundErrorZrate_limit_errorZRateLimitErrorZunprocessable_entity_errorZUnprocessableEntityErrorZvalidation_errorZValidationErrorr
   )r:   r   r   errors	decoratorr#   r#   r$   create_prem_retry_decoratorF   s$    r>   r   Union[str, List[str]]r   )r:   r   r   r(   r   c                 C  s8   t | | jd}|dddddddd	}|| |||d
S )z+Using tenacity for retry in embedding callsr9   r   r   r   r?   r   )r:   r   r   r(   r   c                 S  s   | j jj|||d}|S )Nr   r   r(   )r   r-   create)r:   r   r   r(   Zembedding_responser#   r#   r$   _embed_with_retryv   s    z+embed_with_retry.<locals>._embed_with_retryr@   )r>   r   )r:   r   r   r(   Zretry_decoratorrB   r#   r#   r$   r)   k   s    r)   )
__future__r   loggingtypingr   r   r   r   r   r   Zlangchain_core.embeddingsr	   Z#langchain_core.language_models.llmsr
   Zlangchain_core.utilsr   r   Zpydanticr   r   	getLoggerr4   loggerr   r>   r)   r#   r#   r#   r$   <module>   s    
;%