
    qi.                     t   d Z ddlZddlZddlmZ dej
                  d<   ddlmZ ddlm	Z	 ddl
mZ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dlmZ ddlmZ e G d de             Z G d de      Z y# e$ r7Z e	j:                  de         e	j:                  d        ede       dZ[ww xY w)zGoogle Vertex AI LLM service implementation.

This module provides integration with Google's AI models via Vertex AI,
extending the GoogleLLMService with Vertex AI authentication.
    N)	dataclassfalseGRPC_ENABLE_FORK_SUPPORT)Optional)logger)GoogleLLMServiceGoogleLLMSettings)_warn_deprecated_param)default)GoogleAuthError)Request)Client)HttpOptions)service_accountzException: zIn order to use Google AI, you need to `pip install pipecat-ai[google]`. Also, set `GOOGLE_APPLICATION_CREDENTIALS` environment variable.zMissing module: c                       e Zd ZdZy)GoogleVertexLLMSettingsz$Settings for GoogleVertexLLMService.N)__name__
__module____qualname____doc__     T/opt/pipecat/venv/lib/python3.12/site-packages/pipecat/services/google/vertex/llm.pyr   r   +   s    .r   r   c                   &    e Zd ZU dZeZeed<    G d dej                        Zddddddddddddde	e
   de	e
   d	e	e
   d
e	e
   de	e
   de	ej                     de	e   de	e
   de	e   de	e   de	e   f fdZd Zede	e
   de	e
   fd       Z xZS )GoogleVertexLLMServicea  Google Vertex AI LLM service extending GoogleLLMService.

    Provides access to Google's AI models via Vertex AI while using the same
    Google AI client and message format as GoogleLLMService. Handles authentication
    using Google service account credentials and configures the client for
    Vertex AI endpoints.

    Reference:
        https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/inference
    	_settingsc                   L     e Zd ZU dZdZee   ed<   dZee   ed<    fdZ	 xZ
S )"GoogleVertexLLMService.InputParamsa  Input parameters specific to Vertex AI.

        Parameters:
            location: GCP region for Vertex AI endpoint (e.g., "us-east4").

                .. deprecated:: 0.0.90
                    Use `location` as a direct argument to
                    `GoogleVertexLLMService.__init__()` instead.

            project_id: Google Cloud project ID.

                .. deprecated:: 0.0.90
                    Use `project_id` as a direct argument to
                    `GoogleVertexLLMService.__init__()` instead.
        Nlocation
project_idc                    ddl }|j                         5  |j                  d       d|v r|d   |j                  dt        d       d|v r|d   |j                  d	t        d       ddd       t        |   d
i | y# 1 sw Y   xY w)zInitializes the InputParams.r   Nalwaysr   zGoogleVertexLLMService.InputParams.location is deprecated. Please provide 'location' as a direct argument to GoogleVertexLLMService.__init__() instead.   )
stacklevelr    zGoogleVertexLLMService.InputParams.project_id is deprecated. Please provide 'project_id' as a direct argument to GoogleVertexLLMService.__init__() instead.r   )warningscatch_warningssimplefilterwarnDeprecationWarningsuper__init__)selfkwargsr%   	__class__s      r   r+   z+GoogleVertexLLMService.InputParams.__init__V   s    ((* %%h/'F:,>,JMMw*#$	 "   6)f\.B.NMMy*#$	 " " G&v&# s   ABB)r   r   r   r   r   r   str__annotations__r    r+   __classcell__r.   s   @r   InputParamsr   A   s0    	" #'(3-&$(
HSM(	' 	'r   r3   N)credentialscredentials_pathmodelr   r    paramssettingssystem_instructiontoolstool_confighttp_optionsr4   r5   r6   r   r    r7   r8   r9   r:   r;   r<   c                   d|v r t        j                  d       t        d      |r^t        |t        j
                        rD||j                  }||j                  }t        j
                  di |j                  ddhd      }|t        d	      |t        j                  d
       d}| j                  ||      | _        || _        || _        t        ddddddddddddi       }|t!        dt        d       ||_        |t!        dt        d       ||_        |t!        dt               |s|j&                  |_        |j(                  |_        |j*                  |_        |j,                  |_        |j.                  |_        t        |j0                  t2              r|j0                  |_        ||j5                  |       t7        | p  dd||	|
|d| y)a  Initializes the VertexLLMService.

        Args:
            credentials: JSON string of service account credentials.
            credentials_path: Path to the service account JSON file.
            model: Model identifier (e.g., "gemini-2.5-flash").

                .. deprecated:: 0.0.105
                    Use ``settings=GoogleLLMSettings(model=...)`` instead.

            location: GCP region for Vertex AI endpoint (e.g., "us-east4").
            project_id: Google Cloud project ID.
            params: Input parameters for the model.

                .. deprecated:: 0.0.105
                    Use ``settings=GoogleLLMSettings(...)`` instead.

            settings: Runtime-updatable settings for this service.  When both
                deprecated parameters and *settings* are provided, *settings*
                values take precedence.
            system_instruction: System instruction/prompt for the model.

                .. deprecated:: 0.0.105
                    Use ``settings=GoogleVertexLLMSettings(system_instruction=...)`` instead.
            tools: List of available tools/functions.
            tool_config: Configuration for tool usage.
            http_options: HTTP options for the client.
            **kwargs: Additional arguments passed to GoogleLLMService.
        api_keyzGoogleVertexLLMService does not accept 'api_key' parameter. Use 'credentials' or 'credentials_path' instead for Vertex AI authentication.zbInvalid parameter 'api_key'. Use 'credentials' or 'credentials_path' for Vertex AI authentication.Nr   r    T)excludeexclude_unsetzproject_id is requiredz3location is not provided. Defaulting to 'us-east4'.zus-east4zgemini-2.5-flashi   F)r6   r9   
max_tokenstemperaturetop_ktop_pfrequency_penaltypresence_penaltyseedfilter_incomplete_user_turnsuser_turn_completion_configthinkingextrar6   r9   r7   dummy)r>   r8   r:   r;   r<   r   )r   error
ValueError
isinstancer   r3   r    r   r   
model_dumpwarning_get_credentials_credentials_project_id	_locationr   r
   r6   r9   rA   rB   rC   rD   rJ   rK   dictapply_updater*   r+   )r,   r4   r5   r6   r   r    r7   r8   r9   r:   r;   r<   r-   default_settingsr.   s                 r   r+   zGoogleVertexLLMService.__init__m   s   ^ LL` t  j)?)K)KL!#..
!??%11 ##Z,FVZ#[F 566 NNPQ!H !11+?OP%! 3$#"!).(,
" "7,CWM%*")"$&=?S 3E/ "8-DE.4.?.? +/5/A/A ,)/ &)/ &,2OO )fllD1-3\\$* ))(3 	 	
%#%	
 	
r   c                 ~    t        d| j                  | j                  | j                  | j                        | _        y)z;Create the Gemini client instance configured for Vertex AI.T)vertexair4   projectr   r<   N)r   rS   rT   rU   _http_options_client)r,   s    r   create_clientz$GoogleVertexLLMService.create_client   s3    ))$$^^++
r   c                 P   d}| r6t         j                  j                  t        j                  |       dg      }n6|r#t         j                  j                  |dg      }n	 t        dg      \  }}|st        d      |j                  t                      |S # t        $ r Y 3w xY w)aC  Retrieve Credentials using Google service account credentials.

        Supports multiple authentication methods:
        1. Direct JSON credentials string
        2. Path to service account JSON file
        3. Default application credentials (ADC)

        Args:
            credentials: JSON string of service account credentials.
            credentials_path: Path to the service account JSON file.

        Returns:
            Google credentials object for API authentication.

        Raises:
            ValueError: If no valid credentials are provided or found.
        Nz.https://www.googleapis.com/auth/cloud-platform)scopeszNo valid credentials provided.)r   Credentialsfrom_service_account_infojsonloadsfrom_service_account_filer   r   rN   refreshr   )r4   r5   credsr    s       r   rR   z'GoogleVertexLLMService._get_credentials  s    & 8<#//II

;'HI J E #//II HI J E
$+LM%!z =>>gi  # s   !B 	B%$B%)r   r   r   r   r   Settingsr0   r   r3   r   r/   listrV   r   r+   r^   staticmethodrR   r1   r2   s   @r   r   r   2   s3   	 'H&&*'&22 *'^ &**.#"&$(9=6:,0 $&*.2N
 c]N
 #3-	N

 }N
 3-N
 SMN
 )556N
 23N
 %SMN
 ~N
 d^N
 {+N
`
 -hsm -xPS} - -r   r   )!r   rc   osdataclassesr   environtypingr   logurur   pipecat.services.google.llmr   r	   pipecat.services.settingsr
   google.authr   google.auth.exceptionsr   google.auth.transport.requestsr   google.genair   google.genai.typesr   google.oauth2r   ModuleNotFoundErrorerM   	Exceptionr   r   r   r   r   <module>r{      s     	 ! *1

% &   K <,#66#.- 	/ 	 	C- C  ,FLL;qc"#FLL 	T &qc*
++,s   $A; ;B7 2B22B7