openlit 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,875 @@
1
+ # pylint: disable=duplicate-code, broad-exception-caught, too-many-statements, unused-argument, too-many-branches
2
+ """
3
+ Module for monitoring OpenAI API calls.
4
+ """
5
+
6
+ import logging
7
+ from opentelemetry.trace import SpanKind, Status, StatusCode
8
+ from opentelemetry.sdk.resources import TELEMETRY_SDK_NAME
9
+ from openlit.__helpers import get_chat_model_cost, get_embed_model_cost, get_audio_model_cost
10
+ from openlit.__helpers import get_image_model_cost, openai_tokens, handle_exception
11
+ from openlit.semcov import SemanticConvetion
12
+
13
+ # Initialize logger for logging potential issues and operations
14
+ logger = logging.getLogger(__name__)
15
+
16
+ def async_chat_completions(gen_ai_endpoint, version, environment, application_name,
17
+ tracer, pricing_info, trace_content, metrics, disable_metrics):
18
+ """
19
+ Generates a telemetry wrapper for chat completions to collect metrics.
20
+
21
+ Args:
22
+ gen_ai_endpoint: Endpoint identifier for logging and tracing.
23
+ version: Version of the monitoring package.
24
+ environment: Deployment environment (e.g., production, staging).
25
+ application_name: Name of the application using the OpenAI API.
26
+ tracer: OpenTelemetry tracer for creating spans.
27
+ pricing_info: Information used for calculating the cost of OpenAI usage.
28
+ trace_content: Flag indicating whether to trace the actual content.
29
+
30
+ Returns:
31
+ A function that wraps the chat completions method to add telemetry.
32
+ """
33
+
34
+ async def wrapper(wrapped, instance, args, kwargs):
35
+ """
36
+ Wraps the 'chat.completions' API call to add telemetry.
37
+
38
+ This collects metrics such as execution time, cost, and token usage, and handles errors
39
+ gracefully, adding details to the trace for observability.
40
+
41
+ Args:
42
+ wrapped: The original 'chat.completions' method to be wrapped.
43
+ instance: The instance of the class where the original method is defined.
44
+ args: Positional arguments for the 'chat.completions' method.
45
+ kwargs: Keyword arguments for the 'chat.completions' method.
46
+
47
+ Returns:
48
+ The response from the original 'chat.completions' method.
49
+ """
50
+
51
+ # Check if streaming is enabled for the API call
52
+ streaming = kwargs.get("stream", False)
53
+
54
+ # pylint: disable=no-else-return
55
+ if streaming:
56
+ # Special handling for streaming response to accommodate the nature of data flow
57
+ async def stream_generator():
58
+ with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
59
+ # Placeholder for aggregating streaming response
60
+ llmresponse = ""
61
+
62
+ # Loop through streaming events capturing relevant details
63
+ async for chunk in await wrapped(*args, **kwargs):
64
+ # Collect message IDs and aggregated response from events
65
+ if len(chunk.choices) > 0:
66
+ # pylint: disable=line-too-long
67
+ if hasattr(chunk.choices[0], "delta") and hasattr(chunk.choices[0].delta, "content"):
68
+ content = chunk.choices[0].delta.content
69
+ if content:
70
+ llmresponse += content
71
+ yield chunk
72
+ response_id = chunk.id
73
+
74
+ # Handling exception ensure observability without disrupting operation
75
+ try:
76
+ # Format 'messages' into a single string
77
+ message_prompt = kwargs.get("messages", "")
78
+ formatted_messages = []
79
+ for message in message_prompt:
80
+ role = message["role"]
81
+ content = message["content"]
82
+
83
+ if isinstance(content, list):
84
+ content_str = ", ".join(
85
+ # pylint: disable=line-too-long
86
+ f'{item["type"]}: {item["text"] if "text" in item else item["image_url"]}'
87
+ if "type" in item else f'text: {item["text"]}'
88
+ for item in content
89
+ )
90
+ formatted_messages.append(f"{role}: {content_str}")
91
+ else:
92
+ formatted_messages.append(f"{role}: {content}")
93
+ prompt = "\n".join(formatted_messages)
94
+
95
+ # Calculate tokens using input prompt and aggregated response
96
+ prompt_tokens = openai_tokens(prompt,
97
+ kwargs.get("model", "gpt-3.5-turbo"))
98
+ completion_tokens = openai_tokens(llmresponse,
99
+ kwargs.get("model", "gpt-3.5-turbo"))
100
+
101
+ # Calculate cost of the operation
102
+ cost = get_chat_model_cost(kwargs.get("model", "gpt-3.5-turbo"),
103
+ pricing_info, prompt_tokens,
104
+ completion_tokens)
105
+
106
+ # Set Span attributes
107
+ span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
108
+ span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
109
+ SemanticConvetion.GEN_AI_SYSTEM_OPENAI)
110
+ span.set_attribute(SemanticConvetion.GEN_AI_TYPE,
111
+ SemanticConvetion.GEN_AI_TYPE_CHAT)
112
+ span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,
113
+ gen_ai_endpoint)
114
+ span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_ID,
115
+ response_id)
116
+ span.set_attribute(SemanticConvetion.GEN_AI_ENVIRONMENT,
117
+ environment)
118
+ span.set_attribute(SemanticConvetion.GEN_AI_APPLICATION_NAME,
119
+ application_name)
120
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL,
121
+ kwargs.get("model", "gpt-3.5-turbo"))
122
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_USER,
123
+ kwargs.get("user", ""))
124
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TOP_P,
125
+ kwargs.get("top_p", 1))
126
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MAX_TOKENS,
127
+ kwargs.get("max_tokens", ""))
128
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TEMPERATURE,
129
+ kwargs.get("temperature", 1))
130
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_PRESENCE_PENALTY,
131
+ kwargs.get("presence_penalty", 0))
132
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_FREQUENCY_PENALTY,
133
+ kwargs.get("frequency_penalty", 0))
134
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_SEED,
135
+ kwargs.get("seed", ""))
136
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_IS_STREAM,
137
+ True)
138
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_PROMPT_TOKENS,
139
+ prompt_tokens)
140
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COMPLETION_TOKENS,
141
+ completion_tokens)
142
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
143
+ prompt_tokens + completion_tokens)
144
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
145
+ cost)
146
+ if trace_content:
147
+ span.set_attribute(SemanticConvetion.GEN_AI_CONTENT_PROMPT,
148
+ prompt)
149
+ span.set_attribute(SemanticConvetion.GEN_AI_CONTENT_COMPLETION,
150
+ llmresponse)
151
+
152
+ span.set_status(Status(StatusCode.OK))
153
+
154
+ if disable_metrics is False:
155
+ attributes = {
156
+ TELEMETRY_SDK_NAME:
157
+ "openlit",
158
+ SemanticConvetion.GEN_AI_APPLICATION_NAME:
159
+ application_name,
160
+ SemanticConvetion.GEN_AI_SYSTEM:
161
+ SemanticConvetion.GEN_AI_SYSTEM_OPENAI,
162
+ SemanticConvetion.GEN_AI_ENVIRONMENT:
163
+ environment,
164
+ SemanticConvetion.GEN_AI_TYPE:
165
+ SemanticConvetion.GEN_AI_TYPE_CHAT,
166
+ SemanticConvetion.GEN_AI_REQUEST_MODEL:
167
+ kwargs.get("model", "gpt-3.5-turbo")
168
+ }
169
+
170
+ metrics["genai_requests"].add(1, attributes)
171
+ metrics["genai_total_tokens"].add(
172
+ prompt_tokens + completion_tokens, attributes
173
+ )
174
+ metrics["genai_completion_tokens"].add(completion_tokens, attributes)
175
+ metrics["genai_prompt_tokens"].add(prompt_tokens, attributes)
176
+ metrics["genai_cost"].record(cost, attributes)
177
+
178
+ except Exception as e:
179
+ handle_exception(span, e)
180
+ logger.error("Error in trace creation: %s", e)
181
+
182
+ return stream_generator()
183
+
184
+ # Handling for non-streaming responses
185
+ else:
186
+ # pylint: disable=line-too-long
187
+ with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
188
+ response = await wrapped(*args, **kwargs)
189
+
190
+ try:
191
+ # Format 'messages' into a single string
192
+ message_prompt = kwargs.get("messages", "")
193
+ formatted_messages = []
194
+ for message in message_prompt:
195
+ role = message["role"]
196
+ content = message["content"]
197
+
198
+ if isinstance(content, list):
199
+ content_str = ", ".join(
200
+ # pylint: disable=line-too-long
201
+ f'{item["type"]}: {item["text"] if "text" in item else item["image_url"]}'
202
+ if "type" in item else f'text: {item["text"]}'
203
+ for item in content
204
+ )
205
+ formatted_messages.append(f"{role}: {content_str}")
206
+ else:
207
+ formatted_messages.append(f"{role}: {content}")
208
+ prompt = "\n".join(formatted_messages)
209
+
210
+ # Set base span attribues
211
+ span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
212
+ span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
213
+ SemanticConvetion.GEN_AI_SYSTEM_OPENAI)
214
+ span.set_attribute(SemanticConvetion.GEN_AI_TYPE,
215
+ SemanticConvetion.GEN_AI_TYPE_CHAT)
216
+ span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,
217
+ gen_ai_endpoint)
218
+ span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_ID,
219
+ response.id)
220
+ span.set_attribute(SemanticConvetion.GEN_AI_ENVIRONMENT,
221
+ environment)
222
+ span.set_attribute(SemanticConvetion.GEN_AI_APPLICATION_NAME,
223
+ application_name)
224
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL,
225
+ kwargs.get("model", "gpt-3.5-turbo"))
226
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TOP_P,
227
+ kwargs.get("top_p", 1))
228
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MAX_TOKENS,
229
+ kwargs.get("max_tokens", ""))
230
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_USER,
231
+ kwargs.get("user", ""))
232
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TEMPERATURE,
233
+ kwargs.get("temperature", 1))
234
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_PRESENCE_PENALTY,
235
+ kwargs.get("presence_penalty", 0))
236
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_FREQUENCY_PENALTY,
237
+ kwargs.get("frequency_penalty", 0))
238
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_SEED,
239
+ kwargs.get("seed", ""))
240
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_IS_STREAM,
241
+ False)
242
+ if trace_content:
243
+ span.set_attribute(SemanticConvetion.GEN_AI_CONTENT_PROMPT,
244
+ prompt)
245
+
246
+ span.set_status(Status(StatusCode.OK))
247
+
248
+ # Set span attributes when tools is not passed to the function call
249
+ if "tools" not in kwargs:
250
+ # Calculate cost of the operation
251
+ cost = get_chat_model_cost(kwargs.get("model", "gpt-3.5-turbo"),
252
+ pricing_info, response.usage.prompt_tokens,
253
+ response.usage.completion_tokens)
254
+
255
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_PROMPT_TOKENS,
256
+ response.usage.prompt_tokens)
257
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COMPLETION_TOKENS,
258
+ response.usage.completion_tokens)
259
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
260
+ response.usage.total_tokens)
261
+ span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_FINISH_REASON,
262
+ response.choices[0].finish_reason)
263
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
264
+ cost)
265
+
266
+ # Set span attributes for when n = 1 (default)
267
+ if "n" not in kwargs or kwargs["n"] == 1:
268
+ if trace_content:
269
+ span.set_attribute(SemanticConvetion.GEN_AI_CONTENT_COMPLETION,
270
+ response.choices[0].message.content)
271
+
272
+ # Set span attributes for when n > 0
273
+ else:
274
+ i = 0
275
+ while i < kwargs["n"] and trace_content is True:
276
+ attribute_name = f"gen_ai.content.completion.{i}"
277
+ span.set_attribute(attribute_name,
278
+ response.choices[i].message.content)
279
+ i += 1
280
+
281
+ # Return original response
282
+ return response
283
+
284
+ # Set span attributes when tools is passed to the function call
285
+ elif "tools" in kwargs:
286
+ # Calculate cost of the operation
287
+ cost = get_chat_model_cost(kwargs.get("model", "gpt-3.5-turbo"),
288
+ pricing_info, response.usage.prompt_tokens,
289
+ response.usage.completion_tokens)
290
+
291
+ span.set_attribute(SemanticConvetion.GEN_AI_CONTENT_COMPLETION,
292
+ "Function called with tools")
293
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_PROMPT_TOKENS,
294
+ response.usage.prompt_tokens)
295
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COMPLETION_TOKENS,
296
+ response.usage.completion_tokens)
297
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
298
+ response.usage.total_tokens)
299
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
300
+ cost)
301
+
302
+ span.set_status(Status(StatusCode.OK))
303
+
304
+ if disable_metrics is False:
305
+ attributes = {
306
+ TELEMETRY_SDK_NAME:
307
+ "openlit",
308
+ SemanticConvetion.GEN_AI_APPLICATION_NAME:
309
+ application_name,
310
+ SemanticConvetion.GEN_AI_SYSTEM:
311
+ SemanticConvetion.GEN_AI_SYSTEM_OPENAI,
312
+ SemanticConvetion.GEN_AI_ENVIRONMENT:
313
+ environment,
314
+ SemanticConvetion.GEN_AI_TYPE:
315
+ SemanticConvetion.GEN_AI_TYPE_CHAT,
316
+ SemanticConvetion.GEN_AI_REQUEST_MODEL:
317
+ kwargs.get("model", "gpt-3.5-turbo")
318
+ }
319
+
320
+ metrics["genai_requests"].add(1, attributes)
321
+ metrics["genai_total_tokens"].add(response.usage.total_tokens, attributes)
322
+ metrics["genai_completion_tokens"].add(response.usage.completion_tokens, attributes)
323
+ metrics["genai_prompt_tokens"].add(response.usage.prompt_tokens, attributes)
324
+ metrics["genai_cost"].record(cost, attributes)
325
+
326
+ # Return original response
327
+ return response
328
+
329
+ except Exception as e:
330
+ handle_exception(span, e)
331
+ logger.error("Error in trace creation: %s", e)
332
+
333
+ # Return original response
334
+ return response
335
+
336
+ return wrapper
337
+
338
+ def async_embedding(gen_ai_endpoint, version, environment, application_name,
339
+ tracer, pricing_info, trace_content, metrics, disable_metrics):
340
+ """
341
+ Generates a telemetry wrapper for embeddings to collect metrics.
342
+
343
+ Args:
344
+ gen_ai_endpoint: Endpoint identifier for logging and tracing.
345
+ version: Version of the monitoring package.
346
+ environment: Deployment environment (e.g., production, staging).
347
+ application_name: Name of the application using the OpenAI API.
348
+ tracer: OpenTelemetry tracer for creating spans.
349
+ pricing_info: Information used for calculating the cost of OpenAI usage.
350
+ trace_content: Flag indicating whether to trace the actual content.
351
+
352
+ Returns:
353
+ A function that wraps the embeddings method to add telemetry.
354
+ """
355
+
356
+ async def wrapper(wrapped, instance, args, kwargs):
357
+ """
358
+ Wraps the 'embeddings' API call to add telemetry.
359
+
360
+ This collects metrics such as execution time, cost, and token usage, and handles errors
361
+ gracefully, adding details to the trace for observability.
362
+
363
+ Args:
364
+ wrapped: The original 'embeddings' method to be wrapped.
365
+ instance: The instance of the class where the original method is defined.
366
+ args: Positional arguments for the 'embeddings' method.
367
+ kwargs: Keyword arguments for the 'embeddings' method.
368
+
369
+ Returns:
370
+ The response from the original 'embeddings' method.
371
+ """
372
+
373
+ with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
374
+ response = await wrapped(*args, **kwargs)
375
+
376
+ try:
377
+ # Calculate cost of the operation
378
+ cost = get_embed_model_cost(kwargs.get("model", "text-embedding-ada-002"),
379
+ pricing_info, response.usage.prompt_tokens)
380
+
381
+ # Set Span attributes
382
+ span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
383
+ span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
384
+ SemanticConvetion.GEN_AI_SYSTEM_OPENAI)
385
+ span.set_attribute(SemanticConvetion.GEN_AI_TYPE,
386
+ SemanticConvetion.GEN_AI_TYPE_EMBEDDING)
387
+ span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,
388
+ gen_ai_endpoint)
389
+ span.set_attribute(SemanticConvetion.GEN_AI_ENVIRONMENT,
390
+ environment)
391
+ span.set_attribute(SemanticConvetion.GEN_AI_APPLICATION_NAME,
392
+ application_name)
393
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL,
394
+ kwargs.get("model", "text-embedding-ada-002"))
395
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_EMBEDDING_FORMAT,
396
+ kwargs.get("encoding_format", "float"))
397
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_EMBEDDING_DIMENSION,
398
+ kwargs.get("dimensions", ""))
399
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_USER,
400
+ kwargs.get("user", ""))
401
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_PROMPT_TOKENS,
402
+ response.usage.prompt_tokens)
403
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
404
+ response.usage.total_tokens)
405
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
406
+ cost)
407
+ if trace_content:
408
+ span.set_attribute(SemanticConvetion.GEN_AI_CONTENT_PROMPT,
409
+ kwargs.get("input", ""))
410
+
411
+ span.set_status(Status(StatusCode.OK))
412
+
413
+ if disable_metrics is False:
414
+ attributes = {
415
+ TELEMETRY_SDK_NAME:
416
+ "openlit",
417
+ SemanticConvetion.GEN_AI_APPLICATION_NAME:
418
+ application_name,
419
+ SemanticConvetion.GEN_AI_SYSTEM:
420
+ SemanticConvetion.GEN_AI_SYSTEM_OPENAI,
421
+ SemanticConvetion.GEN_AI_ENVIRONMENT:
422
+ environment,
423
+ SemanticConvetion.GEN_AI_TYPE:
424
+ SemanticConvetion.GEN_AI_TYPE_EMBEDDING,
425
+ SemanticConvetion.GEN_AI_REQUEST_MODEL:
426
+ kwargs.get("model", "text-embedding-ada-002")
427
+ }
428
+
429
+ metrics["genai_requests"].add(1, attributes)
430
+ metrics["genai_total_tokens"].add(response.usage.total_tokens, attributes)
431
+ metrics["genai_prompt_tokens"].add(response.usage.prompt_tokens, attributes)
432
+ metrics["genai_cost"].record(cost, attributes)
433
+
434
+ # Return original response
435
+ return response
436
+
437
+ except Exception as e:
438
+ handle_exception(span, e)
439
+ logger.error("Error in trace creation: %s", e)
440
+
441
+ # Return original response
442
+ return response
443
+
444
+ return wrapper
445
+
446
+ def async_finetune(gen_ai_endpoint, version, environment, application_name,
447
+ tracer, pricing_info, trace_content, metrics, disable_metrics):
448
+ """
449
+ Generates a telemetry wrapper for fine-tuning jobs to collect metrics.
450
+
451
+ Args:
452
+ gen_ai_endpoint: Endpoint identifier for logging and tracing.
453
+ version: Version of the monitoring package.
454
+ environment: Deployment environment (e.g., production, staging).
455
+ application_name: Name of the application using the OpenAI API.
456
+ tracer: OpenTelemetry tracer for creating spans.
457
+ pricing_info: Information used for calculating the cost of OpenAI usage.
458
+ trace_content: Flag indicating whether to trace the actual content.
459
+
460
+ Returns:
461
+ A function that wraps the fine tuning creation method to add telemetry.
462
+ """
463
+
464
+ async def wrapper(wrapped, instance, args, kwargs):
465
+ """
466
+ Wraps the 'fine_tuning.jobs.create' API call to add telemetry.
467
+
468
+ This collects metrics such as execution time, usage stats, and handles errors
469
+ gracefully, adding details to the trace for observability.
470
+
471
+ Args:
472
+ wrapped: The original 'fine_tuning.jobs.create' method to be wrapped.
473
+ instance: The instance of the class where the original method is defined.
474
+ args: Positional arguments for the method.
475
+ kwargs: Keyword arguments for the method.
476
+
477
+ Returns:
478
+ The response from the original 'fine_tuning.jobs.create' method.
479
+ """
480
+
481
+ with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
482
+ response = await wrapped(*args, **kwargs)
483
+
484
+ try:
485
+ # Set Span attributes
486
+ span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
487
+ span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
488
+ SemanticConvetion.GEN_AI_SYSTEM_OPENAI)
489
+ span.set_attribute(SemanticConvetion.GEN_AI_TYPE,
490
+ "fine_tuning")
491
+ span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,
492
+ gen_ai_endpoint)
493
+ span.set_attribute(SemanticConvetion.GEN_AI_ENVIRONMENT,
494
+ environment)
495
+ span.set_attribute(SemanticConvetion.GEN_AI_APPLICATION_NAME,
496
+ application_name)
497
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL,
498
+ kwargs.get("model", "gpt-3.5-turbo"))
499
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TRAINING_FILE,
500
+ kwargs.get("training_file", ""))
501
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_VALIDATION_FILE,
502
+ kwargs.get("validation_file", ""))
503
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_FINETUNE_BATCH_SIZE,
504
+ kwargs.get("hyperparameters.batch_size", "auto"))
505
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_FINETUNE_MODEL_LRM,
506
+ kwargs.get("hyperparameters.learning_rate_multiplier",
507
+ "auto"))
508
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_FINETUNE_MODEL_EPOCHS,
509
+ kwargs.get("hyperparameters.n_epochs", "auto"))
510
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_FINETUNE_MODEL_SUFFIX,
511
+ kwargs.get("suffix", ""))
512
+ span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_ID,
513
+ response.id)
514
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_PROMPT_TOKENS,
515
+ response.usage.prompt_tokens)
516
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_FINETUNE_STATUS,
517
+ response.status)
518
+ span.set_status(Status(StatusCode.OK))
519
+
520
+ if disable_metrics is False:
521
+ metrics["genai_requests"].add(1)
522
+
523
+ # Return original response
524
+ return response
525
+
526
+ except Exception as e:
527
+ handle_exception(span, e)
528
+ logger.error("Error in trace creation: %s", e)
529
+
530
+ # Return original response
531
+ return response
532
+
533
+ return wrapper
534
+
535
+ def async_image_generate(gen_ai_endpoint, version, environment, application_name,
536
+ tracer, pricing_info, trace_content, metrics, disable_metrics):
537
+ """
538
+ Generates a telemetry wrapper for image generation to collect metrics.
539
+
540
+ Args:
541
+ gen_ai_endpoint: Endpoint identifier for logging and tracing.
542
+ version: Version of the monitoring package.
543
+ environment: Deployment environment (e.g., production, staging).
544
+ application_name: Name of the application using the OpenAI API.
545
+ tracer: OpenTelemetry tracer for creating spans.
546
+ pricing_info: Information used for calculating the cost of OpenAI image generation.
547
+ trace_content: Flag indicating whether to trace the input prompt and generated images.
548
+
549
+ Returns:
550
+ A function that wraps the image generation method to add telemetry.
551
+ """
552
+
553
+ async def wrapper(wrapped, instance, args, kwargs):
554
+ """
555
+ Wraps the 'images.generate' API call to add telemetry.
556
+
557
+ This collects metrics such as execution time, cost, and handles errors
558
+ gracefully, adding details to the trace for observability.
559
+
560
+ Args:
561
+ wrapped: The original 'images.generate' method to be wrapped.
562
+ instance: The instance of the class where the original method is defined.
563
+ args: Positional arguments for the 'images.generate' method.
564
+ kwargs: Keyword arguments for the 'images.generate' method.
565
+
566
+ Returns:
567
+ The response from the original 'images.generate' method.
568
+ """
569
+
570
+ with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
571
+ response = await wrapped(*args, **kwargs)
572
+ images_count = 0
573
+
574
+ try:
575
+ # Find Image format
576
+ if "response_format" in kwargs and kwargs["response_format"] == "b64_json":
577
+ image = "b64_json"
578
+ else:
579
+ image = "url"
580
+
581
+ # Calculate cost of the operation
582
+ cost = get_image_model_cost(kwargs.get("model", "dall-e-2"),
583
+ pricing_info, kwargs.get("size", "1024x1024"),
584
+ kwargs.get("quality", "standard"))
585
+
586
+ for items in response.data:
587
+ # Set Span attributes
588
+ span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
589
+ span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
590
+ SemanticConvetion.GEN_AI_SYSTEM_OPENAI)
591
+ span.set_attribute(SemanticConvetion.GEN_AI_TYPE,
592
+ SemanticConvetion.GEN_AI_TYPE_IMAGE)
593
+ span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,
594
+ gen_ai_endpoint)
595
+ span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_ID,
596
+ response.created)
597
+ span.set_attribute(SemanticConvetion.GEN_AI_ENVIRONMENT,
598
+ environment)
599
+ span.set_attribute(SemanticConvetion.GEN_AI_APPLICATION_NAME,
600
+ application_name)
601
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL,
602
+ kwargs.get("model", "dall-e-2"))
603
+ span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_IMAGE_SIZE,
604
+ kwargs.get("size", "1024x1024"))
605
+ span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_IMAGE_QUALITY,
606
+ kwargs.get("quality", "standard"))
607
+ span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_IMAGE_STYLE,
608
+ kwargs.get("style", "vivid"))
609
+ span.set_attribute(SemanticConvetion.GEN_AI_CONTENT_REVISED_PROMPT,
610
+ items.revised_prompt if items.revised_prompt else "")
611
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_USER,
612
+ kwargs.get("user", ""))
613
+ if trace_content:
614
+ span.set_attribute(SemanticConvetion.GEN_AI_CONTENT_PROMPT,
615
+ kwargs.get("prompt", ""))
616
+
617
+ attribute_name = f"gen_ai.response.image.{images_count}"
618
+ span.set_attribute(attribute_name,
619
+ getattr(items, image))
620
+
621
+ images_count+=1
622
+
623
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
624
+ len(response.data) * cost)
625
+ span.set_status(Status(StatusCode.OK))
626
+
627
+ if disable_metrics is False:
628
+ attributes = {
629
+ TELEMETRY_SDK_NAME:
630
+ "openlit",
631
+ SemanticConvetion.GEN_AI_APPLICATION_NAME:
632
+ application_name,
633
+ SemanticConvetion.GEN_AI_SYSTEM:
634
+ SemanticConvetion.GEN_AI_SYSTEM_OPENAI,
635
+ SemanticConvetion.GEN_AI_ENVIRONMENT:
636
+ environment,
637
+ SemanticConvetion.GEN_AI_TYPE:
638
+ SemanticConvetion.GEN_AI_TYPE_IMAGE,
639
+ SemanticConvetion.GEN_AI_REQUEST_MODEL:
640
+ kwargs.get("model", "dall-e-2")
641
+ }
642
+
643
+ metrics["genai_requests"].add(1, attributes)
644
+ metrics["genai_cost"].record(cost, attributes)
645
+
646
+ # Return original response
647
+ return response
648
+
649
+ except Exception as e:
650
+ handle_exception(span, e)
651
+ logger.error("Error in trace creation: %s", e)
652
+
653
+ # Return original response
654
+ return response
655
+
656
+ return wrapper
657
+
658
+ def async_image_variatons(gen_ai_endpoint, version, environment, application_name,
659
+ tracer, pricing_info, trace_content, metrics, disable_metrics):
660
+ """
661
+ Generates a telemetry wrapper for creating image variations to collect metrics.
662
+
663
+ Args:
664
+ gen_ai_endpoint: Endpoint identifier for logging and tracing.
665
+ version: Version of the monitoring package.
666
+ environment: Deployment environment (e.g., production, staging).
667
+ application_name: Name of the application using the OpenAI API.
668
+ tracer: OpenTelemetry tracer for creating spans.
669
+ pricing_info: Information used for calculating the cost of generating image variations.
670
+ trace_content: Flag indicating whether to trace the input image and generated variations.
671
+
672
+ Returns:
673
+ A function that wraps the image variations creation method to add telemetry.
674
+ """
675
+
676
+ async def wrapper(wrapped, instance, args, kwargs):
677
+ """
678
+ Wraps the 'images.create.variations' API call to add telemetry.
679
+
680
+ This collects metrics such as execution time, cost, and handles errors
681
+ gracefully, adding details to the trace for observability.
682
+
683
+ Args:
684
+ wrapped: The original 'images.create.variations' method to be wrapped.
685
+ instance: The instance of the class where the original method is defined.
686
+ args: Positional arguments for the method.
687
+ kwargs: Keyword arguments for the method.
688
+
689
+ Returns:
690
+ The response from the original 'images.create.variations' method.
691
+ """
692
+
693
+ with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
694
+ response = await wrapped(*args, **kwargs)
695
+ images_count = 0
696
+
697
+ try:
698
+ # Find Image format
699
+ if "response_format" in kwargs and kwargs["response_format"] == "b64_json":
700
+ image = "b64_json"
701
+ else:
702
+ image = "url"
703
+
704
+ # Calculate cost of the operation
705
+ cost = get_image_model_cost(kwargs.get("model", "dall-e-2"), pricing_info,
706
+ kwargs.get("size", "1024x1024"), "standard")
707
+
708
+ for items in response.data:
709
+ # Set Span attributes
710
+ span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
711
+ span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
712
+ SemanticConvetion.GEN_AI_SYSTEM_OPENAI)
713
+ span.set_attribute(SemanticConvetion.GEN_AI_TYPE,
714
+ SemanticConvetion.GEN_AI_TYPE_IMAGE)
715
+ span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,
716
+ gen_ai_endpoint)
717
+ span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_ID,
718
+ response.created)
719
+ span.set_attribute(SemanticConvetion.GEN_AI_ENVIRONMENT,
720
+ environment)
721
+ span.set_attribute(SemanticConvetion.GEN_AI_APPLICATION_NAME,
722
+ application_name)
723
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL,
724
+ kwargs.get("model", "dall-e-2"))
725
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_USER,
726
+ kwargs.get("user", ""))
727
+ span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_IMAGE_SIZE,
728
+ kwargs.get("size", "1024x1024"))
729
+ span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_IMAGE_QUALITY,
730
+ "standard")
731
+ if trace_content:
732
+ span.set_attribute(SemanticConvetion.GEN_AI_CONTENT_PROMPT,
733
+ kwargs.get(SemanticConvetion.GEN_AI_TYPE_IMAGE, ""))
734
+
735
+ attribute_name = f"gen_ai.response.image.{images_count}"
736
+ span.set_attribute(attribute_name, getattr(items, image))
737
+
738
+ images_count+=1
739
+
740
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
741
+ len(response.data) * cost)
742
+ span.set_status(Status(StatusCode.OK))
743
+
744
+ if disable_metrics is False:
745
+ attributes = {
746
+ TELEMETRY_SDK_NAME:
747
+ "openlit",
748
+ SemanticConvetion.GEN_AI_APPLICATION_NAME:
749
+ application_name,
750
+ SemanticConvetion.GEN_AI_SYSTEM:
751
+ SemanticConvetion.GEN_AI_SYSTEM_OPENAI,
752
+ SemanticConvetion.GEN_AI_ENVIRONMENT:
753
+ environment,
754
+ SemanticConvetion.GEN_AI_TYPE:
755
+ SemanticConvetion.GEN_AI_TYPE_IMAGE,
756
+ SemanticConvetion.GEN_AI_REQUEST_MODEL:
757
+ kwargs.get("model", "dall-e-2")
758
+ }
759
+
760
+ metrics["genai_requests"].add(1, attributes)
761
+ metrics["genai_cost"].record(cost, attributes)
762
+
763
+ # Return original response
764
+ return response
765
+
766
+ except Exception as e:
767
+ handle_exception(span, e)
768
+ logger.error("Error in trace creation: %s", e)
769
+
770
+ # Return original response
771
+ return response
772
+
773
+ return wrapper
774
+
775
+ def async_audio_create(gen_ai_endpoint, version, environment, application_name,
776
+ tracer, pricing_info, trace_content, metrics, disable_metrics):
777
+ """
778
+ Generates a telemetry wrapper for creating speech audio to collect metrics.
779
+
780
+ Args:
781
+ gen_ai_endpoint: Endpoint identifier for logging and tracing.
782
+ version: Version of the monitoring package.
783
+ environment: Deployment environment (e.g., production, staging).
784
+ application_name: Name of the application using the OpenAI API.
785
+ tracer: OpenTelemetry tracer for creating spans.
786
+ pricing_info: Information used for calculating the cost of generating speech audio.
787
+ trace_content: Flag indicating whether to trace the input text and generated audio.
788
+
789
+ Returns:
790
+ A function that wraps the speech audio creation method to add telemetry.
791
+ """
792
+
793
+ async def wrapper(wrapped, instance, args, kwargs):
794
+ """
795
+ Wraps the 'audio.speech.create' API call to add telemetry.
796
+
797
+ This collects metrics such as execution time, cost, and handles errors
798
+ gracefully, adding details to the trace for observability.
799
+
800
+ Args:
801
+ wrapped: The original 'audio.speech.create' method to be wrapped.
802
+ instance: The instance of the class where the original method is defined.
803
+ args: Positional arguments for the 'audio.speech.create' method.
804
+ kwargs: Keyword arguments for the 'audio.speech.create' method.
805
+
806
+ Returns:
807
+ The response from the original 'audio.speech.create' method.
808
+ """
809
+
810
+ with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
811
+ response = await wrapped(*args, **kwargs)
812
+
813
+ try:
814
+ # Calculate cost of the operation
815
+ cost = get_audio_model_cost(kwargs.get("model", "tts-1"),
816
+ pricing_info, kwargs.get("input", ""))
817
+
818
+ # Set Span attributes
819
+ span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
820
+ span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
821
+ SemanticConvetion.GEN_AI_SYSTEM_OPENAI)
822
+ span.set_attribute(SemanticConvetion.GEN_AI_TYPE,
823
+ SemanticConvetion.GEN_AI_TYPE_AUDIO)
824
+ span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,
825
+ gen_ai_endpoint)
826
+ span.set_attribute(SemanticConvetion.GEN_AI_ENVIRONMENT,
827
+ environment)
828
+ span.set_attribute(SemanticConvetion.GEN_AI_APPLICATION_NAME,
829
+ application_name)
830
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL,
831
+ kwargs.get("model", "tts-1"))
832
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_AUDIO_VOICE,
833
+ kwargs.get("voice", "alloy"))
834
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_AUDIO_RESPONSE_FORMAT,
835
+ kwargs.get("response_format", "mp3"))
836
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_AUDIO_SPEED,
837
+ kwargs.get("speed", 1))
838
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
839
+ cost)
840
+ if trace_content:
841
+ span.set_attribute(SemanticConvetion.GEN_AI_CONTENT_PROMPT,
842
+ kwargs.get("input", ""))
843
+
844
+ span.set_status(Status(StatusCode.OK))
845
+
846
+ if disable_metrics is False:
847
+ attributes = {
848
+ TELEMETRY_SDK_NAME:
849
+ "openlit",
850
+ SemanticConvetion.GEN_AI_APPLICATION_NAME:
851
+ application_name,
852
+ SemanticConvetion.GEN_AI_SYSTEM:
853
+ SemanticConvetion.GEN_AI_SYSTEM_OPENAI,
854
+ SemanticConvetion.GEN_AI_ENVIRONMENT:
855
+ environment,
856
+ SemanticConvetion.GEN_AI_TYPE:
857
+ SemanticConvetion.GEN_AI_TYPE_AUDIO,
858
+ SemanticConvetion.GEN_AI_REQUEST_MODEL:
859
+ kwargs.get("model", "tts-1")
860
+ }
861
+
862
+ metrics["genai_requests"].add(1, attributes)
863
+ metrics["genai_cost"].record(cost, attributes)
864
+
865
+ # Return original response
866
+ return response
867
+
868
+ except Exception as e:
869
+ handle_exception(span, e)
870
+ logger.error("Error in trace creation: %s", e)
871
+
872
+ # Return original response
873
+ return response
874
+
875
+ return wrapper