
    ^hQ              	          S r SSKrSSKJr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7  SSKJr  \" 5       r\R%                  SS	/\" \5      /S
S9\R%                  SS	/\" \5      /S9S\4S j5       5       r\R%                  SS	/\" \5      /S
S9\R%                  SS	/\" \5      /S9S\4S j5       5       rS\S\\   4S jr\R%                  SS	/S
\" \5      /S9\R%                  SS	/\" \5      /S9\" \5      4S\S\4S jj5       5       r\R7                  SS	/\" \5      /\S9\R7                  SS	/S
\" \5      /S9\R:                  " SS94S\4S  jj5       5       r\R%                  S!S	/\" \5      /S9\R%                  S"S	/S
\" \5      /S9\" \5      4S\ S\4S# jj5       5       r!\R%                  S$S	/\" \5      /S9\R%                  S%S	/S
\" \5      /S9\" \5      4S\"S\4S& jj5       5       r#\R7                  S'S	/\" \5      /\\   S9\R7                  S(S	/S
\" \5      /S9\" \5      4S)\	S\4S* jj5       5       r$g)+z}
CUSTOMER MANAGEMENT

All /customer management endpoints 

/customer/new   
/customer/info
/customer/update
/customer/delete
    N)ListOptional)	APIRouterDependsHTTPExceptionRequeststatus)verbose_proxy_logger)*)user_api_key_authz/end_user/blockzCustomer ManagementF)tagsdependenciesinclude_in_schemaz/customer/block)r   r   datac                   #    SSK Jn   / nUb[  U R                   HJ  nUR                  R                  R                  SU0USS.SS0S.S	9I Sh  vN nUR                  U5        ML     O[        S
SS0S9eSU0$  N*! [         a<  n[        R                  " S[        U5       35        [        S
S[        U5      0S9eSnAff = f7f)az  
[BETA] Reject calls with this end-user id

Parameters:
- user_ids (List[str], required): The unique `user_id`s for the users to block

    (any /chat/completion call with this user={end-user-id} param, will be rejected.)

    ```
    curl -X POST "http://0.0.0.0:8000/user/block"
    -H "Authorization: Bearer sk-1234"
    -d '{
    "user_ids": [<user_id>, ...]
    }'
    ```
r   prisma_clientNuser_idT)r   blockedr   )createupdatewherer     errorzPostgres DB Not connectedstatus_codedetailblocked_userszAn error occurred - )litellm.proxy.proxy_serverr   user_idsdblitellm_endusertableupsertappendr   	Exceptionr
   r   str)r   r   recordsidrecordes         o/home/james-whalen/.local/lib/python3.13/site-packages/litellm/proxy/management_endpoints/customer_endpoints.py
block_userr-      s     8 9G$mm,//DDKK$b/.0T"B#,d"3  L    v& $  !<= 
  ))  G""%9#a&#BCWc!f4EFFGs;   CAA< A:)A< 9C:A< <
C7B==CCz/end_user/unblockz/customer/unblockc                   ^#    SSK Jm  [        U4S j[        R                   5       5      (       a  [        R
                  c  [        SSS0S9e[        [        R
                  [        5      (       a3  U R                   H"  n[        R
                  R                  U5        M$     O[        SSS	0S9eS
[        R
                  0$ 7f)z
[BETA] Unblock calls with this user id

Example
```
curl -X POST "http://0.0.0.0:8000/user/unblock"
-H "Authorization: Bearer sk-1234"
-d '{
"user_ids": [<user_id>, ...]
}'
```
r   )_ENTERPRISE_BlockedUserListc              3   <   >#    U  H  n[        UT5      v   M     g 7f)N)
isinstance).0xr/   s     r,   	<genexpr>unblock_user.<locals>.<genexpr>n   s     VDUq
19::DUs     r   z:Blocked user check was never set. This call has no effect.r   r   zF`blocked_user_list` must be set as a list. Filepaths can't be updated.r   )-enterprise.enterprise_hooks.blocked_user_listr/   anylitellm	callbacksblocked_user_listr   r1   listr!   remove)r   r)   r/   s     @r,   unblock_userr>   Q   s     0
 VGDUDUVVV$$,U
 	
 '++T22--B%%,,R0   a
 	
 W6677s   C Creturnc                     [         R                  R                  5       n0 nU H   nUS:X  a  M  [        XS5      nUc  M  XBU'   M"     U(       a  [        S0 UD6$ g)z=
Return a new budget object if new budget params are passed.
	budget_idN )	BudgetNewmodel_fieldskeysgetattr)r   budget_paramsbudget_kv_pairs
field_namevalues        r,   new_budget_requestrK      sd     **//1MO $
$$/*/J' $ +?++    z/end_user/new)r   r   r   z/customer/newuser_api_key_dictc                 F  #     SSK JnJnJn  Uc$  [	        SS[
        R                  R                  0S9e U R                  b  Uc$  [	        SS[
        R                  R                  0S9eU R                  UR                  5       ;  a=  [	        SSSR                  U R                  [        UR                  5       5      5      0S9e0 n[        U 5      nUbv   UR                  R                  R!                  0 UR#                  S	S
9EUR$                  =(       d    UUR$                  =(       d    US.ES9I Sh  vN nUR*                  US'   OU R*                  b  U R*                  US'   U R-                  S	S9n	U	R/                  5        H-  u  pU
[0        R2                  R5                  5       ;  d  M)  XU
'   M/     UR                  R6                  R!                  USS	0S9I Sh  vN nU$  N! [&         a  n[	        SS[)        U5      0S9eSnAff = f N/! [&         Ga  n[8        R:                  " SR                  [)        U5      5      5        S[)        U5      ;   a  [=        SU R$                   S3SSSS9e[?        U[        5      (       aH  [=        [A        USS[)        U5       S35      S[A        USS5      [A        US[B        RD                  5      S 9e[?        U[<        5      (       a  Ue[=        S![)        U5      -   S[A        USS5      [B        RD                  S 9eSnAff = f7f)"a  
Allow creating a new Customer 


Parameters:
- user_id: str - The unique identifier for the user.
- alias: Optional[str] - A human-friendly alias for the user.
- blocked: bool - Flag to allow or disallow requests for this end-user. Default is False.
- max_budget: Optional[float] - The maximum budget allocated to the user. Either 'max_budget' or 'budget_id' should be provided, not both.
- budget_id: Optional[str] - The identifier for an existing budget allocated to the user. Either 'max_budget' or 'budget_id' should be provided, not both.
- allowed_model_region: Optional[Union[Literal["eu"], Literal["us"]]] - Require all user requests to use models in this specific region.
- default_model: Optional[str] - If no equivalent model in the allowed region, default all requests to this model.
- metadata: Optional[dict] = Metadata for customer, store information for customer. Example metadata = {"data_training_opt_out": True}
- budget_duration: Optional[str] - Budget is reset at the end of specified duration. If not set, budget is never reset. You can set duration as seconds ("30s"), minutes ("30m"), hours ("30h"), days ("30d").
- tpm_limit: Optional[int] - [Not Implemented Yet] Specify tpm limit for a given customer (Tokens per minute)
- rpm_limit: Optional[int] - [Not Implemented Yet] Specify rpm limit for a given customer (Requests per minute)
- max_parallel_requests: Optional[int] - [Not Implemented Yet] Specify max parallel requests for a given customer.
- soft_budget: Optional[float] - [Not Implemented Yet] Get alerts when customer crosses given budget, doesn't block requests.


- Allow specifying allowed regions 
- Allow specifying default model

Example curl:
```
curl --location 'http://0.0.0.0:4000/customer/new'         --header 'Authorization: Bearer sk-1234'         --header 'Content-Type: application/json'         --data '{
        "user_id" : "ishaan-jaff-3",
        "allowed_region": "eu",
        "budget_id": "free_tier",
        "default_model": "azure/gpt-3.5-turbo-eu" <- all calls from this user, use this model? 
    }'

    # return end-user object
```

NOTE: This used to be called `/end_user/new`, we will still be maintaining compatibility for /end_user/XXX for these endpoints
r   )litellm_proxy_admin_name
llm_routerr   Nr   r   r   i  zmDefault Model not on proxy. Configure via `/model/new` or config.yaml. Default_model={}, proxy_model_names={}T)exclude_unset)
created_by
updated_by)r   rA   exclude_nonelitellm_budget_table)r   includez\litellm.proxy.management_endpoints.customer_endpoints.new_end_user(): Exception occured - {}z3Unique constraint failed on the fields: (`user_id`)z(Customer already exists, passed user_id=z. Please pass a new user_id.bad_requestr6   r   )messagetypecodeparamr   Internal Server Error()internal_errorr\   Noner   rY   rZ   r\   r[   Internal Server Error, )#r    rO   rP   r   r   CommonProxyErrorsdb_not_connected_errorrJ   default_modelno_llm_routerget_model_namesformatsetrK   r"   litellm_budgettabler   
model_dumpr   r&   r'   rA   dictitemsrC   rD   rE   r#   r
   	exceptionProxyExceptionr1   rF   r	   HTTP_500_INTERNAL_SERVER_ERROR)r   rM   rO   rP   r   new_end_user_obj_new_budgetbudget_recordr+   
_user_datakvend_user_records                r,   new_end_userrx      sO    n
  .EEKKL
 	
Q
 )!# ##%6%D%D%J%JK  ##:+E+E+GG# #  "Q  "X  "X ..J4N4N4P0Q"  "$ )."
O&3&6&6&J&J&Q&Q%00t0D&7&?&?&[C[&7&?&? '43	 'R ' ! -:,C,C[)^^',0NN[)YYDY1
$$&DA	..3355&'# '
 !. 0 0 E E L L!+T2 !M !
 

 7!  O#Wc!f<MNNO
  
&&jqqA	

 ACFJ B4<<.Plm"	  a'' 8/Ec!fXQ-OP%a&1Qv/T/TU	  >**G-A6!!Wf-66	
 	
/
st   3L!BH A G) 6G'7G) ;A0H /1H  H!H &L!'G) )
H3HHH LC<LLL!z/customer/info)r   r   response_modelz/end_user/infoz%End User ID in the request parameters)descriptionend_user_idc                 "  #    SSK Jn  Uc$  [        SS[        R                  R
                  0S9eUR                  R                  R                  SU 0SS	0S
9I Sh  vN nUc  [        SSSR                  U 5      0S9eUR                  S	S9$  N17f)aE  
Get information about an end-user. An `end_user` is a customer (external user) of the proxy.

Parameters:
- end_user_id (str, required): The unique identifier for the end-user

Example curl:
```
curl -X GET 'http://localhost:4000/customer/info?end_user_id=test-litellm-user-4'         -H 'Authorization: Bearer sk-1234'
```
r   r   Nr   r   r   r   rV   T)r   rW   r6   z#End User Id={} does not exist in dbrT   )r    r   r   rc   rd   rJ   r"   r#   
find_firstrh   rk   )r{   r   	user_infos      r,   end_user_infor   ;  s     : 9.EEKKL
 	

 $&&;;FF+&1G0N G  I BII+VW
 	
 T22s   ABB2Bz/customer/updatez/end_user/updatec                   #    SSK Jn   U R                  5       nUc  [        S5      e0 nUR	                  5        H  u  pVUc  M
  U/ 0 S4;  d  M  XdU'   M     [
        R                  " SU 5        U R                  b  [        U R                  5      S:  a  U R                  US'   [
        R                  " S5        UR                  R                  R                  SU R                  0US9I Sh  vN nUc  [        S	U R                   35      e[
        R                  " S
U 35        U$ [        SU R                   35      e NR! [         Ga  n[
        R                  " SR                  [        U5      5      5        [
        R                  " [         R"                  " 5       5        [%        U[&        5      (       aH  [)        [+        USS[        U5       S35      S[+        USS5      [+        US[,        R.                  5      S9e[%        U[(        5      (       a  Ue[)        S[        U5      -   S[+        USS5      [,        R.                  S9eSnAff = f7f)a3  
Example curl 

Parameters:
- user_id: str
- alias: Optional[str] = None  # human-friendly alias
- blocked: bool = False  # allow/disallow requests for this end-user
- max_budget: Optional[float] = None
- budget_id: Optional[str] = None  # give either a budget_id or max_budget
- allowed_model_region: Optional[AllowedModelRegion] = (
    None  # require all user requests to use models in this specific region
)
- default_model: Optional[str] = (
    None  # if no equivalent model in allowed region - default all requests to this model
)

Example curl:
```
curl --location 'http://0.0.0.0:4000/customer/update'     --header 'Authorization: Bearer sk-1234'     --header 'Content-Type: application/json'     --data '{
    "user_id": "test-litellm-user-4",
    "budget_id": "paid_tier"
}'

See below for all params 
```
r   r   NNot connected to DB!z$/customer/update: Received data = %sr   z,In update customer, user_id condition block.r   zEFailed updating customer data. User ID does not exist passed user_id=8received response from updating prisma client. response=&user_id is required, passed user_id = zDlitellm.proxy.proxy_server.update_end_user(): Exception occured - {}r   r]   r^   r_   r\   r`   r   ra   rb   )r    r   jsonr&   rm   r
   debugr   lenr"   r#   r   
ValueErrorr   rh   r'   	traceback
format_excr1   r   ro   rF   r	   rp   )	r   rM   r   	data_jsonnon_default_valuesru   rv   responser+   s	            r,   update_end_userr   l  s8    Z 99
))+	 233  OO%DA}+ "
 )*1% & 	""#I4P<<#DLL(9A(=,0LLy) &&'UV*--BBII $,,/6H J  H  [\`\h\h[ij  !&&J8*U OEdll^TUU  
""RYYA	

 	""9#7#7#9:a'' 8/Ec!fXQ-OP%a&1Qv/T/TU	  >**G-A6!!Wf-66	
 	
!
sH   I6E E BE ,D?-9E &I'E IC<IIIz/customer/deletez/end_user/deletec                   #    SSK Jn   Uc  [        S5      e[        R                  " SU 5        U R
                  Gb  [        U R
                  [        5      (       a  [        U R
                  5      S:  a  UR                  R                  R                  SSU R
                  00S9I Sh  vN nUc  [        S	U R
                   35      eU[        U R
                  5      :w  a2  [        S
U R
                   SU S[        U R
                  5       S35      e[        R                  " SU 35        US[        U R
                  5      -   S.$ [        SU R
                   35      e N! [         Ga  n[        R                  " SR                  [        U5      5      5        [        R                  " [         R"                  " 5       5        [        U[$        5      (       aH  ['        [)        USS[        U5       S35      S[)        USS5      [)        US[*        R,                  5      S9e[        U[&        5      (       a  Ue['        S[        U5      -   S[)        USS5      [*        R,                  S9eSnAff = f7f)a  
Delete multiple end-users.

Parameters:
- user_ids (List[str], required): The unique `user_id`s for the users to delete

Example curl:
```
curl --location 'http://0.0.0.0:4000/customer/delete'         --header 'Authorization: Bearer sk-1234'         --header 'Content-Type: application/json'         --data '{
        "user_ids" :["ishaan-jaff-5"]
}'

See below for all params 
```
r   r   Nr   z$/customer/delete: Received data = %sr   in)r   zEFailed deleting customer data. User ID does not exist passed user_id=zIFailed deleting all customer data. User ID does not exist passed user_id=z
. Deleted z customers, passed z
 customersr   z)Successfully deleted customers with ids: )deleted_customersrY   r   zDlitellm.proxy.proxy_server.delete_end_user(): Exception occured - {}r   r]   r^   r_   r\   r`   r   ra   rb   )r    r   r&   r
   r   r!   r1   r<   r   r"   r#   delete_manyr   r'   r   rh   r   r   r   ro   rF   r	   rp   )r   rM   r   r   r+   s        r,   delete_end_userr     s~    B 96
 233""#I4PMM%4==$//DMM"Q&*--BBNN 4"78 O  H  [\`\i\i[jk  3t}}-- _`d`m`m_nnx  zB  yC  CV  WZ  [_  [h  [h  Wi  Vj  jt  u  !&&J8*U &.Fdmm$%  Edmm_UVV).  
""RYYA	

 	""9#7#7#9:a'' 8/Ec!fXQ-OP%a&1Qv/T/TU	  >**G-A6!!Wf-66	
 	
!
s=   I1BE" )E *BE" I1E" "I.-C<I))I..I1z/customer/listz/end_user/listhttp_requestc           	        #    SSK Jn  UR                  [        R                  :w  aC  UR                  [        R
                  :w  a%  [        SSSR                  UR                  5      0S9eUc$  [        SS[        R                  R                  0S9eUR                  R                  R                  S	S
0S9I Sh  vN n/ nU H+  nUR                  [        S0 UR!                  5       D65        M-     U$  N97f)z
[Admin-only] List all available customers

Example curl:
```
curl --location --request GET 'http://0.0.0.0:4000/customer/list'         --header 'Authorization: Bearer sk-1234'
```

r   r   i  r   z&Admin-only endpoint. Your user role={}r   Nr6   rV   T)rW   rB   )r    r   	user_roleLitellmUserRolesPROXY_ADMINPROXY_ADMIN_VIEW_ONLYr   rh   rc   rd   rJ   r"   r#   	find_manyr%   LiteLLM_EndUserTablerk   )r   rM   r   r   returned_responseitems         r,   list_end_userr   5  s     4 9 	##'7'C'CC''+;+Q+QQAHH%//
 	
 .EEKKL
 	

 #%%::DD'. E  H 57  !5!J8I!JK s   B7C59C3::C5)%__doc__r   typingr   r   fastapir   r   r   r   r	   r9   litellm._loggingr
   litellm.proxy._types$litellm.proxy.auth.user_api_key_authr   routerpost
BlockUsersr-   r>   NewCustomerRequestrC   rK   UserAPIKeyAuthrx   getr   Queryr'   r   UpdateCustomerRequestr   DeleteCustomerRequestr   r   rB   rL   r,   <module>r      s  	  !  F F  1 " B	 
	 +,-	   
	 +,-  
(G: (G(GV 
	 +,-	   
	 +,-  
'8Z '8'8T/ HY4G ( 
	 +,-	   
	 +,-   )00A(BS

S
%S
S
l 
	 +,-'	   
	 +,-	   }};"3"3"3J 
	 +,-  
 
	 +,-	   )00A(B^	
^	%^	^	B 
	 +,-  
 
	 +,-	   )00A(BO	
O	%O	O	d 
	 +,-,-	   
	 +,-	   )00A(B**%**rL   