openlit 1.32.11__py3-none-any.whl → 1.33.0__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,558 @@
1
+ # pylint: disable=duplicate-code, broad-exception-caught, too-many-statements, unused-argument, too-many-branches, too-many-instance-attributes
2
+ """
3
+ Module for monitoring Together 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 (
10
+ get_chat_model_cost,
11
+ get_image_model_cost,
12
+ handle_exception,
13
+ response_as_dict,
14
+ )
15
+ from openlit.semcov import SemanticConvetion
16
+
17
+ # Initialize logger for logging potential issues and operations
18
+ logger = logging.getLogger(__name__)
19
+
20
+ def async_completion(gen_ai_endpoint, version, environment, application_name,
21
+ tracer, pricing_info, trace_content, metrics, disable_metrics):
22
+ """
23
+ Generates a telemetry wrapper for chat completions to collect metrics.
24
+
25
+ Args:
26
+ gen_ai_endpoint: Endpoint identifier for logging and tracing.
27
+ version: Version of the monitoring package.
28
+ environment: Deployment environment (e.g., production, staging).
29
+ application_name: Name of the application using the Together AI SDK.
30
+ tracer: OpenTelemetry tracer for creating spans.
31
+ pricing_info: Information used for calculating the cost of Together AI usage.
32
+ trace_content: Flag indicating whether to trace the actual content.
33
+
34
+ Returns:
35
+ A function that wraps the chat completions method to add telemetry.
36
+ """
37
+
38
+ class TracedAsyncStream:
39
+ """
40
+ Wrapper for streaming responses to collect metrics and trace data.
41
+
42
+ This class implements the '__aiter__' and '__anext__' methods that
43
+ handle asynchronous streaming responses.
44
+
45
+ This class also implements '__aenter__' and '__aexit__' methods that
46
+ handle asynchronous context management protocol.
47
+ """
48
+ def __init__(
49
+ self,
50
+ wrapped,
51
+ span,
52
+ kwargs,
53
+ **args,
54
+ ):
55
+ self.__wrapped__ = wrapped
56
+ self._span = span
57
+ # Placeholder for aggregating streaming response
58
+ self._llmresponse = ""
59
+ self._response_id = ""
60
+ self._prompt_tokens = 0
61
+ self._completion_tokens = 0
62
+ self._total_tokens = 0
63
+
64
+ self._args = args
65
+ self._kwargs = kwargs
66
+
67
+ async def __aenter__(self):
68
+ await self.__wrapped__.__aenter__()
69
+ return self
70
+
71
+ async def __aexit__(self, exc_type, exc_value, traceback):
72
+ await self.__wrapped__.__aexit__(exc_type, exc_value, traceback)
73
+
74
+ def __aiter__(self):
75
+ return self
76
+
77
+ async def __getattr__(self, name):
78
+ """Delegate attribute access to the wrapped object."""
79
+ return getattr(await self.__wrapped__, name)
80
+
81
+ async def __anext__(self):
82
+ try:
83
+ chunk = await self.__wrapped__.__anext__()
84
+ chunked = response_as_dict(chunk)
85
+ # Collect message IDs and aggregated response from events
86
+ if (len(chunked.get('choices')) > 0 and ('delta' in chunked.get('choices')[0] and
87
+ 'content' in chunked.get('choices')[0].get('delta'))):
88
+
89
+ content = chunked.get('choices')[0].get('delta').get('content')
90
+ if content:
91
+ self._llmresponse += content
92
+ if chunked.get("usage"):
93
+ self._prompt_tokens = chunked.get("usage").get("prompt_tokens")
94
+ self._completion_tokens = chunked.get("usage").get("completion_tokens")
95
+ self._total_tokens = chunked.get("usage").get("total_tokens")
96
+ self._response_id = chunked.get('id')
97
+ return chunk
98
+ except StopAsyncIteration:
99
+ # Handling exception ensure observability without disrupting operation
100
+ try:
101
+ # Format 'messages' into a single string
102
+ message_prompt = self._kwargs.get("messages", "")
103
+ formatted_messages = []
104
+ for message in message_prompt:
105
+ role = message["role"]
106
+ content = message["content"]
107
+
108
+ if isinstance(content, list):
109
+ content_str = ", ".join(
110
+ # pylint: disable=line-too-long
111
+ f'{item["type"]}: {item["text"] if "text" in item else item["image_url"]}'
112
+ if "type" in item else f'text: {item["text"]}'
113
+ for item in content
114
+ )
115
+ formatted_messages.append(f"{role}: {content_str}")
116
+ else:
117
+ formatted_messages.append(f"{role}: {content}")
118
+ prompt = "\n".join(formatted_messages)
119
+
120
+ # Calculate cost of the operation
121
+ cost = get_chat_model_cost(self._kwargs.get(
122
+ "model",
123
+ "meta-llama/Llama-3.3-70B-Instruct-Turbo"
124
+ ),
125
+ pricing_info, self._prompt_tokens,
126
+ self._completion_tokens)
127
+
128
+ # Set Span attributes
129
+ self._span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
130
+ self._span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
131
+ SemanticConvetion.GEN_AI_SYSTEM_TOGETHER)
132
+ self._span.set_attribute(SemanticConvetion.GEN_AI_TYPE,
133
+ SemanticConvetion.GEN_AI_TYPE_CHAT)
134
+ self._span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,
135
+ gen_ai_endpoint)
136
+ self._span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_ID,
137
+ self._response_id)
138
+ self._span.set_attribute(SemanticConvetion.GEN_AI_ENVIRONMENT,
139
+ environment)
140
+ self._span.set_attribute(SemanticConvetion.GEN_AI_APPLICATION_NAME,
141
+ application_name)
142
+ self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL,
143
+ self._kwargs.get(
144
+ "model",
145
+ "meta-llama/Llama-3.3-70B-Instruct-Turbo"
146
+ ))
147
+ self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_USER,
148
+ self._kwargs.get("user", ""))
149
+ self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TOP_P,
150
+ self._kwargs.get("top_p", 1.0))
151
+ self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MAX_TOKENS,
152
+ self._kwargs.get("max_tokens", -1))
153
+ self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TEMPERATURE,
154
+ self._kwargs.get("temperature", 1.0))
155
+ self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_PRESENCE_PENALTY,
156
+ self._kwargs.get("presence_penalty", 0.0))
157
+ self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_FREQUENCY_PENALTY,
158
+ self._kwargs.get("frequency_penalty", 0.0))
159
+ self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_SEED,
160
+ self._kwargs.get("seed", ""))
161
+ self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_IS_STREAM,
162
+ True)
163
+ self._span.set_attribute(SemanticConvetion.GEN_AI_USAGE_PROMPT_TOKENS,
164
+ self._prompt_tokens)
165
+ self._span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COMPLETION_TOKENS,
166
+ self._completion_tokens)
167
+ self._span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
168
+ self._total_tokens)
169
+ self._span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
170
+ cost)
171
+ if trace_content:
172
+ self._span.add_event(
173
+ name=SemanticConvetion.GEN_AI_CONTENT_PROMPT_EVENT,
174
+ attributes={
175
+ SemanticConvetion.GEN_AI_CONTENT_PROMPT: prompt,
176
+ },
177
+ )
178
+ self._span.add_event(
179
+ name=SemanticConvetion.GEN_AI_CONTENT_COMPLETION_EVENT,
180
+ attributes={
181
+ SemanticConvetion.GEN_AI_CONTENT_COMPLETION: self._llmresponse,
182
+ },
183
+ )
184
+
185
+ self._span.set_status(Status(StatusCode.OK))
186
+
187
+ if disable_metrics is False:
188
+ attributes = {
189
+ TELEMETRY_SDK_NAME:
190
+ "openlit",
191
+ SemanticConvetion.GEN_AI_APPLICATION_NAME:
192
+ application_name,
193
+ SemanticConvetion.GEN_AI_SYSTEM:
194
+ SemanticConvetion.GEN_AI_SYSTEM_TOGETHER,
195
+ SemanticConvetion.GEN_AI_ENVIRONMENT:
196
+ environment,
197
+ SemanticConvetion.GEN_AI_TYPE:
198
+ SemanticConvetion.GEN_AI_TYPE_CHAT,
199
+ SemanticConvetion.GEN_AI_REQUEST_MODEL:
200
+ self._kwargs.get("model",
201
+ "meta-llama/Llama-3.3-70B-Instruct-Turbo")
202
+ }
203
+
204
+ metrics["genai_requests"].add(1, attributes)
205
+ metrics["genai_total_tokens"].add(
206
+ self._total_tokens, attributes
207
+ )
208
+ metrics["genai_completion_tokens"].add(
209
+ self._completion_tokens, attributes
210
+ )
211
+ metrics["genai_prompt_tokens"].add(
212
+ self._prompt_tokens, attributes
213
+ )
214
+ metrics["genai_cost"].record(cost, attributes)
215
+
216
+ except Exception as e:
217
+ handle_exception(self._span, e)
218
+ logger.error("Error in trace creation: %s", e)
219
+ finally:
220
+ self._span.end()
221
+ raise
222
+
223
+ async def wrapper(wrapped, instance, args, kwargs):
224
+ """
225
+ Wraps the 'chat.completions' API call to add telemetry.
226
+
227
+ This collects metrics such as execution time, cost, and token usage, and handles errors
228
+ gracefully, adding details to the trace for observability.
229
+
230
+ Args:
231
+ wrapped: The original 'chat.completions' method to be wrapped.
232
+ instance: The instance of the class where the original method is defined.
233
+ args: Positional arguments for the 'chat.completions' method.
234
+ kwargs: Keyword arguments for the 'chat.completions' method.
235
+
236
+ Returns:
237
+ The response from the original 'chat.completions' method.
238
+ """
239
+
240
+ # Check if streaming is enabled for the API call
241
+ streaming = kwargs.get("stream", False)
242
+
243
+ # pylint: disable=no-else-return
244
+ if streaming:
245
+ # Special handling for streaming response to accommodate the nature of data flow
246
+ awaited_wrapped = await wrapped(*args, **kwargs)
247
+ span = tracer.start_span(gen_ai_endpoint, kind=SpanKind.CLIENT)
248
+
249
+ return TracedAsyncStream(awaited_wrapped, span, kwargs)
250
+
251
+ # Handling for non-streaming responses
252
+ else:
253
+ # pylint: disable=line-too-long
254
+ with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
255
+ response = await wrapped(*args, **kwargs)
256
+
257
+ response_dict = response_as_dict(response)
258
+
259
+ try:
260
+ # Format 'messages' into a single string
261
+ message_prompt = kwargs.get("messages", "")
262
+ formatted_messages = []
263
+ for message in message_prompt:
264
+ role = message["role"]
265
+ content = message["content"]
266
+
267
+ if isinstance(content, list):
268
+ content_str = ", ".join(
269
+ # pylint: disable=line-too-long
270
+ f'{item["type"]}: {item["text"] if "text" in item else item["image_url"]}'
271
+ if "type" in item else f'text: {item["text"]}'
272
+ for item in content
273
+ )
274
+ formatted_messages.append(f"{role}: {content_str}")
275
+ else:
276
+ formatted_messages.append(f"{role}: {content}")
277
+ prompt = "\n".join(formatted_messages)
278
+
279
+ # Set base span attribues
280
+ span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
281
+ span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
282
+ SemanticConvetion.GEN_AI_SYSTEM_TOGETHER)
283
+ span.set_attribute(SemanticConvetion.GEN_AI_TYPE,
284
+ SemanticConvetion.GEN_AI_TYPE_CHAT)
285
+ span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,
286
+ gen_ai_endpoint)
287
+ span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_ID,
288
+ response_dict.get("id"))
289
+ span.set_attribute(SemanticConvetion.GEN_AI_ENVIRONMENT,
290
+ environment)
291
+ span.set_attribute(SemanticConvetion.GEN_AI_APPLICATION_NAME,
292
+ application_name)
293
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL,
294
+ kwargs.get("model",
295
+ "meta-llama/Llama-3.3-70B-Instruct-Turbo"))
296
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TOP_P,
297
+ kwargs.get("top_p", 1.0))
298
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MAX_TOKENS,
299
+ kwargs.get("max_tokens", -1))
300
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_USER,
301
+ kwargs.get("user", ""))
302
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TEMPERATURE,
303
+ kwargs.get("temperature", 1.0))
304
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_PRESENCE_PENALTY,
305
+ kwargs.get("presence_penalty", 0.0))
306
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_FREQUENCY_PENALTY,
307
+ kwargs.get("frequency_penalty", 0.0))
308
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_SEED,
309
+ kwargs.get("seed", ""))
310
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_IS_STREAM,
311
+ False)
312
+ if trace_content:
313
+ span.add_event(
314
+ name=SemanticConvetion.GEN_AI_CONTENT_PROMPT_EVENT,
315
+ attributes={
316
+ SemanticConvetion.GEN_AI_CONTENT_PROMPT: prompt,
317
+ },
318
+ )
319
+
320
+ # Set span attributes when tools is not passed to the function call
321
+ if "tools" not in kwargs:
322
+ # Calculate cost of the operation
323
+ cost = get_chat_model_cost(kwargs.get(
324
+ "model",
325
+ "meta-llama/Llama-3.3-70B-Instruct-Turbo"
326
+ ),
327
+ pricing_info,
328
+ response_dict.get('usage', {}).get('prompt_tokens', None),
329
+ response_dict.get('usage', {}).get('completion_tokens', None))
330
+
331
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_PROMPT_TOKENS,
332
+ response_dict.get('usage', {}).get('prompt_tokens', None))
333
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COMPLETION_TOKENS,
334
+ response_dict.get('usage', {}).get('completion_tokens', None))
335
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
336
+ response_dict.get('usage', {}).get('total_tokens', None))
337
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
338
+ cost)
339
+
340
+ # Set span attributes for when n = 1 (default)
341
+ if "n" not in kwargs or kwargs["n"] == 1:
342
+ if trace_content:
343
+ span.add_event(
344
+ name=SemanticConvetion.GEN_AI_CONTENT_COMPLETION_EVENT,
345
+ attributes={
346
+ SemanticConvetion.GEN_AI_CONTENT_COMPLETION: response_dict.get('choices', [])[0].get("message").get("content"),
347
+ },
348
+ )
349
+
350
+ # Set span attributes for when n > 0
351
+ else:
352
+ i = 0
353
+ while i < kwargs["n"] and trace_content is True:
354
+ attribute_name = f"gen_ai.content.completion.{i}"
355
+ span.add_event(
356
+ name=attribute_name,
357
+ attributes={
358
+ SemanticConvetion.GEN_AI_CONTENT_COMPLETION: response_dict.get('choices')[i].get("message").get("content"),
359
+ },
360
+ )
361
+ i += 1
362
+
363
+ # Return original response
364
+ return response
365
+
366
+ # Set span attributes when tools is passed to the function call
367
+ elif "tools" in kwargs:
368
+ # Calculate cost of the operation
369
+ cost = get_chat_model_cost(kwargs.get(
370
+ "model",
371
+ "meta-llama/Llama-3.3-70B-Instruct-Turbo"
372
+ ),
373
+ pricing_info,
374
+ response_dict.get('usage').get('prompt_tokens'),
375
+ response_dict.get('usage').get('completion_tokens'))
376
+
377
+ span.add_event(
378
+ name=SemanticConvetion.GEN_AI_CONTENT_COMPLETION_EVENT,
379
+ attributes={
380
+ SemanticConvetion.GEN_AI_CONTENT_COMPLETION: "Function called with tools",
381
+ },
382
+ )
383
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_PROMPT_TOKENS,
384
+ response_dict.get('usage').get('prompt_tokens'))
385
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COMPLETION_TOKENS,
386
+ response_dict.get('usage').get('completion_tokens'))
387
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
388
+ response_dict.get('usage').get('total_tokens'))
389
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
390
+ cost)
391
+
392
+ span.set_status(Status(StatusCode.OK))
393
+
394
+ if disable_metrics is False:
395
+ attributes = {
396
+ TELEMETRY_SDK_NAME:
397
+ "openlit",
398
+ SemanticConvetion.GEN_AI_APPLICATION_NAME:
399
+ application_name,
400
+ SemanticConvetion.GEN_AI_SYSTEM:
401
+ SemanticConvetion.GEN_AI_SYSTEM_TOGETHER,
402
+ SemanticConvetion.GEN_AI_ENVIRONMENT:
403
+ environment,
404
+ SemanticConvetion.GEN_AI_TYPE:
405
+ SemanticConvetion.GEN_AI_TYPE_CHAT,
406
+ SemanticConvetion.GEN_AI_REQUEST_MODEL:
407
+ kwargs.get("model", "meta-llama/Llama-3.3-70B-Instruct-Turbo")
408
+ }
409
+
410
+ metrics["genai_requests"].add(1, attributes)
411
+ metrics["genai_total_tokens"].add(
412
+ response_dict.get('usage').get('total_tokens'), attributes)
413
+ metrics["genai_completion_tokens"].add(
414
+ response_dict.get('usage').get('completion_tokens'), attributes)
415
+ metrics["genai_prompt_tokens"].add(
416
+ response_dict.get('usage').get('prompt_tokens'), attributes)
417
+ metrics["genai_cost"].record(cost, attributes)
418
+
419
+ # Return original response
420
+ return response
421
+
422
+ except Exception as e:
423
+ handle_exception(span, e)
424
+ logger.error("Error in trace creation: %s", e)
425
+
426
+ # Return original response
427
+ return response
428
+
429
+ return wrapper
430
+
431
+ def async_image_generate(gen_ai_endpoint, version, environment, application_name,
432
+ tracer, pricing_info, trace_content, metrics, disable_metrics):
433
+ """
434
+ Generates a telemetry wrapper for image generation to collect metrics.
435
+
436
+ Args:
437
+ gen_ai_endpoint: Endpoint identifier for logging and tracing.
438
+ version: Version of the monitoring package.
439
+ environment: Deployment environment (e.g., production, staging).
440
+ application_name: Name of the application using the Together API.
441
+ tracer: OpenTelemetry tracer for creating spans.
442
+ pricing_info: Information used for calculating the cost of Together image generation.
443
+ trace_content: Flag indicating whether to trace the input prompt and generated images.
444
+
445
+ Returns:
446
+ A function that wraps the image generation method to add telemetry.
447
+ """
448
+
449
+ async def wrapper(wrapped, instance, args, kwargs):
450
+ """
451
+ Wraps the 'images.generate' API call to add telemetry.
452
+
453
+ This collects metrics such as execution time, cost, and handles errors
454
+ gracefully, adding details to the trace for observability.
455
+
456
+ Args:
457
+ wrapped: The original 'images.generate' method to be wrapped.
458
+ instance: The instance of the class where the original method is defined.
459
+ args: Positional arguments for the 'images.generate' method.
460
+ kwargs: Keyword arguments for the 'images.generate' method.
461
+
462
+ Returns:
463
+ The response from the original 'images.generate' method.
464
+ """
465
+
466
+ with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
467
+ response = await wrapped(*args, **kwargs)
468
+ images_count = 0
469
+
470
+ try:
471
+ # Find Image format
472
+ if "response_format" in kwargs and kwargs["response_format"] == "b64_json":
473
+ image = "b64_json"
474
+ else:
475
+ image = "url"
476
+
477
+ # Calculate cost of the operation
478
+ image_size = str(kwargs.get("width", 1024)) + "x" + str(kwargs.get("height", 1024))
479
+ cost_per_million = get_image_model_cost(kwargs.get(
480
+ "model", "black-forest-labs/FLUX.1-dev"
481
+ ),
482
+ pricing_info, "1000000",
483
+ kwargs.get("quality", "standard"))
484
+ pixels = kwargs.get("width", 1024) * kwargs.get("height", 1024)
485
+ cost = pixels / 1_000_000 * cost_per_million
486
+
487
+ for items in response.data:
488
+ # Set Span attributes
489
+ span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
490
+ span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
491
+ SemanticConvetion.GEN_AI_SYSTEM_TOGETHER)
492
+ span.set_attribute(SemanticConvetion.GEN_AI_TYPE,
493
+ SemanticConvetion.GEN_AI_TYPE_IMAGE)
494
+ span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,
495
+ gen_ai_endpoint)
496
+ span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_ID,
497
+ response.id)
498
+ span.set_attribute(SemanticConvetion.GEN_AI_ENVIRONMENT,
499
+ environment)
500
+ span.set_attribute(SemanticConvetion.GEN_AI_APPLICATION_NAME,
501
+ application_name)
502
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL,
503
+ kwargs.get("model", "black-forest-labs/FLUX.1-dev"))
504
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_IMAGE_SIZE,
505
+ image_size)
506
+ span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_IMAGE_QUALITY,
507
+ kwargs.get("quality", "standard"))
508
+ if trace_content:
509
+ span.add_event(
510
+ name=SemanticConvetion.GEN_AI_CONTENT_PROMPT_EVENT,
511
+ attributes={
512
+ SemanticConvetion.GEN_AI_CONTENT_PROMPT: kwargs.get("prompt", ""),
513
+ },
514
+ )
515
+ attribute_name = f"gen_ai.response.image.{images_count}"
516
+ span.add_event(
517
+ name=attribute_name,
518
+ attributes={
519
+ SemanticConvetion.GEN_AI_CONTENT_COMPLETION: getattr(items, image),
520
+ },
521
+ )
522
+
523
+ images_count+=1
524
+
525
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
526
+ len(response.data) * cost)
527
+ span.set_status(Status(StatusCode.OK))
528
+
529
+ if disable_metrics is False:
530
+ attributes = {
531
+ TELEMETRY_SDK_NAME:
532
+ "openlit",
533
+ SemanticConvetion.GEN_AI_APPLICATION_NAME:
534
+ application_name,
535
+ SemanticConvetion.GEN_AI_SYSTEM:
536
+ SemanticConvetion.GEN_AI_SYSTEM_TOGETHER,
537
+ SemanticConvetion.GEN_AI_ENVIRONMENT:
538
+ environment,
539
+ SemanticConvetion.GEN_AI_TYPE:
540
+ SemanticConvetion.GEN_AI_TYPE_IMAGE,
541
+ SemanticConvetion.GEN_AI_REQUEST_MODEL:
542
+ kwargs.get("model", "black-forest-labs/FLUX.1-dev")
543
+ }
544
+
545
+ metrics["genai_requests"].add(1, attributes)
546
+ metrics["genai_cost"].record(cost, attributes)
547
+
548
+ # Return original response
549
+ return response
550
+
551
+ except Exception as e:
552
+ handle_exception(span, e)
553
+ logger.error("Error in trace creation: %s", e)
554
+
555
+ # Return original response
556
+ return response
557
+
558
+ return wrapper