
    g                     V    d dl Z d dlmZmZ d dlmZ er	d dlmZ eZneZ G d d      Z	y)    N)TYPE_CHECKINGAny)SpanAttributes)Spanc                   L    e Zd ZdZdefdZdefdZdefdZdefdZdefdZ	y)	LangtraceAttributeszJ
    This class is used to save trace attributes to Langtrace's spans
    spanc                     |j                  d      j                  d      }|j                  di       }i ||}| j                  |||       | j                  ||       | j                  ||       y)zE
        This function is used to log the event to Langtrace
        litellm_paramscustom_llm_provideroptional_paramsN)getset_request_attributesset_response_attributesset_usage_attributes)selfr	   kwargsresponse_objvendorr   optionss          S/var/www/openai/venv/lib/python3.12/site-packages/litellm/integrations/langtrace.pyset_langtrace_attributesz,LangtraceAttributes.set_langtrace_attributes   sq    
 ,-112GH **%6;/V//##D'6:$$T<8!!$5    c                    ddd|t         j                  j                  |j                  d      t         j                  j                  |j                  d      t         j
                  j                  |j                  d      t         j                  j                  |j                  d      t         j                  j                  |j                  d      t         j                  j                  |j                  d	      t         j                  j                  |j                  d
      t         j                  j                  |j                  d      t         j                  j                  |j                  d      t         j                  j                  |j                  d      i}|j                  d      }|r@|j                  dt         j                  j                  t        j                   |      i       | j#                  ||       y)zR
        This function is used to get span attributes for the LLM request
        zgen_ai.operation.namechatzlangtrace.service.namemodelstreamtemperaturetop_ktop_puser
max_tokensstopfrequency_penaltypresence_penaltymessageszgen_ai.content.promptname
attributesN)r   LLM_REQUEST_MODELvaluer   LLM_IS_STREAMINGLLM_REQUEST_TEMPERATURE	LLM_TOP_KLLM_REQUEST_TOP_PLLM_USERLLM_REQUEST_MAX_TOKENSLLM_RESPONSE_STOP_REASONLLM_FREQUENCY_PENALTYLLM_PRESENCE_PENALTY	add_eventLLM_PROMPTSjsondumpsset_span_attributes)r   r	   r   r   span_attributespromptss         r   r   z*LangtraceAttributes.set_request_attributes   sq   
 $V$f,,22FJJw4G++116::h3G2288&**]:S$$**FJJw,?,,22FJJw4G##))6::f+=1177L9Q33996::f;M0066

CV8W//55vzzBT7U
 **Z(NN,*66<<djj>QR  
 	  7r   c                    d|j                  d      d|j                  d      t        j                  j                  |j                  d      i}g }|j                  dg       D ]V  }|j                  d      j                  d      }|j                  d      j                  d	      }|j	                  ||d
       X |j                  dt        j                  t        j                  |      i       | j                  ||       y)zS
        This function is used to get span attributes for the LLM response
        zgen_ai.response_ididzgen_ai.system_fingerprintsystem_fingerprintr   choicesmessagerolecontent)rA   rB   zgen_ai.content.completionr'   N)
r   r   LLM_RESPONSE_MODELr+   appendr5   LLM_COMPLETIONSr7   r8   r9   )r   r	   r   response_attributescompletionschoicerA   rB   s           r   r   z+LangtraceAttributes.set_response_attributes<   s    
 !,"2"24"8')9)9:N)O--33\5E5Eg5N

 "&&y"5F::i(,,V4Djj+//	:GAB 6
 	,&66

;8OP 	 	

 	  ':;r   c                 J   |j                  d      }|rt        j                  j                  |j                  d      t        j                  j                  |j                  d      t        j
                  j                  |j                  d      i}| j                  ||       yy)zP
        This function is used to get span attributes for the LLM usage
        usageprompt_tokenscompletion_tokenstotal_tokensN)r   r   LLM_USAGE_PROMPT_TOKENSr+   LLM_USAGE_COMPLETION_TOKENSLLM_USAGE_TOTAL_TOKENSr9   )r   r	   r   rJ   usage_attributess        r   r   z(LangtraceAttributes.set_usage_attributesR   s       )66<<eii#? ::@@%))'C 55;;UYY~=V  $$T+;< r   c                 ^    |j                         D ]  \  }}|s	|j                  ||        y)z>
        This function is used to set span attributes
        N)itemsset_attribute)r   r	   r)   keyr+   s        r   r9   z'LangtraceAttributes.set_span_attributesc   s1     %**,JCsE* -r   N)
__name__
__module____qualname____doc__r   r   r   r   r   r9    r   r   r   r      sC    
6T 
684 8:<D <,= ="+ +r   r   )
r7   typingr   r   litellm.proxy._typesr   opentelemetry.tracer   _Spanr   rZ   r   r   <module>r_      s*     % /1DD\+ \+r   