Download langfuse_handler.py from andrei-sferdian/Agents_Course_Final_Assignment: direct link, hf CLI and curl.
- Browser
- Download file 1.04 kB
-
https://huggingface.co/spaces/andrei-sferdian/Agents_Course_Final_Assignment/resolve/main/langfuse_handler.py
- Command line
-
hf download hf://spaces/andrei-sferdian/Agents_Course_Final_Assignment/langfuse_handler.py
-
curl -L -o langfuse_handler.py https://huggingface.co/spaces/andrei-sferdian/Agents_Course_Final_Assignment/resolve/main/langfuse_handler.py
1.04 kB
| import os | |
| from langfuse.callback import CallbackHandler | |
| def create_langfuse_handler(*args, **kwargs) -> CallbackHandler: | |
| """ | |
| Create a Langfuse handler for tracking and logging. | |
| """ | |
| public_key = kwargs.get("public_key") or ( | |
| args[0] if len(args) > 0 else os.getenv("LANGFUSE_PUBLIC_KEY") | |
| ) | |
| secret_key = kwargs.get("secret_key") or ( | |
| args[1] if len(args) > 1 else os.getenv("LANGFUSE_SECRET_KEY") | |
| ) | |
| host = kwargs.get("host") or ( | |
| args[2] | |
| if len(args) > 2 | |
| else os.getenv("LANGFUSE_HOST", "https://api.langfuse.com") | |
| ) | |
| if not public_key: | |
| raise ValueError( | |
| "Please provide a public key or set the environment variable 'LANGFUSE_PUBLIC_KEY'." | |
| ) | |
| if not secret_key: | |
| raise ValueError( | |
| "Please provide a secret key or set the environment variable 'LANGFUSE_SECRET_KEY'." | |
| ) | |
| return CallbackHandler( | |
| public_key=public_key, | |
| secret_key=secret_key, | |
| host=host, | |
| **kwargs, | |
| ) | |