o
    Zha                     @  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Z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                   @  sz   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Z	dddZ
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   clientvaluesr   returnc              
   C  sx   zddl m} W n ty } ztd|d}~ww zt|dd}||d|d< W |S  ty; } ztd	|d}~ww )
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#   \/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/embeddings/premai.pyvalidate_environments#   s(   
z&PremAIEmbeddings.validate_environmentstextList[float]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_querytexts	List[str]List[List[float]]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   s    z4PremAIEmbeddings.embed_documents.<locals>.<listcomp>)r*   r   r   r+   )r-   r0   r.   r#   r#   r$   embed_documents>   s   z PremAIEmbeddings.embed_documents)r   r   r   r   )r&   r   r   r'   )r0   r1   r   r2   )__name__
__module____qualname____doc____annotations__r   r   r   r   r%   r/   r5   r#   r#   r#   r$   r      s"   
 
r   r   r   embedderr   r   r   Callable[[Any], Any]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$   rA   r   r   r   r)   Union[str, List[str]]r   c                 C  s,   t | | jd}|ddd}|| |||dS )z+Using tenacity for retry in embedding callsr;   r<   r   r   r   r   r   r)   rB   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_retryrC   N)
r<   r   r   r   r   r   r)   rB   r   r   )rA   r   )r<   r   r   r)   Zretry_decoratorrE   r#   r#   r$   r*   k   s   r*   )r<   r   r   r   r   r=   )
r<   r   r   r   r   r   r)   rB   r   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   	getLoggerr6   loggerr   rA   r*   r#   r#   r#   r$   <module>   s     
;%