
    h&                        d Z ddlmZ ddlmZmZmZ ddlmZ ddl	 ddl
mZ ddlmZ  e       Zej                  dd	g ee      g
       ee      fdedefd       Zej                  dd	g ee      g
       ee      fdedefd       Zej                  dd	g ee      g
      defd       Zej-                  dd	g ee      g
       ee      fdedefd       Zej-                  dd	g ee      g
       ee      fdefd       Zej                  dd	g ee      g
       ee      fdedefd       Zy)z
BUDGET MANAGEMENT

All /budget management endpoints 

/budget/new   
/budget/info
/budget/update
/budget/delete
/budget/settings
/budget/list
    )	timedelta)	APIRouterDependsHTTPException)duration_in_seconds)*)user_api_key_auth)jsonify_objectz/budget/newzbudget management)tagsdependencies
budget_objuser_api_key_dictc                   K   ddl m}m} |'t        ddt        j
                  j                  i      | j                  F| j                  :t        j                         t        t        | j                              z   | _        | j                  d	
      }t        |      }|j                  j                   j#                  i ||j$                  xs ||j$                  xs |d       d{   }|S 7 w)a  
    Create a new budget object. Can apply this to teams, orgs, end-users, keys.

    Parameters:
    - budget_duration: Optional[str] - Budget reset period ("30d", "1h", etc.)
    - budget_id: Optional[str] - The id of the budget. If not provided, a new id will be generated.
    - max_budget: Optional[float] - The max budget for the budget.
    - soft_budget: Optional[float] - The soft budget for the budget.
    - max_parallel_requests: Optional[int] - The max number of parallel requests for the budget.
    - tpm_limit: Optional[int] - The tokens per minute limit for the budget.
    - rpm_limit: Optional[int] - The requests per minute limit for the budget.
    - model_max_budget: Optional[dict] - Specify max budget for a given model. Example: {"openai/gpt-4o-mini": {"max_budget": 100.0, "budget_duration": "1d", "tpm_limit": 100000, "rpm_limit": 100000}}
    - budget_reset_at: Optional[datetime] - Datetime when the initial budget is reset. Default is now.
    r   litellm_proxy_admin_nameprisma_clientN  errorstatus_codedetail)duration)secondsTexclude_none)
created_by
updated_by)data)litellm.proxy.proxy_serverr   r   r   CommonProxyErrorsdb_not_connected_errorvaluebudget_reset_atbudget_durationdatetimeutcnowr   r   
model_dumpr
   dblitellm_budgettablecreateuser_id)r   r   r   r   budget_obj_jsonbudget_obj_jsonifiedresponses          z/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/proxy/management_endpoints/budget_management_endpoints.py
new_budgetr0      s     . S.EEKKL
 	
 !!)j.H.H.T%-__%6'1K1KL:
 &

" !+++>O)/:"%%99@@
"
+33O7O+33O7O
 A  H Os   C-C8/C60C8z/budget/updatec                 h  K   ddl m}m} |'t        ddt        j
                  j                  i      | j                  t        dddi      |j                  j                  j                  d	| j                  ii | j                  d
      d|j                  xs |i       d{   }|S 7 w)a  
    Update an existing budget object.

    Parameters:
    - budget_duration: Optional[str] - Budget reset period ("30d", "1h", etc.)
    - budget_id: Optional[str] - The id of the budget. If not provided, a new id will be generated.
    - max_budget: Optional[float] - The max budget for the budget.
    - soft_budget: Optional[float] - The soft budget for the budget.
    - max_parallel_requests: Optional[int] - The max number of parallel requests for the budget.
    - tpm_limit: Optional[int] - The tokens per minute limit for the budget.
    - rpm_limit: Optional[int] - The requests per minute limit for the budget.
    - model_max_budget: Optional[dict] - Specify max budget for a given model. Example: {"openai/gpt-4o-mini": {"max_budget": 100.0, "budget_duration": "1d", "tpm_limit": 100000, "rpm_limit": 100000}}
    - budget_reset_at: Optional[datetime] - Update the Datetime when the budget was last reset.
    r   r   Nr   r   r     zbudget_id is required	budget_idTr   r   )wherer   )r   r   r   r   r    r!   r"   r3   r(   r)   updater'   r+   )r   r   r   r   r.   s        r/   update_budgetr6   M   s     . S.EEKKL
 	
 #W>U4VWW"%%99@@J001
###6
+33O7O
 A  H Os   B'B2)B0*B2z/budget/infor   c                    K   ddl m} |t        dddi      t        | j                        dk(  rt        ddd	| j                   i      |j
                  j                  j                  d
d| j                  ii       d{   }|S 7 w)z
    Get the budget id specific information

    Parameters:
    - budgets: List[str] - The list of budget ids to get information for
    r   r   Nr   r   zNo db connectedr   r2   z0Specify list of budget id's to query. Passed in=r3   inr4   )r   r   r   lenbudgetsr(   r)   	find_many)r   r   r.   s      r/   info_budgetr>   y   s      9W>O4PQQ
4<<AKDLL>Z
 	
 #%%99CCT4<<01 D  H O	s   BBBBz/budget/settingsr3   c           
        K   ddl m} |'t        ddt        j                  j
                  i      |j                  t        j                  k7  rAt        dddj                  t        j                  j
                  |j                        i      |j                  j                  j                  d| i	       d{   }||j                  d
      }ni }ddiddiddiddiddiddid}g }t        j                   j#                         D ]\  \  }}||v sd
}	t%        |||   d   |j&                  xs d|j)                  |d      |	|j*                        }
|j-                  |
       ^ |S 7 w)z
    Get list of configurable params + current value for a budget item + description of each field

    Used on Admin UI.

    Query Parameters:
    - budget_id: str - The budget id to get information for
    r   r8   Nr2   r   r   {}, your role={}r3   r:   Tr   typeIntegerStringFloat)max_parallel_requests	tpm_limit	rpm_limitr$   
max_budgetsoft_budget )
field_name
field_typefield_descriptionfield_valuestored_in_dbfield_default_value)r   r   r   r    r!   r"   	user_roleLitellmUserRolesPROXY_ADMINformatnot_allowed_accessr(   r)   
find_firstr'   BudgetNewRequestmodel_fieldsitems
ConfigListdescriptiongetdefaultappend)r3   r   r   db_budget_rowdb_budget_row_dictallowed_args
return_valrK   
field_info_stored_in_db_response_objs              r/   budget_settingsrf      s    " 9.EEKKL
 	

 ""&6&B&BB+22%88>>%//
 	
 (**>>III& J  M  *5545H #))!4i(i("H-w'(L J"2"?"?"E"E"G
J% M&%'
3F;","8"8">B.22:tD*$.$6$6M m, #H Es   B9E/;E-<AE/AE/z/budget/listc                   K   ddl m} |'t        ddt        j                  j
                  i      | j                  t        j                  k7  rAt        dddj                  t        j                  j
                  | j                        i      |j                  j                  j                          d{   }|S 7 w)z;List all the created budgets in proxy db. Used on Admin UI.r   r8   Nr2   r   r   r@   )r   r   r   r    r!   r"   rQ   rR   rS   rT   rU   r(   r)   r=   )r   r   r.   s      r/   list_budgetrh      s      9.EEKKL
 	

 ""&6&B&BB+22%88>>%//
 	
 #%%99CCEEHO Fs   B5C 7B>8C z/budget/deletec                   K   ddl m} |'t        ddt        j                  j
                  i      |j                  t        j                  k7  rAt        dddj                  t        j                  j
                  |j                        i      |j                  j                  j                  d	| j                  i
       d{   }|S 7 w)zP
    Delete budget

    Parameters:
    - id: str - The budget id to delete
    r   r8   Nr   r   r   r2   r@   r3   r:   )r   r   r   r    r!   r"   rQ   rR   rS   rT   rU   r(   r)   deleteid)r   r   r   r.   s       r/   delete_budgetrl     s      9.EEKKL
 	

 ""&6&B&BB+22%88>>%//
 	
 #%%99@@DGG$ A  H O	s   CCCCN)__doc__r%   r   fastapir   r   r   *litellm.litellm_core_utils.duration_parserr   litellm.proxy._types$litellm.proxy.auth.user_api_key_authr	   litellm.proxy.utilsr
   routerpostrW   UserAPIKeyAuthr0   r6   BudgetRequestr>   r\   strrf   rh   BudgetDeleteRequestrl        r/   <module>r{      s    5 5 J " B .	 
	+,-   )00A(B* *%*
*Z 
	+,-   )00A(B$ $%$
$N 
	+,-  
M 
4 
	+,-   )00A(BBB%B
BJ 
	+,-   )00A(B%
8 
	+,-   )00A(B!
!%!
!rz   