a
    bg                     @   s  d Z ddlmZ ddlmZmZmZmZmZ ddl	m
Z
mZ ddlmZmZ ertddlmZ ddlmZ ddlmZ d	d
deeeee
ef  eegef dddZded
dddZd ee ee ed d
dddZd!ee ddddZd"eee ddddZd
S )#zUtilities to init Vertex AI.    )metadata)TYPE_CHECKINGAnyCallableOptionalUnion)AsyncCallbackManagerForLLMRunCallbackManagerForLLMRun)BaseLLMcreate_base_retry_decorator
ClientInfo)CredentialsImage   N)max_retriesrun_manager)llmr   r   returnc                C   sF   ddl }|jjj|jjj|jjj|jjj|jjjg}t|||d}|S )z0Create a retry decorator for Vertex / Palm LLMs.r   N)Zerror_typesr   r   )	Zgoogle.api_coreZapi_core
exceptionsZResourceExhaustedZServiceUnavailableZAbortedZDeadlineExceededZGoogleAPIErrorr   )r   r   r   Zgoogleerrors	decorator r   t/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/utilities/vertexai.pycreate_retry_decorator   s    	r   1.38.0)minimum_expected_versionr   c                 C   s   t d|  dS )zRaise ImportError related to Vertex SDK being not available.

    Args:
        minimum_expected_version: The lowest expected version of the SDK.
    Raises:
        ImportError: an ImportError that mentions a required version of the SDK.
    zePlease, install or upgrade the google-cloud-aiplatform library: pip install google-cloud-aiplatform>=N)ImportError)r   r   r   r   raise_vertex_import_error*   s
    r   r   )projectlocationcredentialsr   c                 C   s:   zddl }W n ty$   t  Y n0 |j| ||d dS )a  Init Vertex AI.

    Args:
        project: The default GCP project to use when making Vertex API calls.
        location: The default location to use when making API calls.
        credentials: The default custom
            credentials to use when making API calls. If not provided credentials
            will be ascertained from the environment.

    Raises:
        ImportError: If importing vertexai SDK did not succeed.
    r   N)r    r!   r"   )vertexair   r   init)r    r!   r"   r#   r   r   r   init_vertexai8   s    r%   r   )moduler   c              
   C   sr   zddl m} W n. ty> } ztd|W Y d}~n
d}~0 0 td}| r\| d|  n|}||d| dS )	zReturn a custom user agent header.

    Args:
        module (Optional[str]):
            Optional. The module for a custom user agent header.
    Returns:
        google.api_core.gapic_v1.client_info.ClientInfo
    r   r   zPCould not import ClientInfo. Please, install it with pip install google-api-coreNZ	langchain-z
langchain/)client_library_version
user_agent)$google.api_core.gapic_v1.client_infor   r   r   version)r&   r   excZlangchain_versionr(   r   r   r   get_client_infoU   s    	
r-   r   )pathr    r   c                 C   s   zddl m} W n ty*   tdY n0 ddlm} |j|d}| d}t|j|d d	|dd	 d
}t
|dkrtd|  d||d  S )z(Load an image from Google Cloud Storage.r   )storagez5Could not import google-cloud-storage python package.r   )r    /      N)prefixr   z"Found more than one candidate for !)Zgoogle.cloudr/   r   "vertexai.preview.generative_modelsr   ZClientsplitlistZ
list_blobsjoinlen
ValueError
from_bytesZdownload_as_bytes)r.   r    r/   r   Z
gcs_clientpiecesZblobsr   r   r   load_image_from_gcsp   s    
$r=   )r   )NNN)N)N)__doc__	importlibr   typingr   r   r   r   r   Zlangchain_core.callbacksr   r	   Z#langchain_core.language_models.llmsr
   r   r*   r   Zgoogle.auth.credentialsr   r5   r   intr   strr   r%   r-   r=   r   r   r   r   <module>   s<   
   