
    ci3                     (   d dl Z d dlZd dlmZmZmZ d dlmZmZ d dl	Z
d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d	d
lmZ er$d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d d
lmZ  dZ!de"e#e#f   ddfdZ$e$e_%         G d d      Z& G d d      Z'y)    N)OptionalUnionTYPE_CHECKING)TracebackType
ModuleType)version)_api_client)_common)client)types   )live)agent_engines)datasets)evals)prompt_optimizer)promptszvertex-genai-modulesheadersreturnc                     t          dt        j                   }| j                  dd      }||vr| d| j	                         | d<   | j                  dd      }||vr| d| j	                         | d<   yy)zJAppends Vertex Gen AI modules tracking information to the request headers./z
user-agent  zx-goog-api-clientN)_GENAI_MODULES_TELEMETRY_HEADERaip_version__version__getstrip)r   tracking_label
user_agent
api_clients       =/tmp/pip-target-z3e9_cxr/lib/python/vertexai/_genai/client.py_add_tracking_headersr#   -   s     88+:Q:Q9RSN\2.JZ'#-,a/? @ F F H0"5JZ'*4Q~6F'G'M'M'O#$ (    c                   2   e Zd ZdZdej
                  fdZe ej                  d      dd              Z
edd       Zedd       Zedd	       Zedd
       Ze ej                  d      dd              ZddZddZdee   dee   dee   ddfdZddZy)AsyncClientz'Async Gen AI Client for the Vertex SDK.r!   c                     || _         t        j                  | j                         | _        d | _        d | _        d | _        d | _        d | _        y N)	r	   r   	AsyncLive_live_evals_agent_engines_prompt_optimizer_prompts	_datasets)selfr!   s     r"   __init__zAsyncClient.__init__A   sE    %^^D$4$45
,0487;.2/3r$   zTThe Vertex SDK GenAI live module is experimental, and may change in future versions.r   c                     | j                   S r(   )r*   r0   s    r"   r   zAsyncClient.liveJ   s     zzr$   c                     | j                    	 t        j                  dt              | _         | j                   j                  | j                        S # t        $ r}t	        d      |d }~ww xY w)N.evalszzThe 'evals' module requires 'pandas' and 'tqdm'. Please install them using pip install google-cloud-aiplatform[evaluation])r+   	importlibimport_module__package__ImportError
AsyncEvalsr	   r0   es     r"   r   zAsyncClient.evalsR   sm    ;;	 (55hL {{%%d&6&677  !: 	   A 	A,A''A,c                     | j                   t        j                  dt              | _         | j                   j	                  | j
                        S Nz.prompt_optimizer)r-   r6   r7   r8   AsyncPromptOptimizerr	   r3   s    r"   r   zAsyncClient.prompt_optimizera   sE    !!)%.%<%<#[&D" %%::4;K;KLLr$   c                     | j                    	 t        j                  dt              | _         | j                   j                  | j                        S # t        $ r}t	        d      |d }~ww xY wNz.agent_engineszThe 'agent_engines' module requires 'additional packages'. Please install them using pip install google-cloud-aiplatform[agent_engines])r,   r6   r7   r8   r9   AsyncAgentEnginesr	   r;   s     r"   r   zAsyncClient.agent_enginesi   sv    & '0&=&=$'# ""44T5E5EFF  != 	r=   c                     | j                   t        j                  dt              | _         | j                   j	                  | j
                        S Nz.prompts)r.   r6   r7   r8   AsyncPromptsr	   r3   s    r"   r   zAsyncClient.prompts{   sA    == %33DM }}))$*:*:;;r$   z^The Vertex SDK GenAI async datasets module is experimental, and may change in future versions.c                     | j                   t        j                  dt              | _         | j                   j	                  | j
                        S Nz	.datasets)r/   r6   r7   r8   AsyncDatasetsr	   r3   s    r"   r   zAsyncClient.datasets   sC     >>!&44DN ~~++D,<,<==r$   Nc                 T   K   | j                   j                          d{    y7 w)a  Closes the async client explicitly.

        Example usage:

        from vertexai import Client

        async_client = vertexai.Client(
            project='my-project-id', location='us-central1'
        ).aio
        prompt_1 = await async_client.prompts.create(...)
        prompt_2 = await async_client.prompts.create(...)
        # Close the client to release resources.
        await async_client.aclose()
        N)r	   acloser3   s    r"   rK   zAsyncClient.aclose   s       %%'''s   (&(c                    K   | S wr(    r3   s    r"   
__aenter__zAsyncClient.__aenter__   s     s   exc_type	exc_value	tracebackc                 @   K   | j                          d {    y 7 wr(   )rK   )r0   rO   rP   rQ   s       r"   	__aexit__zAsyncClient.__aexit__   s      kkms   c                     	 t        j                         j                  | j                                y # t        $ r Y y w xY wr(   )asyncioget_running_loopcreate_taskrK   	Exceptionr3   s    r"   __del__zAsyncClient.__del__   s6    	$$&224;;=A 		s   14 	A A )r   zlive_module.AsyncLive)r   zevals_module.AsyncEvals)r   z,prompt_optimizer_module.AsyncPromptOptimizer)r   z&agent_engines_module.AsyncAgentEngines)r   zprompts_module.AsyncPrompts)r   zdatasets_module.AsyncDatasets)r   Nr   r&   )__name__
__module____qualname____doc__genai_clientBaseApiClientr1   propertyr
   experimental_warningr   r   r   r   r   r   rK   rN   r   rX   r   rS   rY   rM   r$   r"   r&   r&   >   s   14<#=#= 4 !W!!		 
 8 8 M M G G" < < !W!!	->	 
>("9% I& M*	
 
r$   r&   c                   Z   e Zd ZdZddddddddee   deej                  j                  j                     dee   dee   dee
j                     d	eeej                  ej                  f      fd
Zedd       Zedd       Zedd       Ze	 	 	 	 	 	 ddee   deej                  j                  j                     dee   dee   dee
j                     d	eej                     dee
j,                     fd       Zedd       Zedd       Ze ej6                  d      dd              Zy)ClientzmGen AI Client for the Vertex SDK.

    Use this client to interact with Vertex-specific Gemini features.
    Napi_keycredentialsprojectlocationdebug_confighttp_optionsrf   rg   rh   ri   rj   rk   c          	      l   |xs t        j                         | _        t        |t              rt        j                  di |}t         j                  j                  d||||| j                  |      | _	        t        | j                        | _        d| _        d| _        d| _        d| _        d| _        y)a  Initializes the client.

        Args:
           api_key (str): The `API key
           <https://cloud.google.com/vertex-ai/generative-ai/docs/start/express-mode/overview#api-keys>`_
             to use for authentication. Applies to Vertex AI in express mode only.
           credentials (google.auth.credentials.Credentials): The credentials to use
             for authentication when calling the Vertex AI APIs. Credentials can be
             obtained from environment variables and default credentials. For more
             information, see `Set up Application Default Credentials
             <https://cloud.google.com/docs/authentication/provide-credentials-adc>`_.
           project (str): The `Google Cloud project ID
             <https://cloud.google.com/vertex-ai/docs/start/cloud-environment>`_ to
             use for quota. Can be obtained from environment variables (for example,
             ``GOOGLE_CLOUD_PROJECT``).
           location (str): The `location
             <https://cloud.google.com/vertex-ai/generative-ai/docs/learn/locations>`_
             to send API requests to (for example, ``us-central1``). Can be obtained
             from environment variables.
           debug_config (DebugConfig): Config settings that control network behavior
             of the client. This is typically used when running test code.
           http_options (Union[HttpOptions, HttpOptionsDict]): Http options to use
             for the client.
        T)vertexairf   rg   rh   ri   rj   rk   NrM   )r_   DebugConfig_debug_config
isinstancedictr   HttpOptionsrd   _get_api_clientr	   r&   _aior+   r-   r,   r.   r/   )r0   rf   rg   rh   ri   rj   rk   s          r"   r1   zClient.__init__   s    F *G\-E-E-GlD) ,,<|<L'..>>#++% ? 
   0 01	,07;48.2/3r$   r   c                     | j                    	 t        j                  dt              | _         | j                   j                  | j                        S # t        $ r}t	        d      |d }~ww xY w)Nr5   z~The 'evals' module requires additional dependencies. Please install them using pip install google-cloud-aiplatform[evaluation])r+   r6   r7   r8   r9   Evalsr	   r;   s     r"   r   zClient.evals   sm    ;;	 (55hL {{  !1!122  !: 	r=   c                     | j                   t        j                  dt              | _         | j                   j	                  | j
                        S r?   )r-   r6   r7   r8   PromptOptimizerr	   r3   s    r"   r   zClient.prompt_optimizer   sE    !!)%.%<%<#[&D" %%55d6F6FGGr$   c                     | j                   S r(   )rt   r3   s    r"   aioz
Client.aio  s    yyr$   c                     |rJ|j                   dv r<t        j                  |j                   |j                  |j                  d| ||||	      S y )N)recordreplayautoT)	mode	replay_idreplays_directoryrm   rf   rg   rh   ri   rk   )client_moder_   ReplayApiClientr   r   re   s         r"   rs   zClient._get_api_client  s`     L44 9
 

  //!--&00"."@"@'!)
 
 r$   c                     | j                    	 t        j                  dt              | _         | j                   j                  | j                        S # t        $ r}t	        d      |d }~ww xY wrB   )r,   r6   r7   r8   r9   AgentEnginesr	   r;   s     r"   r   zClient.agent_engines(  sv    & '0&=&=$'# ""//0@0@AA  != 	r=   c                     | j                   t        j                  dt              | _         | j                   j	                  | j
                        S rE   )r.   r6   r7   r8   Promptsr	   r3   s    r"   r   zClient.prompts:  sA    == %33DM }}$$T%5%566r$   zXThe Vertex SDK GenAI datasets module is experimental, and may change in future versions.c                     | j                   t        j                  dt              | _         | j                   j	                  | j
                        S rH   )r/   r6   r7   r8   Datasetsr	   r3   s    r"   r   zClient.datasetsD  sC     >>!&44DN ~~&&t'7'788r$   )r   zevals_module.Evals)r   z'prompt_optimizer_module.PromptOptimizerrZ   )NNNNNN)r   z!agent_engines_module.AgentEngines)r   zprompts_module.Prompts)r   zdatasets_module.Datasets)r[   r\   r]   r^   r   strgoogleauthrg   Credentialsr_   rn   r   r   rr   HttpOptionsDictr1   ra   r   r   rz   staticmethodr`   rs   r   r   r
   rb   r   rM   r$   r"   rd   rd      s    "&EI!%"&;?RV54 #54 fkk55AAB	54
 #54 3-54 |77854 uU%6%68M8M%MNO54n 3 3 H H   !%EI!%"&;?48#fkk55AAB # 3-	
 |778 u001 
,,,	- 4 B B" 7 7 !W!!	-9	 
9r$   rd   )(rU   r6   typingr   r   r   r   r   r   google.authr   google.cloud.aiplatformr   r   google.genair	   r
   r   r_   r   r   vertexai._genair   agent_engines_moduler   datasets_moduler   evals_moduler   prompt_optimizer_moduler   prompts_modulelive_moduler   rq   r   r#   append_library_version_headersr&   rd   rM   r$   r"   <module>r      s       1 1 +  : $   /   <5 :3 #9 P4S> Pd P .C *s sl[9 [9r$   