-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtracer.py
More file actions
650 lines (543 loc) · 22.1 KB
/
tracer.py
File metadata and controls
650 lines (543 loc) · 22.1 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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
"""Module with the logic to create and manage traces and steps."""
import asyncio
import contextvars
import inspect
import logging
import time
import traceback
from contextlib import contextmanager
from functools import wraps
from typing import Any, Awaitable, Dict, Generator, List, Optional, Tuple
from ..._base_client import DefaultHttpxClient
from ..._client import Openlayer
from ...types.inference_pipelines.data_stream_params import ConfigLlmData
from .. import utils
from . import enums, steps, traces
logger = logging.getLogger(__name__)
# ----------------------------- Module setup and globals ----------------------------- #
TRUE_LIST = ["true", "on", "1"]
_publish = utils.get_env_variable("OPENLAYER_DISABLE_PUBLISH") not in TRUE_LIST
_verify_ssl = (
utils.get_env_variable("OPENLAYER_VERIFY_SSL") or "true"
).lower() in TRUE_LIST
_client = None
def _get_client() -> Optional[Openlayer]:
"""Get or create the Openlayer client with lazy initialization."""
global _client
if not _publish:
return None
if _client is None:
# Lazy initialization - create client when first needed
if _verify_ssl:
_client = Openlayer()
else:
_client = Openlayer(
http_client=DefaultHttpxClient(
verify=False,
),
)
return _client
_current_step = contextvars.ContextVar("current_step")
_current_trace = contextvars.ContextVar("current_trace")
_rag_context = contextvars.ContextVar("rag_context")
# ----------------------------- Public API functions ----------------------------- #
def get_current_trace() -> Optional[traces.Trace]:
"""Returns the current trace."""
return _current_trace.get(None)
def get_current_step() -> Optional[steps.Step]:
"""Returns the current step."""
return _current_step.get(None)
def get_rag_context() -> Optional[Dict[str, Any]]:
"""Returns the current context."""
return _rag_context.get(None)
@contextmanager
def create_step(
name: str,
step_type: enums.StepType = enums.StepType.USER_CALL,
inputs: Optional[Any] = None,
output: Optional[Any] = None,
metadata: Optional[Dict[str, Any]] = None,
inference_pipeline_id: Optional[str] = None,
) -> Generator[steps.Step, None, None]:
"""Starts a trace and yields a Step object."""
new_step, is_root_step, token = _create_and_initialize_step(
step_name=name,
step_type=step_type,
inputs=inputs,
output=output,
metadata=metadata,
)
try:
yield new_step
finally:
if new_step.end_time is None:
new_step.end_time = time.time()
if new_step.latency is None:
latency = (new_step.end_time - new_step.start_time) * 1000 # in ms
new_step.latency = latency
_current_step.reset(token)
_handle_trace_completion(
is_root_step=is_root_step,
step_name=name,
inference_pipeline_id=inference_pipeline_id,
)
def add_chat_completion_step_to_trace(**kwargs) -> None:
"""Adds a chat completion step to the trace."""
with create_step(
step_type=enums.StepType.CHAT_COMPLETION,
name=kwargs.get("name", "Chat Completion"),
) as step:
step.log(**kwargs)
def trace(
*step_args,
inference_pipeline_id: Optional[str] = None,
context_kwarg: Optional[str] = None,
**step_kwargs,
):
"""Decorator to trace a function.
Examples
--------
To trace a function, simply decorate it with the ``@trace()`` decorator. By doing
so, the functions inputs, outputs, and metadata will be automatically logged to your
Openlayer project.
>>> import os
>>> from openlayer.tracing import tracer
>>>
>>> # Set the environment variables
>>> os.environ["OPENLAYER_API_KEY"] = "YOUR_OPENLAYER_API_KEY_HERE"
>>> os.environ["OPENLAYER_PROJECT_NAME"] = "YOUR_OPENLAYER_PROJECT_NAME_HERE"
>>>
>>> # Decorate all the functions you want to trace
>>> @tracer.trace()
>>> def main(user_query: str) -> str:
>>> context = retrieve_context(user_query)
>>> answer = generate_answer(user_query, context)
>>> return answer
>>>
>>> @tracer.trace()
>>> def retrieve_context(user_query: str) -> str:
>>> return "Some context"
>>>
>>> @tracer.trace()
>>> def generate_answer(user_query: str, context: str) -> str:
>>> return "Some answer"
>>>
>>> # Every time the main function is called, the data is automatically
>>> # streamed to your Openlayer project. E.g.:
>>> main("What is the meaning of life?")
"""
def decorator(func):
func_signature = inspect.signature(func)
@wraps(func)
def wrapper(*func_args, **func_kwargs):
if step_kwargs.get("name") is None:
step_kwargs["name"] = func.__name__
with create_step(
*step_args, inference_pipeline_id=inference_pipeline_id, **step_kwargs
) as step:
output = exception = None
try:
output = func(*func_args, **func_kwargs)
except Exception as exc:
_log_step_exception(step, exc)
exception = exc
# Extract inputs and finalize logging using optimized helper
_process_wrapper_inputs_and_outputs(
step=step,
func_signature=func_signature,
func_args=func_args,
func_kwargs=func_kwargs,
context_kwarg=context_kwarg,
output=output,
)
if exception is not None:
raise exception
return output
return wrapper
return decorator
def trace_async(
*step_args,
inference_pipeline_id: Optional[str] = None,
context_kwarg: Optional[str] = None,
**step_kwargs,
):
"""Decorator to trace async functions and async generators.
This decorator automatically detects whether the function is a regular async function
or an async generator and handles both cases appropriately.
Examples
--------
To trace a regular async function:
>>> @tracer.trace_async()
>>> async def main(user_query: str) -> str:
>>> context = retrieve_context(user_query)
>>> answer = generate_answer(user_query, context)
>>> return answer
To trace an async generator function:
>>> @tracer.trace_async()
>>> async def stream_response(query: str):
>>> async for chunk in openai_client.chat.completions.create(...):
>>> yield chunk.choices[0].delta.content
"""
def decorator(func):
func_signature = inspect.signature(func)
if step_kwargs.get("name") is None:
step_kwargs["name"] = func.__name__
step_name = step_kwargs["name"]
if asyncio.iscoroutinefunction(func) or inspect.isasyncgenfunction(func):
# Check if it's specifically an async generator function
if inspect.isasyncgenfunction(func):
# For async generators, use class-based approach to delay trace creation
# until actual iteration begins (not when generator object is created)
@wraps(func)
def async_generator_wrapper(*func_args, **func_kwargs):
class TracedAsyncGenerator:
def __init__(self):
self._original_gen = None
self._step = None
self._is_root_step = False
self._token = None
self._output_chunks = []
self._trace_initialized = False
def __aiter__(self):
return self
async def __anext__(self):
# Initialize tracing on first iteration only
if not self._trace_initialized:
self._original_gen = func(*func_args, **func_kwargs)
self._step, self._is_root_step, self._token = (
_create_and_initialize_step(
step_name=step_name,
step_type=enums.StepType.USER_CALL,
inputs=None,
output=None,
metadata=None,
)
)
self._inputs = _extract_function_inputs(
func_signature=func_signature,
func_args=func_args,
func_kwargs=func_kwargs,
context_kwarg=context_kwarg,
)
self._trace_initialized = True
try:
chunk = await self._original_gen.__anext__()
self._output_chunks.append(chunk)
return chunk
except StopAsyncIteration:
# Finalize trace when generator is exhausted
output = _join_output_chunks(self._output_chunks)
_finalize_async_generator_step(
step=self._step,
token=self._token,
is_root_step=self._is_root_step,
step_name=step_name,
inputs=self._inputs,
output=output,
inference_pipeline_id=inference_pipeline_id,
)
raise
except Exception as exc:
# Handle exceptions
if self._step:
_log_step_exception(self._step, exc)
output = _join_output_chunks(self._output_chunks)
_finalize_async_generator_step(
step=self._step,
token=self._token,
is_root_step=self._is_root_step,
step_name=step_name,
inputs=self._inputs,
output=output,
inference_pipeline_id=inference_pipeline_id,
)
raise
return TracedAsyncGenerator()
return async_generator_wrapper
else:
# Create wrapper for regular async functions
@wraps(func)
async def async_function_wrapper(*func_args, **func_kwargs):
with create_step(
*step_args,
inference_pipeline_id=inference_pipeline_id,
**step_kwargs,
) as step:
output = exception = None
try:
output = await func(*func_args, **func_kwargs)
except Exception as exc:
_log_step_exception(step, exc)
exception = exc
raise
# Extract inputs and finalize logging
_process_wrapper_inputs_and_outputs(
step=step,
func_signature=func_signature,
func_args=func_args,
func_kwargs=func_kwargs,
context_kwarg=context_kwarg,
output=output,
)
return output
return async_function_wrapper
else:
# For sync functions, use the existing logic with optimizations
@wraps(func)
def sync_wrapper(*func_args, **func_kwargs):
with create_step(
*step_args,
inference_pipeline_id=inference_pipeline_id,
**step_kwargs,
) as step:
output = exception = None
try:
output = func(*func_args, **func_kwargs)
except Exception as exc:
_log_step_exception(step, exc)
exception = exc
# Extract inputs and finalize logging
_process_wrapper_inputs_and_outputs(
step=step,
func_signature=func_signature,
func_args=func_args,
func_kwargs=func_kwargs,
context_kwarg=context_kwarg,
output=output,
)
if exception is not None:
raise exception
return output
return sync_wrapper
return decorator
def log_context(context: List[str]) -> None:
"""Logs context information to the current step of the trace.
The `context` parameter should be a list of strings representing the
context chunks retrieved by the context retriever."""
current_step = get_current_step()
if current_step:
_rag_context.set(context)
current_step.log(metadata={"context": context})
else:
logger.warning("No current step found to log context.")
def run_async_func(coroutine: Awaitable[Any]) -> Any:
"""Runs an async function while preserving the context. This is needed
for tracing async functions.
"""
context, result = asyncio.run(_invoke_with_context(coroutine))
for key, value in context.items():
key.set(value)
return result
# ----------------------------- Helper functions for create_step ----------------------------- #
def _create_and_initialize_step(
step_name: str,
step_type: enums.StepType = enums.StepType.USER_CALL,
inputs: Optional[Any] = None,
output: Optional[Any] = None,
metadata: Optional[Dict[str, Any]] = None,
) -> Tuple[steps.Step, bool, Any]:
"""Create a new step and initialize trace/parent relationships.
Returns:
Tuple of (step, is_root_step, token)
"""
new_step = steps.step_factory(
step_type=step_type,
name=step_name,
inputs=inputs,
output=output,
metadata=metadata,
)
new_step.start_time = time.time()
parent_step = get_current_step()
is_root_step = parent_step is None
if parent_step is None:
logger.debug("Starting a new trace...")
current_trace = traces.Trace()
_current_trace.set(current_trace)
_rag_context.set(None)
current_trace.add_step(new_step)
else:
logger.debug("Adding step %s to parent step %s", step_name, parent_step.name)
current_trace = get_current_trace()
parent_step.add_nested_step(new_step)
token = _current_step.set(new_step)
return new_step, is_root_step, token
def _handle_trace_completion(
is_root_step: bool, step_name: str, inference_pipeline_id: Optional[str] = None
) -> None:
"""Handle trace completion and data streaming."""
if is_root_step:
logger.debug("Ending the trace...")
current_trace = get_current_trace()
trace_data, input_variable_names = post_process_trace(current_trace)
config = dict(
ConfigLlmData(
output_column_name="output",
input_variable_names=input_variable_names,
latency_column_name="latency",
cost_column_name="cost",
timestamp_column_name="inferenceTimestamp",
inference_id_column_name="inferenceId",
num_of_token_column_name="tokens",
)
)
if "groundTruth" in trace_data:
config.update({"ground_truth_column_name": "groundTruth"})
if "context" in trace_data:
config.update({"context_column_name": "context"})
if isinstance(get_current_step(), steps.ChatCompletionStep):
config.update(
{
"prompt": get_current_step().inputs.get("prompt"),
}
)
if _publish:
try:
inference_pipeline_id = inference_pipeline_id or utils.get_env_variable(
"OPENLAYER_INFERENCE_PIPELINE_ID"
)
client = _get_client()
if client:
response = client.inference_pipelines.data.stream(
inference_pipeline_id=inference_pipeline_id,
rows=[trace_data],
config=config,
)
print(
"Successfully streamed data to Openlayer. Response:",
response.to_json(),
)
except Exception as err: # pylint: disable=broad-except
logger.error(traceback.format_exc())
logger.error(
"Could not stream data to Openlayer (pipeline_id: %s, base_url: %s)"
" Error: %s",
inference_pipeline_id,
client.base_url,
err,
)
else:
logger.debug("Ending step %s", step_name)
# ----------------------------- Helper functions for trace decorators ----------------------------- #
def _log_step_exception(step: steps.Step, exception: Exception) -> None:
"""Log exception metadata to a step."""
step.log(metadata={"Exceptions": str(exception)})
def _process_wrapper_inputs_and_outputs(
step: steps.Step,
func_signature: inspect.Signature,
func_args: tuple,
func_kwargs: dict,
context_kwarg: Optional[str],
output: Any,
) -> None:
"""Extract function inputs and finalize step logging - common pattern across wrappers."""
inputs = _extract_function_inputs(
func_signature=func_signature,
func_args=func_args,
func_kwargs=func_kwargs,
context_kwarg=context_kwarg,
)
_finalize_step_logging(
step=step, inputs=inputs, output=output, start_time=step.start_time
)
def _extract_function_inputs(
func_signature: inspect.Signature,
func_args: tuple,
func_kwargs: dict,
context_kwarg: Optional[str] = None,
) -> dict:
"""Extract and clean function inputs for logging."""
bound = func_signature.bind(*func_args, **func_kwargs)
bound.apply_defaults()
inputs = dict(bound.arguments)
inputs.pop("self", None)
inputs.pop("cls", None)
# Handle context kwarg if specified
if context_kwarg:
if context_kwarg in inputs:
log_context(inputs.get(context_kwarg))
else:
logger.warning(
"Context kwarg `%s` not found in inputs of the current function.",
context_kwarg,
)
return inputs
def _finalize_step_logging(
step: steps.Step,
inputs: dict,
output: Any,
start_time: float,
) -> None:
"""Finalize step timing and logging."""
if step.end_time is None:
step.end_time = time.time()
if step.latency is None:
step.latency = (step.end_time - start_time) * 1000 # in ms
step.log(
inputs=inputs,
output=output,
end_time=step.end_time,
latency=step.latency,
)
# ----------------------------- Async generator specific functions ----------------------------- #
def _finalize_async_generator_step(
step: steps.Step,
token: Any,
is_root_step: bool,
step_name: str,
inputs: dict,
output: Any,
inference_pipeline_id: Optional[str] = None,
) -> None:
"""Finalize async generator step - called when generator is consumed."""
_current_step.reset(token)
_finalize_step_logging(
step=step, inputs=inputs, output=output, start_time=step.start_time
)
_handle_trace_completion(
is_root_step=is_root_step,
step_name=step_name,
inference_pipeline_id=inference_pipeline_id,
)
def _join_output_chunks(output_chunks: List[Any]) -> str:
"""Join output chunks into a single string, filtering out None values."""
return "".join(str(chunk) for chunk in output_chunks if chunk is not None)
# ----------------------------- Utility functions ----------------------------- #
async def _invoke_with_context(
coroutine: Awaitable[Any],
) -> Tuple[contextvars.Context, Any]:
"""Runs a coroutine and preserves the context variables set within it."""
result = await coroutine
context = contextvars.copy_context()
return context, result
def post_process_trace(
trace_obj: traces.Trace,
) -> Tuple[Dict[str, Any], List[str]]:
"""Post processing of the trace data before uploading to Openlayer.
This is done to ensure backward compatibility with data on Openlayer.
"""
root_step = trace_obj.steps[0]
input_variables = root_step.inputs
if input_variables:
input_variable_names = list(input_variables.keys())
else:
input_variable_names = []
processed_steps = trace_obj.to_dict()
trace_data = {
"inferenceTimestamp": root_step.start_time,
"inferenceId": str(root_step.id),
"output": root_step.output,
"latency": root_step.latency,
"cost": processed_steps[0].get("cost", 0),
"tokens": processed_steps[0].get("tokens", 0),
"steps": processed_steps,
**root_step.metadata,
}
if root_step.ground_truth:
trace_data["groundTruth"] = root_step.ground_truth
if input_variables:
trace_data.update(input_variables)
context = get_rag_context()
if context:
trace_data["context"] = context
return trace_data, input_variable_names