
    g                     t    d 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	 ddl
mZ defd	efd
Z G d de	      Zy)zE
Module containing "timeout" decorator for sync and async callables.
    N)futures)wraps)iscoroutinefunction)Thread)Timeoutg        timeout_durationc                       fd}|S )aL  
    Wraps a function to raise the specified exception if execution time
    is greater than the specified timeout.

    Works with both synchronous and asynchronous callables, but with synchronous ones will introduce
    some overhead due to the backend use of threads and asyncio.

        :param float timeout_duration: Timeout duration in seconds. If none callable won't time out.
        :param OpenAIError exception_to_raise: Exception to raise when the callable times out.
            Defaults to TimeoutError.
        :return: The decorated function.
        :rtype: callable
    c                 x     t                fd       }t                fd       }t               r|S |S )Nc                       	fd}t               }|j                          t        j                   |       |j                        }
}dv rd   d   }ndv r
d   d   }	 |j                  |      }|j                          |S # t        j                  $ r8 |j                          t               dkD  r d   nd   } d| d|d	
      w xY w)Nc                     K     i S wN )argsfunckwargss   D/var/www/openai/venv/lib/python3.12/site-packages/litellm/timeout.py
async_funcz?timeout.<locals>.decorator.<locals>.wrapper.<locals>.async_func)   s     T,V,,s   	force_timeoutrequest_timeouttimeoutr   model=A timeout error occurred. The function call took longer than  second(s).openair   llm_provider)
_LoopWrapperstartasynciorun_coroutine_threadsafeloopresultr   TimeoutError	stop_looplen)r   r   r   threadfuturelocal_timeout_durationr#   r   exception_to_raiser   r   s   ``      r   wrapperz+timeout.<locals>.decorator.<locals>.wrapper'   s    - "^FLLN55jlFKKPF%5"&(VO-D-P)/)@&"f,8I1J1V)/0A)B&	/EF M ''   "#&t9q=QfWo(STjSkkvw!) s   *B ACc                    K   }d|v r|d   }nd|v r
|d   |d   }	 t        j                   | i |       d {   }|S 7 # t         j                  $ r( t        |       dkD  r| d   n|d   } d| d|d	      w xY ww)
Nr   r   r   r   r   r   r   r   r   )r    wait_forr$   r&   )r   r   r)   valuer   r*   r   r   s        r   async_wrapperz1timeout.<locals>.decorator.<locals>.async_wrapperA   s     %5"&()/)@&"f,8I1J1V)/0A)B&%..$)&)3C   '' #&t9q=QfWo(STjSkkvw!) s-   B A A A BA ;BB)r   r   )r   r+   r/   r*   r   s   `  r   	decoratorztimeout.<locals>.decorator&   sF    	t	 
	2 
t	 
	& t$      r   )r   r*   r0   s   `` r   r   r      s    1f r1   c                   ,     e Zd Z fdZddZd Z xZS )r   c                 X    t         |   d       t        j                         | _        y )NT)daemon)super__init__r    new_event_loopr"   )self	__class__s    r   r6   z_LoopWrapper.__init__]   s#    %**,	r1   c                 |   	 | j                   j                          | j                   j                  | j                   j                         | j                   j                          t        j                  d        y # t        $ r Y ;w xY w# | j                   j                          t        j                  d        w xY wr   )r"   run_forevercall_soon_threadsafeclose	Exceptionr    set_event_loop)r8   s    r   runz_LoopWrapper.runa   s    	)II!!#II**499??;
 IIOO""4(  		 IIOO""4(s$   A	A; ;	BB
 BB
 
1B;c                     t        j                  | j                        D ]  }|j                           | j                  j	                  | j                  j
                         y r   )r    	all_tasksr"   cancelr<   stop)r8   tasks     r   r%   z_LoopWrapper.stop_loopl   s>    %%dii0DKKM 1		&&tyy~~6r1   )returnN)__name__
__module____qualname__r6   r@   r%   __classcell__)r9   s   @r   r   r   \   s    -	)7r1   r   )__doc__r    
concurrentr   	functoolsr   inspectr   	threadingr   litellm.exceptionsr   floatr   r   r   r1   r   <module>rR      s@       '  & '*g Be BJ76 7r1   