o
    ưi                     @   s   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r9ddlmZ neZG d	d
 d
ZdS )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                   @   sR   e Zd ZdZdefddZ	ddee fddZd	d
 Z	dd Z
defddZdS )AlertingHangingRequestCheckzA
    Class to safely handle checking hanging requests alerts
    slack_alerting_objectc                 C   s"   || _ tt| j jt d| _d S )N)Zdefault_ttl)r   r   intalerting_thresholdr   hanging_request_cache)selfr    r   o/home/app/Keep/.python/lib/python3.10/site-packages/litellm/integrations/SlackAlerting/hanging_request_check.py__init__"   s   z$AlertingHangingRequestCheck.__init__Nrequest_datac              	      s   |du rdS t |d}|dd}d}|dddur0t|d tr0tj||d di d}t|dd|||d	d|d
dd}| jj|j	|t
| jjt dI dH  dS )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 Z
deploymentZlitellm_params)r   Zoptional_paramsZlitellm_call_idZuser_api_key_aliasZuser_api_key_team_alias)
request_idr   api_base	key_alias
team_alias)keyvaluettl)r   get
isinstancedictlitellmZget_api_baser
   r   Zasync_set_cacher   r   r   r   r   )r   r   Zrequest_metadatar   r   hanging_request_datar   r   r   $add_request_to_hanging_request_check.   s<   



z@AlertingHangingRequestCheck.add_request_to_hanging_request_checkc                    s   ddl m} |jdu rdS | jjtdI dH }|D ]6}| jj|dI dH }|du r+q|jjd|jdddI dH }|durG| jj	|d q| j
|d	I dH  qdS )
z2
        Send alerts for hanging requests
        r   )proxy_logging_objN)n)r   zrequest_status:{}T)r   Zlitellm_parent_otel_span
local_only)r$   )Zlitellm.proxy.proxy_serverr&   Zinternal_usage_cacher   Zasync_get_oldest_n_keysr	   Zasync_get_cacheformatr   Z_remove_keysend_hanging_request_alert)r   r&   Zhanging_requestsr   r$   Zrequest_statusr   r   r    send_alerts_for_hanging_requestsV   s:   



	z<AlertingHangingRequestCheck.send_alerts_for_hanging_requestsc                    s6   	 t d |  I dH  t| jjd I dH  q)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
        Tz!Checking for hanging requests....N   )r   debugr+   asynciosleepr   r   )r   r   r   r   check_for_hanging_requests   s   
z6AlertingHangingRequestCheck.check_for_hanging_requestsr$   c              	      sz   ddl m} d|j d|j d|j d|j d	}d| jj d	}| jj|d
 | d|j	|j
p1i |j|jdI dH  dS )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`
ZMedium)messagelevelZ
alert_typealerting_metadataZrequest_modelr   N)1litellm.integrations.SlackAlerting.slack_alertingr1   r   r   r   r   r   r   Z
send_alertZllm_requests_hangingr6   )r   r$   r1   Zrequest_infoZalerting_messager   r   r   r*      s$   

z6AlertingHangingRequestCheck.send_hanging_request_alert)N)__name__
__module____qualname____doc__r   r   r   r"   r%   r+   r0   r
   r*   r   r   r   r   r      s    

(5r   )r;   r.   typingr   r   r   r#   Zlitellm._loggingr   Zlitellm.caching.in_memory_cacher   Z'litellm.litellm_core_utils.core_helpersr   Z)litellm.types.integrations.slack_alertingr   r	   r
   r7   r   r   r   r   r   r   <module>   s    	