diff --git a/litellm/integrations/langfuse.py b/litellm/integrations/langfuse/langfuse.py similarity index 88% rename from litellm/integrations/langfuse.py rename to litellm/integrations/langfuse/langfuse.py index 503b86fbdfa..182c8863760 100644 --- a/litellm/integrations/langfuse.py +++ b/litellm/integrations/langfuse/langfuse.py @@ -803,105 +803,3 @@ def log_requester_metadata(clean_metadata: dict): returned_metadata.update({"requester_metadata": requester_metadata}) return returned_metadata - - -def get_langfuse_logger_for_request( - standard_callback_dynamic_params: StandardCallbackDynamicParams, - in_memory_dynamic_logger_cache: DynamicLoggingCache, - globalLangfuseLogger: Optional[LangFuseLogger] = None, -) -> LangFuseLogger: - temp_langfuse_logger: Optional[LangFuseLogger] = globalLangfuseLogger - if ( - _dynamic_langfuse_credentials_are_passed(standard_callback_dynamic_params) - is False - ): - if temp_langfuse_logger is None: - credentials_dict = {} - temp_langfuse_logger = in_memory_dynamic_logger_cache.get_cache( - credentials=credentials_dict, - service_name="langfuse", - ) - if temp_langfuse_logger is None: - temp_langfuse_logger = create_langfuse_logger_from_credentials( - credentials=credentials_dict, - in_memory_dynamic_logger_cache=in_memory_dynamic_logger_cache, - ) - - return temp_langfuse_logger - - # get langfuse logging config to use for this request, based on standard_callback_dynamic_params - _credentials = get_dynamic_langfuse_logging_config( - globalLangfuseLogger=globalLangfuseLogger, - standard_callback_dynamic_params=standard_callback_dynamic_params, - ) - credentials_dict = dict(_credentials) - - # check if langfuse logger is already cached - temp_langfuse_logger = in_memory_dynamic_logger_cache.get_cache( - credentials=credentials_dict, service_name="langfuse" - ) - - # if not cached, create a new langfuse logger and cache it - if temp_langfuse_logger is None: - temp_langfuse_logger = create_langfuse_logger_from_credentials( - credentials=credentials_dict, - in_memory_dynamic_logger_cache=in_memory_dynamic_logger_cache, - ) - - return temp_langfuse_logger - - -def create_langfuse_logger_from_credentials( - credentials: Dict, - in_memory_dynamic_logger_cache: DynamicLoggingCache, -) -> LangFuseLogger: - langfuse_logger = LangFuseLogger( - langfuse_public_key=credentials.get("langfuse_public_key"), - langfuse_secret=credentials.get("langfuse_secret"), - langfuse_host=credentials.get("langfuse_host"), - ) - in_memory_dynamic_logger_cache.set_cache( - credentials=credentials, - service_name="langfuse", - logging_obj=langfuse_logger, - ) - return langfuse_logger - - -def get_dynamic_langfuse_logging_config( - standard_callback_dynamic_params: StandardCallbackDynamicParams, - globalLangfuseLogger: Optional[LangFuseLogger] = None, -) -> LangfuseLoggingConfig: - """ - 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 - """ - # only use dynamic params if langfuse credentials are passed dynamically - return LangfuseLoggingConfig( - langfuse_secret=standard_callback_dynamic_params.get("langfuse_secret") - or standard_callback_dynamic_params.get("langfuse_secret_key"), - langfuse_public_key=standard_callback_dynamic_params.get("langfuse_public_key"), - langfuse_host=standard_callback_dynamic_params.get("langfuse_host"), - ) - - -def _dynamic_langfuse_credentials_are_passed( - standard_callback_dynamic_params: StandardCallbackDynamicParams, -) -> bool: - """ - 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 - """ - if ( - standard_callback_dynamic_params.get("langfuse_host") is not None - or standard_callback_dynamic_params.get("langfuse_public_key") is not None - or standard_callback_dynamic_params.get("langfuse_secret") is not None - or standard_callback_dynamic_params.get("langfuse_secret_key") is not None - ): - return True - return False diff --git a/litellm/integrations/langfuse/langfuse_handler.py b/litellm/integrations/langfuse/langfuse_handler.py new file mode 100644 index 00000000000..53a90a1e2cb --- /dev/null +++ b/litellm/integrations/langfuse/langfuse_handler.py @@ -0,0 +1,127 @@ +""" +This file contains the LangFuseHandler class + +Used to get the LangFuseLogger for a given request + +Handles Key/Team Based Langfuse Logging +""" + +from typing import Dict, Optional + +from litellm.litellm_core_utils.litellm_logging import ( + DynamicLoggingCache, + StandardCallbackDynamicParams, +) + +from .langfuse import LangFuseLogger, LangfuseLoggingConfig + + +class LangFuseHandler: + + @staticmethod + def get_langfuse_logger_for_request( + standard_callback_dynamic_params: StandardCallbackDynamicParams, + in_memory_dynamic_logger_cache: DynamicLoggingCache, + globalLangfuseLogger: Optional[LangFuseLogger] = None, + ) -> LangFuseLogger: + temp_langfuse_logger: Optional[LangFuseLogger] = globalLangfuseLogger + if ( + LangFuseHandler._dynamic_langfuse_credentials_are_passed( + standard_callback_dynamic_params + ) + is False + ): + if temp_langfuse_logger is None: + credentials_dict = {} + temp_langfuse_logger = in_memory_dynamic_logger_cache.get_cache( + credentials=credentials_dict, + service_name="langfuse", + ) + if temp_langfuse_logger is None: + temp_langfuse_logger = LangFuseHandler.create_langfuse_logger_from_credentials( + credentials=credentials_dict, + in_memory_dynamic_logger_cache=in_memory_dynamic_logger_cache, + ) + + return temp_langfuse_logger + + # get langfuse logging config to use for this request, based on standard_callback_dynamic_params + _credentials = LangFuseHandler.get_dynamic_langfuse_logging_config( + globalLangfuseLogger=globalLangfuseLogger, + standard_callback_dynamic_params=standard_callback_dynamic_params, + ) + credentials_dict = dict(_credentials) + + # check if langfuse logger is already cached + temp_langfuse_logger = in_memory_dynamic_logger_cache.get_cache( + credentials=credentials_dict, service_name="langfuse" + ) + + # if not cached, create a new langfuse logger and cache it + if temp_langfuse_logger is None: + temp_langfuse_logger = ( + LangFuseHandler.create_langfuse_logger_from_credentials( + credentials=credentials_dict, + in_memory_dynamic_logger_cache=in_memory_dynamic_logger_cache, + ) + ) + + return temp_langfuse_logger + + @staticmethod + def create_langfuse_logger_from_credentials( + credentials: Dict, + in_memory_dynamic_logger_cache: DynamicLoggingCache, + ) -> LangFuseLogger: + langfuse_logger = LangFuseLogger( + langfuse_public_key=credentials.get("langfuse_public_key"), + langfuse_secret=credentials.get("langfuse_secret"), + langfuse_host=credentials.get("langfuse_host"), + ) + in_memory_dynamic_logger_cache.set_cache( + credentials=credentials, + service_name="langfuse", + logging_obj=langfuse_logger, + ) + return langfuse_logger + + @staticmethod + def get_dynamic_langfuse_logging_config( + standard_callback_dynamic_params: StandardCallbackDynamicParams, + globalLangfuseLogger: Optional[LangFuseLogger] = None, + ) -> LangfuseLoggingConfig: + """ + 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 + """ + # only use dynamic params if langfuse credentials are passed dynamically + return LangfuseLoggingConfig( + langfuse_secret=standard_callback_dynamic_params.get("langfuse_secret") + or standard_callback_dynamic_params.get("langfuse_secret_key"), + langfuse_public_key=standard_callback_dynamic_params.get( + "langfuse_public_key" + ), + langfuse_host=standard_callback_dynamic_params.get("langfuse_host"), + ) + + @staticmethod + def _dynamic_langfuse_credentials_are_passed( + standard_callback_dynamic_params: StandardCallbackDynamicParams, + ) -> bool: + """ + 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 + """ + if ( + standard_callback_dynamic_params.get("langfuse_host") is not None + or standard_callback_dynamic_params.get("langfuse_public_key") is not None + or standard_callback_dynamic_params.get("langfuse_secret") is not None + or standard_callback_dynamic_params.get("langfuse_secret_key") is not None + ): + return True + return False diff --git a/litellm/litellm_core_utils/litellm_logging.py b/litellm/litellm_core_utils/litellm_logging.py index 6643a5ebdeb..fc8b9ceb9cf 100644 --- a/litellm/litellm_core_utils/litellm_logging.py +++ b/litellm/litellm_core_utils/litellm_logging.py @@ -71,7 +71,8 @@ from ..integrations.gcs_bucket.gcs_bucket import GCSBucketLogger from ..integrations.greenscale import GreenscaleLogger from ..integrations.helicone import HeliconeLogger from ..integrations.lago import LagoLogger -from ..integrations.langfuse import LangFuseLogger, get_langfuse_logger_for_request +from ..integrations.langfuse.langfuse import LangFuseLogger +from ..integrations.langfuse.langfuse_handler import LangFuseHandler from ..integrations.langsmith import LangsmithLogger from ..integrations.litedebugger import LiteDebugger from ..integrations.literal_ai import LiteralAILogger @@ -1136,7 +1137,7 @@ class Logging: print_verbose("reaches langfuse for streaming logging!") result = kwargs["complete_streaming_response"] - langfuse_logger_to_use = get_langfuse_logger_for_request( + langfuse_logger_to_use = LangFuseHandler.get_langfuse_logger_for_request( globalLangfuseLogger=langFuseLogger, standard_callback_dynamic_params=self.standard_callback_dynamic_params, in_memory_dynamic_logger_cache=in_memory_dynamic_logger_cache, diff --git a/litellm/proxy/health_endpoints/_health_endpoints.py b/litellm/proxy/health_endpoints/_health_endpoints.py index 78b2a3d205a..6b5839ed0db 100644 --- a/litellm/proxy/health_endpoints/_health_endpoints.py +++ b/litellm/proxy/health_endpoints/_health_endpoints.py @@ -120,7 +120,7 @@ async def health_services_endpoint( # noqa: PLR0915 } if service == "langfuse": - from litellm.integrations.langfuse import LangFuseLogger + from litellm.integrations.langfuse.langfuse import LangFuseLogger langfuse_logger = LangFuseLogger() langfuse_logger.Langfuse.auth_check() diff --git a/tests/local_testing/test_alangfuse.py b/tests/local_testing/test_alangfuse.py index 802aebc0bde..f87ad1f253f 100644 --- a/tests/local_testing/test_alangfuse.py +++ b/tests/local_testing/test_alangfuse.py @@ -620,7 +620,7 @@ def test_aaalangfuse_existing_trace_id(): import datetime import litellm - from litellm.integrations.langfuse import LangFuseLogger + from litellm.integrations.langfuse.langfuse import LangFuseLogger langfuse_Logger = LangFuseLogger( langfuse_public_key=os.getenv("LANGFUSE_PROJECT2_PUBLIC"), @@ -1120,7 +1120,7 @@ generation_params = { ) def test_langfuse_prompt_type(prompt): - from litellm.integrations.langfuse import _add_prompt_to_generation_params + from litellm.integrations.langfuse.langfuse import _add_prompt_to_generation_params clean_metadata = { "prompt": { @@ -1227,7 +1227,7 @@ def test_langfuse_prompt_type(prompt): def test_langfuse_logging_metadata(): - from litellm.integrations.langfuse import log_requester_metadata + from litellm.integrations.langfuse.langfuse import log_requester_metadata metadata = {"key": "value", "requester_metadata": {"key": "value"}} diff --git a/tests/logging_callback_tests/test_langfuse_unit_tests.py b/tests/logging_callback_tests/test_langfuse_unit_tests.py index a702e541f6c..cacffe7e6a6 100644 --- a/tests/logging_callback_tests/test_langfuse_unit_tests.py +++ b/tests/logging_callback_tests/test_langfuse_unit_tests.py @@ -14,12 +14,10 @@ import litellm import asyncio import logging from litellm._logging import verbose_logger -from litellm.integrations.langfuse import ( +from litellm.integrations.langfuse.langfuse import ( LangFuseLogger, - get_langfuse_logger_for_request, - get_dynamic_langfuse_logging_config, - _dynamic_langfuse_credentials_are_passed, ) +from litellm.integrations.langfuse.langfuse_handler import LangFuseHandler from litellm.types.utils import StandardCallbackDynamicParams from litellm.litellm_core_utils.litellm_logging import DynamicLoggingCache @@ -63,7 +61,7 @@ def test_get_langfuse_logger_for_request_with_dynamic_params( Even if globalLangfuseLogger is provided, it should use dynamic params if they are passed """ - result = get_langfuse_logger_for_request( + result = LangFuseHandler.get_langfuse_logger_for_request( standard_callback_dynamic_params=standard_params, in_memory_dynamic_logger_cache=dynamic_logging_cache, globalLangfuseLogger=globalLangfuseLogger, @@ -96,7 +94,7 @@ def test_get_langfuse_logger_for_request_with_no_dynamic_params( """ If StandardCallbackDynamicParams are not provided, the globalLangfuseLogger should be returned """ - result = get_langfuse_logger_for_request( + result = LangFuseHandler.get_langfuse_logger_for_request( standard_callback_dynamic_params=StandardCallbackDynamicParams(), in_memory_dynamic_logger_cache=dynamic_logging_cache, globalLangfuseLogger=globalLangfuseLogger,