
    h{                        d Z ddlZddlZddlZddlZddlmZmZmZm	Z	m
Z
mZmZ ddlmZmZmZmZ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"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/m0Z0 ddl1m2Z2 ddl3m4Z4 ddl5m6Z6 ddl7m8Z8m9Z9m:Z:m;Z; ddl<m=Z=  e       Z> G d de      Z?de@de4de	e8   fdZAde8de;de"fdZBe>j                  ddg ee)      g       ee)      fde@de;de'fd        ZD	 	 d?d!e8de'de4d"e	e@   d#eEde	e   fd$ZFd!e8de'de4de	e   fd%ZG G d& d'      ZHe>j                  d(d)dg ee)      g*       ee)      fd+e!de'fd,       ZJd-e@de4dee
e@e@f      fd.ZKe>j                  d/d0dg ee)      g*       ee)      fd!e8de'fd1       ZLe>j                  d2d3dg ee)      g*       ee)      fd!e;de'fd4       ZMe>j                  d5d6dg ee)      g*       ee)      fd7e?de'fd8       ZNe>j                  d9d:dg ee)      g*       ee)      fd7e6de'fd;       ZOd<ee   dee   fd=ZPd> ZQy)@z
Allow proxy admin to add/update/delete models in the db

Currently most endpoints are in `proxy_server.py`, but those should  be moved here over time.

Endpoints here:

model/{model_id}/update - PATCH endpoint for model update.
    N)DictListLiteralOptionalTupleUnioncast)	APIRouterDependsHTTPExceptionRequeststatus)	BaseModel
ConfigDictField)verbose_proxy_logger)LITELLM_PROXY_ADMIN_NAME)CommonProxyErrorsLiteLLM_ProxyModelTableLiteLLM_TeamTableLitellmTableNamesLitellmUserRolesModelInfoDeletePrismaCompatibleUpdateDBModelProxyErrorTypesProxyExceptionTeamModelAddRequestUpdateTeamRequestUserAPIKeyAuth)user_api_key_auth)encrypt_value_helper)_is_user_team_admin)team_model_addupdate_team)create_object_audit_log)PrismaClient)UpdateUsefulLinksRequest)
DeploymentDeploymentTypedDictLiteLLMParamsTypedDictupdateDeployment)get_utc_datetimec                   F    e Zd ZU dZ ed      Zee   ed<    e	d      Z
y)UpdatePublicModelGroupsRequestz.Request model for updating public model groupsz(List of model group names to make public)descriptionmodel_groupsforbid)extraN)__name__
__module____qualname____doc__r   r0   r   str__annotations__r   model_config     y/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/proxy/management_endpoints/model_management_endpoints.pyr.   r.   =   s)    8#>L$s)  H-Lr;   r.   model_idprisma_clientreturnc                    K   t        t        t           |j                  j                  j                  d| i       d {         }|sy t        di |j                  d      }|S 7 (w)Nr=   whereTexclude_noner:   )r	   r   r   dblitellm_proxymodeltablefind_uniquer(   
model_dump)r=   r>   db_modeldeployment_pydantic_objs       r<   get_db_modelrK   G   sv      66BBx( C 
 	
H (R8+>+>D+>+QR""	
s   =A*A(
 )A*rI   updated_patchc           
      |   t        | j                  t        di | j                  j	                  d      | j
                  j	                  d            }|j                  r|j                  |d<   |j                  rZ|j                  j	                  d      j                         D ci c]  \  }}|t        |       }}}|d   j                  |       |j
                  r7d|vri |d<   |d   j                  |j
                  j	                  d             t               }d|v r|d   |d<   d|v rt        j                  |d         |d<   d|v rc|d   }|j                         D ]3  \  }}	t        |	t        j                        s!|	j                         ||<   5 t        j                  |      |d<   |S c c}}w )NTrC   )
model_namelitellm_params
model_inforN   rO   rP   r:   )r)   rN   r*   rO   rH   rP   itemsr!   updater   jsondumps
isinstancedatetime	isoformat)
rI   rL   merged_deployment_dictkvencrypted_paramsprisma_compatible_model_dictrP   keyvalues
             r<   update_db_modelr_   X   s    1&&- 
%%00d0C
 &&11t1D /</G/G|, ## &44??! @ eg	
1 #A&&
 
 	/0778HI 5535"<0|,33$$//T/B	
 $A#B --5K6
$\2 119="#34:
$%56 --+L9
$**,JC%!2!23"'//"3
3 - 6:ZZ
5K$\2''K
s   *F8z/model/{model_id}/updatezmodel management)tagsdependencies
patch_datauser_api_key_dictc                    K   ddl m}m}m}m}m} 	 |'t        ddt        j                  j                  i      |s5t        dt        j                  j                  t        j                  d      t        | |	       d{   }|x|rG|j!                  | 
      5t        dt        j                  j                  t        j                  d      t        d|  dt        j"                  t        j$                  d      t&        j)                  ||||       d{    t+        ||      }	|j,                  xs ||	d<   t/        t0        t3                     |	d<   |j4                  j6                  j9                  d| i|	       d{   }
t;                d{    t=        j>                  tA        | d|tB        jD                  |jG                  d      |
jG                  d      |j,                  tH                     |
S 7 7 7 7 o# tJ        $ ru}tM        jN                  dt1        |              tQ        |t        t        f      r|t        dt1        |       t        jR                  t        jT                  d      d}~ww xY ww)a*  
    PATCH Endpoint for partial model updates.

    Only updates the fields specified in the request while preserving other existing values.
    Follows proper PATCH semantics by only modifying provided fields.

    Args:
        model_id: The ID of the model to update
        patch_data: The fields to update and their new values
        user_api_key_dict: User authentication information

    Returns:
        Updated model information

    Raises:
        ProxyException: For various error conditions including authentication and database errors
    r   )litellm_proxy_admin_name
llm_routerpremium_userr>   store_model_in_dbN  errorstatus_codedetailz1Model updates only supported for DB-stored modelsmessagetypecodeparam)r=   r>   r=   zGCannot edit config-based model. Store model in DB via /model/new first.zModel z not found on proxy.model_paramsrc   r>   rg   )rI   rL   
updated_by
updated_atr=   rB   dataupdatedTrC   	object_idactionrc   
table_namebefore_valueafter_valuelitellm_changed_byre   zError in patch_model: zError updating model: )+litellm.proxy.proxy_serverre   rf   rg   r>   rh   r   r   db_not_connected_errorr^   r   r   validation_errorr   HTTP_400_BAD_REQUESTrK   get_deploymentnot_found_errorHTTP_404_NOT_FOUNDModelManagementAuthCheckscan_user_make_model_callr_   user_idr	   r7   r,   rE   rF   rR   clear_cacheasynciocreate_taskr%   r   PROXY_MODEL_TABLE_NAMEmodel_dump_jsonr   	Exceptionr   	exceptionrU   internal_server_errorHTTP_500_INTERNAL_SERVER_ERROR)r=   rb   rc   re   rf   rg   r>   rh   rI   update_dataupdated_modeles               r<   patch_modelr      s    6 V
 !2!I!I!O!OP  ! K$55;;00	  &x}UUj777JV$e(99??44	  ! 
*>?$44..	  (@@!/'%	 A 
 	
 	
 &xzR %%A)A 	L! %).>.@$AL! ,..FFMMx( N 
 
 m 	#" "3,CC%5545H)99t9L#4#<#<)A		
 q V$	
 
 	$  
&&)?Ax'HIa-89G,SVH5 6666	
 	

su   JA0H HBH HA+H 	H	
H HA'H JH H 	H H 	JA0JJJru   new_encryption_keyshould_create_model_in_dbc                   K   | j                   j                  d      }| j                   j                  }|j                         D ]!  \  }}t	        ||      }	|	| j                   |<   # | j
                  j                  | j                  | j                   j                  d      | j
                  j                  d      |j                  xs t        |j                  xs t        d}
| j
                  j                  | j
                  j                  |
d<   |r0|j                  j                  j                  |
       d {   }|S t        di |
}|S 7 w)NTrC   )r^   r   )r=   rN   rO   rP   
created_byrv   r=   )ry   r:   )rO   dictmodelrQ   r!   rP   idrN   r   r   r   rE   rF   creater   )ru   rc   r>   r   r   _litellm_params_dict_orignal_litellm_model_namerY   rZ   encrypted_value_datamodel_responses               r<   _add_model_to_dbr     s[     (66;;;N"."="="C"C$**,1.(:
 *9##A&	 - !++.."--&55EESWEX"--== > 
 (//K3K'//K3K	E !!-(3366j ,//GGNN  O  
 

  1959
s   E EEEc                   K   | j                   j                  }|y| j                  }|r|| j                   _        d| dt	        j
                          }|| _        t        | ||       d{   }t        t        |||i      |t        ddi      	       d{    t        t        ||g
      t        ddi      |       d{    |S 7 c7 67 
w)a  
    If 'team_id' is provided,

    - generate a unique 'model_name' for the model (e.g. 'model_name_{team_id}_{uuid})
    - store the model in the db with the unique 'model_name'
    - store a team model alias mapping {"model_name": "model_name_{team_id}_{uuid}"}
    Nmodel_name__ru   rc   r>   )team_idmodel_aliasesrp   http)scope)ry   rc   http_request)r   models)ry   r   rc   )rP   r   rN   team_public_model_nameuuiduuid4r   r$   r   r   r#   r   )ru   rc   r>   _team_idoriginal_model_nameunique_model_namer   s          r<   _add_team_model_to_dbr   7  s     &&..H&119L6%hZq?/L ,!+# N .0AB
 ,FF#34    '(
 FF#34+   5s6   A+C-C..CC-C
CCCCc                       e Zd ZdZe	 	 ddededee   de	de
d   f
d	       Zed
eeef   dedede	de
d   f
d       Zed
ededede	de
d   f
d       Zy)r   z;
    Common auth checks for model management endpoints
    Nr   rc   team_objrg   r?   Tc                    |du r't        ddt        j                  j                  i      |j                  r|j                  t
        j                  k(  ry|t        ||      s)t        dddj                  | |j                        i      y)NF  rj   rk   T)rc   r   zTeam ID={} does not match the API key's team ID={}, OR you are not the admin for this team. Check `/user/info` to verify your team admin status.)
r   r   not_premium_userr^   	user_roler   PROXY_ADMINr"   formatr   r   rc   r   rg   s       r<   can_user_make_team_model_callz7ModelManagementAuthChecks.can_user_make_team_model_callq  s     5 !2!C!C!I!IJ 
 ''!++/?/K/KK%8/(&
    p  w  w!2!:!:  r;   ru   r>   c                 T  K   | j                   | j                   j                  y| j                   j                  +|dur't        ddt        j                  j
                  i      |j                  j                  j                  d| j                   j                  i       d {   }|2t        dddj                  | j                   j                        i      t        d
i |j                         }t        j                  | j                   j                  |||	       y7 w)NTr   rj   rk   r   rA     Team id={} does not exist in dbr   r:   )rP   r   r   r   r   r^   rE   litellm_teamtablerG   r   r   rH   r   r   )ru   rc   r>   rg   _existing_team_rowexisting_team_rows         r<   allow_team_model_actionz1ModelManagementAuthChecks.allow_team_model_action  s1     ""*l.E.E.M.M.U""**6<t;S!2!C!C!I!IJ 
 $1#3#3#E#E#Q#Ql55==> $R $
 
 %>EE$//77  .P0B0M0M0OP!?? ++33/&%	 	@ 	
 +
s   B$D(&D&'B D(c                 Z  K   | j                   | j                   j                  |j                  j                  j	                  d| j                   j                  i       d {   }|2t        dddj                  | j                   j                        i      t        di |j                         }t        j                  | j                   j                  |||      S |j                  t        j                  k7  r(t        ddd	j                  |j                        i      y
7 ĭw)Nr   rA   r   rj   r   rk   r   r   zUser does not have permission to make this model call. Your role={}. You can only make model calls if you are a PROXY_ADMIN or if you are a team admin, by specifying a team_id in the model_info.Tr:   )rP   r   rE   r   rG   r   r   r   rH   r   r   r   r   r   )ru   rc   r>   rg   team_obj_rowr   s         r<   r   z2ModelManagementAuthChecks.can_user_make_model_call  s@     ##/''//;!.!1!1!C!C!O!O ,"9"9"A"AB "P " L ## #!B!I!I(33;;"  )E<+B+B+DEH,JJ$//77"3!)	 K   ((,<,H,HH  b  i  i)33  ?s   A"D+$D)%CD+)NF)r3   r4   r5   r6   staticmethodr7   r   r   r   boolr   r   r   r(   r+   r&   r   r   r:   r;   r<   r   r   l  s      15"	) ,- 	
 
 : #J(889#)# $# 	#
 
# #J , ,), $, 	,
 
, ,r;   r   z/model/deletez;Allows deleting models in the model list in the config.yaml)r/   r`   ra   rP   c                   K   ddl m} 	 	 ddl m}m}m}m} |t        dddi      |j                  j                  j                  d| j                  i	       d {   }|t        d
dd| j                   di      t        d$i |j                         }t        j                  ||||       d {    |j                  j                  t!        |j"                  |       d {   }|D 	
cg c]   \  }	}
|	|j                  j                  k(  r|
" }}	}
|j                  j$                  j                  d|j                  j                  i	       d {   }|t|j&                  D 
cg c]  }
|
|vr|

 c}
|_        |j                  j$                  j)                  d|j                  j                  id|j&                  i       d {    |du r	 |j                  j                  j+                  d| j                  i	       d {   }|t        d
dd| j                   di      ||j-                  | j                         t/        j0                  t3        | j                  d|t4        j6                  |j9                  d      d |j:                  t<                     dd|j>                   diS t        dddi      7 t7 7 c c}
}	w 7 c c}
w 7 7 # t@        $ r}tC        jD                  dtG        |              tI        |t
              rYtK        tM        |ddtG        |       d      tN        jP                  tM        |dd       tM        |d!tR        jT                        "      tI        |tJ              r|tK        d#tG        |      z   tN        jP                  tM        |dd       tR        jT                  "      d }~ww xY ww)%Nr   )rf   )rf   rg   r>   rh   ri   rj   VNo DB Connected. Here's how to do it - https://docs.litellm.ai/docs/proxy/virtual_keysrk   r=   rA   r   zModel with id=z not found in dbrt   )public_model_namer>   r   r   rx   T)r   deletedrC   r{   ro   zModel: z deleted successfullyCSet `'STORE_MODEL_IN_DB='True'` in your env to enable this feature.z'Failed to delete model. Due to error - rm   Authentication Error()rr   Nonerl   ro   rp   rr   rq   Authentication Error, r:   )+r   rf   rg   r>   rh   r   rE   rF   rG   r   r(   rH   r   r   rP   r   delete_team_model_aliasrN   r   r   rR   deletedelete_deploymentr   r   r%   r   r   r   r   r   r=   r   r   r   r7   rU   r   getattrr   
auth_errorr   r   )rP   rc   rf   rg   r>   rh   model_in_dbru   removed_model_aliasesr   r   valid_team_model_aliasesr   resultr   s                  r<   delete_modelr     s!     6B
		
 	
  u  *,,DDPPz}}- Q 
 
 >*--@P!QR 
 "=K$:$:$<='@@%/'%	 A 
 	
 	
 ""**6*A"."9"9++ %! '<("GUl55=== ($ ( '4&6&6&H&H&T&T ,"9"9"A"AB 'U ' ! !, "3!9!9,$<< ,!( $&&88??$l&=&=&E&EF"$5$<$<= @    $
 )++CCJJ!:==1 K  F ~# ##~jmm_DT%UV  %,,
,> '(mm$&70GG!'!7!7T!7!J $'8'@'@-E	 (99NOPPb e
	
%
(!,H  
&&5c!fX>	
 a' 8/DSVHA-NO$//a&1Qv/J/JK	  >*G,s1v5 ++!Wf-,,	
 	

s   OAK- KAK- 6K75K- ,K-	K- 6%KAK- K K- 2K#?AK- K(>K- K+B.K- OK- K- K- K- #K- +K- -	O6COOOr   c           
        K   |j                   j                  j                  ddi       d{   }g }g }|D ]  }|j                  }|j                  }| |j                         v s.t        |j                               t        |j                               j                  |          }|j                  '|j                  |j                  j                  |f       ||= |j                  |j                   j                  j                  d|idt        j                  |      i              t        j                   |  d{    |S 7 7 	w)z
    Delete a team model alias

    Iterate through all team model aliases and delete the one that matches the model_id

    Returns:
    - List of team id + model alias pairs that were removed
    teamT)includeNr   r   rx   )rE   litellm_modeltable	find_manyr   r   valueslistkeysindexr   appendr   rR   rS   rT   r   gather)	r   r>   team_model_aliasestasksr   team_model_aliasr   r   r]   s	            r<   r   r   w  sI      -//BBLL  M    E.(66   4 4 66}))+,]))+,223DEC  $$0%,,.>.C.C.K.KS-QRc"LL  33::*)4::m+DE ;  /" ..%
     1, !s(   ,EE
7E'CEEEEz
/model/newz=Allows adding new models to the model list in the config.yamlc                   K   ddl m}m}m}m}m}m} 	 |t        dddi      t        j                  | |||       d {    d }|du r	 	 | j                  }	| j                  j                  t        | ||	       d {   }nt        | ||	       d {   }|j                  ||
       d {    |j!                  dg       xs g }
d|
v r:|j"                  j%                  | j                  |	| j                         d {    nt        dddi      |t        dddi      t-        j.                  t1        |j2                  d|t4        j6                  d t9        |t:              r|j=                  d      nd |j>                  t@                     |S 7 \7 7 7 7 # t&        $ r"}t)        j*                  d|        Y d }~d }~ww xY w# t&        $ r}t)        j*                  djC                  tE        |                   t9        |t              rYtG        tI        |ddtE        |       d      tJ        jL                  tI        |dd      tI        |dtN        jP                              t9        |tF              r|tG        dtE        |      z   tJ        jL                  tI        |dd      tN        jP                        d }~ww xY ww)Nr   )general_settingsrg   r>   proxy_configproxy_logging_objrh   ri   rj   r   rk   rt   Tr   r>   r   alertingslack)rN   litellm_model_namepassed_model_infozException in add_new_model: r   zCFailed to add model to db. Check your server logs for more details.createdrC   r{   zBlitellm.proxy.proxy_server.add_new_model(): Exception occured - {}rm   r   r   rr   r   rl   r   r   ))r   r   rg   r>   r   r   rh   r   r   r   rN   rP   r   r   r   add_deploymentgetslack_alerting_instancemodel_added_alertr   r   r   r   r   r%   r=   r   r   rU   r   r   r   r   r   r7   r   r   r   r   r   r   )ru   rc   r   rg   r>   r   r   rh   r   _original_litellm_model_name	_alertingr   s               r<   add_new_modelr     s     n
 u  (@@%/'%	 A 
 	
 	
 =A$
S/;/F/F,**22:+;%1*;&3, &N ,A%1*;&3, &N
 #11"/CT 2    -00R@FB	i'+CCUU#/#:#:+G*6*A*A V     b  !b  	#(11 "3,CC! ".)< #222E#4#<#<)A	
" ]	
$&&

  S$..1MaS/QRRSN  
&&PWWA	

 a' 8/DSVHA-NO$//a&1Qv/J/JK	  >*G,s1v5 ++!Wf-,,	
 	

s   K-G FG 3F* F F* F#F* 3F&4AF* F(F* BG KG  F* #F* &F* (F* *	G3GG GG 	K!C*KKKz/model/updatezEdit existing model paramsc                 B  K   ddl m}m}m}m}m} 	 |t        dddi      d}t        | dd      }|t        d	      |j                  }|t        d
      |j                  j                  j                  d|i       d{   }	|	.|!|j                  |      t        dddi      t        d      t        d#i |	j                         }
t         j#                  |
|||       d{    |du rt%        |	j&                        }| j&                  t        d      | j&                  j%                  d      }|j)                         D ]   \  }}t+        |      }|| j&                  |<   " i }| j&                  j%                         }|j)                         D ]  \  }}||||<   ||v r||   	||   ||<    ! t-        j.                  |      |j0                  xs |d}|j                  j                  j3                  d|i|       d{   }t5        j6                  t9        |d|t:        j<                  t?        |	t@              r|	jC                  d      ndt?        |t@              r|jC                  d      nd|j0                  |             |S y7 7 7 # t        $ r}tE        jF                  djI                  tK        |                   t?        |t              rYtM        t        |ddtK        |       d      tN        jP                  t        |dd      t        |d tR        jT                        !      t?        |tL              r|tM        d"tK        |      z   tN        jP                  t        |dd      tR        jT                  !      d}~ww xY ww)$z
    Old endpoint for model update. Makes a PUT request.

    Use `/model/{model_id}/update` to PATCH the stored model in db.
    r   )r   rf   rg   r>   rh   Nri   rj   r   rk   rP   zmodel_info not providedzmodel_info.id not providedr=   rA   rs   r   zOCan't edit model. Model in config. Store model in db via `/model/new`. to edit.zmodel not foundrt   Tzlitellm_params not providedrC   )r^   )rO   rv   rx   rz   r{   zAlitellm.proxy.proxy_server.update_model(): Exception occured - {}rm   r   r   rr   r   rl   r   r   r:   )+r   r   rf   rg   r>   rh   r   r   r   r   rE   rF   rG   r   r(   rH   r   r   r   rO   rQ   r!   rS   rT   r   rR   r   r   r%   r   r   rU   r   r   r   r   r   r7   r   r   r   r   r   )ru   rc   r   rf   rg   r>   rh   	_model_id_model_info_existing_litellm_params
deployment_existing_litellm_params_dict_new_litellm_params_dictrY   rZ   r   merged_dictionary_mpr]   r^   r   r   r   s                          r<   update_modelr	  $  s     C
 u  	lL$?566NN	899  ""::FF!9- G   	! $+&--y-AM# #!r    122H":"E"E"GH
'@@#/'%	 A 
 	
 	
 $,0(77-) **2 =>>'3'B'B'G'G! (H ($
 16681"6Q"?1@++A. 9
 !#--224C!iik
U$-2%c*885c:F-J3-O%c* * #'**->"?/77S;SE $1#3#3#K#K#R#R!9- $S $ N ''$&70GG &&>	J 1@@d@S! &ni@ '66D6I!'8'@'@-E!* "! %9(	
X8  
&&OVVA	

 a' 8/DSVHA-NO$//a&1Qv/J/JK	  >*G,s1v5 ++!Wf-,,	
 	

sc   NA1J$ JA)J$ -J.D$J$ J"BJ$ NJ$ J$ "J$ $	N-C*NNNz/model_group/make_publicz$Update which model groups are publicrequestc                   K   	 ddl }ddlm} |j                  t        j
                  k7  r(t        dddj                  |j                        i      | j                  |_	        |j                          d{   }d|vri |d<   | j                  |d   d	<   |j                  |
       d{    t        j                  d| j                   d|j                          d| j                  |j                  dS 7 7 O# t        $ ro}t        j                   dt#        |              t%        |t              r|t'        dt#        |       t(        j*                  t,        j.                  d      d}~ww xY ww)a  
    Update which model groups are public.

    This endpoint allows admins to specify which model groups should be publicly accessible.
    Public model groups are visible via the /public/model_hub endpoint.

    Args:
        request: Request containing list of model group names to make public
        user_api_key_dict: User authentication information

    Returns:
        Success message with updated public model groups

    Raises:
        ProxyException: For various error conditions including authentication errors
    r   Nr   r   rj   >Only proxy admins can update public model groups. Your role={}rk   litellm_settingspublic_model_groups
new_configz Updated public model groups to: 
 by user: z(Successfully updated public model groups)ro   r  rv   $Error updating public model groups: rn   )litellmr   r   r   r   r   r   r   r0   r  
get_configsave_configr   infor   r   r   r7   rU   r   r   r   r   r   r
  rc   r  r   configr   s         r<   update_public_model_groupsr    s    43
; &&*:*F*FF]dd)33  '.&:&:# $..00 V+)+F%&<C<P<P!"#89 &&&&999!!.w/C/C.DJO`OhOhNij	

 B#*#7#7+33
 	
 1 	:  
&&)McRSfX'VWa'G:3q6(C 6666	
 	

O   E9A3C> 7C:84C> ,C<-AC> 9E9:C> <C> >	E6A*E11E66E9z/model_hub/update_useful_linkszUpdate useful linksc                   K   	 ddl }ddlm} |j                  t        j
                  k7  r(t        dddj                  |j                        i      | j                  |_	        |j                          d{   }d|vri |d<   | j                  |d   d	<   |j                  |
       d{    t        j                  d| j                   d|j                          d| j                  |j                  dS 7 7 O# t        $ ro}t        j                   dt#        |              t%        |t              r|t'        dt#        |       t(        j*                  t,        j.                  d      d}~ww xY ww)z
    Update useful links.
    r   Nr  r   rj   r  rk   r  public_model_groups_linksr  zUpdated useful links to: r  z!Successfully updated useful links)ro   useful_linksrv   r  rn   )r  r   r   r   r   r   r   r   r  r  r  r  r   r  r   r   r   r7   rU   r   r   r   r   r   r  s         r<   update_useful_linksr    s    3
; &&*:*F*FF]dd)33  -4,@,@) $..00 V+)+F%&BIBVBV!"#>? &&&&999!!'(<(<'=ZHYHaHaGbc	

 ;#00+33
 	
 1 	:  
&&)McRSfX'VWa'G:3q6(C 6666	
 	

r  r   c                     t               }g }| D ]N  }|j                  di       j                  dd      }|(||vs-|j                  |       |j                  |       P |S )a"  
    Deduplicate models based on their model_info.id field.
    Returns a list of unique models keeping only the first occurrence of each model ID.

    Args:
        models: List of model dictionaries containing model_info

    Returns:
        List of deduplicated model dictionaries
    rP   r   N)setr   r   add)r   seen_idsunique_modelsr   r=   s        r<   "_deduplicate_litellm_router_modelsr%  T  sf     uHM99\2.224>HH$<  'LL"	 
 r;   c                  b  K   ddl m} m}m}m}m} | | |j                  d       y	 | j                  j                          | j                  j                          |j                  ||       d{    y7 # t        $ r(} |j                  dt        |              Y d}~yd}~ww xY ww)z0
    Clear router caches and reload models.
    r   )rf   r>   r   r   r   Nz9llm_router or prisma_client is None, skipping cache clearr   z8Failed to clear cache and reload models. Due to error - )r   rf   r>   r   r   r   debug
model_listclearauto_routersr   r   r   r7   )rf   r>   r   r   r   r   s         r<   r   r   i  s       ]2"""G	
 	

##%%%'))';L * 
 	
 	
  
&&&Fs1vhO	
 	

sA   &B/A
A; 3A94A; 8B/9A; ;	B,B'"B/'B,,B/)NT)Rr6   r   rV   rS   r   typingr   r   r   r   r   r   r	   fastapir
   r   r   r   r   pydanticr   r   r   litellm._loggingr   litellm.constantsr   litellm.proxy._typesr   r   r   r   r   r   r   r   r   r   r   r   $litellm.proxy.auth.user_api_key_authr    0litellm.proxy.common_utils.encrypt_decrypt_utilsr!   /litellm.proxy.management_endpoints.common_utilsr"   1litellm.proxy.management_endpoints.team_endpointsr#   r$   +litellm.proxy.management_helpers.audit_logsr%   litellm.proxy.utilsr&   Clitellm.types.proxy.management_endpoints.model_management_endpointsr'   litellm.types.routerr(   r)   r*   r+   litellm.utilsr,   routerr.   r7   rK   r_   patchr   r   r   r   r   postr   r   r   r	  r  r  r%  r   r:   r;   r<   <module>r=     s       D D D F F 1 1 1 6    C Q O P ,  +	.Y .##".#j#"6(6()96("6(r 
	+,-   )00A(Bt
t
 t
 &t

t
B )-&*!!%!  ! !	!
  $! %&!H22%2  2 %&	2jv vt M
	+,-	   )00A(BH
H
%H
H
V$!$!$! 
%S/$!P O
	+,-	   )00A(B{
{
%{
{
~ ,
	+,-	   )00A(BT
"T
%T
T
n 6
	+,-	   )00A(BG
+G
%G
G
T $%
	+,-	   )00A(B:
%:
%:
:
ztDz d4j *
r;   