o
    Aga                     @  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   PREMAI_API_KEY)api_keyr   z,Your API Key is incorrect. Please try again.)premair   ImportErrorr   	Exception
ValueError)clsr   r   errorr    r%   l/var/www/html/development/chatbot/venv/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/   r2   r0   r%   r%   r&   embed_documents>   s   z PremAIEmbeddings.embed_documents)r   r   r   r   )r(   r   r   r)   )r2   r3   r   r4   )__name__
__module____qualname____doc____annotations__r   r   r   r   r'   r1   r7   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)error_typesr   run_manager)premai.modelsmodelsapi_response_validation_errorAPIResponseValidationErrorconflict_errorConflictErrormodel_not_found_errorModelNotFoundErrorpermission_denied_errorPermissionDeniedErrorprovider_api_connection_errorProviderAPIConnectionErrorprovider_api_status_errorProviderAPIStatusErrorprovider_api_timeout_errorProviderAPITimeoutErrorprovider_internal_server_errorProviderInternalServerErrorprovider_not_found_errorProviderNotFoundErrorrate_limit_errorRateLimitErrorunprocessable_entity_errorUnprocessableEntityErrorvalidation_errorValidationErrorr
   )r>   r   r   errors	decoratorr%   r%   r&   create_prem_retry_decoratorF   s$   r^   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+   r_   r   r   c                 S  s   | j jj|||d}|S )Nr   r   r+   )r   r0   create)r>   r   r   r+   embedding_responser%   r%   r&   _embed_with_retryv   s   z+embed_with_retry.<locals>._embed_with_retryr`   N)
r>   r   r   r   r   r   r+   r_   r   r   )r^   r   )r>   r   r   r+   retry_decoratorrc   r%   r%   r&   r,   k   s   r,   )r>   r   r   r   r   r?   )
r>   r   r   r   r   r   r+   r_   r   r   )
__future__r   loggingtypingr   r   r   r   r   r   langchain_core.embeddingsr	   #langchain_core.language_models.llmsr
   langchain_core.utilsr   r   pydanticr   r   	getLoggerr8   loggerr   r^   r,   r%   r%   r%   r&   <module>   s     
;%