
    ^h$+                         S r SSKrSSKrSSKJrJrJrJrJr  SSK	J
r
  SSKJr  SSKJr  SSKJr  SS	KJrJrJr  \(       a  SS
KJr  O\r " S S\5      rg)z
Base Vertex, Google AI Studio LLM Class

Handles Authentication and generating request urls for Vertex AI and Google AI Studio
    N)TYPE_CHECKINGAnyLiteralOptionalTuple)verbose_logger)asyncify)BaseLLM)AsyncHTTPHandler   )_get_gemini_url_get_vertex_urlall_gemini_url_modes)Credentialsc                     ^  \ rS rSrS"U 4S jjrS\\   S\4S jrS\\   S\\   S\\	\4   4S	 jr
S\	SS4S
 jrS\\   S\\   S\S   S\\\4   4S jrS\S\4S jrS\\   S\S\\   S\S\\   S\\   S\S\\\   \4   4S jr  S#S\S\\   S\\   S\\   S\\   S\\   S\\   S\S   S\\   S\\   S\S\\\   \4   4S jjrS\\   S\\   S\S   S\\\4   4S jrS\\   S\\   S\4S  jrS!rU =r$ )$
VertexBase   returnNc                 h   > [         TU ]  5         S U l        S U l        S U l        S U l        S U l        g )N)super__init__access_tokenrefresh_token_credentials
project_idasync_handler)self	__class__s    `/home/james-whalen/.local/lib/python3.13/site-packages/litellm/llms/vertex_ai/vertex_llm_base.pyr   VertexBase.__init__   s5    +/,0?C)-9=    vertex_regionc                     U=(       d    S$ )Nzus-central1 )r   r"   s     r   get_vertex_regionVertexBase.get_vertex_region!   s    --r!   credentialsr   c                    SS K Jn  SSK Jn  SSKJn  UGbA  [        U[        5      (       Ga+  SS Kn[        R                  " SU5        [        R                  " SU[        R                  R                  U5      [        R                  " 5       5         [        R                  R                  U5      (       a   [        R                  " [!        U5      5      nO[        R"                  " U5      n SU;   a%  US   S:X  a  UR(                  R+                  U5      nO/UR,                  R.                  R(                  R1                  US	/S
9nUc  [3        USS 5      nOUR5                  US	/S9u  pUc  U	nUR7                  U" 5       5        U(       d  [9        S5      e[        U[        5      (       d  [;        S[=        U5       35      eX4$ ! [$         a    [%        SR'                  U5      5      ef = f)Nr   )identity_poolRequestz*Vertex: Loading vertex credentials from %szVVertex: checking if credentials is a valid path, os.path.exists(%s)=%s, current dir %sz:Unable to load vertex credentials from environment. Got={}typeexternal_accountz.https://www.googleapis.com/auth/cloud-platform)scopesr   )quota_project_idr.   Could not resolve project_idz(Expected project_id to be a str but got )google.authauthr)   google.auth.transport.requestsr+   
isinstancestrgoogle.oauth2.service_accountr   debugospathexistsgetcwdjsonloadopenloads	Exceptionformatr   	from_infooauth2service_accountfrom_service_account_infogetattrdefaultrefresh
ValueError	TypeErrorr,   )
r   r'   r   google_authr)   r+   googlejson_objcredscreds_project_ids
             r   	load_authVertexBase.load_auth$   s    	*-	
 "z+s'C'C0  <k   h{+			
77>>+..#yyk):;H#zz+6H !hv&6:L&L%11;;HE MM11==WW  PQ X   !$UL$?
&1&9&9!+HI ': '#E !-
gi ;<<*c**:4
;K:LM    M  PWW# s   AG G %G,c                 <    SSK Jn  UR                  U" 5       5        g )Nr   r*   )r3   r+   rH   )r   r'   r+   s      r   refresh_authVertexBase.refresh_authg   s    	
 	GI&r!   custom_llm_provider)	vertex_aivertex_ai_betageminic                     US:X  a  gU R                   b6  Ub  U R                   U4$ U R                  b  U R                   U R                  4$ U R                  (       d9  U R                  XS9u  U l        nU R                  (       d  U=(       d    UU l        O}U R                  R                  (       d  U R                  R
                  (       d  U R                  U R                  5        U R                  (       d  U R                  R                  U l        U R                  (       d  [        S5      eU R                  (       a  U R                  R
                  (       d  [        S5      eU R                  R
                  U=(       d    U R                  4$ )z#
Returns auth token and project id
rX    r[   r'   r   r0   0Could not resolve API token from the environment)
r   r   r   rP   expiredtokenrS   r/   rI   RuntimeErrorr   r'   r   rU   cred_project_ids        r   _ensure_access_tokenVertexBase._ensure_access_tokenn   s0    (*(%((*44,(($//99  15' 2@ 2.D ??","?  ((0A0A0G0G!!$"3"34??"&"3"3"D"D;<<  (9(9(?(?QRR  &&
(EdooEEr!   optional_paramsc                      SU;   a  gSU;   a  gg)z
VertexAI only supports ContextCaching on v1beta1

use this helper to decide if request should be sent to v1 or v1beta1

Returns v1beta1 if context caching is enabled
Returns v1 in all other cases
cached_contentTCachedContentFr$   )r   re   s     r   is_using_v1beta1_features$VertexBase.is_using_v1beta1_features   s     .o-r!   api_basegemini_api_keyendpointstreamauth_headerurlc                     U(       aC  US:X  a"  SR                  X5      nUc  [        S5      eUnOSR                  X5      nUSL a  US-   nXg4$ )z
for cloudflare ai gateway - https://github.com/BerriAI/litellm/issues/4317

## Returns
- (auth_header, url) - Tuple[Optional[str], str]
rX   z{}:{}z3Missing gemini_api_key, please set `GEMINI_API_KEY`Tz?alt=sse)rA   rI   )r   rk   rU   rl   rm   rn   ro   rp   s           r   _check_custom_proxyVertexBase._check_custom_proxy   sg      "h.nnX8!)$M  #  nnX8~J&r!   modelvertex_projectvertex_locationvertex_credentialsshould_use_v1beta1_featuresmodec           
          US:X  a  [        UUUUS9u  pSnO(U R                  US9nU
SL a  SOSn[        UUUUUUS9u  pU R                  U	UUUUUUS	9$ )
z
Internal function. Returns the token and url for the call.

Handles logic if it's google ai studio vs. vertex ai.

Returns
    token, url
rX   )ry   rt   rn   rl   N)r"   Tv1beta1v1)ry   rt   rn   ru   rv   vertex_api_version)rk   ro   rU   rl   rm   rn   rp   )r   r%   r   rr   )r   rt   ro   rl   ru   rv   rw   rn   rU   rk   rx   ry   rp   rm   versions                  r   _get_token_and_urlVertexBase._get_token_and_url   s    , (*+-	MC K"44?4SO 9D@	d  ,- /#*MC ''# 3) ( 
 	
r!   c                   #    US:X  a  gU R                   b6  Ub  U R                   U4$ U R                  b  U R                   U R                  4$ U R                  (       dL   [        U R                  5      " XS9I Sh  vN u  U l        nU R                  (       d  U=(       d    UU l        OU R                  R                  (       d  U R                  R                  (       d-  [        U R                  5      " U R                  5      I Sh  vN   U R                  (       d  U R                  R                  U l        U R                  (       d  [        S5      eU R                  (       a  U R                  R                  (       d  [        S5      eU R                  R                  U=(       d    U R                  4$  GN<! [
         a    [        R                  " S5        e f = f N7f)z'
Async version of _ensure_access_token
rX   rZ   Nr\   zfFailed to load vertex credentials. Check to see if credentials containing partial/invalid information.r0   r]   )r   r   r   r	   rP   r@   r   	exceptionr^   r_   rS   r/   rI   r`   ra   s        r   _ensure_access_token_async%VertexBase._ensure_access_token_async   sy     (*(%((*44,(($//99  ;CDNN;S +< 62!? ??","?  ((0A0A0G0Gt001$2C2CDDD??"&"3"3"D"D;<<  (9(9(?(?QRR  &&
(EdooEE/6  ((| 	 Es>   AG!F: ;F7<F: B G!G	B.G!7F: :"GG!extra_headersc                 L    SS0nUb  SU 3US'   Ub  UR                  U5        U$ )NzContent-Typezapplication/jsonzBearer Authorization)update)r   ro   r   headerss       r   set_headersVertexBase.set_headers,  sA     .
 ")0'>GO$$NN=)r!   )r   r   r   r   r   )r   N)Fchat)__name__
__module____qualname____firstlineno__r   r   r5   r%   r   r   rP   rS   r   rc   dictboolri   rr   r   r   r   r   __static_attributes____classcell__)r   s   @r   r   r      s]   >.x} . .A!#C=A!6>smA!	sCxA!F' ' '&Fc]&F SM&F %3
	&F 
sCx&FP $  3-  !  !	 
     c]    
x}c!	" X 7<%+6
6
 c]6
 !	6

 !6
 "#6
 %SM6
 6
 %%LM6
 3-6
 &.d^6
 #6
 
x}c!	"6
p,Fc],F SM,F %3
	,F 
sCx,F\#C=9A$	 r!   r   )__doc__r<   r8   typingr   r   r   r   r   litellm._loggingr   #litellm.litellm_core_utils.asyncifyr	   litellm.llms.baser
   &litellm.llms.custom_httpx.http_handlerr   common_utilsr   r   r   google.auth.credentialsr   GoogleCredentialsObjectr   r$   r!   r   <module>r      sD     	 ? ? + 8 % C P PN!_ _r!   