o
    ưi                     @   s\   d dl Z d dlmZmZmZ d dlmZ er#d dlmZ	 ee	ef ZneZG dd dZ
dS )    N)TYPE_CHECKINGAnyUnion)SpanAttributes)Spanc                   @   sV   e Zd ZdZdefddZdefddZdefddZdefd	d
ZdefddZ	dS )LangtraceAttributeszJ
    This class is used to save trace attributes to Langtrace's spans
    spanc                 C   sR   | d d}| di }i ||}| ||| | || | || dS )zE
        This function is used to log the event to Langtrace
        Zlitellm_paramsZcustom_llm_provideroptional_paramsN)getset_request_attributesset_response_attributesset_usage_attributes)selfr   kwargsresponse_objvendorr	   options r   U/home/app/Keep/.python/lib/python3.10/site-packages/litellm/integrations/langtrace.pyset_langtrace_attributes   s   z,LangtraceAttributes.set_langtrace_attributesc                 C   s   ddd|t jj|dt jj|dt jj|dt jj|dt jj|dt jj|d	t j	j|d
t j
j|dt jj|dt jj|di}|d}|ra|jdt jjt|id | || dS )zR
        This function is used to get span attributes for the LLM request
        zgen_ai.operation.nameZchatzlangtrace.service.namemodelstreamZtemperatureZtop_kZtop_puserZ
max_tokensstopZfrequency_penaltyZpresence_penaltymessageszgen_ai.content.promptname
attributesN)r   ZLLM_REQUEST_MODELvaluer
   ZLLM_IS_STREAMINGZLLM_REQUEST_TEMPERATUREZ	LLM_TOP_KZLLM_REQUEST_TOP_PZLLM_USERZLLM_REQUEST_MAX_TOKENSZLLM_RESPONSE_STOP_REASONZLLM_FREQUENCY_PENALTYZLLM_PRESENCE_PENALTY	add_eventZLLM_PROMPTSjsondumpsset_span_attributes)r   r   r   r   Zspan_attributesZpromptsr   r   r   r      s(   
z*LangtraceAttributes.set_request_attributesc                 C   s   d| dd| dtjj| di}g }| dg D ]}| d d}| d d	}|||d
 q|jdtjt|id | 	|| dS )zS
        This function is used to get span attributes for the LLM response
        zgen_ai.response_ididzgen_ai.system_fingerprintZsystem_fingerprintr   choicesmessagerolecontent)r&   r'   zgen_ai.content.completionr   N)
r
   r   ZLLM_RESPONSE_MODELr   appendr   ZLLM_COMPLETIONSr    r!   r"   )r   r   r   Zresponse_attributesZcompletionschoicer&   r'   r   r   r   r   <   s   

z+LangtraceAttributes.set_response_attributesc                 C   sP   | d}|r&tjj| dtjj| dtjj| di}| || dS dS )zP
        This function is used to get span attributes for the LLM usage
        usageZprompt_tokensZcompletion_tokensZtotal_tokensN)r
   r   ZLLM_USAGE_PROMPT_TOKENSr   ZLLM_USAGE_COMPLETION_TOKENSZLLM_USAGE_TOTAL_TOKENSr"   )r   r   r   r*   Zusage_attributesr   r   r   r   R   s   


	z(LangtraceAttributes.set_usage_attributesc                 C   s(   |  D ]\}}|sq||| qdS )z>
        This function is used to set span attributes
        N)itemsZset_attribute)r   r   r   keyr   r   r   r   r"   c   s
   z'LangtraceAttributes.set_span_attributesN)
__name__
__module____qualname____doc__r   r   r   r   r   r"   r   r   r   r   r      s    r   )r    typingr   r   r   Zlitellm.proxy._typesr   Zopentelemetry.tracer   _Spanr   r   r   r   r   <module>   s    