o
    i                     @   s   d dl 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 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di  defddZdeeef fddZd#ddZdd Zdd Zdd Zdd Zdd Zdd  Zd!d" ZdS )$    N)config)LiteLLMAsyncStreamHandler)LiteLLMStreamHandler)extract_host_tag)unwrap)wrap)get_argument_value)LITELLM_ROUTER_INSTANCE_KEY)LiteLLMIntegration)make_traced_streamlitellmreturnc                  C   s   t tdd } t | ddS )N_versionversion )getattrr   )version_module r   Z/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/contrib/internal/litellm/patch.pyget_version   s   r   c                   C   s   ddiS )Nr   *r   r   r   r   r   _supported_versions   s   r   Fc                 C   sR   t | drt | jdr| j||| | S ||t< |rtnt}t| |||||S )a  
    Handle router streaming responses with fallback for different wrapper types.

    In litellm>=1.74.15, router streaming responses may be wrapped in FallbackStreamWrapper
    (for mid-stream fallback support) or other types that don't expose the .handler attribute.
    handleradd_span)hasattrr   r   r	   r   r   r   )respspankwargsinstanceintegrationargsis_asynchandler_classr   r   r   _handle_router_stream_response   s   r#   c                 C   s"  | j }tj}t||ddd }t|}|j||||dd p"|dd ||| d}|dd}	d }
zKz;| |i |}
|	r[t|
t	||||W W |	sZ|j
||||
|d |  S S |
W W |	so|j
||||
|d |  S S  ty~   |jt    w |	s|j
||||
|d |  w w 	Nr   modelbase_urlapi_baser%   hostr&   submit_to_llmobsstreamFr    r   response	operation)__name__r   _datadog_integrationr   r   traceget_has_downstream_openai_spanr   r   llmobs_set_tagsfinish	Exceptionset_exc_infosysexc_infofuncr   r    r   r.   r   r%   r)   r   r+   r   r   r   r   traced_completion.   sD   


r<   c                    s*  | j }tj}t||ddd }t|}|j||||dd p#|dd ||| d}|dd}	d }
zNz>| |i |I d H }
|	r_t|
t	||||W W |	s^|j
||||
|d |  S S |
W W |	ss|j
||||
|d |  S S  ty   |jt    w |	s|j
||||
|d |  w w r$   )r/   r   r0   r   r   r1   r2   r3   r   r   r4   r5   r6   r7   r8   r9   r:   r   r   r   traced_acompletionK   sF   


r=   c                 C   s8  d| j  }tj}t||ddd }t|}|j||||dd p%|dd dd}|dd	}	d }
zTzD| |i |}
|	r^t|
|||||d	d
W W |	s]||t< |j	||||
|d |
  S S |
W W |	sv||t< |j	||||
|d |
  S S  ty   |jt    w |	s||t< |j	||||
|d |
  w w Nzrouter.r   r%   r&   r'   Tr(   r+   F)r!   r,   r/   r   r0   r   r   r1   r2   r#   r	   r4   r5   r6   r7   r8   r9   r:   r   r   r   traced_router_completionh   sJ   


r@   c                    s@  d| j  }tj}t||ddd }t|}|j||||dd p&|dd dd}|dd	}	d }
zWzG| |i |I d H }
|	rbt|
|||||dd
W W |	sa||t< |j	||||
|d |
  S S |
W W |	sz||t< |j	||||
|d |
  S S  ty   |jt    w |	s||t< |j	||||
|d |
  w w r>   r?   r:   r   r   r   traced_router_acompletion   sL   


rA   c           
      C   sF   t ||ddd }tj}| |i |\}}}}	||f|j|< ||||	fS )Nr   r%   )r   r   r0   
_model_map)
r;   r   r    r   requested_modelr   r%   custom_llm_providerdynamic_api_keyr'   r   r   r   traced_get_llm_provider   s
   rF   c                  C   s   t tddrd S dt_ttjd} | t_tddt tddt tddt tdd	t tdd
t	 tddt	 tddt
 tddt tddt
 tddt d S )N_datadog_patchFT)integration_configr   
completionacompletiontext_completionatext_completionget_llm_providerzmain.get_llm_providerzrouter.Router.completionzrouter.Router.acompletionzrouter.Router.text_completionzrouter.Router.atext_completion)r   r   rG   r
   r   r0   r   r<   r=   rF   r@   rA   )r   r   r   r   patch   s   rN   c                   C   s   t tddsd S dt_ttd ttd ttd ttd ttd ttjd ttjjd ttjjd ttjjd ttjjd ttd d S )	NrG   FrI   rJ   rK   rL   rM   r0   )r   r   rG   r   mainrouterRouterdelattrr   r   r   r   unpatch   s   




rS   )F) r8   r   ddtracer   &ddtrace.contrib.internal.litellm.utilsr   r   r   ddtrace.contrib.trace_utilsr   r   ddtrace.internal.utilsr   ddtrace.llmobs._constantsr	   ddtrace.llmobs._integrationsr
   0ddtrace.llmobs._integrations.base_stream_handlerr   _addstrr   dictr   r#   r<   r=   r@   rA   rF   rN   rS   r   r   r   r   <module>   s.    
	