
    qig,                     j   d Z ddlZddlmZ ddlmZmZmZ ddlm	Z	 ddl
mZ ddlmZmZ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 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	jD                  de!         e	jD                  d        e#de!       dZ![!ww xY w)zService for accessing Gemini Live via Google Vertex AI.

This module provides integration with Google's Gemini Live model via
Vertex AI, supporting both text and audio modalities with voice transcription,
streaming responses, and tool usage.
    N)	dataclass)ListOptionalUnion)logger)ToolsSchema)GeminiLiveLLMServiceGeminiLiveLLMSettingsGeminiMediaResolutionGeminiModalitiesHttpOptionsInputParamslanguage_to_gemini_language)_warn_deprecated_param)default)GoogleAuthError)Request)Client)service_accountzException: zOIn order to use Google Vertex AI, you need to `pip install pipecat-ai[google]`.zMissing module: c                       e Zd ZdZy)GeminiLiveVertexLLMSettingsz(Settings for GeminiLiveVertexLLMService.N)__name__
__module____qualname____doc__     `/opt/pipecat/venv/lib/python3.12/site-packages/pipecat/services/google/gemini_live/vertex/llm.pyr   r   -   s    2r   r   c                        e Zd ZU dZeZeed<   ddddddddddddddd	ee   d
ee   dededee   dede	de	dee   dee
ee   ef      dee   dee   de	dedee   f fdZd Zed        Zed	ee   d
ee   defd       Z xZS )GeminiLiveVertexLLMServicezProvides access to Google's Gemini Live model via Vertex AI.

    This service enables real-time conversations with Gemini, supporting both
    text and audio modalities. It handles voice transcription, streaming audio
    responses, and tool usage.
    	_settingsNCharonFTz6https://generativelanguage.googleapis.com/v1beta/files)credentialscredentials_pathmodelvoice_idstart_audio_pausedstart_video_pausedsystem_instructiontoolsparamssettings#inference_on_context_initializationfile_api_base_urlhttp_optionsr#   r$   location
project_idr%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   c                   d|v r t        j                  d       t        d      | j                  ||      | _        || _        || _        t        d"i dddddd	d
ddd	dd	dd	dd	dd	dddd	dt        j                  dddt        j                  dd	di di dddi di }|t        dt        d       ||_        |dk7  rt        dt        d       ||_        |kt        dt               |sX|j                  |_        |j                   |_        |j"                  |_        |j$                  |_        |j&                  |_        |j(                  |_        |j*                  |_        |j,                  rt/        |j,                        nd|_        |j0                  |_        |j2                  |_        |j4                  r|j4                  j7                         ni |_        |j8                  xs i |_        |j:                  xs d|_        |j<                  xs i |_        t?        |j@                  tB              r|j@                  |_         ||jE                  |       tG        |   d"d |||	|
||||d!	| y	)#a*  Initialize the service for accessing Gemini Live via Google Vertex AI.

        Args:
            credentials: JSON string of service account credentials.
            credentials_path: Path to the service account JSON file.
            location: GCP region for Vertex AI endpoint (e.g., "us-east4").
            project_id: Google Cloud project ID.
            model: Model identifier to use.

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

            voice_id: TTS voice identifier. Defaults to "Charon".

                .. deprecated:: 0.0.105
                    Use ``settings=GeminiLiveVertexLLMSettings(voice=...)`` instead.
            start_audio_paused: Whether to start with audio input paused. Defaults to False.
            start_video_paused: Whether to start with video input paused. Defaults to False.
            system_instruction: System prompt for the model. Defaults to None.
            tools: Tools/functions available to the model. Defaults to None.
            params: Configuration parameters for the model along with Vertex AI
                location and project ID.

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

            settings: Gemini Live LLM settings. If provided together with deprecated
                top-level parameters, the ``settings`` values take precedence.
            inference_on_context_initialization: Whether to generate a response when context
                is first set. Defaults to True.
            file_api_base_url: Base URL for the Gemini File API. Defaults to the official endpoint.
            http_options: HTTP options for the client.
            **kwargs: Additional arguments passed to parent GeminiLiveLLMService.
        api_keyzGeminiLiveVertexLLMService 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.r%   z)google/gemini-live-2.5-flash-native-audiovoicer"   frequency_penaltyN
max_tokensi   presence_penaltytemperaturetop_ktop_pseedfilter_incomplete_user_turnsFuser_turn_completion_config
modalitieslanguagezen-USmedia_resolutionvadcontext_window_compressionthinkingenable_affective_dialogproactivityextrar&   r+   dummy)	r3   r'   r(   r)   r*   r,   r-   r.   r/   r   )%r   error
ValueError_get_credentials_credentials_project_id	_locationr   r   AUDIOr   UNSPECIFIEDr   r%   r4   r5   r6   r7   r8   r9   r:   r>   r?   r   r@   rA   rB   
model_dumprC   rD   rE   
isinstancerF   dictapply_updatesuper__init__)selfr#   r$   r0   r1   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   kwargsdefault_settings	__class__s                     r   rU   z#GeminiLiveVertexLLMService.__init__?   s   p LL` t  !11+?OP%! 7 
=

 #
 	

 "
 
 
 
 
 */
 )-
 (--
 
 3>>
 
  (*!
" #
$ %*%
& '
( )
0 "7,GQ%*"x":/JGT%-" "8-HI5;5M5M 2.4.?.? +4:4K4K 1/5/A/A ,)/ &)/ &.4.?.? +DJOO/@Y` !) 5;4K4K 1'-zz $ 88 55@@B !;
 -3OO,Ar );A;Y;Y;b]b 8/5/A/A/GR ,fllD1-3\\$* ))(3 	 	
 111%0S/%	
 	
r   c                 ~    t        d| j                  | j                  | j                  | j                        | _        y)z"Create the Gemini client instance.T)vertexair#   projectr0   r/   N)r   rK   rL   rM   _http_options_clientrV   s    r   create_clientz(GeminiLiveVertexLLMService.create_client   s3    ))$$^^++
r   c                     t        d      )z0Gemini File API is not supported with Vertex AI.zWhen using Vertex AI, the recommended approach is to use Google Cloud Storage for file handling. The Gemini File API is not directly supported in this context.)NotImplementedErrorr_   s    r   file_apiz#GeminiLiveVertexLLMService.file_api   s     " n
 	
r   returnc                 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)a:  Retrieve Credentials using Google service account credentials JSON.

        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:
            OAuth token 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   rI   refreshr   )r#   r$   credsr1   s       r   rJ   z+GeminiLiveVertexLLMService._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   Settings__annotations__r   strboolr   r   rR   r   r   r   rU   r`   propertyrc   staticmethodrJ   __classcell__)rY   s   @r   r    r    4   sk    +H**
 &**.  $ #(#(,0:>(,:>48!Y.2#X
 c]X
 #3-	X

 X
 X
 }X
 X
 !X
 !X
 %SMX
 d4j+567X
 %X
 67X
 .2X
  !X
" {+#X
t
 
 
 -hsm -xPS} -Y\ - -r   r    )&r   ri   dataclassesr   typingr   r   r   logurur   %pipecat.adapters.schemas.tools_schemar   'pipecat.services.google.gemini_live.llmr	   r
   r   r   r   r   r   pipecat.services.settingsr   google.authr   google.auth.exceptionsr   google.auth.transport.requestsr   google.genair   google.oauth2r   ModuleNotFoundErrorerH   	Exceptionr   r    r   r   r   <module>r      s     ! ( (  =   =
,#66#- 	"7 	 	d!5 d  ,FLL;qc"#FLLbc
&qc*
++,s   A6 6B2;2B--B2