
    hQ                         d Z ddlZddlZddlmZmZmZmZ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mZ d	d
lmZmZmZmZ erddlmZ neZ G d d      Zy)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DictLiteralOptionalTuple)verbose_logger)asyncify)AsyncHTTPHandler)get_secret_str)VERTEX_CREDENTIALS_TYPESVertexPartnerProvider   )_get_gemini_url_get_vertex_urlall_gemini_url_modesis_global_only_vertex_model)Credentialsc                   2    e Zd Zd0 fdZdee   dedefdZdee   dee   dee	ef   fd	Z
d
 Zd Zd Zd Zd ZdefdZdee   dee   defdZe	 d1dedededee   dedee   defd       Zdee   dee   dee   dededee   dedefdZde	ddfdZdee   dee   ded   deeef   fdZdedefdZdee   dedee   d edee   d!ee   d"edeee   ef   fd#Z	 	 d2ded!ee   dee   dee   dee   d$ee   dee   ded   dee   d%ee   d&edeee   ef   fd'Zdee   dee   deeef   fd(Zdee   dee   ded   deeef   fd)Z d!ee   d*ee   defd+Z!ed,edee   fd-       Z"ed,edee   fd.       Z#ed,edee   fd/       Z$ xZ%S )3
VertexBasereturnNc                 v    t         |           d | _        d | _        d | _        i | _        d | _        d | _        y N)super__init__access_tokenrefresh_token_credentials_credentials_project_mapping
project_idasync_handler)self	__class__s    b/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/llms/vertex_ai/vertex_llm_base.pyr   zVertexBase.__init__    sD    +/,0?C  	) *.9=    vertex_regionmodelc                 &    t        |      ry|xs dS )Nglobalus-central1)r   )r"   r&   r'   s      r$   get_vertex_regionzVertexBase.get_vertex_region,   s    &u---r%   credentialsr    c                    |t        |t              rt        j                  d|       t        j                  d|t        j
                  j                  |      t	        j                                	 t        j
                  j                  |      rt        j                  t        |            }nt        j                  |      }n6t        |t              r|}n#t        dj                  t!        |                  d|v rv|d   dk(  rn|j#                  di       }t        |t              r|j#                  dd	      nd	}t        |t              rd
|v r| j%                  |      }nU| j'                  |      }nCd|v r+|d   dk(  r#| j)                  |dg      }|!|j*                  }n| j-                  |dg      }|(t/        |dd       }n| j1                  dg      \  }}||}| j3                  |       |st        d      t        |t              st5        dt!        |             ||fS # t        $ r t        dj                  |            w xY w)Nz*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={}zInvalid credentials type: {}typeexternal_accountcredential_sourceenvironment_id awsauthorized_userz.https://www.googleapis.com/auth/cloud-platformscopesr    Could not resolve project_idz(Expected project_id to be a str but got )
isinstancestrr	   debugospathexistsgetcwdjsonloadopenloads	Exceptionformatdict
ValueErrorr.   get(_credentials_from_identity_pool_with_aws_credentials_from_identity_pool!_credentials_from_authorized_userquota_project_id!_credentials_from_service_accountgetattr_credentials_from_default_authrefresh_auth	TypeError)r"   r,   r    json_objr0   r1   credscreds_project_ids           r$   	load_authzVertexBase.load_auth1   sq    "+s+$$@+ $$lGGNN;/IIK	
ww~~k2#'99T+->#?#'::k#: K.& 299${:KL 
 !hv&6:L&L$,LL1Db$I! ""3T: &))*:B? 
 nc2u7N II(SE @@JE8#(8<M(M>>LM ?  %..  >>LM ? 
 !$UL$?
&*&I&IHI 'J '#E# !-
% ;<<*c*:4
;K:LM  j  { ! #T[[' s   2AH, ,$Ic                 D    ddl m} |j                  j                  |      S )Nr   )identity_pool)google.authrV   r   	from_info)r"   rQ   rV   s      r$   rI   z*VertexBase._credentials_from_identity_pool   s    -((228<<r%   c                 D    ddl m} |j                  j                  |      S )Nr   )r3   )rW   r3   r   rX   )r"   rQ   r3   s      r$   rH   z3VertexBase._credentials_from_identity_pool_with_aws   s    #((22r%   c                 l    dd l }|j                  j                  j                  j	                  ||      S Nr   r5   )google.oauth2.credentialsoauth2r,   r   from_authorized_user_infor"   rQ   r6   googles       r$   rJ   z,VertexBase._credentials_from_authorized_user   s3    (}}((44NNV O 
 	
r%   c                 l    dd l }|j                  j                  j                  j	                  ||      S r[   )google.oauth2.service_accountr]   service_accountr   from_service_account_infor_   s       r$   rL   z,VertexBase._credentials_from_service_account   s3    ,}},,88RRV S 
 	
r%   c                 2    dd l m} |j                  |      S r[   )rW   authdefault)r"   r6   google_auths      r$   rN   z)VertexBase._credentials_from_default_auth   s    )""&"11r%   c                      y)Nr*    )r"   s    r$   get_default_vertex_locationz&VertexBase.get_default_vertex_location   s    r%   api_basevertex_locationc                 N    |r|S |dk(  ry|rd| dS d| j                          dS )Nr)   z!https://aiplatform.googleapis.comhttps://-aiplatform.googleapis.com)rk   )r"   rl   rm   s      r$   get_api_basezVertexBase.get_api_base   sE     O(6o..HIId>>@AA[\\r%   vertex_projectpartnerstreamc                 z   |xs d|  d}|t         j                  k(  r| d| d|  dS |t         j                  k(  r|r| d| d|  d| dS | d| d|  d| dS |t         j                  k(  r|r| d	| d|  d
| dS | d	| d|  d
| dS |t         j                  k(  r|r| d| d|  d| dS | d| d|  d| dS y)z1Return the base url for the vertex partner modelsro   rp   z/v1/projects/z/locations/z#/endpoints/openapi/chat/completionsz/publishers/mistralai/models/z:streamRawPredictz:rawPredictz/v1beta1/projects/z/publishers/ai21/models/z/publishers/anthropic/models/N)r   llama	mistralaiai21clauderm   rr   rs   rt   r'   rl   s         r$   create_vertex_urlzVertexBase.create_vertex_url   s    U/)::TU+111Z}^,<KGXX{||-777"=0@OK\\yz  zA  AR  S  S"=0@OK\\yz  zA  AL  M  M-222"#5n5E[Q`Paayz  zA  AR  S  S"#5n5E[Q`Paayz  zA  AL  M  M-444"=0@OK\\yz  zA  AR  S  S"=0@OK\\yz  zA  AL  M  M	 5r%   custom_api_basec           	         | j                  ||      }t        j                  |xs d|xs |||||      }	t        |	j	                  d            dkD  r|	j	                  d      d   }
nd}
| j                  |dd |
|d |		      \  }}|S )
N)rl   rm   r*   rz   :r   r2   	vertex_ai)rl   custom_llm_providergemini_api_keyendpointrt   auth_headerurl)rq   r   r{   lensplit_check_custom_proxy)r"   r|   rm   rr   r    rs   rt   r'   rl   default_api_baser   _s               r$   get_complete_vertex_urlz"VertexBase.get_complete_vertex_url   s     $$$o % 
 &77+<})7Z 8 
 %%c*+a/'--c226HH..$ +  / 
8 r%   c                 <    ddl m} |j                   |              y )Nr   )Request)google.auth.transport.requestsr   refresh)r"   r,   r   s      r$   rO   zVertexBase.refresh_auth   s    	
 	GI&r%   r   )r   vertex_ai_betageminic                 4    |dk(  ry| j                  ||      S )z3
        Returns auth token and project id
        r   r2   r2   r,   r    )get_access_token)r"   r,   r    r   s       r$   _ensure_access_tokenzVertexBase._ensure_access_token   s-     (*(('% )  r%   optional_paramsc                     d|v ryd|v ryy)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CachedContentFrj   )r"   r   s     r$   is_using_v1beta1_featuresz$VertexBase.is_using_v1beta1_features  s     .o-r%   r   r   r   r   c                     |rB|dk(  r"dj                  ||      }|t        d      |}ndj                  ||      }|du r|dz   }||fS )z
        for cloudflare ai gateway - https://github.com/BerriAI/litellm/issues/4317

        ## Returns
        - (auth_header, url) - Tuple[Optional[str], str]
        r   z{}:{}z3Missing gemini_api_key, please set `GEMINI_API_KEY`Tz?alt=sse)rD   rF   )r"   rl   r   r   r   rt   r   r   s           r$   r   zVertexBase._check_custom_proxy  sm      "h.nnXx8!)$M  #  nnXx8~J&Cr%   vertex_credentialsshould_use_v1beta1_featuresmodec           	          |dk(  rt        ||||      \  }}d}n/| j                  ||      }|
du rdnd}t        ||||||      \  }}| j                  |	||||||	      S )
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
        r   )r   r'   rt   r   N)r&   r'   Tv1beta1v1)r   r'   rt   rr   rm   vertex_api_version)rl   r   r   r   r   rt   r   )r   r+   r   r   )r"   r'   r   r   rr   rm   r   rt   r   rl   r   r   r   r   versions                  r$   _get_token_and_urlzVertexBase._get_token_and_url?  s    , (*+-	MC K"44- 5 O 9D@	d  ,- /#*MC ''# 3) ( 
 	
r%   c                    t        |t              rt        j                  |      n|}||f}d}t	        j
                  d|        || j                  v rt	        j
                  d| d       | j                  |   }t	        j
                  d|       t        |t              r|\  }}n|}|j                  xs t        |dd      }t	        j
                  d|       n]t	        j
                  d| d	       	 | j                  ||
      \  }}|t        dj                  |            ||f| j                  |<   t	        j
                  d|        |7|5t        |t              r%|}||f}	|	| j                  vr||f| j                  |	<   |t        d      |j                   r:t	        j
                  d|        | j#                  |       ||f| j                  |<   |j$                  t        |j$                  t              s$t        dj                  |j$                              |t        d      |j$                  |fS # t        $ r(}t	        j                  dt        |              |d}~ww xY w)aa  
        Get access token and project id

        1. Check if credentials are already in self._credentials_project_mapping
        2. If not, load credentials and add to self._credentials_project_mapping
        3. Check if loaded credentials have expired
        4. If expired, refresh credentials
        5. Return access token and project id
        Nz,Checking cached credentials for project_id: z)Cached credentials found for project_id: .zcached_entry: %sr    z+Using cached credentials for project_id: %sz/Credential cache key not found for project_id: z, loading new credentialsr   znFailed to load vertex credentials. Check to see if credentials containing partial/invalid information. Error: zZCould not resolve credentials - either dynamically or from environment, for project_id: {}z'Validating credentials for project_id: z"Credentials are None after loadingz0Credentials expired, refreshing for project_id: zFCould not resolve credentials token. Got None or non-string token - {}r7   )r8   rE   r?   dumpsr	   r:   r   tuplerK   rM   rT   rC   	exceptionr9   rF   rD   expiredrO   token)
r"   r,   r    cache_credentialscredential_cache_keyr   cached_entrycredential_project_ideresolved_cache_keys
             r$   r   zVertexBase.get_access_tokenz  s   " (2+t'DDJJ{#+ 	 !2:>:>::,G	
  4#D#DD  ;J<qI  <<=QRL  !3\B,.6B33  ,(4(E(E ) ,J%   =%   A*Mfg6:nn +
 7E 733 # pww"  %GD--.BC 	FzlST%10#6.J"3Z!@!)J)JJ )I112DE ABB  B:,O l+%GD--.BC %Z8J8JC-PX__ &&  ;<<!!:--w  (( E  FI  JK  FL  EM  N 	s   6I 	I3#I..I3c                    K   |dk(  ry	  t        | j                        ||       d{   S 7 # t        $ r}|d}~ww xY ww)z7
        Async version of _ensure_access_token
        r   r   r   N)r
   r   rC   )r"   r,   r    r   r   s        r$   _ensure_access_token_asyncz%VertexBase._ensure_access_token_async  sX      (*<Xd&;&;< +)     s.   A 1 /1 A1 	A<AAextra_headersc                 H    ddi}|d| |d<   ||j                  |       |S )NzContent-Typezapplication/jsonzBearer Authorization)update)r"   r   r   headerss       r$   set_headerszVertexBase.set_headers  sA     .
 ")0'>GO$$NN=)r%   litellm_paramsc                     | j                  dd       xs1 | j                  dd       xs t        j                  xs t        d      S )Nrr   vertex_ai_projectVERTEXAI_PROJECT)poplitellmrr   r   r   s    r$   get_vertex_ai_projectz VertexBase.get_vertex_ai_project  sN     /6 2!!"5t<2%%2 01		
r%   c                 h    | j                  dd       xs | j                  dd       xs t        d      S )Nr   vertex_ai_credentialsVERTEXAI_CREDENTIALS)r   r   r   s    r$   get_vertex_ai_credentialsz$VertexBase.get_vertex_ai_credentials  s>     3T: 6!!"94@645	
r%   c                     | j                  dd       xs> | j                  dd       xs* t        j                  xs t        d      xs t        d      S )Nrm   vertex_ai_locationVERTEXAI_LOCATIONVERTEX_LOCATION)r   r   rm   r   r   s    r$   get_vertex_ai_locationz!VertexBase.get_vertex_ai_location"  s^     0$7 1!!"6=1&&1 121 /0	
r%   )r   Nr   )Fchat)&__name__
__module____qualname__r   r   r9   r+   r   r   r   rT   rI   rH   rJ   rL   rN   rk   rq   staticmethodr   boolr{   r   rO   r   r   rE   r   r   r   r   r   r   r   r   r   r   __classcell__)r#   s   @r$   r   r      s$   
>.x} .S .S .
Q!#$<=Q!KSTW=Q!	sCxQ!h=
3


2
S 
] 
]8@
]	
]  #'MMM 'M 	M
 M 3-M 
M M:$!#$ "#$ !	$
 $ '$ $ $ 
$L' ' '67 SM %3
	 
sCx& $  3-  !  !	 
     c]    
x}c!	" X 7<%+9
9
 c]9
 !	9

 !9
 "#9
 %%=>9
 9
 %%LM9
 3-9
 &.d^9
 #9
 
x}c!	"9
vr.67r. SMr. 
sCx	r.h67 SM %3
	 
sCx,#C=9A$	 
d 
x} 
 
 
$ 
8C= 
 
 
t 
 
 
r%   r   )__doc__r?   r;   typingr   r   r   r   r   r   r   litellm._loggingr	   #litellm.litellm_core_utils.asyncifyr
   &litellm.llms.custom_httpx.http_handlerr   litellm.secret_managers.mainr   litellm.types.llms.vertex_air   r   common_utilsr   r   r   r   google.auth.credentialsr   GoogleCredentialsObjectr   rj   r%   r$   <module>r      sN     	 E E  + 8 C 7 X  N!K
 K
r%   