-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlangfuse.py
More file actions
165 lines (132 loc) · 4.33 KB
/
Copy pathlangfuse.py
File metadata and controls
165 lines (132 loc) · 4.33 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from __future__ import annotations
import logging
import os
from contextlib import contextmanager
from typing import Any, Iterator
try: # pragma: no cover - exercised in container runtime
from langfuse import Langfuse as _Langfuse
from langfuse import get_client as _get_client
from langfuse import propagate_attributes as _propagate_attributes
from langfuse.langchain import CallbackHandler as _CallbackHandler
except Exception: # pragma: no cover - local env may not have langfuse installed
_Langfuse = None
_get_client = None
_propagate_attributes = None
_CallbackHandler = None
logger = logging.getLogger(__name__)
_client_configured = False
def _normalize_langfuse_host() -> None:
host = (os.getenv("LANGFUSE_HOST") or "").strip()
if host and not os.getenv("LANGFUSE_BASE_URL"):
os.environ["LANGFUSE_BASE_URL"] = host
def langfuse_enabled() -> bool:
_normalize_langfuse_host()
return bool(
_Langfuse
and _get_client
and _CallbackHandler
and os.getenv("LANGFUSE_PUBLIC_KEY")
and os.getenv("LANGFUSE_SECRET_KEY")
and os.getenv("LANGFUSE_BASE_URL")
)
def _langfuse_timeout() -> int:
return max(5, int(os.getenv("LANGFUSE_TIMEOUT", "15")))
def _langfuse_flush_at() -> int:
return max(1, int(os.getenv("LANGFUSE_FLUSH_AT", "64")))
def _langfuse_flush_interval() -> float:
return max(1.0, float(os.getenv("LANGFUSE_FLUSH_INTERVAL", "2")))
def configure_langfuse() -> None:
global _client_configured
if _client_configured or not langfuse_enabled() or _Langfuse is None:
return
_Langfuse(
public_key=os.getenv("LANGFUSE_PUBLIC_KEY"),
secret_key=os.getenv("LANGFUSE_SECRET_KEY"),
base_url=os.getenv("LANGFUSE_BASE_URL"),
timeout=_langfuse_timeout(),
flush_at=_langfuse_flush_at(),
flush_interval=_langfuse_flush_interval(),
environment=os.getenv("LANGFUSE_TRACING_ENVIRONMENT"),
debug=os.getenv("LANGFUSE_DEBUG", "").lower() == "true",
)
_client_configured = True
logger.info(
"Langfuse configured base_url=%s timeout=%ss flush_at=%s flush_interval=%ss",
os.getenv("LANGFUSE_BASE_URL"),
_langfuse_timeout(),
_langfuse_flush_at(),
_langfuse_flush_interval(),
)
def auth_check_langfuse() -> bool:
if not langfuse_enabled():
return False
configure_langfuse()
client = _get_client()
try:
return bool(client.auth_check())
except Exception:
logger.exception("Langfuse auth_check failed")
return False
def get_langfuse_client():
if not langfuse_enabled():
return None
configure_langfuse()
return _get_client()
def get_langfuse_handler():
if not langfuse_enabled():
return None
configure_langfuse()
return _CallbackHandler()
@contextmanager
def propagate_langfuse_attributes(
*,
session_id: str | None = None,
user_id: str | None = None,
tags: list[str] | None = None,
metadata: dict[str, Any] | None = None,
trace_name: str | None = None,
as_baggage: bool = False,
) -> Iterator[None]:
if not langfuse_enabled() or _propagate_attributes is None:
yield
return
kwargs: dict[str, Any] = {}
if session_id:
kwargs["session_id"] = session_id
if user_id:
kwargs["user_id"] = user_id
if tags:
kwargs["tags"] = tags
if metadata:
kwargs["metadata"] = metadata
if trace_name:
kwargs["trace_name"] = trace_name
if as_baggage:
kwargs["as_baggage"] = True
if not kwargs:
yield
return
with _propagate_attributes(**kwargs):
yield
@contextmanager
def start_langfuse_observation(
*,
name: str,
as_type: str = "span",
input: Any | None = None,
metadata: dict[str, Any] | None = None,
trace_context: dict[str, str] | None = None,
) -> Iterator[Any | None]:
client = get_langfuse_client()
if client is None:
yield None
return
kwargs: dict[str, Any] = {"as_type": as_type, "name": name}
if input is not None:
kwargs["input"] = input
if metadata:
kwargs["metadata"] = metadata
if trace_context:
kwargs["trace_context"] = trace_context
with client.start_as_current_observation(**kwargs) as observation:
yield observation