o
    i.                     @   s   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 dl	m
Z
 d dl	mZ eeZdd	 ZG d
d dZG dd deeZG dd deeZdS )    )defaultdict)
get_logger)LITELLM_ROUTER_INSTANCE_KEY)AsyncStreamHandler)StreamHandler0openai_construct_completion_from_streamed_chunks-openai_construct_message_from_streamed_chunksc                 C   s,   d|  di  di v r| d d d S d S )Nhostmetadataheaders)get)kwargs r   Z/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/contrib/internal/litellm/utils.pyextract_host_tag   s   r   c                   @   s8   e Zd Zdd Zdd ZdddZddd	Zd
d ZdS )BaseLiteLLMStreamHandlerc                 C   s   t tS N)r   list)selfr   r   r   initialize_chunk_storage   s   z1BaseLiteLLMStreamHandler.initialize_chunk_storagec                 C   s   ||t < | j||f d S r   )r   spansappend)r   spanr   instancer   r   r   add_span   s   z!BaseLiteLLMStreamHandler.add_spanNc                 C   sT   t |dg D ]}t |dd}| j| | qt |dd r(| jd d| d S d S )Nchoicesindexr   usage)getattrchunksr   insert)r   chunkiteratorchoicechoice_indexr   r   r   _process_chunk   s   z'BaseLiteLLMStreamHandler._process_chunkc                 C   sR   d }| j D ]!\}}|s| |}| j|r"| jj|g |||jd |  qd S )N)argsr   response	operation)r   _process_finished_streamintegrationis_pc_sampled_llmobsllmobs_set_tagsresourcefinish)r   	exceptionformatted_completionsr   r   r   r   r   finalize_stream#   s   

z(BaseLiteLLMStreamHandler.finalize_streamc                 C   sp   z%|j }d }| j|rdd | j D }|W S dd | j D }|W S  ty7   tjddd | Y S w )Nc                 S      g | ]}t |qS r   r   .0r%   r   r   r   
<listcomp>3       zEBaseLiteLLMStreamHandler._process_finished_stream.<locals>.<listcomp>c                 S   r4   r   r	   r5   r   r   r   r7   7   r8   z3Error processing streamed completion/chat response.T)exc_info)r/   r,   is_completion_operationr!   values	Exceptionlogwarning)r   r   r*   r2   r   r   r   r+   .   s    z1BaseLiteLLMStreamHandler._process_finished_streamr   )__name__
__module____qualname__r   r   r'   r3   r+   r   r   r   r   r      s    

r   c                   @      e Zd ZdddZdS )LiteLLMStreamHandlerNc                 C   s   |  || d S r   r'   r   r#   r$   r   r   r   process_chunkA   s   z"LiteLLMStreamHandler.process_chunkr   r?   r@   rA   rF   r   r   r   r   rC   @       rC   c                   @   rB   )LiteLLMAsyncStreamHandlerNc                    s   |  || d S r   rD   rE   r   r   r   rF   F   s   z'LiteLLMAsyncStreamHandler.process_chunkr   rG   r   r   r   r   rI   E   rH   rI   N)collectionsr   ddtrace.internal.loggerr   ddtrace.llmobs._constantsr   0ddtrace.llmobs._integrations.base_stream_handlerr   r   "ddtrace.llmobs._integrations.utilsr   r
   r?   r=   r   r   rC   rI   r   r   r   r   <module>   s    ,