o
    ưi                     @   s   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r1d dlmZ neZe ZG dd	 d	ZG d
d deZG dd deZdS )    N)datetime)TYPE_CHECKINGAnyListOptional)Logging)PassThroughEndpointLogging)EndpointType)$BaseGoogleGenAIGenerateContentConfigc                   @   s.   e Zd ZdZdededefddZdd Zd	S )
/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                 C   s&   || _ || _t | _g | _|| _d S N)r   r   r   now
start_timecollected_chunksr   )selfr   r   r    r   ^/home/app/Keep/.python/lib/python3.10/site-packages/litellm/google_genai/streaming_iterator.py__init__   s
   

z8BaseGoogleGenAIGenerateContentStreamingIterator.__init__c                    sL   ddl m} t }t|j| jtd| j	pi t
j| j| j|| jd	 dS )z8Handle the logging after all chunks have been collected.r   )PassThroughStreamingHandlerz/v1/generateContent)	r   Zpassthrough_success_handler_objZ	url_router   Zendpoint_typer   Z	raw_bytesend_timer   N)Z6litellm.proxy.pass_through_endpoints.streaming_handlerr   r   r   asynciocreate_taskZ#_route_streaming_logging_to_handlerr   'GLOBAL_PASS_THROUGH_SUCCESS_HANDLER_OBJr   r	   Z	VERTEX_AIr   r   r   )r   r   r   r   r   r   _handle_async_streaming_logging&   s    zOBaseGoogleGenAIGenerateContentStreamingIterator._handle_async_streaming_loggingN)	__name__
__module____qualname____doc__LiteLLMLoggingObjdictstrr   r   r   r   r   r   r      s    
r   c                       sb   e Zd ZdZ	ddedededededee f fd	d
Z	dd Z
dd Zdd Zdd Z  ZS )+GoogleGenAIGenerateContentStreamingIteratorzP
    Streaming iterator specifically for Google GenAI generate content API.
    Nr   logging_obj generate_content_provider_configlitellm_metadatacustom_llm_providerr   c                    B   t  j||pi |d || _|| _|| _|| _|| _| | _d S N)r   r   r   )	superr   responser   r&   r'   r(   Z
iter_bytesstream_iteratorr   r,   r   r%   r&   r'   r(   r   	__class__r   r   r   B      
z4GoogleGenAIGenerateContentStreamingIterator.__init__c                 C      | S r   r   r   r   r   r   __iter__Y      z4GoogleGenAIGenerateContentStreamingIterator.__iter__c                 C   s0   zt | j}| j| |W S  ty   tw r   )nextr-   r   appendStopIterationr   chunkr   r   r   __next__\   s   
z4GoogleGenAIGenerateContentStreamingIterator.__next__c                 C   r2   r   r   r3   r   r   r   	__aiter__f   r5   z5GoogleGenAIGenerateContentStreamingIterator.__aiter__c                    s
   t d)NzHUse AsyncGoogleGenAIGenerateContentStreamingIterator for async iteration)NotImplementedErrorr3   r   r   r   	__anext__i   s   z5GoogleGenAIGenerateContentStreamingIterator.__anext__r   )r   r   r   r    r#   r!   r
   r"   r   r   r4   r;   r<   r>   __classcell__r   r   r/   r   r$   =   s(    
r$   c                       sR   e Zd ZdZ	ddedededededee f fd	d
Z	dd Z
dd Z  ZS )0AsyncGoogleGenAIGenerateContentStreamingIteratorzV
    Async streaming iterator specifically for Google GenAI generate content API.
    Nr   r%   r&   r'   r(   r   c                    r)   r*   )	r+   r   r,   r   r&   r'   r(   Zaiter_bytesr-   r.   r/   r   r   r   t   r1   z9AsyncGoogleGenAIGenerateContentStreamingIterator.__init__c                 C   r2   r   r   r3   r   r   r   r<      r5   z:AsyncGoogleGenAIGenerateContentStreamingIterator.__aiter__c                    sF   z| j  I d H }| j| |W S  ty"   |  I d H  tw r   )r-   r>   r   r7   StopAsyncIterationr   r9   r   r   r   r>      s   z:AsyncGoogleGenAIGenerateContentStreamingIterator.__anext__r   )r   r   r   r    r#   r!   r
   r"   r   r   r<   r>   r?   r   r   r/   r   r@   o   s$    r@   )r   r   typingr   r   r   r   Z*litellm.litellm_core_utils.litellm_loggingr   r!   Z4litellm.proxy.pass_through_endpoints.success_handlerr   Z:litellm.types.passthrough_endpoints.pass_through_endpointsr	   Z1litellm.llms.base_llm.google_genai.transformationr
   r   r   r$   r@   r   r   r   r   <module>   s    )2