Download app/monitoring/langfuse_setup.py from acpcchatbotadmin/acpcchatbot_testing_first: direct link, hf CLI and curl.
- Browser
- Download file 2.78 kB
-
https://huggingface.co/spaces/acpcchatbotadmin/acpcchatbot_testing_first/resolve/main/app/monitoring/langfuse_setup.py
- Command line
-
hf download hf://spaces/acpcchatbotadmin/acpcchatbot_testing_first/app/monitoring/langfuse_setup.py
-
curl -L -o langfuse_setup.py https://huggingface.co/spaces/acpcchatbotadmin/acpcchatbot_testing_first/resolve/main/app/monitoring/langfuse_setup.py
2.78 kB
| import logging | |
| import os | |
| from app.config import settings | |
| logger = logging.getLogger(__name__) | |
| _langfuse_handler = None | |
| def _configure_langfuse_env(): | |
| """Configure Langfuse environment variables from settings.""" | |
| if settings.langfuse_public_key: | |
| os.environ["LANGFUSE_PUBLIC_KEY"] = settings.langfuse_public_key | |
| if settings.langfuse_secret_key: | |
| os.environ["LANGFUSE_SECRET_KEY"] = settings.langfuse_secret_key | |
| if settings.langfuse_host: | |
| os.environ["LANGFUSE_BASE_URL"] = settings.langfuse_host | |
| def get_langfuse_callback_handler(): | |
| """ | |
| Get singleton Langfuse CallbackHandler for LangChain. | |
| Uses environment-based configuration per official Langfuse docs: | |
| https://langfuse.com/guides/cookbook/integration_langchain | |
| The handler is automatically initialized via environment variables, | |
| so no explicit initialization is needed. | |
| Returns None if Langfuse is not configured or if the integration | |
| is not available. | |
| """ | |
| global _langfuse_handler | |
| if _langfuse_handler is not None: | |
| return _langfuse_handler | |
| if not settings.langfuse_public_key: | |
| logger.debug("Langfuse not configured (no public key) — tracing disabled") | |
| return None | |
| try: | |
| # Configure environment variables | |
| _configure_langfuse_env() | |
| # Try modern import path first (langfuse 3.x+) | |
| try: | |
| from langfuse.langchain import CallbackHandler | |
| except ImportError: | |
| # Try alternative path for different versions | |
| try: | |
| from langfuse.callback.langchain import CallbackHandler | |
| except ImportError: | |
| # Try direct import with environment-based init | |
| from langfuse import CallbackHandler # type: ignore | |
| _langfuse_handler = CallbackHandler() | |
| logger.info("Langfuse CallbackHandler initialized successfully via environment") | |
| return _langfuse_handler | |
| except ImportError as e: | |
| logger.debug( | |
| "Langfuse CallbackHandler not available in this environment: %s — " | |
| "tracing will use LangRecorder (local) instead", | |
| e, | |
| ) | |
| _langfuse_handler = None | |
| return None | |
| except Exception as e: | |
| logger.warning( | |
| "Failed to initialize Langfuse CallbackHandler: %s — tracing disabled", e | |
| ) | |
| _langfuse_handler = None | |
| return None | |
| def flush_langfuse() -> None: | |
| """Flush pending Langfuse events (call on shutdown).""" | |
| # The CallbackHandler handles flushing automatically | |
| # This is a no-op but kept for API compatibility | |
| logger.debug("Langfuse shutdown hook called") | |