
    hH                         d Z ddlZddlmZmZmZ ddlZddlmZ ddl	m
Z
 ddlmZ ddlmZmZmZ erddlmZ neZ G d	 d
      Zy)z
Class to check for LLM API hanging requests


Notes:
- Do not create tasks that sleep, that can saturate the event loop
- Do not store large objects (eg. messages in memory) that can increase RAM usage
    N)TYPE_CHECKINGAnyOptional)verbose_proxy_logger)InMemoryCache) get_litellm_metadata_from_kwargs)!HANGING_ALERT_BUFFER_TIME_SECONDS$MAX_OLDEST_HANGING_REQUESTS_TO_CHECKHangingRequestData)SlackAlertingc                   J    e Zd ZdZdefdZ	 ddee   fdZd Z	d Z
d	efd
Zy)AlertingHangingRequestCheckzA
    Class to safely handle checking hanging requests alerts
    slack_alerting_objectc                 |    || _         t        t        | j                   j                  t        z               | _        y )N)default_ttl)r   r   intalerting_thresholdr	   hanging_request_cache)selfr   s     t/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/integrations/SlackAlerting/hanging_request_check.py__init__z$AlertingHangingRequestCheck.__init__"   s8     &;"%2**==34&
"    Nrequest_datac           
        K   |yt        |      }|j                  dd      }d}|j                  dd      =t        |d   t              r*t	        j
                  ||d   j                  di             }t        |j                  dd      |||j                  d	d      |j                  d
d            }| j                  j                  |j                  |t        | j                  j                  t        z                d{    y7 w)z
        Add a request to the hanging request cache. This is the list of request_ids that gets periodicall checked for hanging requests
        N)kwargsmodel 
deploymentlitellm_params)r   optional_paramslitellm_call_iduser_api_key_aliasuser_api_key_team_alias)
request_idr   api_base	key_alias
team_alias)keyvaluettl)r   get
isinstancedictlitellmget_api_baser   r   async_set_cacher$   r   r   r   r	   )r   r   request_metadatar   r%   hanging_request_datas         r   $add_request_to_hanging_request_checkz@AlertingHangingRequestCheck.add_request_to_hanging_request_check.   s$     ;<P  "-"&L$/;
&A
 ++ ,\ : > >?OQS TH
  2#''(92>&**+?D'++,ErJ 
 ((88$//&**==34 9 
 	
 	
 		
s   DDD
Dc                   K   ddl m} |j                  y| j                  j	                  t
               d{   }|D ]  }| j                  j                  |       d{   }|*|j                  j                  dj                  |j                        dd       d{   }|| j                  j                  |       | j                  |	       d{     y7 7 7 D7 w)
z2
        Send alerts for hanging requests
        r   )proxy_logging_objN)n)r(   zrequest_status:{}T)r(   litellm_parent_otel_span
local_only)r2   )litellm.proxy.proxy_serverr5   internal_usage_cacher   async_get_oldest_n_keysr
   async_get_cacheformatr$   _remove_keysend_hanging_request_alert)r   r5   hanging_requestsr$   r2   request_statuss         r    send_alerts_for_hanging_requestsz<AlertingHangingRequestCheck.send_alerts_for_hanging_requestsV   s*     	A 119!%!;!;!S!S2 "T "
 
 +J00@@" A   ! $+ (<<LL+223G3R3RS-1# M    )**66" 7  
 11%9 2   ; +B 	K
$sF   7C-C%(C-"C'#AC-$C)%8C-C+C-'C-)C-+C-c                    K   	 t        j                  d       | j                          d{    t        j                  | j
                  j                  dz         d{    b7 97 w)z
        Background task that checks all request ids in self.hanging_request_cache to check if they have completed

        Runs every alerting_threshold/2 seconds to check for hanging requests
        z!Checking for hanging requests....N   )r   debugrB   asynciosleepr   r   )r   s    r   check_for_hanging_requestsz6AlertingHangingRequestCheck.check_for_hanging_requests   s\       &&'JK77999-- : : M MPQ QRRR 9Rs!   *A*A&3A* A(!A*(A*r2   c           	      H  K   ddl m} d|j                   d|j                   d|j                   d|j
                   d	}d| j                  j                   d	}| j                  j                  |d
z   |z   d|j                  |j                  xs i        d{    y7 w)z.
        Send a hanging request alert
        r   )	AlertTypezRequest Model: `z`
API Base: `z`
Key Alias: `z`
Team Alias: ``z`Requests are hanging - zs+ request time`
Medium)messagelevel
alert_typealerting_metadataN)1litellm.integrations.SlackAlerting.slack_alertingrJ   r   r%   r&   r'   r   r   
send_alertllm_requests_hangingrQ   )r   r2   rJ   request_infoalerting_messages        r   r?   z6AlertingHangingRequestCheck.send_hanging_request_alert   s      	P
 ,,@,F,F+G H ))* +!++, -"--.a3
 6d6P6P6c6c5ddtu((33$t+l: 552DDJ	 4 
 	
 	
s   BB"B B")N)__name__
__module____qualname____doc__r   r   r   r-   r3   rB   rH   r   r?    r   r   r   r      sD    

,

 (,&tn&P3jS
0
r   r   )rZ   rF   typingr   r   r   r.   litellm._loggingr   litellm.caching.in_memory_cacher   'litellm.litellm_core_utils.core_helpersr   )litellm.types.integrations.slack_alertingr	   r
   r   rR   r   r   r[   r   r   <module>ra      sD     / /  1 9 T  OMR
 R
r   