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.
- openlit/__helpers.py +14 -0
- openlit/__init__.py +11 -3
- openlit/evals/utils.py +5 -2
- openlit/guard/utils.py +5 -1
- openlit/instrumentation/azure_ai_inference/async_azure_ai_inference.py +0 -1
- openlit/instrumentation/azure_ai_inference/azure_ai_inference.py +0 -1
- openlit/instrumentation/crewai/__init__.py +50 -0
- openlit/instrumentation/crewai/crewai.py +149 -0
- openlit/instrumentation/google_ai_studio/google_ai_studio.py +0 -1
- openlit/instrumentation/litellm/__init__.py +68 -0
- openlit/instrumentation/litellm/async_litellm.py +521 -0
- openlit/instrumentation/litellm/litellm.py +521 -0
- openlit/instrumentation/openai/async_openai.py +71 -42
- openlit/instrumentation/openai/openai.py +216 -162
- openlit/otel/metrics.py +6 -3
- openlit/otel/tracing.py +5 -2
- openlit/semcov/__init__.py +21 -0
- {openlit-1.29.2.dist-info → openlit-1.30.3.dist-info}/METADATA +12 -11
- {openlit-1.29.2.dist-info → openlit-1.30.3.dist-info}/RECORD +21 -16
- {openlit-1.29.2.dist-info → openlit-1.30.3.dist-info}/LICENSE +0 -0
- {openlit-1.29.2.dist-info → openlit-1.30.3.dist-info}/WHEEL +0 -0
@@ -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
|
10
|
-
|
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
|
-
|
50
|
-
**
|
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(
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
self._response_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
|
-
|
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,
|
304
|
-
|
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
|
-
|
319
|
+
response_dict.get('usage', {}).get('prompt_tokens', None))
|
308
320
|
span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COMPLETION_TOKENS,
|
309
|
-
|
321
|
+
response_dict.get('usage', {}).get('completion_tokens', None))
|
310
322
|
span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
|
311
|
-
|
323
|
+
response_dict.get('usage', {}).get('total_tokens', None))
|
312
324
|
span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_FINISH_REASON,
|
313
|
-
|
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:
|
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:
|
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,
|
348
|
-
|
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
|
-
|
368
|
+
response_dict.get('usage').get('prompt_tokens'))
|
358
369
|
span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COMPLETION_TOKENS,
|
359
|
-
|
370
|
+
response_dict.get('usage').get('completion_tokens'))
|
360
371
|
span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
|
361
|
-
|
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(
|
385
|
-
metrics["genai_completion_tokens"].add(
|
386
|
-
metrics["genai_prompt_tokens"].add(
|
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
|
-
|
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
|
-
|
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
|
-
|
476
|
+
response_dict.get('usage').get('prompt_tokens'))
|
466
477
|
span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
|
467
|
-
|
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(
|
498
|
-
|
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
|
-
|
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
|
-
|
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
|