
    Ng                     ^   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rddlmZ ddlmZ ddlmZ d	d
ddededeee
ef                  deegef         fdZddedd
fdZ	 	 	 d dee         dee         ded         dd
fdZd!dee         ddfdZd!dedee         ddf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                    ddl }|j        j        j        |j        j        j        |j        j        j        |j        j        j        |j        j        j        g}t          |||          }|S )z0Create a retry decorator for Vertex / Palm LLMs.r   N)error_typesr   r   )	google.api_coreapi_core
exceptionsResourceExhaustedServiceUnavailableAbortedDeadlineExceededGoogleAPIErrorr   )r   r   r   googleerrors	decorators         b/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/utilities/vertexai.pycreate_retry_decoratorr%      su      	"4"5"*"3"1F ,  I     1.38.0minimum_expected_versionc                 &    t          d|            )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>=)ImportError)r(   s    r$   raise_vertex_import_errorr+   *   s+     	K0H	K 	K  r&   projectlocationcredentialsr   c                 ~    	 ddl }n# t          $ r t                       Y nw xY w|                    | ||           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.   r0   s       r$   init_vertexair2   8   so    "$ $ $ $!#####$ MM      s    ""moduler   c                     	 ddl m} n"# t          $ r}t          d          |d}~ww xY wt          j        d          }| r| d|  n|} ||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-coreN	langchain-z
langchain/)client_library_version
user_agent)$google.api_core.gapic_v1.client_infor   r*   r   version)r3   r   exclangchain_versionr7   s        r$   get_client_infor=   U   s    CCCCCCC   *
 
 	 !(55+1H''v'''7H  :58 688   s   	 
(#(pathr   c           
         	 ddl m} n# t          $ r t          d          w xY wddlm} |                    |          }|                     d          }t          |                    |d         d	                    |dd	                   
                    }t          |          dk    rt          d|  d           |j        |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 !)google.cloudr@   r*   "vertexai.preview.generative_modelsr   Clientsplitlist
list_blobsjoinlen
ValueError
from_bytesdownload_as_bytes)r>   r,   r@   r   
gcs_clientpiecesblobss          r$   load_image_from_gcsrT   p   s   S((((((( S S SQRRRS88888800JZZ__F&&vay&*9M9M&NNOOE
5zzA~~EdEEEFFF5E!H6688999s   	 #)r'   )NNN)N)__doc__	importlibr   typingr   r   r   r   r   langchain_core.callbacksr	   r
   #langchain_core.language_models.llmsr   r   r9   r   google.auth.credentialsr   rG   r   intr%   strr+   r2   r=   rT    r&   r$   <module>r^      s   " "       @ @ @ @ @ @ @ @ @ @ @ @ @ @        U T T T T T T T 9??????333333888888  	  	  +-EEF	 seSj   0  4     ""+/ c]sm -( 
	   : HSM \    6: :c :HSM :W : : : : : :r&   