o
    ưiT                     @   s^   d dl Z d dlmZ d dlZd dlmZ d dlmZmZmZ d dl	m
Z
mZ G dd dZdS )    N)Optional)verbose_logger)ChatCompletionToolCallChunk#ChatCompletionToolCallFunctionChunkChatCompletionUsageBlock)GenericStreamingChunkUsagec                   @   sL   e Zd ZdefddZdedefddZdd	 Zd
d Z	dd Z
dd ZdS )ModelResponseIteratorsync_streamc                 C   s
   || _ d S N)streaming_response)selfr   r
    r   ^/home/app/Keep/.python/lib/python3.10/site-packages/litellm/llms/databricks/streaming_utils.py__init__   s   
zModelResponseIterator.__init__chunkreturnc           	      C   sx  zt jdi |}d}d }d}d}d }|jd jjd ur#|jd jj}|jd jjd ur~t|jd jjdkr~|jd jjd jd ur~|jd jjd jjd ur~t	|jd jjd j
dt|jd jjd jj|jd jjd jjd|jd jjd jd}|jd jd urd}|jd j}t|dd }|d urt|j|j|jd	}t|||||dd
W S  tjy   td| w )N Fr   function)name	arguments)idtyper   indexTusage)prompt_tokenscompletion_tokenstotal_tokens)texttool_useis_finishedfinish_reasonr   r   z"Failed to decode JSON from chunk: r   )litellmZModelResponseStreamchoicesdeltacontentZ
tool_callslenr   r   r   r   r   r   r   r!   getattrr   r   r   r   r   jsonJSONDecodeError
ValueError)	r   r   Zprocessed_chunkr   r   r    r!   r   Zusage_chunkr   r   r   chunk_parser   sf   z"ModelResponseIterator.chunk_parserc                 C   s   | j | _| S r   )r   response_iteratorr   r   r   r   __iter__L   s   zModelResponseIterator.__iter__c                 C   s  t | ds	| j| _z| j }W n ty   t ty+ } ztd| d }~ww z)tj	|p4d}|
 }t|dkrKt|}| j|dW S tdddd dd dW S  ty^   t ty } ztd| d	| d
 tdddd dd dW  Y d }~S d }~ww )Nr,   #Error receiving chunk from stream: r   r   r   Fr   r    r!   r   r   r   Error parsing chunk: ,
Received chunk: !. Defaulting to empty chunk here.)hasattrr   r,   __next__StopIterationr*   RuntimeErrorr"   CustomStreamWrapper_strip_sse_data_from_chunkstripr&   r(   loadsr+   r   r   debugr   r   eZ
json_chunkr   r   r   r6   P   sP   

zModelResponseIterator.__next__c                 C   s   | j  | _| S r   )r   	__aiter__async_response_iteratorr-   r   r   r   r@   y   s   zModelResponseIterator.__aiter__c                    s2  z
| j  I d H }W n- ty   t ty& } ztd| d }~w ty8 } ztd| d }~ww z/tj|pAd}|	 }|dkrLtt
|dkr^t|}| j|dW S tdddd dd dW S  tyq   t ty } ztd| d	| d
 tdddd dd dW  Y d }~S d }~ww )Nr/   r   z[DONE]r   r0   Fr1   r2   r3   r4   )rA   	__anext__StopAsyncIterationr*   r8   	Exceptionr"   r9   r:   r;   r&   r(   r<   r+   r   r   r=   r>   r   r   r   rB   }   sX   
zModelResponseIterator.__anext__N)__name__
__module____qualname__boolr   dictr   r+   r.   r6   r@   rB   r   r   r   r   r	      s    :)r	   )r(   typingr   r"   r   Zlitellm.types.llms.openair   r   r   Zlitellm.types.utilsr   r   r	   r   r   r   r   <module>   s    