0i'ddlmZddlZddlmZddlmZddlmZmZm Z ddl m Z ddl m Z mZddlmZdd lmZerdd lmZmZdd lmZ ddlZd ZdZdZdZGddeZy#e$r e d wxYw)) TYPE_CHECKINGN)consts)record_token_usage)get_start_span_functionset_data_normalizedtruncate_and_annotate_messages)SPANDATA) DidNotEnable Integration)should_send_default_pii)event_from_exception)AnyDict)datetimezLiteLLM not installedc^|jdi}|jd}|i}||d<|S)z,Get the metadata dictionary from the kwargs.litellm_paramsmetadata) setdefaultget)kwargsrrs N/opt/hc_python/lib/python3.12/site-packages/sentry_sdk/integrations/litellm.py_get_metadata_dictrsC&&'7t"j@t"jBt"jDd}|jGD]&\}}|j |}|t!|||(|j d|j d|j dd}|jGD]\}}| t!|d||y#t$r|}d}YwxYw)zHandle the start of a request.Nmodelunknown call_type embedding embeddingschat )opnameorigin _sentry_spaninputF)unpackmessages)rstream max_tokenspresence_penaltyfrequency_penalty temperaturetop_papi_base api_versioncustom_llm_provider)r0r1r2zgen_ai.litellm.)$ sentry_sdk get_clientget_integrationLiteLLMIntegrationrlitellmget_llm_provider ExceptionrrOP GEN_AI_CHATGEN_AI_EMBEDDINGSr% __enter__rrr GEN_AI_SYSTEMGEN_AI_OPERATION_NAMEr include_promptsget_current_scope isinstancelistrGEN_AI_EMBEDDINGS_INPUTGEN_AI_REQUEST_MESSAGESGEN_AI_REQUEST_MODELGEN_AI_RESPONSE_STREAMINGGEN_AI_REQUEST_MAX_TOKENSGEN_AI_REQUEST_PRESENCE_PENALTY GEN_AI_REQUEST_FREQUENCY_PENALTYGEN_AI_REQUEST_TEMPERATUREGEN_AI_REQUEST_TOP_Pitems)r integration full_modelrprovider_r operationspanembedding_inputscope input_list messages_datar)paramskey attributevaluers r_input_callbackr\'s'')99:LMKGR(J ' 8 8 DxA  ;-IK   % " $F" II ! !,,{!E7 #!(( D NN26v~.h44h?h<xu U  ,' 88%$ ..4488$DD%FF::..F!,,.Y 3   i 7)JJz*zz-0%zz*?@N %**, U   u&=u E-k sL LLc t|jd}|ytjj t }|y t |dr%t|tj|jtry|jrlt |dr_g}|jD]0}t |dst |jdr*|j|jj!Qt |jdr*|j|jj#i}t |jdr|jj$|d<t |jd r|jj&|d <t |jd r|jj(|d <|j|3|rt|tj*|t |d r<|j,} t/|t1| d dt1| d dt1| dd|j3dddy#|j3dddwxYw)zHandle successful completion.r&Nrchoicesmessage model_dumpdictrolecontent tool_callsusage prompt_tokenscompletion_tokens total_tokens) input_tokens output_tokensrh)rrr3r4r5r6hasattrrr GEN_AI_RESPONSE_MODELrr r@r^r_appendr`rarbrcrdGEN_AI_RESPONSE_TEXTrergetattr__exit__) rcompletion_response start_timeend_timerSrNresponse_messageschoicemsgres r_success_callbackrws f % ) ). 9D |'')99:LMK-( & 0 h446I6O6O  # $)D)D*I6$&!199Fvy1"6>><@-44V^^5N5N5PQ$V^^V<-44V^^5H5H5JK#%C&v~~v>.4nn.A.AF &v~~yA171G1GI&v~~|D4:NN4M4ML 1-44S9:"%'h;;=N & 0'--E $UOTB%e-@$G$UNDA   dD$' dD$'s A4I>FII/cHt|jd}|y t|tjj ddd\}}tj |||jt||dy#|jt||dwxYw)zHandle request failure.r&Nr7F)typehandled)client_options mechanism)hint) rrr r3r4options capture_eventrpry)r exceptionrrrsrSeventr}s r_failure_callbackrs f % ) ). 9D | 8* %002::(U; t   T2 d9oy$7 d9oy$7s ABB!c6eZdZdZdZdeZddZedZy)r6a" 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. r7zauto.ai.c||_y)N)r@)selfr@s r__init__zLiteLLMIntegration.__init__ s .rc tjxsgt_ttjvr#tjjttjxsgt_t tjvr#tjjt tj xsgt_ttj vr$tj jtyy)z(Set up LiteLLM callbacks for monitoring.N)r7input_callbackr\rmsuccess_callbackrwfailure_callbackrrr setup_oncezLiteLLMIntegration.setup_onces")!7!7!=2 '"8"8 8  " " ) )/ :#*#;#;#Ar G$<$< <  $ $ + +,= >#*#;#;#Ar G$<$< <  $ $ + +,= > =rN)T) __name__ __module__ __qualname____doc__ identifierr%r staticmethodrrrrr6r6s4*XJ | $F/ ? ?rr6)typingrr3rsentry_sdk.ai.monitoringrsentry_sdk.ai.utilsrrrsentry_sdk.constsr sentry_sdk.integrationsr r sentry_sdk.scoper sentry_sdk.utilsr rrrr7 ImportErrorrr\rwrr6rrrrs~ 7 '=41 !0 cFL9(x8(B?B?O0 . //0s A((A6