o
    a+ i                     @  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)Zgoogler   Zmin_secondsZmax_secondsmax_retries r$   v/home/app/PaddleOCR-VL-test/.venv_paddleocr/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.r/   ZGOOGLE_API_KEYr   N)Zapi_keyz4Could not import google.generativeai python package.r+   )r	   Zgoogle.generativeaiZgenerativeai	configureImportError)clsr4   r/   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>)r3   r<   r6   r    warning)rA   r9   r<   Ziter_r$   r@   r%   embed_documentsT   s   z$GooglePalmEmbeddings.embed_documentsr?   List[float]c                 C  s   t | | j|}|d S )zEmbed query text.	embedding)r.   r1   )rA   r?   rF   r$   r$   r%   r=   d   s   z GooglePalmEmbeddings.embed_queryN)r4   r   r   r   )r9   r:   r   r;   )r?   r0   r   rE   )__name__
__module____qualname____doc____annotations__r1   r3   r   Zmodel_configr
   r8   rD   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   	getLoggerrG   r    r&   r.   r(   r$   r$   r$   r%   <module>   s    


