
    h                         d dl Z d dlmZ d dlmZmZmZmZ d dlmZ	 d dl
mZ d dlmZ erd dlmZ neZ e       Z G d d	      Z G d
 de      Z G d de      Zy)    N)datetime)TYPE_CHECKINGAnyListOptional)Logging)PassThroughEndpointLogging)EndpointType)$BaseGoogleGenAIGenerateContentConfigc                   *    e Zd ZdZdededefdZd Zy)/BaseGoogleGenAIGenerateContentStreamingIteratorz
    Base class for Google GenAI Generate Content streaming iterators that provides common logic
    for streaming response handling and logging.
    litellm_logging_objrequest_bodymodelc                 n    || _         || _        t        j                         | _        g | _        || _        y N)r   r   r   now
start_timecollected_chunksr   )selfr   r   r   s       c/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/google_genai/streaming_iterator.py__init__z8BaseGoogleGenAIGenerateContentStreamingIterator.__init__   s0     $7 (",,.-/
    c                 *  K   ddl m} t        j                         }t	        j
                  |j                  | j                  t        d| j                  xs i t        j                  | j                  | j                  || j                  	             yw)z8Handle the logging after all chunks have been collected.r   )PassThroughStreamingHandlerz/v1/generateContent)	r   passthrough_success_handler_obj	url_router   endpoint_typer   	raw_bytesend_timer   N)6litellm.proxy.pass_through_endpoints.streaming_handlerr   r   r   asynciocreate_task#_route_streaming_logging_to_handlerr   'GLOBAL_PASS_THROUGH_SUCCESS_HANDLER_OBJr   r
   	VERTEX_AIr   r   r   )r   r   r    s      r   _handle_async_streaming_loggingzOBaseGoogleGenAIGenerateContentStreamingIterator._handle_async_streaming_logging&   sz     	
 <<>'KK$($<$<0W/!..4"*44??//!jj L 
	
s   BBN)	__name__
__module____qualname____doc__LiteLLMLoggingObjdictstrr   r'    r   r   r   r      s-    

.
 
 	

r   r   c                   ^     e Zd ZdZ	 ddedededededee   f fdZ	d	 Z
d
 Zd Zd Z xZS )+GoogleGenAIGenerateContentStreamingIteratorzP
    Streaming iterator specifically for Google GenAI generate content API.
    r   logging_obj generate_content_provider_configlitellm_metadatacustom_llm_providerr   c                     t         |   ||xs i |       || _        || _        || _        || _        || _        |j                         | _        y N)r   r   r   )	superr   responser   r3   r4   r5   
iter_bytesstream_iterator	r   r9   r   r2   r3   r4   r5   r   	__class__s	           r   r   z4GoogleGenAIGenerateContentStreamingIterator.__init__B   s^     	 +%+ 	 	

 !
0P- 0#6 '224r   c                     | S r   r/   r   s    r   __iter__z4GoogleGenAIGenerateContentStreamingIterator.__iter__Y       r   c                     	 t        | j                        }| j                  j                  |       |S # t        $ r t        w xY wr   )nextr;   r   appendStopIterationr   chunks     r   __next__z4GoogleGenAIGenerateContentStreamingIterator.__next__\   sE    	 --.E!!((/L 	 	 s	   14 Ac                     | S r   r/   r?   s    r   	__aiter__z5GoogleGenAIGenerateContentStreamingIterator.__aiter__f   rA   r   c                     K   t        d      w)NzHUse AsyncGoogleGenAIGenerateContentStreamingIterator for async iteration)NotImplementedErrorr?   s    r   	__anext__z5GoogleGenAIGenerateContentStreamingIterator.__anext__i   s      ""lmms   r   )r(   r)   r*   r+   r.   r,   r   r-   r   r   r@   rH   rJ   rM   __classcell__r=   s   @r   r1   r1   =   sh     (,5 5 '	5
 +O5 5 !5 tn5. nr   r1   c                   R     e Zd ZdZ	 ddedededededee   f fdZ	d	 Z
d
 Z xZS )0AsyncGoogleGenAIGenerateContentStreamingIteratorzV
    Async streaming iterator specifically for Google GenAI generate content API.
    r   r2   r3   r4   r5   r   c                     t         |   ||xs i |       || _        || _        || _        || _        || _        |j                         | _        y r7   )	r8   r   r9   r   r3   r4   r5   aiter_bytesr;   r<   s	           r   r   z9AsyncGoogleGenAIGenerateContentStreamingIterator.__init__t   s^     	 +%+ 	 	

 !
0P- 0#6 '335r   c                     | S r   r/   r?   s    r   rJ   z:AsyncGoogleGenAIGenerateContentStreamingIterator.__aiter__   rA   r   c                    K   	 | j                   j                          d {   }| j                  j                  |       |S 7 !# t        $ r  | j                          d {  7   t        w xY wwr   )r;   rM   r   rD   StopAsyncIterationr'   rF   s     r   rM   z:AsyncGoogleGenAIGenerateContentStreamingIterator.__anext__   sh     	%..88::E!!((/L ; " 	%66888$$	%s8   A1A A A A1A A.!A$"A..A1r   )r(   r)   r*   r+   r.   r,   r   r-   r   r   rJ   rM   rN   rO   s   @r   rQ   rQ   o   s]     (,6 6 '	6
 +O6 6 !6 tn6.	%r   rQ   )r"   r   typingr   r   r   r   *litellm.litellm_core_utils.litellm_loggingr   r,   4litellm.proxy.pass_through_endpoints.success_handlerr	   :litellm.types.passthrough_endpoints.pass_through_endpointsr
   1litellm.llms.base_llm.google_genai.transformationr   r%   r   r1   rQ   r/   r   r   <module>r\      s`      5 5 S T ,/(*D*F '&
 &
R/n2a /nd(%7f (%r   