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	 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 d dlmZ d dlmZ d dlmZ dZdZdZdZ G dd deZ!dS )    )Any)Optional)get_argument_value)INPUT_TOKENS_METRIC_KEY)LITELLM_ROUTER_INSTANCE_KEY)METADATA)METRICS)
MODEL_NAME)MODEL_PROVIDER)OUTPUT_TOKENS_METRIC_KEY)PROXY_REQUEST)	SPAN_KIND)TOTAL_TOKENS_METRIC_KEY)BaseLLMIntegration)openai_set_meta_tags_from_chat)$openai_set_meta_tags_from_completion)(update_proxy_workflow_input_output_value)LLMObs)	_get_attr)Span)text_completionatext_completionzrouter.text_completionzrouter.atext_completion)api_basemodelorganization
max_tokenstemperaturetags)
deploymentendpointheaderslitellm_api_versionmodel_groupmodel_group_size
model_infor   )bodyurlmethodc                   @   s  e Zd ZU dZi Zeeeeef f ed< 	d)de	de
e de
e deeef ddf
d	d
Z		d*de	dee deeef de
e deddfddZde	deeef defddZdeeeef  deeeef  fddZd+deeef de
e defddZ	d)de	deeef de
e de
e def
ddZdeeef deedf deeef fddZdedefdd Zde
e defd!d"Zed#ed$edeeef fd%d&Zdeeef de
e fd'd(ZdS ),LiteLLMIntegrationlitellm
_model_mapNspanr   hostkwargsreturnc                 K   s0   |d ur
| d| |d ur| d| d S d S )Nzlitellm.request.modelzlitellm.request.host)_set_tag_str)selfr+   r   r,   r-    r1   X/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/llmobs/_integrations/litellm.py_set_base_span_tags2   s
   z&LiteLLMIntegration._set_base_span_tags argsresponse	operationc           
   
   C   s   t ||dddp	d}| j||df\}}| |r#t|||dd nt|||dd | ||| | ||||}t|| | 	||}	|
t|t|pMdt|t|	i d S )Nr   r   Fr4   r)   )integration_name)r   r*   getis_completion_operationr   r   _update_litellm_metadata_get_span_kindr   _extract_llmobs_metrics_set_ctx_itemsr   r	   r
   r   )
r0   r+   r5   r-   r6   r7   
model_namemodel_provider	span_kindmetricsr1   r1   r2   _llmobs_set_tags:   s   

z#LiteLLMIntegration._llmobs_set_tagsc           
   	   C   sp  | tpi }|dp|d}d|v r:i }tD ]}|d |}|dkr/|r/d|di}|r5|||< q||d< d|v rH| |d t|d< |r]d|v r]|d |d< |t|i d S |scd|vrl|t|i d S |t}	|	s||t|i d S t|	d	d t|	d
d t|	dd t|	dd t|	dd t|	dd d|d< t	|	dr| 
|	 |d d< |t|i d S )Nbase_urlr   metadatar    r,   proxy_server_requestr   routerrouter_general_settingsrouting_strategyrouting_strategy_argsprovider_budget_configretry_policyenable_tag_filtering)rH   rI   rJ   rK   rL   rM   router_settingsget_model_list
model_list)_get_ctx_itemr   r9   METADATA_KEYS_select_keysPROXY_SERVER_REQUEST_KEYSr>   r   getattrhasattr_construct_litellm_model_listrO   )
r0   r+   r-   r7   rE   rD   inner_metadatakeyvalue
llm_routerr1   r1   r2   r;   W   sJ   








z+LiteLLMIntegration._update_litellm_metadatarP   c                 C   s@   g }|D ]}| di }| |t}|| d|d q|S )Nlitellm_paramsr?   )r?   r\   )r9   rS   MODEL_LIST_KEYSappend)r0   rP   new_model_listr   r\   litellm_params_dictr1   r1   r2   rW      s   z0LiteLLMIntegration._construct_litellm_model_listc                    sF   | dd}|r| nd t fdddD }|o"| o"tdS )am  
        Determine whether an LLM span will be submitted for the given request from outside the LiteLLM integration.

        Currently, this only applies to the OpenAI integration when the following conditions are met:
            - request is not streamed
            - model provider is OpenAI/AzureOpenAI
            - the OpenAI integration is enabled
        streamFr4   c                 3   s    | ]}| v V  qd S Nr1   ).0prefixmodel_lowerr1   r2   	<genexpr>   s    zALiteLLMIntegration._has_downstream_openai_span.<locals>.<genexpr>)gptopenaiazureri   )r9   loweranyr   _integration_is_enabled)r0   r-   r   ra   is_openai_modelr1   re   r2   _has_downstream_openai_span   s   	z.LiteLLMIntegration._has_downstream_openai_spanc                 C   s   |  |s
|trdS dS )a#  
        Workflow span should be submitted to LLMObs if:
            - span represents a router operation OR
            - span represents a proxy request
        LLM spans should be submitted to LLMObs if:
            - span does not represent a router operation or a proxy request
        workflowllm)is_router_operationrQ   r   )r0   r+   r-   r   r7   r1   r1   r2   r<      s   
z!LiteLLMIntegration._get_span_kinddatakeys_to_select.c                 C   s(   i }|D ]}| |}|r|||< q|S rb   )r9   )r0   rs   rt   new_datarY   rZ   r1   r1   r2   rS      s   
zLiteLLMIntegration._select_keysc                 C   s   |t v S rb   )TEXT_COMPLETION_OPERATIONSr0   r7   r1   r1   r2   r:      s   z*LiteLLMIntegration.is_completion_operationc                 C   s   |rd|v S dS )NrG   Fr1   rw   r1   r1   r2   rr      s   z&LiteLLMIntegration.is_router_operationresprA   c                 C   sp   | r|dkri S t | trt| d dd }nt| dd }|d u r"i S t|dd}t|dd}t|t|t|| iS )Nrq   r   usageprompt_tokenscompletion_tokens)
isinstancelistr   r   r   r   )rx   rA   token_usagerz   r{   r1   r1   r2   r=      s   
z*LiteLLMIntegration._extract_llmobs_metricsc                 K   s   | d}|rt|S d S )NrD   )r9   str)r0   r-   rD   r1   r1   r2   _get_base_url   s   
z LiteLLMIntegration._get_base_url)NN)Nr4   rb   )__name__
__module____qualname___integration_namer*   dictr   tuple__annotations__r   r   r   r3   r}   rC   r;   rW   boolro   r<   rS   r:   rr   staticmethodr=   r   r1   r1   r1   r2   r(   -   sf   
 



*,$

. "r(   N)"typingr   r   ddtrace.internal.utilsr   ddtrace.llmobs._constantsr   r   r   r   r	   r
   r   r   r   r   !ddtrace.llmobs._integrations.baser   #ddtrace.llmobs._integrations.openair   r   "ddtrace.llmobs._integrations.utilsr   ddtrace.llmobs._llmobsr   ddtrace.llmobs._utilsr   ddtrace.tracer   rv   r]   rR   rT   r(   r1   r1   r1   r2   <module>   s2    
