
    h!                     t    d dl Z d dlZd dlZd dlZd dlmZmZ d dlZd dlm	Z	m
Z
mZmZ d dlmZ  G d d      Zy)    N)LiteralOptional)DAYS_IN_A_MONTHDAYS_IN_A_WEEKDAYS_IN_A_YEARHOURS_IN_A_DAY)ModelResponsec                   0   e Zd Z	 	 	 d!dededee   dee   fdZd Zd Zd e	j                         fd	e
d
edeed      de
fdZdeded
efdZd
efdZ	 	 	 	 d"d
edee   dee   dee   dee   f
dZd Zd Zd
edefdZd Zd Zd
efdZd Zd Zd  Zy)#BudgetManagerNproject_nameclient_typeapi_baseheadersc                 p    || _         || _        |xs d| _        |xs ddi| _        | j	                          y )Nzhttps://api.litellm.aizContent-Typezapplication/json)r   r   r   r   	load_data)selfr   r   r   r   s        R/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/budget_manager.py__init__zBudgetManager.__init__   s=     '( <$<F>3E"F    c                 p    	 t         j                  rdd l}|j                  |       y y # t        $ r Y y w xY w)Nr   )litellmset_verboselogginginfo	Exception)r   print_statementr   s      r   print_verbosezBudgetManager.print_verbose)   s8    	""_- #  		s   %) 	55c                 L   | j                   dk(  rt        j                  j                  d      r0t	        dd      5 }t        j                  |      | _        d d d        n| j                  d       i | _        | j                  d| j                          y | j                   dk(  rt| j                  dz   }d| j                  i}t        j                  j                  || j                  |	      }|j                         }|d
   dk(  ri | _        y |d   | _        y y # 1 sw Y   xY w)Nlocaluser_cost.jsonrzUser Dictionary not found!zuser dict from local: hostedz/get_budgetr   r   jsonstatuserrordata)r   ospathisfileopenr$   load	user_dictr   r   r   r   module_level_clientpostr   )r   	json_fileurlr'   responses        r   r   zBudgetManager.load_data2   s   w&ww~~./*C0 :I%)YYy%9DN: : ""#?@!#!77GHI)---/C"D$5$56D2277T\\ 8 H  }}H!W,  "*&!1 *: :s   DD#total_budgetuserduration)dailyweeklymonthlyyearly
created_atc                    d|i| j                   |<   || j                   |   S |dk(  rd}n/|dk(  rt        }n#|dk(  rt        }n|dk(  rt        }nt	        d      ||||d| j                   |<   | j                          | j                   |   S )	Nr3   r6      r7   r8   r9   zDduration needs to be one of ["daily", "weekly", "monthly", "yearly"])r3   r5   r:   last_updated_at)r-   r   r   r   
ValueError_save_data_thread)r   r3   r4   r5   r:   duration_in_dayss         r   create_budgetzBudgetManager.create_budgetL   s     !/=t>>$''w !-".!-Z  )($)	 
t 	 ~~d##r   modelmessagesc                     dj                  d |D              }t        j                  ||      }t        j                  ||d      \  }}| j                  |   j                  dd      }||z   }	|	S )N c              3   &   K   | ]	  }|d      yw)contentN ).0messages     r   	<genexpr>z/BudgetManager.projected_cost.<locals>.<genexpr>m   s     Bgwy)Bs   )rB   textr   rB   prompt_tokenscompletion_tokenscurrent_cost)joinr   token_countercost_per_tokenr-   get)
r   rB   rC   r4   rL   rN   prompt_cost_rP   projected_costs
             r   rW   zBudgetManager.projected_costl   sp    wwBBB--EE //}
Q ~~d+//B$|3r   c                 &    | j                   |   d   S )Nr3   r-   r   r4   s     r   get_total_budgetzBudgetManager.get_total_budgetv   s    ~~d#N33r   completion_obj
input_textoutput_textc                 p   |r[|rY|rWt        j                  |d|dg      }t        j                  |d|dg      }t        j                  |||      \  }}	||	z   }
n)|rt        j                  |      }
|d   }nt	        d      |
| j
                  |   j                  dd	      z   | j
                  |   d<   d
| j
                  |   v r9|
| j
                  |   d
   j                  |d	      z   | j
                  |   d
   |<   n||
i| j
                  |   d
<   | j                          d| j
                  |   iS )Nr4   )rolerG   )rB   rC   rM   )completion_responserB   zEither a chat completion object or the text response needs to be passed in. Learn more - https://docs.litellm.ai/docs/budget_managerrP   r   
model_cost)r   rR   rS   completion_costr>   r-   rT   r?   )r   r4   r\   rB   r]   r^   rN   rO   prompt_tokens_cost_usd_dollar!completion_tokens_cost_usd_dollarcosts              r   update_costzBudgetManager.update_costy   sk    ZK#11:'N&OM !( 5 5;'O&P! &&+"3-1 13TTD**~ND"E  W  04dnnT6J6N6NA7
 0
t^, 4>>$//8<t~~d?S@c%m9DNN4 .u5 38DNN4 . t,--r   c                 @    | j                   |   j                  dd      S )NrP   r   r-   rT   rZ   s     r   get_current_costzBudgetManager.get_current_cost   s    ~~d#''::r   c                 @    | j                   |   j                  dd      S )Nrb   r   ri   rZ   s     r   get_model_costzBudgetManager.get_model_cost   s    ~~d#''a88r   returnc                     || j                   v S NrY   rZ   s     r   is_valid_userzBudgetManager.is_valid_user   s    t~~%%r   c                 H    t        | j                  j                               S ro   )listr-   keys)r   s    r   	get_userszBudgetManager.get_users   s    DNN'')**r   c                 l    d| j                   |   d<   i | j                   |   d<   d| j                   |   iS )Nr   rP   rb   r4   rY   rZ   s     r   
reset_costzBudgetManager.reset_cost   s<    /0t^,-/t\*t,--r   c                    | j                   |   d   }t        j                         }| j                   |   d   t        z  dz  dz  }||z
  |k\  r4| j                  |       || j                   |   d<   | j	                          y y )Nr=   r5   <   )r-   timer   rv   r?   )r   r4   r=   current_timeduration_in_secondss        r   reset_on_durationzBudgetManager.reset_on_duration   s    .../@Ayy{ NN4 ,~=BRG 	
 /)-@@OOD!6BDNN4 !23""$	 Ar   c                 t    | j                         D ]%  }d| j                  |   v s| j                  |       ' y )Nr5   )rt   r-   r|   rZ   s     r   update_budget_all_usersz%BudgetManager.update_budget_all_users   s2    NN$DT^^D11&&t, %r   c                 d    t        j                  | j                        }|j                          y )N)target)	threadingThread	save_datastart)r   threads     r   r?   zBudgetManager._save_data_thread   s$    !!>>
 	r   c                    | j                   dk(  r<dd l}t        dd      5 } |j                  | j                  |d       d d d        ddiS | j                   d	k(  rf| j
                  d
z   }| j                  | j                  d}t        j                  j                  || j                  |      }|j                         }|S y # 1 sw Y   ddiS xY w)Nr   r   r    w   )indentr%   successr"   z/set_budget)r   r-   r#   )r   r$   r+   dumpr-   r   r   r   r.   r/   r   )r   r$   r0   r1   r'   r2   s         r   r   zBudgetManager.save_data   s    w& &, 			NNIa i(()---/C$($5$5DNNSD2277T\\ 8 H  }}HO * i((s    CC)r   NN)NNNN)__name__
__module____qualname__strr   dictr   r   r   ry   floatr   rA   rr   rW   r[   r	   rg   rj   rl   boolrp   rt   rv   r|   r~   r?   r   rH   r   r   r   r      sP    #"&"&  3-	
 $2< OS%DIIK$$ $ 7#IJK	$
 $@C 4 s 4S 4 37#$(%)-.-. !/-. }	-.
 SM-. c]-.^;9&# &$ &+.
%c %"-
r   r   )r$   r(   r   ry   typingr   r   r   litellm.constantsr   r   r   r   litellm.utilsr	   r   rH   r   r   <module>r      s4     	   $   (L Lr   