
    h-:                        d Z ddlZddlZddlmZmZmZmZmZ ddl	Z	ddl
mZ ddlmZmZmZ ddlmZ ddlmZmZ erdd	lmZ dd
lmZ eZeeef   ZneZeZ	 ddededeeef   dee   def
dZ	 ddedee   deeef   dedee    defdZ!dededeeef   dedef
dZ"	 	 ddededeeef   dee   dee    defdZ#dedee   dee   fdZ$dedee   dee%   fdZ&dedee   dee   fdZ'dedededefdZ(dedefdZ)deeef   defdZ*y) a  
Router cooldown handlers
- _set_cooldown_deployments: puts a deployment in the cooldown list
- get_cooldown_deployments: returns the list of deployments in the cooldown list
- async_get_cooldown_deployments: ASYNC: returns the list of deployments in the cooldown list

    N)TYPE_CHECKINGAnyListOptionalUnion)verbose_router_logger)DEFAULT_COOLDOWN_TIME_SECONDS!DEFAULT_FAILURE_THRESHOLD_PERCENT+SINGLE_DEPLOYMENT_TRAFFIC_FAILURE_THRESHOLD)router_cooldown_event_callback   )*get_deployment_failures_for_current_minute+get_deployment_successes_for_current_minute)Span)Routerlitellm_router_instancemodel_idexception_statusexception_strreturnc                     	 dg}||D ]  }||v s y t        |t              rt        |      }|dk\  r|dk  r|dk(  ry|dk(  ry|dk(  ry|d	k(  ryyy# t        $ r Y yw xY w)
a>  
    A function to determine if a cooldown is required based on the exception status.

    Parameters:
        model_id (str) The id of the model in the model list
        exception_status (Union[str, int]): The status of the exception.

    Returns:
        bool: True if a cooldown is required, False otherwise.
    APIConnectionErrorFi      Ti  i  i  )
isinstancestrint	Exception)r   r   r   r   ignored_stringsignored_strings         b/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/router_utils/cooldown_handlers.py_is_cooldown_requiredr"   '   s     %/0%"1!]2  #2 &,"#34s"'7#'=3&!S(!S(!S(   s,   A A +A A A A 	AA
deploymentoriginal_exceptiontime_to_cooldownc                    || j                  |      t        j                  d       y|.t        j                  |dd      rt        j                  d       y| j
                  rt        j                  d       y|t        j                  d	       yt        | ||t        |      
      st        j                  d       y|| j                  v rt        j                  d       yy)a  
    Helper that decides if cooldown logic should be run
    Returns False if cooldown logic should not be run

    Does not run cooldown logic when:
    - router.disable_cooldowns is True
    - deployment is None
    - _is_cooldown_required() returns False
    - deployment is in litellm_router_instance.provider_default_deployment_ids
    - exception_status is not one that should be immediately retried (e.g. 401)
    idzSShould Not Run Cooldown Logic: deployment id is none or model group can't be found.F        g&.>)ababs_tolz@Should Not Run Cooldown Logic: time_to_cooldown is effectively 0z8Should Not Run Cooldown Logic: disable_cooldowns is Truez1Should Not Run Cooldown Logic: deployment is None)r   r   r   r   zCShould Not Run Cooldown Logic: _is_cooldown_required returned FalsezOShould Not Run Cooldown Logic: deployment is in provider_default_deployment_idsT)	get_model_groupr   debugmathisclosedisable_cooldownsr"   r   provider_default_deployment_idsr   r#   r   r$   r%   s        r!   _should_run_cooldown_logicr4   _   s    & 	"22j2AI##a	
 
 #

)
 	##$fg00##F	
 ##$WX  7),-	 	##Q	
 ,LLL##]	
     c                    | j                  |      }d}|t        |      dk(  rd}| j                  t        |       du rt	        | |      }t        | |      }||z   }d}	|dkD  r|||z   z  }	t        j                  d	||	||       t        |      }
|
d
k(  r|sy|	dk(  r
|t        k\  ry|	t        kD  r|syt        j                  t        |            du ryyt        | ||      S )a_  
    Helper that decides if a deployment should be put in cooldown

    Returns True if the deployment should be put in cooldown
    Returns False if the deployment should not be put in cooldown


    Deployment is put in cooldown when:
    - v2 logic (Current):
    cooldown if:
        - got a 429 error from LLM API
        - if %fails/%(successes + fails) > ALLOWED_FAILURE_RATE_PER_MINUTE
        - got 401 Auth error, 404 NotFounder - checked by litellm._should_retry()



    - v1 logic (Legacy): if allowed fails or allowed fail policy set, coolsdown if num fails in this minute > allowed fails
    r'   Fr   Tr   )r   deployment_idr)   r   zYpercent fails for deployment = %s, percent fails = %s, num successes = %s, num fails = %sr   g      ?)status_code)r   r#   r$   )r-   lenallowed_fails_policy_is_allowed_fails_set_on_routerr   r   r   r.   cast_exception_status_to_intr   r
   litellm_should_retry-should_cooldown_based_on_allowed_fails_policy)r   r#   r   r$   model_group is_single_deployment_model_groupnum_successes_this_minutenum_fails_this_minutetotal_requests_this_minutepercent_failsexception_status_ints              r!   _should_cooldown_deploymentrH      sM   2 *99Z9HK',$3{#3q#8+/(44<+$;
 
 %P$;:%
! !K$;:!
 &?AV%V"%)1),AAM 	##g%!	
  <<LM3&/OS *:; ==4 !!89IJ 
 <$;!1
 	
r5   c                 l   t        j                  d       t        | ||||      du s|t        j                  d       yt        |      }t        j                  d| d       t	        | |||      rB| j
                  j                  ||||       t        j                  t        | |||	             y
y)aL  
    Add a model to the list of models being cooled down for that minute, if it exceeds the allowed fails / minute

    or

    the exception is not one that should be immediately retried (e.g. 401)

    Returns:
    - True if the deployment should be put in cooldown
    - False if the deployment should not be put in cooldown
    z"checks 'should_run_cooldown_logic'r3   Fz(should_run_cooldown_logic returned FalsezAttempting to add z to cooldown list)r   r#   r   r$   )r   r$   r   cooldown_time)r   r8   r   rJ   T)
r   r.   r4   r=   rH   cooldown_cacheadd_deployment_to_cooldownasynciocreate_taskr   )r   r$   r   r#   r%   rG   s         r!   _set_cooldown_deploymentsrO      s    $  DE 	#$;!-1-	
 	 ##$NO78HI"4ZL@Q RS" 7)-	 	 ..II11*	 	J 	
 	*(?(!1.		
 r5   parent_otel_spanc                 D  K   | j                         }| j                  j                  ||       d{   }g }|Ct        |t              r3t        |      dkD  r%t        |d   t              r|D cg c]  }|d   	 }}t        j                  d|        |S 7 ec c}w w)=
    Async implementation of '_get_cooldown_deployments'
    	model_idsrP   Nr   retrieve cooldown models: )	get_model_idsrK   async_get_active_cooldownsr   listr:   tupler   r.   r   rP   rT   cooldown_modelscached_value_deployment_idscvs         r!   _async_get_cooldown_deploymentsr^   >  s      (557I%44OO- P 
 	
  #%#- 1$q)517F&Gr!u&G#&G"<_<M NO&&!	
 'Hs!   1B B=B 1B=B B c                    K   | j                         }| j                  j                  ||       d{   }t        j                  d|        |S 7 w)rR   rS   NrU   )rV   rK   rW   r   r.   )r   rP   rT   r[   s       r!   /_async_get_cooldown_deployments_with_debug_infor`   Z  sd      (557I%44OO2B P 
 	
  "<_<M NO	
s   1AAAc                     | j                         }| j                  j                  ||      }g }|Ct        |t              r3t        |      dkD  r%t        |d   t              r|D cg c]  }|d   	 }}|S c c}w )zB
    Get the list of models being cooled down for this minute
    rS   r   )rV   rK   get_active_cooldownsr   rX   r:   rY   rZ   s         r!   _get_cooldown_deploymentsrc   l  s     (557I-<<QQ.> R O #%#- 1$q)517F&Gr!u&G#&G&& 'Hs   'A7c                     | j                  |      xs | j                  }| j                  xs t        }| j                  j                  |      xs d}|dz   }||kD  ry| j                  j                  |||       y)z
    Check if fails are within the allowed limit and update the number of fails.

    Returns:
    - True if fails exceed the allowed limit (should cooldown)
    - False if fails are within the allowed limit (should not cooldown)
    )	exception)keyr   r   T)rf   valuettlF)get_allowed_fails_from_policyallowed_failsrJ   r	   failed_calls	get_cache	set_cache)r   r#   r$   rj   rJ   current_failsupdated_failss          r!   r@   r@     s     	 ==( 	> 	
 	1 #00	  	 --N1N  ,88BBzBRWVWM!A%M}$,,66-] 	7 	
 r5   c                 Z    | j                   y| j                   t        j                   k7  ryy)z
    Check if Router.allowed_fails is set or is Non-default Value

    Returns:
    - True if Router.allowed_fails is set or is Non-default Value
    - False if Router.allowed_fails is None or is Default Value
    FT)rj   r>   r7   s    r!   r<   r<     s-     ,,4,,0E0EEr5   c                     t        | t              r	 t        |       } | S | S # t        $ r t	        j
                  d|  d       d} Y | S w xY w)Nz'Unable to cast exception status to int z. Defaulting to status=500.r   )r   r   r   r   r   r.   )r   s    r!   r=   r=     sh    "C(	#"#34   	#!''9:J9KKfg  #	#s   ! $A	A	)N)NN)+__doc__rM   r/   typingr   r   r   r   r   r>   litellm._loggingr   litellm.constantsr	   r
   r   'litellm.router_utils.cooldown_callbacksr   )router_callbacks.track_deployment_metricsr   r   opentelemetry.tracer   _Spanlitellm.routerr   _RouterLitellmRouterr   r   boolr"   floatr4   rH   rO   r^   rY   r`   rc   r@   r<   r=    r5   r!   <module>r      s_     < <  2 
 S
 10MDMD $(	5*55 CHo5 C=	5
 
5z )-A*AA CHoA 	A
 uoA 
AHY*YY CHoY 	Y
 
Y@ !%(,<*<< CHo< 	<
 uo< 
<~'*'tn' 
#Y'8*tn 
%[$'*'>Ftn'	#Y': *     
	 F*	"	5c? 	s 	r5   