
    ^h"              
          % S SK r S SKJr  S SKrS SKJrJrJrJrJ	r	  S SK
r
S SKJr  S SKJr  S SK7  S SKJr  S SKJr  S S	KJr  S SK7  \" 5       r\" 5       q\\S
'   S\4S jrS#S\\   4S jjrS\4S jrS\ S\ S\\    S\\    S\RB                  4
S jr"\RG                  S/ SQSS/SS9\RG                  S/ SQSS/S9S\ S\S \4S! j5       5       r$S\S\ 4S" jr%g)$    N)Optional)	APIRouterHTTPExceptionRequestResponsestatus)verbose_proxy_logger) vertex_fine_tuning_apis_instance)*)user_api_key_auth)create_pass_through_route)get_secret_strdefault_vertex_configreturnc                  N    [        [        S5      [        S5      [        S5      S9$ )a  
Helper to get vertex pass through config from environment variables

The following environment variables are used:
- DEFAULT_VERTEXAI_PROJECT (project id)
- DEFAULT_VERTEXAI_LOCATION (location)
- DEFAULT_GOOGLE_APPLICATION_CREDENTIALS (path to credentials file)
DEFAULT_VERTEXAI_PROJECTDEFAULT_VERTEXAI_LOCATION&DEFAULT_GOOGLE_APPLICATION_CREDENTIALS)vertex_projectvertex_locationvertex_credentials)VertexPassThroughCredentialsr        l/home/james-whalen/.local/lib/python3.13/site-packages/litellm/proxy/vertex_ai_endpoints/vertex_endpoints.py_get_vertex_env_varsr      s,     (%&@A&'BC)*RS r   configc                     U c  [        5       qg[        U [        5      (       a`  U R	                  5        HL  u  p[        U[
        5      (       d  M  UR                  S5      (       d  M4  [        R                  " U5      X'   MN     [        S0 U D6qg)a,  Sets vertex configuration from provided config and/or environment variables

Args:
    config (Optional[dict]): Configuration dictionary
    Example: {
        "vertex_project": "my-project-123",
        "vertex_location": "us-central1",
        "vertex_credentials": "os.environ/GOOGLE_CREDS"
    }
Nzos.environ/r   )
r   r   
isinstancedictitemsstr
startswithlitellm
get_secretr   )r   keyvalues      r   set_default_vertex_configr(   '   ss     ~ 4 6&$ ,,.JC%%%%*:*:=*I*I%007 ) 9B6Br   ec           
         [         R                  " SR                  [        U 5      5      5        [         R                  " [
        R                  " 5       5        [        U [        5      (       aY  [        [        U S[        U R                  5      5      [        U SS5      [        U SS5      [        U S[        R                  5      S9$ [        U 5       n[        [        U SU5      [        U SS5      [        U SS5      [        U SS5      S9$ )	NzLlitellm.proxy.proxy_server.v1/projects/tuningJobs(): Exception occurred - {}messagetypeNoneparamstatus_code)r+   r,   r.   codei  )r	   errorformatr"   debug	traceback
format_excr   r   ProxyExceptiongetattrdetailr   HTTP_400_BAD_REQUEST)r)   	error_msgs     r   exception_handlerr;   A   s    V]]F	

 y3356!]##Ay#ahh-8FF+!Wf-M6+F+FG	
 	
 1vh	Ay)4FF+!Wf-M3/	
 	
r   base_urlrequested_routedefault_vertex_locationdefault_vertex_projectc                     [         R                  " U 5      nSU;   a  UR                  US9nU$  SnSU;   a  SnSR                  XcU5      nSU-   U-   nUR                  US9nU$ )a$  
Allow user to specify their own project id / location.

If missing, use defaults

Handle cachedContent scenario - https://github.com/BerriAI/litellm/issues/5460

Constructed Url:
POST https://LOCATION-aiplatform.googleapis.com/{version}/projects/PROJECT_ID/locations/LOCATION/cachedContents
	locations)pathv1cachedContentv1beta1z{}/projects/{}/locations/{}/)httpxURL	copy_withr2   )	r<   r=   r>   r?   new_base_urlupdated_urlvertex_versionbase_requested_routeupdated_requested_routes	            r   construct_target_urlrO   Y   s      99X&Lo%",,/,B
 04N/)"8??0G "$88?J((.E(FKr   z/vertex-ai/{endpoint:path})GETPOSTPUTDELETEPATCHzVertex AI Pass-throughzpass-throughF)methodstagsinclude_in_schemaz/vertex_ai/{endpoint:path})rU   rV   endpointrequestfastapi_responsec                 0  #    [         R                  " U 5      R                  nSSKn[        R
                  " SU 5        0 n[        US9n[        UUS9I Sh  vN nSnSn	[        R                  c  [        UR                  5      =(       d    0 n[        R
                  " SU5        UR                  SU 5      n
U
(       a  U
R                  S5      OSn	S	U	 S
3nUR                  SS5        UR                  SS5        O{[        R                  n[        R                  n	[        R                   nS	U	 S
3n["        R$                  " UUSS9I Sh  vN u  p["        R&                  " SUSUUU	SSSS9	u  pSSU 30nUn[        R
                  " SU5        UR)                  S5      (       d  SU-   n[+        UUU	US9n[        R
                  " SU5        [-        U5      nSnS[-        U5      ;   a  SnUS-  n[/        U UUS9nU" UUUUS9I Sh  vN nU$  GN N N7f)zd
Call LiteLLM proxy via Vertex AI SDK.

[Docs](https://docs.litellm.ai/docs/pass_through/vertex_ai)
r   Nzrequested endpoint %s)rY   )rY   api_keyz;default_vertex_config  not set, incoming request headers %sz/locations/([^/]+)   zhttps://z-aiplatform.googleapis.com/zcontent-lengthhostvertex_ai_beta)credentials
project_idcustom_llm_provider F)	modelauth_headergemini_api_keyr   r   r   streamrb   api_baseAuthorizationBearer zrequest_route %srF   )r<   r=   r>   r?   zupdated url %srg   Tz?alt=sse)rX   targetcustom_headers)rg   )rG   rH   rB   rer	   r3   get_litellm_virtual_keyr   r   r   r    headerssearchgrouppopr   r   r
   _ensure_access_token_async_get_token_and_urlr#   rO   r"   r   )rX   rY   rZ   encoded_endpointrm   ro   api_key_to_useuser_api_key_dictr   r   matchbase_target_urlr   _auth_headerre   _request_routerK   rk   is_streaming_requestendpoint_funcreceived_values                         r   vertex_proxy_router      s[    * yy*//6AG,W=N/ 
 NO++3w'-2""I7	

 		/:,1%++a.t$_$55PQ$d+FD!.==/??2EE$_$55PQ 3MM.)$4  	% :LL$1)+ 0

 w{m4
 %M1=A &&s++!11 ' ( /-	K /= F 3{###* .M
 )#	 N u8ns8   AHHC4HHB<HH	HHHc                     U R                   R                  S5      nU(       a  SU 3$ U R                   R                  SS5      $ )z
Extract and format API key from request headers.
Prioritizes x-litellm-api-key over Authorization header.


Vertex JS SDK uses `Authorization` header, we use `x-litellm-api-key` to pass litellm virtual key

zx-litellm-api-keyrj   ri   rc   )ro   get)rY   litellm_api_keys     r   rn   rn      sA     oo))*=>O)**??33r   )N)&r4   typingr   rG   fastapir   r   r   r   r   r$   litellm._loggingr	   litellm.fine_tuning.mainr
   litellm.proxy._types$litellm.proxy.auth.user_api_key_authr   ;litellm.proxy.pass_through_endpoints.pass_through_endpointsr   litellm.secret_managers.mainr   -litellm.types.passthrough_endpoints.vertex_airouterr   r   __annotations__r   r    r(   	Exceptionr;   r"   rH   rO   	api_router   rn   r   r   r   <module>r      sA      G G  1 E " B 8 ;	6R6T 3 T:  Chtn C4
 
0$$$ &c]$ %SM	$
 YY$N  5
"N	3	    5
"N	3  
kkk kk\4W 4 4r   