File size: 1,041 Bytes
b249147 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | 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,
)
|