
    i"                         d dl mZ d dlZd dlmZ d dlmZ d dlmZmZ d dl	m
Z
 d dlmZmZ d dlmZ d d	lmZ erd d
l mZmZ d dlmZ 	 d dlZd Zd Zd Zd Z G d de      Zy# e$ r	  ed      w xY w)    )TYPE_CHECKINGN)consts)record_token_usage)get_start_span_functionset_data_normalized)SPANDATA)DidNotEnableIntegration)should_send_default_pii)event_from_exception)AnyDict)datetimezLiteLLM not installedc                 ^    | j                  di       }|j                  d      }|i }||d<   |S )z,Get the metadata dictionary from the kwargs.litellm_paramsmetadata)
setdefaultget)kwargsr   r   s      /var/www/html/hwPaymentPortal-be-dev/hw-payment-portal-api/venv/lib/python3.12/site-packages/sentry_sdk/integrations/litellm.py_get_metadata_dictr      sC     &&'7<N !!*-H%-z"O    c                    t        j                         j                  t              }|y| j	                  dd      }	 t        j                  |      \  }}}}| j	                  dg       }|rdnd} t               |dk(  rt        j                  j                  nt        j                  j                  | d| t        j                  	      }|j                          |t        |       d
<   t!        |t"        j$                  |       t!        |t"        j&                  |       |r3t)               r)|j*                  rt!        |t"        j,                  |d       t"        j.                  t"        j0                  t"        j2                  t"        j4                  t"        j6                  t"        j8                  t"        j:                  d}	|	j=                         D ]&  \  }
}| j	                  |
      }|t!        |||       ( | j	                  d      | j	                  d      | j	                  d      d}|j=                         D ]  \  }
}|	t!        |d|
 |        y# t        $ r |}d}Y w xY w)zHandle the start of a request.Nmodel unknownmessageschat
embeddings )opnameorigin_sentry_spanF)unpack)r   stream
max_tokenspresence_penaltyfrequency_penaltytemperaturetop_papi_baseapi_versioncustom_llm_provider)r,   r-   r.   zgen_ai.litellm.)
sentry_sdk
get_clientget_integrationLiteLLMIntegrationr   litellmget_llm_provider	Exceptionr   r   OPGEN_AI_CHATGEN_AI_EMBEDDINGSr#   	__enter__r   r   r   GEN_AI_SYSTEMGEN_AI_OPERATION_NAMEr   include_promptsGEN_AI_REQUEST_MESSAGESGEN_AI_REQUEST_MODELGEN_AI_RESPONSE_STREAMINGGEN_AI_REQUEST_MAX_TOKENSGEN_AI_REQUEST_PRESENCE_PENALTY GEN_AI_REQUEST_FREQUENCY_PENALTYGEN_AI_REQUEST_TEMPERATUREGEN_AI_REQUEST_TOP_Pitems)r   integration
full_modelr   provider_r   	operationspanparamskey	attributevaluer   s                 r   _input_callbackrP   #   s4    '')99:LMK GR(J ' 8 8 DxA
 zz*b)H"I %"$ F" II!!,,{!E7#!((D 	NN 26v~. h44h?h<<iH +-+2M2M(22HU	
 ..4488$DD%FF::..F !,,. 8Y

3i78 JJz*zz-0%zz*?@N
 %**, F
Uu&=uEFk  s   I I+*I+c           
         t        |       j                  d      }|yt        j                         j	                  t
              }|y	 t        |d      r%t        |t        j                  |j                         t               ry|j                  rlt        |d      r_g }|j                  D ]0  }t        |d      st        |j                  d      r*|j                  |j                  j!                                Qt        |j                  d      r*|j                  |j                  j#                                i }t        |j                  d      r|j                  j$                  |d<   t        |j                  d	      r|j                  j&                  |d	<   t        |j                  d
      r|j                  j(                  |d
<   |j                  |       3 |rt        |t        j*                  |       t        |d      r<|j,                  }	t/        |t1        |	dd      t1        |	dd      t1        |	dd             |j3                  ddd       y# |j3                  ddd       w xY w)zHandle successful completion.r$   Nr   choicesmessage
model_dumpdictrolecontent
tool_callsusageprompt_tokenscompletion_tokenstotal_tokens)input_tokensoutput_tokensr\   )r   r   r/   r0   r1   r2   hasattrr   r   GEN_AI_RESPONSE_MODELr   r   r<   rR   rS   appendrT   rU   rV   rW   rX   GEN_AI_RESPONSE_TEXTrY   r   getattr__exit__)
r   completion_response
start_timeend_timerK   rF   response_messageschoicemsgrY   s
             r   _success_callbackrk   i   s    f%)).9D|'')99:LMK-(&0h446I6O6O
 #$)D)D*I6$&!199 :Fvy1"6>><@-44V^^5N5N5PQ$V^^V<-44V^^5H5H5JK #%C&v~~v>.4nn.A.AF&v~~yA171G1GI&v~~|D4:NN4M4ML 1-44S9:" %'h;;=N
 &0'--E$UOTB%e-@$G$UNDA	 	dD$'dD$'s   	A4I >FI I/c                 H   t        |       j                  d      }|y	 t        |t        j                         j
                  ddd      \  }}t        j                  ||       |j                  t        |      |d       y# |j                  t        |      |d       w xY w)zHandle request failure.r$   Nr3   F)typehandled)client_options	mechanism)hint)	r   r   r   r/   r0   optionscapture_eventrd   rm   )r   	exceptionrf   rg   rK   eventrq   s          r   _failure_callbackrv      s     f%)).9D|
8*%002::(U;
t
 	  T2 	d9oy$7d9oy$7s   AB B!c                   6    e Zd ZdZdZde ZddZed        Zy)r2   a"  
    LiteLLM integration for Sentry.

    This integration automatically captures LiteLLM API calls and sends them to Sentry
    for monitoring and error tracking. It supports all 100+ LLM providers that LiteLLM
    supports, including OpenAI, Anthropic, Google, Cohere, and many others.

    Features:
    - Automatic exception capture for all LiteLLM calls
    - Token usage tracking across all providers
    - Provider detection and attribution
    - Input/output message capture (configurable)
    - Streaming response support
    - Cost tracking integration

    Usage:

    ```python
    import litellm
    import sentry_sdk

    # Initialize Sentry with the LiteLLM integration
    sentry_sdk.init(
        dsn="your-dsn",
        send_default_pii=True
        integrations=[
            sentry_sdk.integrations.LiteLLMIntegration(
                include_prompts=True  # Set to False to exclude message content
            )
        ]
    )

    # All LiteLLM calls will now be monitored
    response = litellm.completion(
        model="gpt-3.5-turbo",
        messages=[{"role": "user", "content": "Hello!"}]
    )
    ```

    Configuration:
    - include_prompts (bool): Whether to include prompts and responses in spans.
      Defaults to True. Set to False to exclude potentially sensitive data.
    r3   zauto.ai.c                     || _         y )N)r<   )selfr<   s     r   __init__zLiteLLMIntegration.__init__   s
    .r   c                  
   t         j                  xs g t         _        t        t         j                  vr#t         j                  j                  t               t         j                  xs g t         _        t
        t         j                  vr#t         j                  j                  t
               t         j                  xs g t         _        t        t         j                  vr$t         j                  j                  t               yy)z(Set up LiteLLM callbacks for monitoring.N)r3   input_callbackrP   ra   success_callbackrk   failure_callbackrv    r   r   
setup_oncezLiteLLMIntegration.setup_once   s     ")!7!7!=2'"8"88""))/:#*#;#;#Ar G$<$<<$$++,=>#*#;#;#Ar G$<$<<$$++,=> =r   N)T)	__name__
__module____qualname____doc__
identifierr#   rz   staticmethodr   r   r   r   r2   r2      s4    *X J
|$F/ ? ?r   r2   )typingr   r/   r   sentry_sdk.ai.monitoringr   sentry_sdk.ai.utilsr   r   sentry_sdk.constsr   sentry_sdk.integrationsr	   r
   sentry_sdk.scoper   sentry_sdk.utilsr   r   r   r   r3   ImportErrorr   rP   rk   rv   r2   r   r   r   <module>r      su        7 L & = 4 1 !0

CFL9(x8(B? B?O  0
.
//0s   
A& &A4