-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtracer.py
More file actions
401 lines (337 loc) · 13.3 KB
/
tracer.py
File metadata and controls
401 lines (337 loc) · 13.3 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
"""Module with the logic to create and manage traces and steps."""
import time
import asyncio
import inspect
import logging
import contextvars
from typing import Any, Dict, List, Tuple, Optional, Awaitable, Generator
from functools import wraps
from contextlib import contextmanager
from . import enums, steps, traces
from .. import utils
from ..._client import Openlayer
from ..._base_client import DefaultHttpxClient
from ...types.inference_pipelines.data_stream_params import ConfigLlmData
logger = logging.getLogger(__name__)
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").lower() in TRUE_LIST
_client = None
if _publish:
if _verify_ssl:
_client = Openlayer()
else:
_client = Openlayer(
http_client=DefaultHttpxClient(
verify=False,
),
)
_current_step = contextvars.ContextVar("current_step")
_current_trace = contextvars.ContextVar("current_trace")
_rag_context = contextvars.ContextVar("rag_context")
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: steps.Step = steps.step_factory(
step_type=step_type, name=name, inputs=inputs, output=output, metadata=metadata
)
new_step.start_time = time.time()
parent_step: Optional[steps.Step] = get_current_step()
is_root_step: bool = parent_step is None
if parent_step is None:
logger.debug("Starting a new trace...")
current_trace = traces.Trace()
_current_trace.set(current_trace) # Set the current trace in context
_rag_context.set(None) # Reset the context
current_trace.add_step(new_step)
else:
logger.debug("Adding step %s to parent step %s", name, parent_step.name)
current_trace = get_current_trace()
parent_step.add_nested_step(new_step)
token = _current_step.set(new_step)
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)
if is_root_step:
logger.debug("Ending the trace...")
trace_data, input_variable_names = post_process_trace(current_trace)
config = dict(
ConfigLlmData(
output_column_name="output",
input_variable_names=input_variable_names,
ground_truth_column_name="groundTruth",
latency_column_name="latency",
cost_column_name="cost",
timestamp_column_name="inferenceTimestamp",
inference_id_column_name="inferenceId",
num_of_token_column_name="tokens",
)
)
if "context" in trace_data:
config.update({"context_column_name": "context"})
if isinstance(new_step, steps.ChatCompletionStep):
config.update(
{
"prompt": new_step.inputs.get("prompt"),
}
)
if _publish:
try:
_client.inference_pipelines.data.stream(
inference_pipeline_id=inference_pipeline_id
or utils.get_env_variable("OPENLAYER_INFERENCE_PIPELINE_ID"),
rows=[trace_data],
config=config,
)
except Exception as err: # pylint: disable=broad-except
logger.error("Could not stream data to Openlayer %s", err)
else:
logger.debug("Ending step %s", name)
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)
# ----------------------------- Tracing decorator ---------------------------- #
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)
# pylint: disable=broad-except
except Exception as exc:
step.log(metadata={"Exceptions": str(exc)})
exception = exc
end_time = time.time()
latency = (end_time - step.start_time) * 1000 # in ms
bound = func_signature.bind(*func_args, **func_kwargs)
bound.apply_defaults()
inputs = dict(bound.arguments)
inputs.pop("self", None)
inputs.pop("cls", None)
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,
)
step.log(
inputs=inputs,
output=output,
end_time=end_time,
latency=latency,
)
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 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_async()
>>> async def main(user_query: str) -> str:
>>> context = retrieve_context(user_query)
>>> answer = generate_answer(user_query, context)
>>> return answer
>>>
>>> @tracer.trace_async()
>>> def retrieve_context(user_query: str) -> str:
>>> return "Some context"
>>>
>>> @tracer.trace_async()
>>> 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.:
>>> tracer.run_async_func(main("What is the meaning of life?"))
"""
def decorator(func):
func_signature = inspect.signature(func)
@wraps(func)
async 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 = await func(*func_args, **func_kwargs)
# pylint: disable=broad-except
except Exception as exc:
step.log(metadata={"Exceptions": str(exc)})
exception = exc
end_time = time.time()
latency = (end_time - step.start_time) * 1000 # in ms
bound = func_signature.bind(*func_args, **func_kwargs)
bound.apply_defaults()
inputs = dict(bound.arguments)
inputs.pop("self", None)
inputs.pop("cls", None)
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,
)
step.log(
inputs=inputs,
output=output,
end_time=end_time,
latency=latency,
)
if exception is not None:
raise exception
return output
return wrapper
return decorator
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 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
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.")
# --------------------- Helper post-processing functions --------------------- #
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,
"groundTruth": root_step.ground_truth,
"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 input_variables:
trace_data.update(input_variables)
context = get_rag_context()
if context:
trace_data["context"] = context
return trace_data, input_variable_names