openlit 1.29.2__py3-none-any.whl → 1.30.3__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.
@@ -6,8 +6,15 @@ Module for monitoring OpenAI API calls.
6
6
  import logging
7
7
  from opentelemetry.trace import SpanKind, Status, StatusCode
8
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
9
+ from openlit.__helpers import (
10
+ get_chat_model_cost,
11
+ get_embed_model_cost,
12
+ get_audio_model_cost,
13
+ get_image_model_cost,
14
+ openai_tokens,
15
+ handle_exception,
16
+ response_as_dict,
17
+ )
11
18
  from openlit.semcov import SemanticConvetion
12
19
 
13
20
  # Initialize logger for logging potential issues and operations
@@ -46,8 +53,8 @@ def async_chat_completions(gen_ai_endpoint, version, environment, application_na
46
53
  self,
47
54
  wrapped,
48
55
  span,
49
- *args,
50
- **kwargs,
56
+ kwargs,
57
+ **args,
51
58
  ):
52
59
  self.__wrapped__ = wrapped
53
60
  self._span = span
@@ -68,17 +75,22 @@ def async_chat_completions(gen_ai_endpoint, version, environment, application_na
68
75
  def __aiter__(self):
69
76
  return self
70
77
 
78
+ async def __getattr__(self, name):
79
+ """Delegate attribute access to the wrapped object."""
80
+ return getattr(await self.__wrapped__, name)
81
+
71
82
  async def __anext__(self):
72
83
  try:
73
84
  chunk = await self.__wrapped__.__anext__()
85
+ chunked = response_as_dict(chunk)
74
86
  # Collect message IDs and aggregated response from events
75
- if len(chunk.choices) > 0:
76
- # pylint: disable=line-too-long
77
- if hasattr(chunk.choices[0], "delta") and hasattr(chunk.choices[0].delta, "content"):
78
- content = chunk.choices[0].delta.content
79
- if content:
80
- self._llmresponse += content
81
- self._response_id = chunk.id
87
+ if (len(chunked.get('choices')) > 0 and ('delta' in chunked.get('choices')[0] and
88
+ 'content' in chunked.get('choices')[0].get('delta'))):
89
+
90
+ content = chunked.get('choices')[0].get('delta').get('content')
91
+ if content:
92
+ self._llmresponse += content
93
+ self._response_id = chunked.get('id')
82
94
  return chunk
83
95
  except StopAsyncIteration:
84
96
  # Handling exception ensure observability without disrupting operation
@@ -226,7 +238,7 @@ def async_chat_completions(gen_ai_endpoint, version, environment, application_na
226
238
  awaited_wrapped = await wrapped(*args, **kwargs)
227
239
  span = tracer.start_span(gen_ai_endpoint, kind=SpanKind.CLIENT)
228
240
 
229
- return TracedAsyncStream(awaited_wrapped, span)
241
+ return TracedAsyncStream(awaited_wrapped, span, kwargs)
230
242
 
231
243
  # Handling for non-streaming responses
232
244
  else:
@@ -234,6 +246,8 @@ def async_chat_completions(gen_ai_endpoint, version, environment, application_na
234
246
  with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
235
247
  response = await wrapped(*args, **kwargs)
236
248
 
249
+ response_dict = response_as_dict(response)
250
+
237
251
  try:
238
252
  # Format 'messages' into a single string
239
253
  message_prompt = kwargs.get("messages", "")
@@ -263,7 +277,7 @@ def async_chat_completions(gen_ai_endpoint, version, environment, application_na
263
277
  span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,
264
278
  gen_ai_endpoint)
265
279
  span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_ID,
266
- response.id)
280
+ response_dict.get("id"))
267
281
  span.set_attribute(SemanticConvetion.GEN_AI_ENVIRONMENT,
268
282
  environment)
269
283
  span.set_attribute(SemanticConvetion.GEN_AI_APPLICATION_NAME,
@@ -294,23 +308,21 @@ def async_chat_completions(gen_ai_endpoint, version, environment, application_na
294
308
  },
295
309
  )
296
310
 
297
- span.set_status(Status(StatusCode.OK))
298
-
299
311
  # Set span attributes when tools is not passed to the function call
300
312
  if "tools" not in kwargs:
301
313
  # Calculate cost of the operation
302
314
  cost = get_chat_model_cost(kwargs.get("model", "gpt-3.5-turbo"),
303
- pricing_info, response.usage.prompt_tokens,
304
- response.usage.completion_tokens)
315
+ pricing_info, response_dict.get('usage', {}).get('prompt_tokens', None),
316
+ response_dict.get('usage', {}).get('completion_tokens', None))
305
317
 
306
318
  span.set_attribute(SemanticConvetion.GEN_AI_USAGE_PROMPT_TOKENS,
307
- response.usage.prompt_tokens)
319
+ response_dict.get('usage', {}).get('prompt_tokens', None))
308
320
  span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COMPLETION_TOKENS,
309
- response.usage.completion_tokens)
321
+ response_dict.get('usage', {}).get('completion_tokens', None))
310
322
  span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
311
- response.usage.total_tokens)
323
+ response_dict.get('usage', {}).get('total_tokens', None))
312
324
  span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_FINISH_REASON,
313
- [response.choices[0].finish_reason])
325
+ [response_dict.get('choices', [])[0].get('finish_reason', None)])
314
326
  span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
315
327
  cost)
316
328
 
@@ -320,7 +332,7 @@ def async_chat_completions(gen_ai_endpoint, version, environment, application_na
320
332
  span.add_event(
321
333
  name=SemanticConvetion.GEN_AI_CONTENT_COMPLETION_EVENT,
322
334
  attributes={
323
- SemanticConvetion.GEN_AI_CONTENT_COMPLETION: response.choices[0].message.content,
335
+ SemanticConvetion.GEN_AI_CONTENT_COMPLETION: response_dict.get('choices', [])[0].get("message").get("content"),
324
336
  },
325
337
  )
326
338
 
@@ -332,7 +344,7 @@ def async_chat_completions(gen_ai_endpoint, version, environment, application_na
332
344
  span.add_event(
333
345
  name=attribute_name,
334
346
  attributes={
335
- SemanticConvetion.GEN_AI_CONTENT_COMPLETION: response.choices[i].message.content,
347
+ SemanticConvetion.GEN_AI_CONTENT_COMPLETION: response_dict.get('choices')[i].get("message").get("content"),
336
348
  },
337
349
  )
338
350
  i += 1
@@ -344,9 +356,8 @@ def async_chat_completions(gen_ai_endpoint, version, environment, application_na
344
356
  elif "tools" in kwargs:
345
357
  # Calculate cost of the operation
346
358
  cost = get_chat_model_cost(kwargs.get("model", "gpt-3.5-turbo"),
347
- pricing_info, response.usage.prompt_tokens,
348
- response.usage.completion_tokens)
349
-
359
+ pricing_info, response_dict.get('usage').get('prompt_tokens'),
360
+ response_dict.get('usage').get('completion_tokens'))
350
361
  span.add_event(
351
362
  name=SemanticConvetion.GEN_AI_CONTENT_COMPLETION_EVENT,
352
363
  attributes={
@@ -354,11 +365,11 @@ def async_chat_completions(gen_ai_endpoint, version, environment, application_na
354
365
  },
355
366
  )
356
367
  span.set_attribute(SemanticConvetion.GEN_AI_USAGE_PROMPT_TOKENS,
357
- response.usage.prompt_tokens)
368
+ response_dict.get('usage').get('prompt_tokens'))
358
369
  span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COMPLETION_TOKENS,
359
- response.usage.completion_tokens)
370
+ response_dict.get('usage').get('completion_tokens'))
360
371
  span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
361
- response.usage.total_tokens)
372
+ response_dict.get('usage').get('total_tokens'))
362
373
  span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
363
374
  cost)
364
375
 
@@ -381,9 +392,9 @@ def async_chat_completions(gen_ai_endpoint, version, environment, application_na
381
392
  }
382
393
 
383
394
  metrics["genai_requests"].add(1, attributes)
384
- metrics["genai_total_tokens"].add(response.usage.total_tokens, attributes)
385
- metrics["genai_completion_tokens"].add(response.usage.completion_tokens, attributes)
386
- metrics["genai_prompt_tokens"].add(response.usage.prompt_tokens, attributes)
395
+ metrics["genai_total_tokens"].add(response_dict.get('usage').get('total_tokens'), attributes)
396
+ metrics["genai_completion_tokens"].add(response_dict.get('usage').get('completion_tokens'), attributes)
397
+ metrics["genai_prompt_tokens"].add(response_dict.get('usage').get('prompt_tokens'), attributes)
387
398
  metrics["genai_cost"].record(cost, attributes)
388
399
 
389
400
  # Return original response
@@ -435,11 +446,11 @@ def async_embedding(gen_ai_endpoint, version, environment, application_name,
435
446
 
436
447
  with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
437
448
  response = await wrapped(*args, **kwargs)
438
-
449
+ response_dict = response_as_dict(response)
439
450
  try:
440
451
  # Calculate cost of the operation
441
452
  cost = get_embed_model_cost(kwargs.get("model", "text-embedding-ada-002"),
442
- pricing_info, response.usage.prompt_tokens)
453
+ pricing_info, response_dict.get('usage').get('prompt_tokens'))
443
454
 
444
455
  # Set Span attributes
445
456
  span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
@@ -457,14 +468,14 @@ def async_embedding(gen_ai_endpoint, version, environment, application_name,
457
468
  kwargs.get("model", "text-embedding-ada-002"))
458
469
  span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_EMBEDDING_FORMAT,
459
470
  kwargs.get("encoding_format", "float"))
460
- span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_EMBEDDING_DIMENSION,
461
- kwargs.get("dimensions", ""))
471
+ # span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_EMBEDDING_DIMENSION,
472
+ # kwargs.get("dimensions", "null"))
462
473
  span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_USER,
463
474
  kwargs.get("user", ""))
464
475
  span.set_attribute(SemanticConvetion.GEN_AI_USAGE_PROMPT_TOKENS,
465
- response.usage.prompt_tokens)
476
+ response_dict.get('usage').get('prompt_tokens'))
466
477
  span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
467
- response.usage.total_tokens)
478
+ response_dict.get('usage').get('total_tokens'))
468
479
  span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
469
480
  cost)
470
481
  if trace_content:
@@ -494,8 +505,10 @@ def async_embedding(gen_ai_endpoint, version, environment, application_name,
494
505
  }
495
506
 
496
507
  metrics["genai_requests"].add(1, attributes)
497
- metrics["genai_total_tokens"].add(response.usage.total_tokens, attributes)
498
- metrics["genai_prompt_tokens"].add(response.usage.prompt_tokens, attributes)
508
+ metrics["genai_total_tokens"].add(
509
+ response_dict.get('usage').get('total_tokens'), attributes)
510
+ metrics["genai_prompt_tokens"].add(
511
+ response_dict.get('usage').get('prompt_tokens'), attributes)
499
512
  metrics["genai_cost"].record(cost, attributes)
500
513
 
501
514
  # Return original response
@@ -548,13 +561,14 @@ def async_finetune(gen_ai_endpoint, version, environment, application_name,
548
561
  with tracer.start_as_current_span(gen_ai_endpoint, kind= SpanKind.CLIENT) as span:
549
562
  response = await wrapped(*args, **kwargs)
550
563
 
564
+ # Handling exception ensure observability without disrupting operation
551
565
  try:
552
566
  # Set Span attributes
553
567
  span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
554
568
  span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
555
569
  SemanticConvetion.GEN_AI_SYSTEM_OPENAI)
556
570
  span.set_attribute(SemanticConvetion.GEN_AI_TYPE,
557
- "fine_tuning")
571
+ SemanticConvetion.GEN_AI_TYPE_FINETUNING)
558
572
  span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,
559
573
  gen_ai_endpoint)
560
574
  span.set_attribute(SemanticConvetion.GEN_AI_ENVIRONMENT,
@@ -585,7 +599,22 @@ def async_finetune(gen_ai_endpoint, version, environment, application_name,
585
599
  span.set_status(Status(StatusCode.OK))
586
600
 
587
601
  if disable_metrics is False:
588
- metrics["genai_requests"].add(1)
602
+ attributes = {
603
+ TELEMETRY_SDK_NAME:
604
+ "openlit",
605
+ SemanticConvetion.GEN_AI_APPLICATION_NAME:
606
+ application_name,
607
+ SemanticConvetion.GEN_AI_SYSTEM:
608
+ SemanticConvetion.GEN_AI_SYSTEM_OPENAI,
609
+ SemanticConvetion.GEN_AI_ENVIRONMENT:
610
+ environment,
611
+ SemanticConvetion.GEN_AI_TYPE:
612
+ SemanticConvetion.GEN_AI_TYPE_FINETUNING,
613
+ SemanticConvetion.GEN_AI_REQUEST_MODEL:
614
+ kwargs.get("model", "gpt-3.5-turbo")
615
+ }
616
+
617
+ metrics["genai_requests"].add(1, attributes)
589
618
 
590
619
  # Return original response
591
620
  return response