o
    Zh                     @  s   d dl mZ d dlZd dlmZmZmZmZmZ d dl	m
Z
 d dlmZmZ d dlmZmZ d dlmZmZmZmZmZ eeZdd
dZdddZG dd dee
ZdS )    )annotationsN)AnyCallableDictListOptional)
Embeddings)get_from_dict_or_envpre_init)	BaseModel
ConfigDict)before_sleep_logretryretry_if_exception_typestop_after_attemptwait_exponentialreturnCallable[[Any], Any]c                  C  sf   ddl } d}d}d}d}tdt|t|||dt| jjjt| jjjB t| jjj	B t
ttjd	S )
zKReturns a tenacity retry decorator, preconfigured to handle PaLM exceptionsr   N      <   
   T)
multiplierminmax)reraisestopwaitr   before_sleep)Zgoogle.api_core.exceptionsr   r   r   r   Zapi_core
exceptionsZResourceExhaustedZServiceUnavailableZGoogleAPIErrorr   loggerloggingWARNING)googler   Zmin_secondsZmax_secondsmax_retries r%   a/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/embeddings/google_palm.py_create_retry_decorator   s    
r'   
embeddingsGooglePalmEmbeddingsargsr   kwargsc                   s&   t  }|d fdd}||i |S )	z*Use tenacity to retry the completion call.r*   r   r+   r   c                    s    j j| i |S )N)clientZgenerate_embeddings)r*   r+   r(   r%   r&   _embed_with_retry0   s   z+embed_with_retry.<locals>._embed_with_retryN)r*   r   r+   r   r   r   )r'   )r(   r*   r+   Zretry_decoratorr.   r%   r-   r&   embed_with_retry*   s   r/   c                   @  sj   e Zd ZU dZded< ded< dZded< 	 d	Zd
ed< 	 eddZe	dddZ
dddZdddZdS ) r)   zGoogle's PaLM Embeddings APIs.r   r,   zOptional[str]google_api_keyzmodels/embedding-gecko-001str
model_nameFboolshow_progress_barr%   )Zprotected_namespacesvaluesr   r   c                 C  sL   t |dd}zddlm} |j|d W n ty   tdw ||d< |S )z(Validate api key, python package exists.r0   ZGOOGLE_API_KEYr   N)Zapi_keyz4Could not import google.generativeai python package.r,   )r	   Zgoogle.generativeaiZgenerativeai	configureImportError)clsr5   r0   Zgenair%   r%   r&   validate_environmentC   s   z)GooglePalmEmbeddings.validate_environmenttexts	List[str]List[List[float]]c                   sZ    j r"zddlm} ||dd}W n ty!   td |}Y nw |} fdd|D S )Nr   )tqdmr)   )ZdesczgUnable to show progress bar because tqdm could not be imported. Please install with `pip install tqdm`.c                   s   g | ]}  |qS r%   )embed_query).0textselfr%   r&   
<listcomp>b   s    z8GooglePalmEmbeddings.embed_documents.<locals>.<listcomp>)r4   r=   r7   r    warning)rB   r:   r=   Ziter_r%   rA   r&   embed_documentsT   s   z$GooglePalmEmbeddings.embed_documentsr@   List[float]c                 C  s   t | | j|}|d S )zEmbed query text.	embedding)r/   r2   )rB   r@   rG   r%   r%   r&   r>   d   s   z GooglePalmEmbeddings.embed_queryN)r5   r   r   r   )r:   r;   r   r<   )r@   r1   r   rF   )__name__
__module____qualname____doc____annotations__r2   r4   r   Zmodel_configr
   r9   rE   r>   r%   r%   r%   r&   r)   7   s   
 

)r   r   )r(   r)   r*   r   r+   r   r   r   )
__future__r   r!   typingr   r   r   r   r   Zlangchain_core.embeddingsr   Zlangchain_core.utilsr	   r
   Zpydanticr   r   tenacityr   r   r   r   r   	getLoggerrH   r    r'   r/   r)   r%   r%   r%   r&   <module>   s    


