U
    h                     @  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 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
dZdddddddZG dd dee
ZdS )    )annotationsN)AnyCallableDictListOptional)
Embeddings)	BaseModel)get_from_dict_or_envpre_init)before_sleep_logretryretry_if_exception_typestop_after_attemptwait_exponentialzCallable[[Any], Any])returnc                  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   Z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!   N/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/embeddings/google_palm.py_create_retry_decorator   s     
r#   GooglePalmEmbeddingsr   )
embeddingsargskwargsr   c                   s*   t  }|dddd fdd}|||S )z*Use tenacity to retry the completion call.r   )r&   r'   r   c                    s    j j| |S )N)clientZgenerate_embeddings)r&   r'   r%   r!   r"   _embed_with_retry0   s    z+embed_with_retry.<locals>._embed_with_retry)r#   )r%   r&   r'   Zretry_decoratorr*   r!   r)   r"   embed_with_retry*   s    r+   c                   @  sn   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dddZdddddZ	dd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   )valuesr   c                 C  sR   t |dd}zddlm} |j|d W n tk
rD   tdY nX ||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)clsr1   r,   Zgenair!   r!   r"   validate_environmentA   s      z)GooglePalmEmbeddings.validate_environmentz	List[str]zList[List[float]])textsr   c                   s^    j rHzddlm} ||dd}W qL tk
rD   td |}Y qLX n|} fdd|D S )Nr   )tqdmr$   )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>`   s     z8GooglePalmEmbeddings.embed_documents.<locals>.<listcomp>)r0   r7   r3   r   warning)r=   r6   r7   Ziter_r!   r<   r"   embed_documentsR   s    z$GooglePalmEmbeddings.embed_documentszList[float])r;   r   c                 C  s   t | | j|}|d S )zEmbed query text.	embedding)r+   r.   )r=   r;   rA   r!   r!   r"   r9   b   s    z GooglePalmEmbeddings.embed_queryN)__name__
__module____qualname____doc____annotations__r.   r0   r   r5   r@   r9   r!   r!   r!   r"   r$   7   s   
)
__future__r   r   typingr   r   r   r   r   Zlangchain_core.embeddingsr   Zlangchain_core.pydantic_v1r	   Zlangchain_core.utilsr
   r   Ztenacityr   r   r   r   r   	getLoggerrB   r   r#   r+   r$   r!   r!   r!   r"   <module>   s   
