
    g                     f    d Z ddlmZmZmZmZ ddlmZ ddlm	Z	m
Z
 erddlmZ neZ G d d      Zy	)
z
This file contains the LangFuseHandler class

Used to get the LangFuseLogger for a given request

Handles Key/Team Based Langfuse Logging
    )TYPE_CHECKINGAnyDictOptional)StandardCallbackDynamicParams   )LangFuseLoggerLangfuseLoggingConfig)DynamicLoggingCachec            
           e Zd Ze	 ddededee   defd       Zedee   dedefd       Z	ede
dedefd	       Ze	 ddedee   defd
       Zededefd       Zy)LangFuseHandlerN standard_callback_dynamic_paramsin_memory_dynamic_logger_cacheglobalLangfuseLoggerreturnc                    |}t         j                  |       du rt         j                  ||      S t         j                  ||       }t	        |      }|j                  |d      }|t         j                  ||      }|S )a  
        This function is used to get the LangFuseLogger for a given request

        1. If dynamic credentials are passed
            - check if a LangFuseLogger is cached for the dynamic credentials
            - if cached LangFuseLogger is not found, create a new LangFuseLogger and cache it

        2. If dynamic credentials are not passed return the globalLangfuseLogger

        F)r   r   )r   r   langfusecredentialsservice_namer   r   )r   (_dynamic_langfuse_credentials_are_passed_return_global_langfuse_logger#get_dynamic_langfuse_logging_configdict	get_cache(_create_langfuse_logger_from_credentials)r   r   r   temp_langfuse_logger_credentialscredentials_dicts         c/var/www/openai/venv/lib/python3.12/site-packages/litellm/integrations/langfuse/langfuse_handler.pyget_langfuse_logger_for_requestz/LangFuseHandler.get_langfuse_logger_for_request   s      :NDD0 
 #AA%9/M B   'JJ!5-M K 
  -  >GG(z  H  

  'HH 03Q I  ! $#    c                 j    | | S i }|j                  |d      } | t        j                  ||      } | S )a  
        Returns the Global LangfuseLogger set on litellm

        (this is the default langfuse logger - used when no dynamic credentials are passed)

        If no Global LangfuseLogger is set, it will check in_memory_dynamic_logger_cache for a cached LangFuseLogger
        This function is used to return the globalLangfuseLogger if it exists, otherwise it will check in_memory_dynamic_logger_cache for a cached LangFuseLogger
        r   r   r   )r   r   r   )r   r   r    s      r!   r   z.LangFuseHandler._return_global_langfuse_loggerJ   sh      +''  	  >GG(#  H  
  'HH 03Q I  ! $#r#   r   c                     t        | j                  d      | j                  d      | j                  d            }|j                  | d|       |S )z
        This function is used to
        1. create a LangFuseLogger from the credentials
        2. cache the LangFuseLogger to prevent re-creating it for the same credentials
        langfuse_public_keylangfuse_secretlangfuse_host)r&   r'   r(   r   )r   r   logging_obj)r	   get	set_cache)r   r   langfuse_loggers      r!   r   z8LangFuseHandler._create_langfuse_logger_from_credentialsj   sZ     ) +0E F'OO,=>%///:

 	'00##' 	1 	

 r#   c                     t        | j                  d      xs | j                  d      | j                  d      | j                  d            S )aT  
        This function is used to get the Langfuse logging config to use for a given request.

        It checks if the dynamic parameters are provided in the standard_callback_dynamic_params and uses them to get the Langfuse logging config.

        If no dynamic parameters are provided, it uses the `globalLangfuseLogger` values
        r'   langfuse_secret_keyr&   r(   )r'   r&   r(   )r
   r*   )r   r   s     r!   r   z3LangFuseHandler.get_dynamic_langfuse_logging_config   sV     %<@@ARS K/334IJ @ D D%! ;>>O
 	
r#   c                     | j                  d      3| j                  d      "| j                  d      | j                  d      yy)z
        This function is used to check if the dynamic langfuse credentials are passed in standard_callback_dynamic_params

        Returns:
            bool: True if the dynamic langfuse credentials are passed, False otherwise
        r(   r&   r'   r.   TF)r*   )r   s    r!   r   z8LangFuseHandler._dynamic_langfuse_credentials_are_passed   sQ     -00AM/334IJV/334EFR/334IJVr#   )N)__name__
__module____qualname__staticmethodr   r   r   r	   r"   r   r   r   r
   r   boolr    r#   r!   r   r      s     :>0$*G0$(;0$ '~60$ 
	0$ 0$d $&~6$(;$ 
$ $> (; 
 ,  :>
*G
&~6
 

 
* *G	 r#   r   N)__doc__typingr   r   r   r   *litellm.litellm_core_utils.litellm_loggingr   r   r	   r
   r   r   r5   r#   r!   <module>r9      s0    6 5 T ;NT Tr#   