File size: 10,087 Bytes
a1e2111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38bdec7
a1e2111
38bdec7
a1e2111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38bdec7
a1e2111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38bdec7
 
 
 
 
 
 
 
 
 
a1e2111
 
 
 
 
 
38bdec7
 
 
 
 
 
 
 
a1e2111
 
 
38bdec7
 
 
 
 
 
 
 
 
a1e2111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38bdec7
 
 
 
 
 
 
a1e2111
 
 
38bdec7
 
 
 
 
 
 
 
 
a1e2111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38bdec7
 
 
 
 
 
 
 
 
a1e2111
 
 
 
 
 
 
38bdec7
 
 
 
 
 
 
 
 
 
 
a1e2111
 
 
38bdec7
 
 
 
 
 
 
 
 
a1e2111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38bdec7
 
a1e2111
 
38bdec7
 
 
 
 
 
 
a1e2111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38bdec7
a1e2111
 
 
38bdec7
a1e2111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38bdec7
a1e2111
 
 
 
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
"""Langfuse tracking integration for GAIA Benchmark Agent.

This module provides decorators and context managers for tracking:
- Agent execution sessions
- LLM invocations
- Tool calls
- Question processing
"""

import os
import functools
import time
from typing import Any, Optional, Dict
from contextlib import contextmanager

# Langfuse will be imported conditionally
langfuse = None

try:
    from langfuse import Langfuse, observe
    LANGFUSE_AVAILABLE = True
except ImportError:
    LANGFUSE_AVAILABLE = False
    print("[INFO] Langfuse not installed. Tracking is disabled. Install with: pip install langfuse")


class LangfuseTracker:
    """Singleton class to manage Langfuse client and tracking state."""

    _instance = None
    _client = None
    _enabled = False

    def __new__(cls):
        if cls._instance is None:
            cls._instance = super().__new__(cls)
        return cls._instance

    def __init__(self):
        """Initialize Langfuse client if not already initialized."""
        if self._client is None and LANGFUSE_AVAILABLE:
            public_key = os.getenv("LANGFUSE_PUBLIC_KEY")
            secret_key = os.getenv("LANGFUSE_SECRET_KEY")
            host = os.getenv("LANGFUSE_HOST", "https://us.cloud.langfuse.com")

            if public_key and secret_key:
                self._client = Langfuse(
                    public_key=public_key,
                    secret_key=secret_key,
                    host=host
                )
                self._enabled = True
                print(f"[LANGFUSE] Tracking enabled (host: {host})")
            else:
                print("[LANGFUSE] Tracking disabled. Set LANGFUSE_PUBLIC_KEY and LANGFUSE_SECRET_KEY to enable.")

    @property
    def enabled(self) -> bool:
        """Check if Langfuse tracking is enabled."""
        return self._enabled and LANGFUSE_AVAILABLE

    @property
    def client(self):
        """Get Langfuse client instance."""
        return self._client if self.enabled else None


# Global tracker instance
tracker = LangfuseTracker()


def track_agent_execution(agent_type: str):
    """Decorator to track agent execution lifecycle.

    Args:
        agent_type: Type of agent (e.g., "LangGraph", "ReAct", "LlamaIndex")

    Usage:
        @track_agent_execution("LangGraph")
        def __call__(self, question: str, file_name: str = None) -> str:
            ...
    """
    def decorator(func):
        if not tracker.enabled:
            return func

        @observe(name=f"{agent_type}_Agent_Execution")
        @functools.wraps(func)
        def wrapper(self, question: str, file_name: str = None, *args, **kwargs):
            # Add metadata to current observation
            if tracker.client:
                tracker.client.update_current_span(
                    metadata={
                        "agent_type": agent_type,
                        "has_file": file_name is not None,
                        "file_name": file_name or "none",
                        "question_length": len(question)
                    },
                    input={"question": question[:500], "file_name": file_name}  # Limit question length
                )

            start_time = time.time()
            try:
                result = func(self, question, file_name, *args, **kwargs)

                # Update with output and success metrics
                if tracker.client:
                    tracker.client.update_current_span(
                        output={"answer": str(result)[:500]},  # Limit answer length
                        metadata={
                            "execution_time_seconds": time.time() - start_time,
                            "success": not result.startswith("Error:")
                        }
                    )
                return result
            except Exception as e:
                # Track errors
                if tracker.client:
                    tracker.client.update_current_span(
                        level="ERROR",
                        status_message=str(e),
                        metadata={
                            "execution_time_seconds": time.time() - start_time,
                            "error": str(e)
                        }
                    )
                raise

        return wrapper
    return decorator


def track_llm_call(model_name: str):
    """Decorator to track LLM invocations.

    Args:
        model_name: Name of the LLM model being called

    Usage:
        @track_llm_call("gemini-1.5-flash")
        def _assistant(self, state):
            response = self.llm_client_with_tools.invoke(state["messages"])
            ...
    """
    def decorator(func):
        if not tracker.enabled:
            return func

        @observe(as_type="generation", name=f"LLM_Call_{model_name}")
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            start_time = time.time()

            try:
                result = func(*args, **kwargs)

                # Update generation with model info
                if tracker.client:
                    tracker.client.update_current_generation(
                        model=model_name,
                        metadata={
                            "latency_seconds": time.time() - start_time,
                        }
                    )

                return result
            except Exception as e:
                if tracker.client:
                    tracker.client.update_current_generation(
                        level="ERROR",
                        status_message=str(e),
                        metadata={
                            "latency_seconds": time.time() - start_time,
                            "error": str(e)
                        }
                    )
                raise

        return wrapper
    return decorator


def track_tool_call(tool_name: str):
    """Decorator to track tool/function calls.

    Args:
        tool_name: Name of the tool being called

    Usage:
        @track_tool_call("websearch")
        def websearch(query: str, num_results: int = 5):
            ...
    """
    def decorator(func):
        if not tracker.enabled:
            return func

        @observe(name=f"Tool_{tool_name}")
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            # Capture input parameters
            if tracker.client:
                tracker.client.update_current_span(
                    input={
                        "tool": tool_name,
                        "args": args[:3] if args else [],  # Limit args
                        "kwargs": {k: str(v)[:100] for k, v in list(kwargs.items())[:5]}  # Limit kwargs
                    },
                    metadata={"tool_name": tool_name}
                )

            start_time = time.time()
            try:
                result = func(*args, **kwargs)

                # Track output
                result_str = str(result)
                if tracker.client:
                    tracker.client.update_current_span(
                        output={
                            "result_preview": result_str[:500],
                            "result_length": len(result_str)
                        },
                        metadata={
                            "execution_time_seconds": time.time() - start_time,
                            "success": True
                        }
                    )

                return result
            except Exception as e:
                if tracker.client:
                    tracker.client.update_current_span(
                        level="ERROR",
                        status_message=str(e),
                        metadata={
                            "execution_time_seconds": time.time() - start_time,
                            "error": str(e)
                        }
                    )
                raise

        return wrapper
    return decorator


@contextmanager
def track_session(session_name: str, metadata: Optional[Dict[str, Any]] = None):
    """Context manager to track a complete session (batch processing).

    Args:
        session_name: Name of the session (e.g., "Test_Run", "Full_Submission")
        metadata: Optional metadata dict

    Usage:
        with track_session("Test_Run", {"agent": "LangGraph", "questions": 20}):
            # Run agent on questions
            ...
    """
    if not tracker.enabled:
        yield
        return

    # Use start_as_current_span to create a root span/trace for the session
    with tracker.client.start_as_current_span(
        name=session_name,
        metadata=metadata or {}
    ) as span:
        try:
            yield span
        finally:
            # Flush to ensure data is sent
            if tracker.client:
                tracker.client.flush()


@contextmanager
def track_question_processing(task_id: str, question: str):
    """Context manager to track individual question processing.

    Args:
        task_id: Unique task identifier
        question: Question text

    Usage:
        with track_question_processing(task_id, question_text) as span:
            answer = agent(question_text)
            span.update(output={"answer": answer})
    """
    if not tracker.enabled:
        yield None
        return

    with tracker.client.start_as_current_span(
        name=f"Question_{task_id[:8]}",
        input={"task_id": task_id, "question": question[:300]},
        metadata={"task_id": task_id}
    ) as span:
        yield span


# Convenience function for manual span creation
def create_span(name: str, input_data: Optional[Dict] = None, metadata: Optional[Dict] = None):
    """Create a manual span for tracking custom operations.

    Args:
        name: Span name
        input_data: Input data dict
        metadata: Metadata dict

    Returns:
        Span context manager or None if tracking disabled
    """
    if not tracker.enabled:
        return None

    return tracker.client.start_span(
        name=name,
        input=input_data,
        metadata=metadata
    )