openlit 1.34.7__py3-none-any.whl → 1.34.10__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.
@@ -1,145 +1,55 @@
1
1
  """
2
- Module for monitoring Ollama API calls.
2
+ Module for monitoring ElevenLabs API calls.
3
3
  """
4
4
 
5
- import logging
6
5
  import time
7
- from opentelemetry.trace import SpanKind, Status, StatusCode
8
- from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
9
- from openlit.__helpers import (
10
- get_audio_model_cost,
11
- handle_exception,
12
- create_metrics_attributes,
13
- )
6
+ from opentelemetry.trace import SpanKind
7
+ from openlit.__helpers import handle_exception
8
+ from openlit.instrumentation.elevenlabs.utils import process_audio_response
14
9
  from openlit.semcov import SemanticConvention
15
10
 
16
- # Initialize logger for logging potential issues and operations
17
- logger = logging.getLogger(__name__)
18
-
19
11
  def generate(gen_ai_endpoint, version, environment, application_name,
20
- tracer, pricing_info, capture_message_content, metrics, disable_metrics):
12
+ tracer, pricing_info, capture_message_content, metrics, disable_metrics):
21
13
  """
22
- Generates a telemetry wrapper for creating speech audio to collect metrics.
23
-
24
- Args:
25
- version: Version of the monitoring package.
26
- environment: Deployment environment (e.g., production, staging).
27
- application_name: Name of the application using the ElevenLabs API.
28
- tracer: OpenTelemetry tracer for creating spans.
29
- pricing_info: Information used for calculating the cost of generating speech audio.
30
- capture_message_content: Flag indicating whether to trace the input text and generated audio.
31
-
32
- Returns:
33
- A function that wraps the speech audio creation method to add telemetry.
14
+ Generates a telemetry wrapper for GenAI function call
34
15
  """
35
16
 
36
17
  def wrapper(wrapped, instance, args, kwargs):
37
18
  """
38
- Wraps the 'generate' API call to add telemetry.
39
-
40
- This collects metrics such as execution time, cost, and handles errors
41
- gracefully, adding details to the trace for observability.
42
-
43
- Args:
44
- wrapped: The original 'generate' method to be wrapped.
45
- instance: The instance of the class where the original method is defined.
46
- args: Positional arguments for the 'generate' method.
47
- kwargs: Keyword arguments for the 'generate' method.
48
-
49
- Returns:
50
- The response from the original 'generate' method.
19
+ Wraps the GenAI function call.
51
20
  """
52
21
 
53
22
  server_address, server_port = "api.elevenlabs.io", 443
54
- request_model = kwargs.get('model', kwargs.get('model_id', 'eleven_multilingual_v2'))
23
+ request_model = kwargs.get("model", kwargs.get("model_id", "eleven_multilingual_v2"))
55
24
 
56
- span_name = f'{SemanticConvention.GEN_AI_OPERATION_TYPE_AUDIO} {request_model}'
25
+ span_name = f"{SemanticConvention.GEN_AI_OPERATION_TYPE_AUDIO} {request_model}"
57
26
 
58
- with tracer.start_as_current_span(span_name, kind= SpanKind.CLIENT) as span:
27
+ with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
59
28
  start_time = time.time()
60
29
  response = wrapped(*args, **kwargs)
61
- end_time = time.time()
62
30
 
63
31
  try:
64
- # Calculate cost of the operation
65
- cost = get_audio_model_cost(request_model,
66
- pricing_info, kwargs.get('text', ''))
67
-
68
- # Set Span attributes
69
- span.set_attribute(TELEMETRY_SDK_NAME, 'openlit')
70
- span.set_attribute(SemanticConvention.GEN_AI_OPERATION,
71
- SemanticConvention.GEN_AI_OPERATION_TYPE_AUDIO)
72
- span.set_attribute(SemanticConvention.GEN_AI_SYSTEM,
73
- SemanticConvention.GEN_AI_SYSTEM_ASSEMBLYAI)
74
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL,
75
- request_model)
76
- span.set_attribute(SemanticConvention.SERVER_ADDRESS,
77
- server_address)
78
- span.set_attribute(SemanticConvention.SERVER_PORT,
79
- server_port)
80
- span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_MODEL,
81
- request_model)
82
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL,
83
- request_model)
84
- span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE,
85
- 'audio')
86
-
87
- # Set Span attributes (Extras)
88
- if gen_ai_endpoint == 'elevenlabs.generate':
89
- if isinstance(kwargs.get('voice', 'Rachel'), str):
90
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_AUDIO_VOICE,
91
- kwargs.get('voice', 'Rachel'))
92
- else:
93
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_AUDIO_VOICE,
94
- kwargs.get('voice_id', ''))
95
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_AUDIO_RESPONSE_FORMAT,
96
- kwargs.get('output_format', 'mp3'))
97
- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_AUDIO_SETTINGS,
98
- str(kwargs.get('voice_settings', '')))
99
- span.set_attribute(DEPLOYMENT_ENVIRONMENT,
100
- environment)
101
- span.set_attribute(SERVICE_NAME,
102
- application_name)
103
- span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST,
104
- cost)
105
- span.set_attribute(SemanticConvention.GEN_AI_SDK_VERSION,
106
- version)
107
- if capture_message_content:
108
- span.add_event(
109
- name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,
110
- attributes={
111
- SemanticConvention.GEN_AI_CONTENT_PROMPT: str(kwargs.get('text', '')),
112
- },
113
- )
114
-
115
- span.set_status(Status(StatusCode.OK))
116
-
117
- if disable_metrics is False:
118
- attributes = create_metrics_attributes(
119
- service_name=application_name,
120
- deployment_environment=environment,
121
- operation=SemanticConvention.GEN_AI_OPERATION_TYPE_AUDIO,
122
- system=SemanticConvention.GEN_AI_SYSTEM_ELEVENLABS,
123
- request_model=request_model,
124
- server_address=server_address,
125
- server_port=server_port,
126
- response_model=request_model,
127
- )
128
-
129
- metrics['genai_client_operation_duration'].record(
130
- end_time - start_time, attributes
131
- )
132
- metrics['genai_requests'].add(1, attributes)
133
- metrics['genai_cost'].record(cost, attributes)
134
-
135
- # Return original response
136
- return response
32
+ response = process_audio_response(
33
+ response=response,
34
+ gen_ai_endpoint=gen_ai_endpoint,
35
+ pricing_info=pricing_info,
36
+ server_port=server_port,
37
+ server_address=server_address,
38
+ environment=environment,
39
+ application_name=application_name,
40
+ metrics=metrics,
41
+ start_time=start_time,
42
+ span=span,
43
+ args=args,
44
+ kwargs=kwargs,
45
+ capture_message_content=capture_message_content,
46
+ disable_metrics=disable_metrics,
47
+ version=version
48
+ )
137
49
 
138
50
  except Exception as e:
139
51
  handle_exception(span, e)
140
- logger.error('Error in trace creation: %s', e)
141
52
 
142
- # Return original response
143
- return response
53
+ return response
144
54
 
145
55
  return wrapper
@@ -0,0 +1,133 @@
1
+ """
2
+ ElevenLabs OpenTelemetry instrumentation utility functions
3
+ """
4
+ import time
5
+
6
+ from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
7
+ from opentelemetry.trace import Status, StatusCode
8
+
9
+ from openlit.__helpers import (
10
+ get_audio_model_cost,
11
+ create_metrics_attributes,
12
+ )
13
+ from openlit.semcov import SemanticConvention
14
+
15
+ def format_content(text):
16
+ """
17
+ Process text input to extract content.
18
+ """
19
+ return str(text) if text else ""
20
+
21
+ def common_span_attributes(scope, gen_ai_operation, gen_ai_system, server_address, server_port,
22
+ request_model, response_model, environment, application_name, is_stream, tbt, ttft, version):
23
+ """
24
+ Set common span attributes for both chat and RAG operations.
25
+ """
26
+
27
+ scope._span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
28
+ scope._span.set_attribute(SemanticConvention.GEN_AI_OPERATION, gen_ai_operation)
29
+ scope._span.set_attribute(SemanticConvention.GEN_AI_SYSTEM, gen_ai_system)
30
+ scope._span.set_attribute(SemanticConvention.SERVER_ADDRESS, server_address)
31
+ scope._span.set_attribute(SemanticConvention.SERVER_PORT, server_port)
32
+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL, request_model)
33
+ scope._span.set_attribute(SemanticConvention.GEN_AI_RESPONSE_MODEL, scope._response_model)
34
+ scope._span.set_attribute(DEPLOYMENT_ENVIRONMENT, environment)
35
+ scope._span.set_attribute(SERVICE_NAME, application_name)
36
+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_IS_STREAM, is_stream)
37
+ scope._span.set_attribute(SemanticConvention.GEN_AI_SERVER_TBT, scope._tbt)
38
+ scope._span.set_attribute(SemanticConvention.GEN_AI_SERVER_TTFT, scope._ttft)
39
+ scope._span.set_attribute(SemanticConvention.GEN_AI_SDK_VERSION, version)
40
+
41
+ def record_audio_metrics(metrics, gen_ai_operation, gen_ai_system, server_address, server_port,
42
+ request_model, response_model, environment, application_name, start_time, end_time, cost):
43
+ """
44
+ Record audio generation metrics for the operation.
45
+ """
46
+
47
+ attributes = create_metrics_attributes(
48
+ operation=gen_ai_operation,
49
+ system=gen_ai_system,
50
+ server_address=server_address,
51
+ server_port=server_port,
52
+ request_model=request_model,
53
+ response_model=response_model,
54
+ service_name=application_name,
55
+ deployment_environment=environment,
56
+ )
57
+ metrics["genai_client_operation_duration"].record(end_time - start_time, attributes)
58
+ metrics["genai_requests"].add(1, attributes)
59
+ metrics["genai_cost"].record(cost, attributes)
60
+
61
+ def common_audio_logic(scope, gen_ai_endpoint, pricing_info, environment, application_name,
62
+ metrics, capture_message_content, disable_metrics, version):
63
+ """
64
+ Process audio generation request and generate Telemetry
65
+ """
66
+
67
+ text = format_content(scope._kwargs.get("text", ""))
68
+ request_model = scope._kwargs.get("model", scope._kwargs.get("model_id", "eleven_multilingual_v2"))
69
+ is_stream = False # ElevenLabs audio generation is not streaming
70
+
71
+ cost = get_audio_model_cost(request_model, pricing_info, text)
72
+
73
+ # Common Span Attributes
74
+ common_span_attributes(scope,
75
+ SemanticConvention.GEN_AI_OPERATION_TYPE_AUDIO, SemanticConvention.GEN_AI_SYSTEM_ELEVENLABS,
76
+ scope._server_address, scope._server_port, request_model, request_model,
77
+ environment, application_name, is_stream, scope._tbt, scope._ttft, version)
78
+
79
+ # Span Attributes for Cost and Tokens
80
+ scope._span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)
81
+
82
+ # Span Attributes for Response parameters
83
+ scope._span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_TYPE, scope._kwargs.get("output_format", "mp3_44100_128"))
84
+
85
+ # Audio-specific span attributes
86
+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_AUDIO_VOICE, scope._kwargs.get("voice_id", ""))
87
+ scope._span.set_attribute(SemanticConvention.GEN_AI_REQUEST_AUDIO_SETTINGS, str(scope._kwargs.get("voice_settings", "")))
88
+
89
+ # Span Attributes for Content
90
+ if capture_message_content:
91
+ scope._span.set_attribute(SemanticConvention.GEN_AI_CONTENT_PROMPT, text)
92
+
93
+ # To be removed once the change to span_attributes (from span events) is complete
94
+ scope._span.add_event(
95
+ name=SemanticConvention.GEN_AI_CONTENT_PROMPT_EVENT,
96
+ attributes={
97
+ SemanticConvention.GEN_AI_CONTENT_PROMPT: text,
98
+ },
99
+ )
100
+
101
+ scope._span.set_status(Status(StatusCode.OK))
102
+
103
+ # Metrics
104
+ if not disable_metrics:
105
+ record_audio_metrics(metrics, SemanticConvention.GEN_AI_OPERATION_TYPE_AUDIO, SemanticConvention.GEN_AI_SYSTEM_ELEVENLABS,
106
+ scope._server_address, scope._server_port, request_model, request_model, environment,
107
+ application_name, scope._start_time, scope._end_time, cost)
108
+
109
+ def process_audio_response(response, gen_ai_endpoint, pricing_info, server_port, server_address,
110
+ environment, application_name, metrics, start_time, span, args, kwargs, capture_message_content=False,
111
+ disable_metrics=False, version="1.0.0"):
112
+ """
113
+ Process audio generation request and generate Telemetry
114
+ """
115
+
116
+ scope = type("GenericScope", (), {})()
117
+
118
+ scope._start_time = start_time
119
+ scope._end_time = time.time()
120
+ scope._span = span
121
+ scope._server_address, scope._server_port = server_address, server_port
122
+ scope._kwargs = kwargs
123
+ scope._args = args
124
+
125
+ # Initialize streaming and timing values for ElevenLabs audio generation
126
+ scope._response_model = kwargs.get("model", kwargs.get("model_id", "eleven_multilingual_v2"))
127
+ scope._tbt = 0.0
128
+ scope._ttft = scope._end_time - scope._start_time
129
+
130
+ common_audio_logic(scope, gen_ai_endpoint, pricing_info, environment, application_name,
131
+ metrics, capture_message_content, disable_metrics, version)
132
+
133
+ return response
@@ -1,4 +1,3 @@
1
- # pylint: disable=useless-return, bad-staticmethod-argument, disable=duplicate-code
2
1
  """Initializer of Auto Instrumentation of GPT4All Functions"""
3
2
 
4
3
  from typing import Collection
@@ -14,15 +13,15 @@ _instruments = ("gpt4all >= 2.6.0",)
14
13
 
15
14
  class GPT4AllInstrumentor(BaseInstrumentor):
16
15
  """
17
- An instrumentor for GPT4All's client library.
16
+ An instrumentor for GPT4All client library.
18
17
  """
19
18
 
20
19
  def instrumentation_dependencies(self) -> Collection[str]:
21
20
  return _instruments
22
21
 
23
22
  def _instrument(self, **kwargs):
24
- application_name = kwargs.get("application_name", "default_application")
25
- environment = kwargs.get("environment", "default_environment")
23
+ application_name = kwargs.get("application_name", "default")
24
+ environment = kwargs.get("environment", "default")
26
25
  tracer = kwargs.get("tracer")
27
26
  metrics = kwargs.get("metrics_dict")
28
27
  pricing_info = kwargs.get("pricing_info", {})
@@ -46,7 +45,5 @@ class GPT4AllInstrumentor(BaseInstrumentor):
46
45
  tracer, pricing_info, capture_message_content, metrics, disable_metrics),
47
46
  )
48
47
 
49
-
50
48
  def _uninstrument(self, **kwargs):
51
- # Proper uninstrumentation logic to revert patched methods
52
49
  pass