openlit 1.30.3__py3-none-any.whl → 1.30.5__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.
@@ -12,6 +12,26 @@ from openlit.semcov import SemanticConvetion
12
12
  # Initialize logger for logging potential issues and operations
13
13
  logger = logging.getLogger(__name__)
14
14
 
15
+ def get_attribute_from_instance_or_kwargs(instance, attribute_name, default=-1):
16
+ """Return attribute from instance or kwargs"""
17
+ # Attempt to retrieve model_kwargs from the instance
18
+ model_kwargs = getattr(instance, 'model_kwargs', None)
19
+
20
+ # Check for attribute in model_kwargs if it exists
21
+ if model_kwargs and attribute_name in model_kwargs:
22
+ return model_kwargs[attribute_name]
23
+
24
+ # Attempt to get the attribute directly from the instance
25
+ try:
26
+ return getattr(instance, attribute_name)
27
+ except AttributeError:
28
+ # Special handling for 'model' attribute to consider 'model_id'
29
+ if attribute_name == 'model':
30
+ return getattr(instance, 'model_id', 'default_model_id')
31
+
32
+ # Default if the attribute isn't found in model_kwargs or the instance
33
+ return default
34
+
15
35
  def general_wrap(gen_ai_endpoint, version, environment, application_name,
16
36
  tracer, pricing_info, trace_content, metrics, disable_metrics):
17
37
  """
@@ -207,15 +227,18 @@ def allm(gen_ai_endpoint, version, environment, application_name,
207
227
  response = await wrapped(*args, **kwargs)
208
228
 
209
229
  try:
210
- prompt = args[0] or ""
211
- # input_tokens = general_tokens(prompt)
212
- # output_tokens = general_tokens(response)
230
+ if args:
231
+ prompt = str(args[0]) if args[0] is not None else ""
232
+ else:
233
+ prompt = ""
234
+ input_tokens = general_tokens(prompt)
235
+ output_tokens = general_tokens(response)
213
236
 
214
- # # Calculate cost of the operation
215
- # cost = get_chat_model_cost(
216
- # str(getattr(instance, 'model')),
217
- # pricing_info, input_tokens, output_tokens
218
- # )
237
+ # Calculate cost of the operation
238
+ cost = get_chat_model_cost(
239
+ str(get_attribute_from_instance_or_kwargs(instance, 'model')),
240
+ pricing_info, input_tokens, output_tokens
241
+ )
219
242
 
220
243
  span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
221
244
  span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
@@ -229,23 +252,23 @@ def allm(gen_ai_endpoint, version, environment, application_name,
229
252
  span.set_attribute(SemanticConvetion.GEN_AI_APPLICATION_NAME,
230
253
  application_name)
231
254
  span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL,
232
- str(getattr(instance, 'model')))
255
+ str(get_attribute_from_instance_or_kwargs(instance, 'model')))
233
256
  span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TEMPERATURE,
234
- str(getattr(instance, 'temperature')))
257
+ str(get_attribute_from_instance_or_kwargs(instance, 'temperature')))
235
258
  span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TOP_K,
236
- str(getattr(instance, 'top_k')))
259
+ str(get_attribute_from_instance_or_kwargs(instance, 'top_k')))
237
260
  span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TOP_P,
238
- str(getattr(instance, 'top_p')))
261
+ str(get_attribute_from_instance_or_kwargs(instance, 'top_p')))
239
262
  span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_IS_STREAM,
240
263
  False)
241
- # span.set_attribute(SemanticConvetion.GEN_AI_USAGE_PROMPT_TOKENS,
242
- # input_tokens)
243
- # span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COMPLETION_TOKENS,
244
- # output_tokens)
245
- # span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
246
- # input_tokens + output_tokens)
247
- # span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
248
- # cost)
264
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_PROMPT_TOKENS,
265
+ input_tokens)
266
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COMPLETION_TOKENS,
267
+ output_tokens)
268
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
269
+ input_tokens + output_tokens)
270
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
271
+ cost)
249
272
  if trace_content:
250
273
  span.add_event(
251
274
  name=SemanticConvetion.GEN_AI_CONTENT_PROMPT_EVENT,
@@ -262,29 +285,29 @@ def allm(gen_ai_endpoint, version, environment, application_name,
262
285
 
263
286
  span.set_status(Status(StatusCode.OK))
264
287
 
265
- # if disable_metrics is False:
266
- # attributes = {
267
- # TELEMETRY_SDK_NAME:
268
- # "openlit",
269
- # SemanticConvetion.GEN_AI_APPLICATION_NAME:
270
- # application_name,
271
- # SemanticConvetion.GEN_AI_SYSTEM:
272
- # SemanticConvetion.GEN_AI_SYSTEM_LANGCHAIN,
273
- # SemanticConvetion.GEN_AI_ENVIRONMENT:
274
- # environment,
275
- # SemanticConvetion.GEN_AI_TYPE:
276
- # SemanticConvetion.GEN_AI_TYPE_CHAT,
277
- # SemanticConvetion.GEN_AI_REQUEST_MODEL:
278
- # str(getattr(instance, 'model'))
279
- # }
280
-
281
- # metrics["genai_requests"].add(1, attributes)
282
- # metrics["genai_total_tokens"].add(
283
- # input_tokens + output_tokens, attributes
284
- # )
285
- # metrics["genai_completion_tokens"].add(output_tokens, attributes)
286
- # metrics["genai_prompt_tokens"].add(input_tokens, attributes)
287
- # metrics["genai_cost"].record(cost, attributes)
288
+ if disable_metrics is False:
289
+ attributes = {
290
+ TELEMETRY_SDK_NAME:
291
+ "openlit",
292
+ SemanticConvetion.GEN_AI_APPLICATION_NAME:
293
+ application_name,
294
+ SemanticConvetion.GEN_AI_SYSTEM:
295
+ SemanticConvetion.GEN_AI_SYSTEM_LANGCHAIN,
296
+ SemanticConvetion.GEN_AI_ENVIRONMENT:
297
+ environment,
298
+ SemanticConvetion.GEN_AI_TYPE:
299
+ SemanticConvetion.GEN_AI_TYPE_CHAT,
300
+ SemanticConvetion.GEN_AI_REQUEST_MODEL:
301
+ str(get_attribute_from_instance_or_kwargs(instance, 'model'))
302
+ }
303
+
304
+ metrics["genai_requests"].add(1, attributes)
305
+ metrics["genai_total_tokens"].add(
306
+ input_tokens + output_tokens, attributes
307
+ )
308
+ metrics["genai_completion_tokens"].add(output_tokens, attributes)
309
+ metrics["genai_prompt_tokens"].add(input_tokens, attributes)
310
+ metrics["genai_cost"].record(cost, attributes)
288
311
 
289
312
  # Return original response
290
313
  return response
@@ -344,15 +367,18 @@ def llm(gen_ai_endpoint, version, environment, application_name,
344
367
  response = wrapped(*args, **kwargs)
345
368
 
346
369
  try:
347
- prompt = args[0] or ""
348
- # input_tokens = general_tokens(prompt)
349
- # output_tokens = general_tokens(response)
370
+ if args:
371
+ prompt = str(args[0]) if args[0] is not None else ""
372
+ else:
373
+ prompt = ""
374
+ input_tokens = general_tokens(prompt)
375
+ output_tokens = general_tokens(response)
350
376
 
351
- # # Calculate cost of the operation
352
- # cost = get_chat_model_cost(
353
- # str(getattr(instance, 'model')),
354
- # pricing_info, input_tokens, output_tokens
355
- # )
377
+ # Calculate cost of the operation
378
+ cost = get_chat_model_cost(
379
+ str(get_attribute_from_instance_or_kwargs(instance, 'model')),
380
+ pricing_info, input_tokens, output_tokens
381
+ )
356
382
 
357
383
  span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
358
384
  span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
@@ -366,23 +392,23 @@ def llm(gen_ai_endpoint, version, environment, application_name,
366
392
  span.set_attribute(SemanticConvetion.GEN_AI_APPLICATION_NAME,
367
393
  application_name)
368
394
  span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL,
369
- str(getattr(instance, 'model')))
395
+ str(get_attribute_from_instance_or_kwargs(instance, 'model')))
370
396
  span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TEMPERATURE,
371
- str(getattr(instance, 'temperature')))
397
+ str(get_attribute_from_instance_or_kwargs(instance, 'temperature')))
372
398
  span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TOP_K,
373
- str(getattr(instance, 'top_k')))
399
+ str(get_attribute_from_instance_or_kwargs(instance, 'top_k')))
374
400
  span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TOP_P,
375
- str(getattr(instance, 'top_p')))
401
+ str(get_attribute_from_instance_or_kwargs(instance, 'top_p')))
376
402
  span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_IS_STREAM,
377
403
  False)
378
- # span.set_attribute(SemanticConvetion.GEN_AI_USAGE_PROMPT_TOKENS,
379
- # input_tokens)
380
- # span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COMPLETION_TOKENS,
381
- # output_tokens)
382
- # span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
383
- # input_tokens + output_tokens)
384
- # span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
385
- # cost)
404
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_PROMPT_TOKENS,
405
+ input_tokens)
406
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COMPLETION_TOKENS,
407
+ output_tokens)
408
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
409
+ input_tokens + output_tokens)
410
+ span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
411
+ cost)
386
412
  if trace_content:
387
413
  span.add_event(
388
414
  name=SemanticConvetion.GEN_AI_CONTENT_PROMPT_EVENT,
@@ -399,29 +425,29 @@ def llm(gen_ai_endpoint, version, environment, application_name,
399
425
 
400
426
  span.set_status(Status(StatusCode.OK))
401
427
 
402
- # if disable_metrics is False:
403
- # attributes = {
404
- # TELEMETRY_SDK_NAME:
405
- # "openlit",
406
- # SemanticConvetion.GEN_AI_APPLICATION_NAME:
407
- # application_name,
408
- # SemanticConvetion.GEN_AI_SYSTEM:
409
- # SemanticConvetion.GEN_AI_SYSTEM_LANGCHAIN,
410
- # SemanticConvetion.GEN_AI_ENVIRONMENT:
411
- # environment,
412
- # SemanticConvetion.GEN_AI_TYPE:
413
- # SemanticConvetion.GEN_AI_TYPE_CHAT,
414
- # SemanticConvetion.GEN_AI_REQUEST_MODEL:
415
- # str(getattr(instance, 'model'))
416
- # }
417
-
418
- # metrics["genai_requests"].add(1, attributes)
419
- # metrics["genai_total_tokens"].add(
420
- # input_tokens + output_tokens, attributes
421
- # )
422
- # metrics["genai_completion_tokens"].add(output_tokens, attributes)
423
- # metrics["genai_prompt_tokens"].add(input_tokens, attributes)
424
- # metrics["genai_cost"].record(cost, attributes)
428
+ if disable_metrics is False:
429
+ attributes = {
430
+ TELEMETRY_SDK_NAME:
431
+ "openlit",
432
+ SemanticConvetion.GEN_AI_APPLICATION_NAME:
433
+ application_name,
434
+ SemanticConvetion.GEN_AI_SYSTEM:
435
+ SemanticConvetion.GEN_AI_SYSTEM_LANGCHAIN,
436
+ SemanticConvetion.GEN_AI_ENVIRONMENT:
437
+ environment,
438
+ SemanticConvetion.GEN_AI_TYPE:
439
+ SemanticConvetion.GEN_AI_TYPE_CHAT,
440
+ SemanticConvetion.GEN_AI_REQUEST_MODEL:
441
+ str(get_attribute_from_instance_or_kwargs(instance, 'model'))
442
+ }
443
+
444
+ metrics["genai_requests"].add(1, attributes)
445
+ metrics["genai_total_tokens"].add(
446
+ input_tokens + output_tokens, attributes
447
+ )
448
+ metrics["genai_completion_tokens"].add(output_tokens, attributes)
449
+ metrics["genai_prompt_tokens"].add(input_tokens, attributes)
450
+ metrics["genai_cost"].record(cost, attributes)
425
451
 
426
452
  # Return original response
427
453
  return response
@@ -481,11 +507,12 @@ def chat(gen_ai_endpoint, version, environment, application_name,
481
507
  response = wrapped(*args, **kwargs)
482
508
 
483
509
  try:
484
- input_tokens = response.response_metadata.get("prompt_eval_count", 0)
485
- output_tokens = response.response_metadata.get("eval_count", 0)
510
+ token_usage = response.response_metadata.get("token_usage", {})
511
+ input_tokens = token_usage.get("prompt_tokens", 0)
512
+ output_tokens = token_usage.get("completion_tokens", 0)
513
+ model = response.response_metadata.get("model_name", "gpt-4")
486
514
 
487
515
  prompt = "" if isinstance(args[0], list) else args[0]
488
- model = getattr(instance, 'model_name', getattr(instance, 'model', 'gpt-4'))
489
516
 
490
517
  # Calculate cost of the operation
491
518
  cost = get_chat_model_cost(
@@ -620,11 +647,12 @@ def achat(gen_ai_endpoint, version, environment, application_name,
620
647
  response = await wrapped(*args, **kwargs)
621
648
 
622
649
  try:
623
- input_tokens = response.response_metadata.get("prompt_eval_count", 0)
624
- output_tokens = response.response_metadata.get("eval_count", 0)
650
+ token_usage = response.response_metadata.get("token_usage", {})
651
+ input_tokens = token_usage.get("prompt_tokens", 0)
652
+ output_tokens = token_usage.get("completion_tokens", 0)
653
+ model = response.response_metadata.get("model_name", "gpt-4")
625
654
 
626
655
  prompt = "" if isinstance(args[0], list) else args[0]
627
- model = getattr(instance, 'model_name', getattr(instance, 'model', 'gpt-4'))
628
656
  # Calculate cost of the operation
629
657
  cost = get_chat_model_cost(
630
658
  model,
@@ -67,7 +67,7 @@ def text_wrap(gen_ai_endpoint, version, environment, application_name,
67
67
  else:
68
68
  prompt = kwargs.get("args", "")
69
69
 
70
- prompt_tokens = general_tokens(prompt)
70
+ prompt_tokens = general_tokens(prompt[0])
71
71
 
72
72
  span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
73
73
  span.set_attribute(SemanticConvetion.GEN_AI_ENDPOINT,
@@ -106,14 +106,20 @@ def text_wrap(gen_ai_endpoint, version, environment, application_name,
106
106
  else:
107
107
  attribute_name = SemanticConvetion.GEN_AI_CONTENT_COMPLETION_EVENT
108
108
  if trace_content:
109
+ # pylint: disable=bare-except
110
+ try:
111
+ llm_response = completion.get('generated_text', '')
112
+ except:
113
+ llm_response = completion[i].get('generated_text', '')
114
+
109
115
  span.add_event(
110
116
  name=attribute_name,
111
117
  attributes={
112
118
  # pylint: disable=line-too-long
113
- SemanticConvetion.GEN_AI_CONTENT_COMPLETION: completion["generated_text"],
119
+ SemanticConvetion.GEN_AI_CONTENT_COMPLETION: llm_response,
114
120
  },
115
121
  )
116
- completion_tokens += general_tokens(completion["generated_text"])
122
+ completion_tokens += general_tokens(llm_response)
117
123
 
118
124
  i=i+1
119
125
  span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COMPLETION_TOKENS,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: openlit
3
- Version: 1.30.3
3
+ Version: 1.30.5
4
4
  Summary: OpenTelemetry-native Auto instrumentation library for monitoring LLM Applications and GPUs, facilitating the integration of observability into your GenAI-driven projects
5
5
  Home-page: https://github.com/openlit/openlit/tree/main/openlit/python
6
6
  Keywords: OpenTelemetry,otel,otlp,llm,tracing,openai,anthropic,claude,cohere,llm monitoring,observability,monitoring,gpt,Generative AI,chatGPT,gpu
@@ -43,7 +43,7 @@ openlit/instrumentation/groq/groq.py,sha256=m4gFPbYzjUUIgjXZ0Alu2Zy1HcO5takCFA2X
43
43
  openlit/instrumentation/haystack/__init__.py,sha256=QK6XxxZUHX8vMv2Crk7rNBOc64iOOBLhJGL_lPlAZ8s,1758
44
44
  openlit/instrumentation/haystack/haystack.py,sha256=oQIZiDhdp3gnJnhYQ1OouJMc9YT0pQ-_31cmNuopa68,3891
45
45
  openlit/instrumentation/langchain/__init__.py,sha256=0AI2Dnqw81IcJw3jM--gGkv_HRh2GtosOGJjvOpw7Zk,3431
46
- openlit/instrumentation/langchain/langchain.py,sha256=g3HDKPq498KitHuQxxfQzvRq9MKAZaR0jStQYTLx_-M,35592
46
+ openlit/instrumentation/langchain/langchain.py,sha256=jZgWBBWYHYSNnkf5wKyNFF_z9M9YxaZKGI_uyfvtMBU,36909
47
47
  openlit/instrumentation/litellm/__init__.py,sha256=Z-LsVHKJdPganHfJA_rWg7xAfQYkvLfpLdF-eckU4qY,2401
48
48
  openlit/instrumentation/litellm/async_litellm.py,sha256=1MKNZbvKaf1lFWbXi1MQy3qFNNeXawav34SDlOQ_H3w,27544
49
49
  openlit/instrumentation/litellm/litellm.py,sha256=4YqCQ4CEQ4sfDu7pTlnflL_AfUqYEQdJDTO7nHJ6noY,27450
@@ -68,7 +68,7 @@ openlit/instrumentation/qdrant/__init__.py,sha256=GMlZgRBKoQMgrL4cFbAKwytfdTHLzJ
68
68
  openlit/instrumentation/qdrant/async_qdrant.py,sha256=Xuyw2N75mRIjltrmY8wJes5DHal0Ku3A8VcUqfbsOl0,15071
69
69
  openlit/instrumentation/qdrant/qdrant.py,sha256=K0cvEUbNx0hnk8AbEheYPSHcCgjFC482IZyHF9-P_b8,15488
70
70
  openlit/instrumentation/transformers/__init__.py,sha256=4GBtjzcJU4XiPexIUYEqF3pNZMeQw4Gm5B-cyumaFjs,1468
71
- openlit/instrumentation/transformers/transformers.py,sha256=KNAT2ROjziW6OAP6Y0Ec4oS2T2jx9y2mzpBgR_e78bI,7625
71
+ openlit/instrumentation/transformers/transformers.py,sha256=MWEVkxHRWTHrpD85I1leksDIVtBiTtR5fQCO3Z62qb4,7875
72
72
  openlit/instrumentation/vertexai/__init__.py,sha256=N3E9HtzefD-zC0fvmfGYiDmSqssoavp_i59wfuYLyMw,6079
73
73
  openlit/instrumentation/vertexai/async_vertexai.py,sha256=8JwSwLPPA4lAatf4w_5kJ5_YZDLwl5yG8N59cTD-EZM,55198
74
74
  openlit/instrumentation/vertexai/vertexai.py,sha256=R6dDQfC3YFoZDygxU2fkflcMsqIv8AVoU3XOwWSvpwA,54951
@@ -77,7 +77,7 @@ openlit/instrumentation/vllm/vllm.py,sha256=lDzM7F5pgxvh8nKL0dcKB4TD0Mc9wXOWeXOs
77
77
  openlit/otel/metrics.py,sha256=y7SQDTyfLakMrz0V4DThN-WAeap7YZzyndeYGSP6nVg,4516
78
78
  openlit/otel/tracing.py,sha256=fG3vl-flSZ30whCi7rrG25PlkIhhr8PhnfJYCkZzCD0,3895
79
79
  openlit/semcov/__init__.py,sha256=_IjU498Sc0Rjz55y9S3dUelgRalmrzzBgFglPzOlIfk,9137
80
- openlit-1.30.3.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
81
- openlit-1.30.3.dist-info/METADATA,sha256=Ej7sFOJ8OV2V00Vx_Oki13Q4pyvqYQS7_inRYngrIx8,20915
82
- openlit-1.30.3.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
83
- openlit-1.30.3.dist-info/RECORD,,
80
+ openlit-1.30.5.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
81
+ openlit-1.30.5.dist-info/METADATA,sha256=XbOkItQMtarnp-R6j9ERRAjNQnnaQ6iTI05BScDY24k,20915
82
+ openlit-1.30.5.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
83
+ openlit-1.30.5.dist-info/RECORD,,