openlit 1.33.15__py3-none-any.whl → 1.33.16__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/__init__.py +3 -0
- openlit/instrumentation/openai/__init__.py +18 -2
- openlit/instrumentation/openai/async_openai.py +405 -0
- openlit/instrumentation/openai/openai.py +405 -0
- openlit/instrumentation/openai_agents/__init__.py +42 -0
- openlit/instrumentation/openai_agents/openai_agents.py +65 -0
- {openlit-1.33.15.dist-info → openlit-1.33.16.dist-info}/METADATA +1 -1
- {openlit-1.33.15.dist-info → openlit-1.33.16.dist-info}/RECORD +10 -8
- {openlit-1.33.15.dist-info → openlit-1.33.16.dist-info}/LICENSE +0 -0
- {openlit-1.33.15.dist-info → openlit-1.33.16.dist-info}/WHEEL +0 -0
openlit/__init__.py
CHANGED
@@ -65,6 +65,7 @@ from openlit.instrumentation.controlflow import ControlFlowInstrumentor
|
|
65
65
|
from openlit.instrumentation.crawl4ai import Crawl4AIInstrumentor
|
66
66
|
from openlit.instrumentation.firecrawl import FireCrawlInstrumentor
|
67
67
|
from openlit.instrumentation.letta import LettaInstrumentor
|
68
|
+
from openlit.instrumentation.openai_agents import OpenAIAgentsInstrumentor
|
68
69
|
from openlit.instrumentation.gpu import GPUInstrumentor
|
69
70
|
import openlit.guard
|
70
71
|
import openlit.evals
|
@@ -293,6 +294,7 @@ def init(
|
|
293
294
|
"firecrawl": "firecrawl",
|
294
295
|
"letta": "letta",
|
295
296
|
"together": "together",
|
297
|
+
"openai-agents": "agents"
|
296
298
|
}
|
297
299
|
|
298
300
|
invalid_instrumentors = [
|
@@ -411,6 +413,7 @@ def init(
|
|
411
413
|
"firecrawl": FireCrawlInstrumentor(),
|
412
414
|
"letta": LettaInstrumentor(),
|
413
415
|
"together": TogetherInstrumentor(),
|
416
|
+
"openai-agents": OpenAIAgentsInstrumentor(),
|
414
417
|
}
|
415
418
|
|
416
419
|
# Initialize and instrument only the enabled instrumentors
|
@@ -5,11 +5,11 @@ import importlib.metadata
|
|
5
5
|
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
|
6
6
|
from wrapt import wrap_function_wrapper
|
7
7
|
|
8
|
-
from openlit.instrumentation.openai.openai import chat_completions, embedding
|
8
|
+
from openlit.instrumentation.openai.openai import chat_completions, embedding, responses
|
9
9
|
from openlit.instrumentation.openai.openai import image_generate, image_variatons, audio_create
|
10
10
|
from openlit.instrumentation.openai.async_openai import async_chat_completions, async_embedding
|
11
11
|
from openlit.instrumentation.openai.async_openai import async_image_generate, async_image_variatons
|
12
|
-
from openlit.instrumentation.openai.async_openai import async_audio_create
|
12
|
+
from openlit.instrumentation.openai.async_openai import async_audio_create, async_responses
|
13
13
|
|
14
14
|
_instruments = ("openai >= 1.1.1",)
|
15
15
|
|
@@ -45,6 +45,22 @@ class OpenAIInstrumentor(BaseInstrumentor):
|
|
45
45
|
metrics, disable_metrics),
|
46
46
|
)
|
47
47
|
|
48
|
+
wrap_function_wrapper(
|
49
|
+
"openai.resources.responses.responses",
|
50
|
+
"Responses.create",
|
51
|
+
responses(version, environment, application_name,
|
52
|
+
tracer, pricing_info, capture_message_content,
|
53
|
+
metrics, disable_metrics),
|
54
|
+
)
|
55
|
+
|
56
|
+
wrap_function_wrapper(
|
57
|
+
"openai.resources.responses.responses",
|
58
|
+
"AsyncResponses.create",
|
59
|
+
async_responses(version, environment, application_name,
|
60
|
+
tracer, pricing_info, capture_message_content,
|
61
|
+
metrics, disable_metrics),
|
62
|
+
)
|
63
|
+
|
48
64
|
wrap_function_wrapper(
|
49
65
|
"openai.resources.images",
|
50
66
|
"Images.generate",
|
@@ -13,6 +13,8 @@ from openlit.__helpers import (
|
|
13
13
|
get_image_model_cost,
|
14
14
|
general_tokens,
|
15
15
|
handle_exception,
|
16
|
+
extract_and_format_input,
|
17
|
+
concatenate_all_contents,
|
16
18
|
response_as_dict,
|
17
19
|
calculate_ttft,
|
18
20
|
calculate_tbt,
|
@@ -24,6 +26,409 @@ from openlit.semcov import SemanticConvetion
|
|
24
26
|
# Initialize logger for logging potential issues and operations
|
25
27
|
logger = logging.getLogger(__name__)
|
26
28
|
|
29
|
+
def async_responses(version, environment, application_name,
|
30
|
+
tracer, pricing_info, capture_message_content, metrics, disable_metrics):
|
31
|
+
"""
|
32
|
+
Generates a telemetry wrapper for chat completions to collect metrics.
|
33
|
+
|
34
|
+
Args:
|
35
|
+
version: Version of the monitoring package.
|
36
|
+
environment: Deployment environment (e.g., production, staging).
|
37
|
+
application_name: Name of the application using the OpenAI API.
|
38
|
+
tracer: OpenTelemetry tracer for creating spans.
|
39
|
+
pricing_info: Information used for calculating the cost of OpenAI usage.
|
40
|
+
capture_message_content: Flag indicating whether to trace the actual content.
|
41
|
+
|
42
|
+
Returns:
|
43
|
+
A function that wraps the chat completions method to add telemetry.
|
44
|
+
"""
|
45
|
+
|
46
|
+
class TracedAsyncStream:
|
47
|
+
"""
|
48
|
+
Wrapper for streaming responses to collect metrics and trace data.
|
49
|
+
Wraps the response to collect message IDs and aggregated response.
|
50
|
+
|
51
|
+
This class implements the '__aiter__' and '__anext__' methods that
|
52
|
+
handle asynchronous streaming responses.
|
53
|
+
|
54
|
+
This class also implements '__aenter__' and '__aexit__' methods that
|
55
|
+
handle asynchronous context management protocol.
|
56
|
+
"""
|
57
|
+
def __init__(
|
58
|
+
self,
|
59
|
+
wrapped,
|
60
|
+
span,
|
61
|
+
kwargs,
|
62
|
+
server_address,
|
63
|
+
server_port,
|
64
|
+
**args,
|
65
|
+
):
|
66
|
+
self.__wrapped__ = wrapped
|
67
|
+
self._span = span
|
68
|
+
# Placeholder for aggregating streaming response
|
69
|
+
self._llmresponse = ""
|
70
|
+
self._response_id = ""
|
71
|
+
self._response_model = ""
|
72
|
+
self._finish_reason = ""
|
73
|
+
self._input_tokens = ""
|
74
|
+
self._output_tokens = ""
|
75
|
+
|
76
|
+
self._args = args
|
77
|
+
self._kwargs = kwargs
|
78
|
+
self._start_time = time.time()
|
79
|
+
self._end_time = None
|
80
|
+
self._timestamps = []
|
81
|
+
self._ttft = 0
|
82
|
+
self._tbt = 0
|
83
|
+
self._server_address = server_address
|
84
|
+
self._server_port = server_port
|
85
|
+
|
86
|
+
async def __aenter__(self):
|
87
|
+
await self.__wrapped__.__aenter__()
|
88
|
+
return self
|
89
|
+
|
90
|
+
async def __aexit__(self, exc_type, exc_value, traceback):
|
91
|
+
await self.__wrapped__.__aexit__(exc_type, exc_value, traceback)
|
92
|
+
|
93
|
+
def __aiter__(self):
|
94
|
+
return self
|
95
|
+
|
96
|
+
async def __getattr__(self, name):
|
97
|
+
"""Delegate attribute access to the wrapped object."""
|
98
|
+
return getattr(await self.__wrapped__, name)
|
99
|
+
|
100
|
+
async def __anext__(self):
|
101
|
+
try:
|
102
|
+
chunk = await self.__wrapped__.__anext__()
|
103
|
+
end_time = time.time()
|
104
|
+
# Record the timestamp for the current chunk
|
105
|
+
self._timestamps.append(end_time)
|
106
|
+
|
107
|
+
if len(self._timestamps) == 1:
|
108
|
+
# Calculate time to first chunk
|
109
|
+
self._ttft = calculate_ttft(self._timestamps, self._start_time)
|
110
|
+
|
111
|
+
chunked = response_as_dict(chunk)
|
112
|
+
# Collect message IDs and aggregated response from events
|
113
|
+
if chunked.get('type') == "response.output_text.delta":
|
114
|
+
self._llmresponse += chunked.get('delta')
|
115
|
+
if chunked.get('type') == "response.completed":
|
116
|
+
self._response_id = chunked.get('response').get('id')
|
117
|
+
self._response_model = chunked.get('response').get('model')
|
118
|
+
self._finish_reason = chunked.get('response').get('status')
|
119
|
+
self._input_tokens = chunked.get('response').get('usage').get('input_tokens')
|
120
|
+
self._output_tokens = chunked.get('response').get('usage').get('output_tokens')
|
121
|
+
return chunk
|
122
|
+
except StopAsyncIteration:
|
123
|
+
# Handling exception ensure observability without disrupting operation
|
124
|
+
try:
|
125
|
+
self._end_time = time.time()
|
126
|
+
if len(self._timestamps) > 1:
|
127
|
+
self._tbt = calculate_tbt(self._timestamps)
|
128
|
+
|
129
|
+
try:
|
130
|
+
formatted_messages = extract_and_format_input(self._kwargs.get('input', ''))
|
131
|
+
prompt = concatenate_all_contents(formatted_messages)
|
132
|
+
except:
|
133
|
+
prompt = self._kwargs.get('input', '')
|
134
|
+
|
135
|
+
request_model = self._kwargs.get("model", "gpt-4o")
|
136
|
+
|
137
|
+
# Calculate cost of the operation
|
138
|
+
cost = get_chat_model_cost(request_model,
|
139
|
+
pricing_info, self._input_tokens,
|
140
|
+
self._output_tokens)
|
141
|
+
|
142
|
+
# Set Span attributes (OTel Semconv)
|
143
|
+
self._span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
|
144
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_OPERATION,
|
145
|
+
SemanticConvetion.GEN_AI_OPERATION_TYPE_CHAT)
|
146
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
|
147
|
+
SemanticConvetion.GEN_AI_SYSTEM_OPENAI)
|
148
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL,
|
149
|
+
request_model)
|
150
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_SEED,
|
151
|
+
self._kwargs.get("seed", ""))
|
152
|
+
self._span.set_attribute(SemanticConvetion.SERVER_PORT,
|
153
|
+
self._server_port)
|
154
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MAX_TOKENS,
|
155
|
+
self._kwargs.get("max_output_tokens", -1))
|
156
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_STOP_SEQUENCES,
|
157
|
+
self._kwargs.get("stop", []))
|
158
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TEMPERATURE,
|
159
|
+
self._kwargs.get("temperature", 1.0))
|
160
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TOP_P,
|
161
|
+
self._kwargs.get("top_p", 1.0))
|
162
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_FINISH_REASON,
|
163
|
+
[self._finish_reason])
|
164
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_ID,
|
165
|
+
self._response_id)
|
166
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_MODEL,
|
167
|
+
self._response_model)
|
168
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_USAGE_INPUT_TOKENS,
|
169
|
+
self._input_tokens)
|
170
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_USAGE_OUTPUT_TOKENS,
|
171
|
+
self._output_tokens)
|
172
|
+
self._span.set_attribute(SemanticConvetion.SERVER_ADDRESS,
|
173
|
+
self._server_address)
|
174
|
+
if isinstance(self._llmresponse, str):
|
175
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_OUTPUT_TYPE,
|
176
|
+
"text")
|
177
|
+
else:
|
178
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_OUTPUT_TYPE,
|
179
|
+
"json")
|
180
|
+
|
181
|
+
# Set Span attributes (Extra)
|
182
|
+
self._span.set_attribute(DEPLOYMENT_ENVIRONMENT,
|
183
|
+
environment)
|
184
|
+
self._span.set_attribute(SERVICE_NAME,
|
185
|
+
application_name)
|
186
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_USER,
|
187
|
+
self._kwargs.get("user", ""))
|
188
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_IS_STREAM,
|
189
|
+
True)
|
190
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
|
191
|
+
self._input_tokens + self._output_tokens)
|
192
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
|
193
|
+
cost)
|
194
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_SERVER_TBT,
|
195
|
+
self._tbt)
|
196
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_SERVER_TTFT,
|
197
|
+
self._ttft)
|
198
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_SDK_VERSION,
|
199
|
+
version)
|
200
|
+
|
201
|
+
if capture_message_content:
|
202
|
+
self._span.add_event(
|
203
|
+
name=SemanticConvetion.GEN_AI_CONTENT_PROMPT_EVENT,
|
204
|
+
attributes={
|
205
|
+
SemanticConvetion.GEN_AI_CONTENT_PROMPT: prompt,
|
206
|
+
},
|
207
|
+
)
|
208
|
+
self._span.add_event(
|
209
|
+
name=SemanticConvetion.GEN_AI_CONTENT_COMPLETION_EVENT,
|
210
|
+
attributes={
|
211
|
+
SemanticConvetion.GEN_AI_CONTENT_COMPLETION: self._llmresponse,
|
212
|
+
},
|
213
|
+
)
|
214
|
+
self._span.set_status(Status(StatusCode.OK))
|
215
|
+
|
216
|
+
if disable_metrics is False:
|
217
|
+
attributes = create_metrics_attributes(
|
218
|
+
service_name=application_name,
|
219
|
+
deployment_environment=environment,
|
220
|
+
operation=SemanticConvetion.GEN_AI_OPERATION_TYPE_CHAT,
|
221
|
+
system=SemanticConvetion.GEN_AI_SYSTEM_OPENAI,
|
222
|
+
request_model=request_model,
|
223
|
+
server_address=self._server_address,
|
224
|
+
server_port=self._server_port,
|
225
|
+
response_model=self._response_model,
|
226
|
+
)
|
227
|
+
|
228
|
+
metrics["genai_client_usage_tokens"].record(
|
229
|
+
self._input_tokens + self._output_tokens, attributes
|
230
|
+
)
|
231
|
+
metrics["genai_client_operation_duration"].record(
|
232
|
+
self._end_time - self._start_time, attributes
|
233
|
+
)
|
234
|
+
metrics["genai_server_tbt"].record(
|
235
|
+
self._tbt, attributes
|
236
|
+
)
|
237
|
+
metrics["genai_server_ttft"].record(
|
238
|
+
self._ttft, attributes
|
239
|
+
)
|
240
|
+
metrics["genai_requests"].add(1, attributes)
|
241
|
+
metrics["genai_completion_tokens"].add(self._output_tokens, attributes)
|
242
|
+
metrics["genai_prompt_tokens"].add(self._input_tokens, attributes)
|
243
|
+
metrics["genai_cost"].record(cost, attributes)
|
244
|
+
|
245
|
+
except Exception as e:
|
246
|
+
handle_exception(self._span, e)
|
247
|
+
logger.error("Error in trace creation: %s", e)
|
248
|
+
finally:
|
249
|
+
self._span.end()
|
250
|
+
raise
|
251
|
+
|
252
|
+
async def wrapper(wrapped, instance, args, kwargs):
|
253
|
+
"""
|
254
|
+
Wraps the 'chat.completions' API call to add telemetry.
|
255
|
+
|
256
|
+
This collects metrics such as execution time, cost, and token usage, and handles errors
|
257
|
+
gracefully, adding details to the trace for observability.
|
258
|
+
|
259
|
+
Args:
|
260
|
+
wrapped: The original 'chat.completions' method to be wrapped.
|
261
|
+
instance: The instance of the class where the original method is defined.
|
262
|
+
args: Positional arguments for the 'chat.completions' method.
|
263
|
+
kwargs: Keyword arguments for the 'chat.completions' method.
|
264
|
+
|
265
|
+
Returns:
|
266
|
+
The response from the original 'chat.completions' method.
|
267
|
+
"""
|
268
|
+
|
269
|
+
# Check if streaming is enabled for the API call
|
270
|
+
streaming = kwargs.get("stream", False)
|
271
|
+
server_address, server_port = set_server_address_and_port(instance, "api.openai.com", 443)
|
272
|
+
request_model = kwargs.get("model", "gpt-4o")
|
273
|
+
|
274
|
+
span_name = f"{SemanticConvetion.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
|
275
|
+
|
276
|
+
# pylint: disable=no-else-return
|
277
|
+
if streaming:
|
278
|
+
# Special handling for streaming response to accommodate the nature of data flow
|
279
|
+
awaited_wrapped = await wrapped(*args, **kwargs)
|
280
|
+
span = tracer.start_span(span_name, kind=SpanKind.CLIENT)
|
281
|
+
|
282
|
+
return TracedAsyncStream(awaited_wrapped, span, kwargs, server_address, server_port)
|
283
|
+
|
284
|
+
# Handling for non-streaming responses
|
285
|
+
else:
|
286
|
+
with tracer.start_as_current_span(span_name, kind= SpanKind.CLIENT) as span:
|
287
|
+
start_time = time.time()
|
288
|
+
response = await wrapped(*args, **kwargs)
|
289
|
+
end_time = time.time()
|
290
|
+
|
291
|
+
response_dict = response_as_dict(response)
|
292
|
+
|
293
|
+
try:
|
294
|
+
try:
|
295
|
+
formatted_messages = extract_and_format_input(kwargs.get('input', ''))
|
296
|
+
prompt = concatenate_all_contents(formatted_messages)
|
297
|
+
except:
|
298
|
+
prompt = kwargs.get('input', '')
|
299
|
+
|
300
|
+
input_tokens = response_dict.get('usage').get('input_tokens')
|
301
|
+
output_tokens = response_dict.get('usage').get('output_tokens')
|
302
|
+
|
303
|
+
# Calculate cost of the operation
|
304
|
+
cost = get_chat_model_cost(request_model,
|
305
|
+
pricing_info, input_tokens,
|
306
|
+
output_tokens)
|
307
|
+
|
308
|
+
# Set base span attribues (OTel Semconv)
|
309
|
+
span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
|
310
|
+
span.set_attribute(SemanticConvetion.GEN_AI_OPERATION,
|
311
|
+
SemanticConvetion.GEN_AI_OPERATION_TYPE_CHAT)
|
312
|
+
span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
|
313
|
+
SemanticConvetion.GEN_AI_SYSTEM_OPENAI)
|
314
|
+
span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL,
|
315
|
+
request_model)
|
316
|
+
span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_SEED,
|
317
|
+
kwargs.get("seed", ""))
|
318
|
+
span.set_attribute(SemanticConvetion.SERVER_PORT,
|
319
|
+
server_port)
|
320
|
+
span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MAX_TOKENS,
|
321
|
+
kwargs.get("max_output_tokens", -1))
|
322
|
+
span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_STOP_SEQUENCES,
|
323
|
+
kwargs.get("stop", []))
|
324
|
+
span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TEMPERATURE,
|
325
|
+
str(response_dict.get("temperature", 1.0)))
|
326
|
+
span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TOP_P,
|
327
|
+
str(response_dict.get("top_p", 1.0)))
|
328
|
+
span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_ID,
|
329
|
+
response_dict.get("id"))
|
330
|
+
span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_MODEL,
|
331
|
+
response_dict.get('model'))
|
332
|
+
span.set_attribute(SemanticConvetion.GEN_AI_USAGE_INPUT_TOKENS,
|
333
|
+
input_tokens)
|
334
|
+
span.set_attribute(SemanticConvetion.GEN_AI_USAGE_OUTPUT_TOKENS,
|
335
|
+
output_tokens)
|
336
|
+
span.set_attribute(SemanticConvetion.SERVER_ADDRESS,
|
337
|
+
server_address)
|
338
|
+
|
339
|
+
# Set base span attribues (Extras)
|
340
|
+
span.set_attribute(DEPLOYMENT_ENVIRONMENT,
|
341
|
+
environment)
|
342
|
+
span.set_attribute(SERVICE_NAME,
|
343
|
+
application_name)
|
344
|
+
span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_USER,
|
345
|
+
kwargs.get("user", ""))
|
346
|
+
span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_IS_STREAM,
|
347
|
+
False)
|
348
|
+
span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
|
349
|
+
input_tokens + output_tokens)
|
350
|
+
span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
|
351
|
+
cost)
|
352
|
+
span.set_attribute(SemanticConvetion.GEN_AI_SERVER_TTFT,
|
353
|
+
end_time - start_time)
|
354
|
+
span.set_attribute(SemanticConvetion.GEN_AI_SDK_VERSION,
|
355
|
+
version)
|
356
|
+
|
357
|
+
if capture_message_content:
|
358
|
+
span.add_event(
|
359
|
+
name=SemanticConvetion.GEN_AI_CONTENT_PROMPT_EVENT,
|
360
|
+
attributes={
|
361
|
+
SemanticConvetion.GEN_AI_CONTENT_PROMPT: prompt,
|
362
|
+
},
|
363
|
+
)
|
364
|
+
|
365
|
+
for i in range(kwargs.get('n',1)):
|
366
|
+
span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_FINISH_REASON,
|
367
|
+
[response_dict.get('status')])
|
368
|
+
try:
|
369
|
+
llm_response = str(response_dict.get('output')[i].get('content')[0].get('text',''))
|
370
|
+
except:
|
371
|
+
llm_response = ''
|
372
|
+
|
373
|
+
if capture_message_content:
|
374
|
+
span.add_event(
|
375
|
+
name=SemanticConvetion.GEN_AI_CONTENT_COMPLETION_EVENT,
|
376
|
+
attributes={
|
377
|
+
# pylint: disable=line-too-long
|
378
|
+
SemanticConvetion.GEN_AI_CONTENT_COMPLETION: llm_response,
|
379
|
+
},
|
380
|
+
)
|
381
|
+
if kwargs.get('tools'):
|
382
|
+
span.set_attribute(SemanticConvetion.GEN_AI_TOOL_CALLS,
|
383
|
+
str(response_dict.get('tools')))
|
384
|
+
|
385
|
+
if isinstance(llm_response, str):
|
386
|
+
span.set_attribute(SemanticConvetion.GEN_AI_OUTPUT_TYPE,
|
387
|
+
"text")
|
388
|
+
elif llm_response is not None:
|
389
|
+
span.set_attribute(SemanticConvetion.GEN_AI_OUTPUT_TYPE,
|
390
|
+
"json")
|
391
|
+
|
392
|
+
span.set_status(Status(StatusCode.OK))
|
393
|
+
|
394
|
+
if disable_metrics is False:
|
395
|
+
attributes = create_metrics_attributes(
|
396
|
+
service_name=application_name,
|
397
|
+
deployment_environment=environment,
|
398
|
+
operation=SemanticConvetion.GEN_AI_OPERATION_TYPE_CHAT,
|
399
|
+
system=SemanticConvetion.GEN_AI_SYSTEM_OPENAI,
|
400
|
+
request_model=request_model,
|
401
|
+
server_address=server_address,
|
402
|
+
server_port=server_port,
|
403
|
+
response_model=response_dict.get('model'),
|
404
|
+
)
|
405
|
+
|
406
|
+
metrics["genai_client_usage_tokens"].record(
|
407
|
+
input_tokens + output_tokens, attributes
|
408
|
+
)
|
409
|
+
metrics["genai_client_operation_duration"].record(
|
410
|
+
end_time - start_time, attributes
|
411
|
+
)
|
412
|
+
metrics["genai_server_ttft"].record(
|
413
|
+
end_time - start_time, attributes
|
414
|
+
)
|
415
|
+
metrics["genai_requests"].add(1, attributes)
|
416
|
+
metrics["genai_completion_tokens"].add(output_tokens, attributes)
|
417
|
+
metrics["genai_prompt_tokens"].add(input_tokens, attributes)
|
418
|
+
metrics["genai_cost"].record(cost, attributes)
|
419
|
+
|
420
|
+
# Return original response
|
421
|
+
return response
|
422
|
+
|
423
|
+
except Exception as e:
|
424
|
+
handle_exception(span, e)
|
425
|
+
logger.error("Error in trace creation: %s", e)
|
426
|
+
|
427
|
+
# Return original response
|
428
|
+
return response
|
429
|
+
|
430
|
+
return wrapper
|
431
|
+
|
27
432
|
def async_chat_completions(version, environment, application_name,
|
28
433
|
tracer, pricing_info, capture_message_content, metrics, disable_metrics):
|
29
434
|
"""
|
@@ -13,6 +13,8 @@ from openlit.__helpers import (
|
|
13
13
|
get_image_model_cost,
|
14
14
|
general_tokens,
|
15
15
|
handle_exception,
|
16
|
+
extract_and_format_input,
|
17
|
+
concatenate_all_contents,
|
16
18
|
response_as_dict,
|
17
19
|
calculate_ttft,
|
18
20
|
calculate_tbt,
|
@@ -24,6 +26,409 @@ from openlit.semcov import SemanticConvetion
|
|
24
26
|
# Initialize logger for logging potential issues and operations
|
25
27
|
logger = logging.getLogger(__name__)
|
26
28
|
|
29
|
+
def responses(version, environment, application_name,
|
30
|
+
tracer, pricing_info, capture_message_content, metrics, disable_metrics):
|
31
|
+
"""
|
32
|
+
Generates a telemetry wrapper for chat completions to collect metrics.
|
33
|
+
|
34
|
+
Args:
|
35
|
+
version: Version of the monitoring package.
|
36
|
+
environment: Deployment environment (e.g., production, staging).
|
37
|
+
application_name: Name of the application using the OpenAI API.
|
38
|
+
tracer: OpenTelemetry tracer for creating spans.
|
39
|
+
pricing_info: Information used for calculating the cost of OpenAI usage.
|
40
|
+
capture_message_content: Flag indicating whether to trace the actual content.
|
41
|
+
|
42
|
+
Returns:
|
43
|
+
A function that wraps the chat completions method to add telemetry.
|
44
|
+
"""
|
45
|
+
|
46
|
+
class TracedSyncStream:
|
47
|
+
"""
|
48
|
+
Wrapper for streaming responses to collect metrics and trace data.
|
49
|
+
Wraps the response to collect message IDs and aggregated response.
|
50
|
+
|
51
|
+
This class implements the '__aiter__' and '__anext__' methods that
|
52
|
+
handle asynchronous streaming responses.
|
53
|
+
|
54
|
+
This class also implements '__aenter__' and '__aexit__' methods that
|
55
|
+
handle asynchronous context management protocol.
|
56
|
+
"""
|
57
|
+
def __init__(
|
58
|
+
self,
|
59
|
+
wrapped,
|
60
|
+
span,
|
61
|
+
kwargs,
|
62
|
+
server_address,
|
63
|
+
server_port,
|
64
|
+
**args,
|
65
|
+
):
|
66
|
+
self.__wrapped__ = wrapped
|
67
|
+
self._span = span
|
68
|
+
# Placeholder for aggregating streaming response
|
69
|
+
self._llmresponse = ""
|
70
|
+
self._response_id = ""
|
71
|
+
self._response_model = ""
|
72
|
+
self._finish_reason = ""
|
73
|
+
self._input_tokens = ""
|
74
|
+
self._output_tokens = ""
|
75
|
+
|
76
|
+
self._args = args
|
77
|
+
self._kwargs = kwargs
|
78
|
+
self._start_time = time.time()
|
79
|
+
self._end_time = None
|
80
|
+
self._timestamps = []
|
81
|
+
self._ttft = 0
|
82
|
+
self._tbt = 0
|
83
|
+
self._server_address = server_address
|
84
|
+
self._server_port = server_port
|
85
|
+
|
86
|
+
def __enter__(self):
|
87
|
+
self.__wrapped__.__enter__()
|
88
|
+
return self
|
89
|
+
|
90
|
+
def __exit__(self, exc_type, exc_value, traceback):
|
91
|
+
self.__wrapped__.__exit__(exc_type, exc_value, traceback)
|
92
|
+
|
93
|
+
def __iter__(self):
|
94
|
+
return self
|
95
|
+
|
96
|
+
def __getattr__(self, name):
|
97
|
+
"""Delegate attribute access to the wrapped object."""
|
98
|
+
return getattr(self.__wrapped__, name)
|
99
|
+
|
100
|
+
def __next__(self):
|
101
|
+
try:
|
102
|
+
chunk = self.__wrapped__.__next__()
|
103
|
+
end_time = time.time()
|
104
|
+
# Record the timestamp for the current chunk
|
105
|
+
self._timestamps.append(end_time)
|
106
|
+
|
107
|
+
if len(self._timestamps) == 1:
|
108
|
+
# Calculate time to first chunk
|
109
|
+
self._ttft = calculate_ttft(self._timestamps, self._start_time)
|
110
|
+
|
111
|
+
chunked = response_as_dict(chunk)
|
112
|
+
# Collect message IDs and aggregated response from events
|
113
|
+
if chunked.get('type') == "response.output_text.delta":
|
114
|
+
self._llmresponse += chunked.get('delta')
|
115
|
+
if chunked.get('type') == "response.completed":
|
116
|
+
self._response_id = chunked.get('response').get('id')
|
117
|
+
self._response_model = chunked.get('response').get('model')
|
118
|
+
self._finish_reason = chunked.get('response').get('status')
|
119
|
+
self._input_tokens = chunked.get('response').get('usage').get('input_tokens')
|
120
|
+
self._output_tokens = chunked.get('response').get('usage').get('output_tokens')
|
121
|
+
return chunk
|
122
|
+
except StopIteration:
|
123
|
+
# Handling exception ensure observability without disrupting operation
|
124
|
+
try:
|
125
|
+
self._end_time = time.time()
|
126
|
+
if len(self._timestamps) > 1:
|
127
|
+
self._tbt = calculate_tbt(self._timestamps)
|
128
|
+
|
129
|
+
try:
|
130
|
+
formatted_messages = extract_and_format_input(self._kwargs.get('input', ''))
|
131
|
+
prompt = concatenate_all_contents(formatted_messages)
|
132
|
+
except:
|
133
|
+
prompt = self._kwargs.get('input', '')
|
134
|
+
|
135
|
+
request_model = self._kwargs.get("model", "gpt-4o")
|
136
|
+
|
137
|
+
# Calculate cost of the operation
|
138
|
+
cost = get_chat_model_cost(request_model,
|
139
|
+
pricing_info, self._input_tokens,
|
140
|
+
self._output_tokens)
|
141
|
+
|
142
|
+
# Set Span attributes (OTel Semconv)
|
143
|
+
self._span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
|
144
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_OPERATION,
|
145
|
+
SemanticConvetion.GEN_AI_OPERATION_TYPE_CHAT)
|
146
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
|
147
|
+
SemanticConvetion.GEN_AI_SYSTEM_OPENAI)
|
148
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL,
|
149
|
+
request_model)
|
150
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_SEED,
|
151
|
+
self._kwargs.get("seed", ""))
|
152
|
+
self._span.set_attribute(SemanticConvetion.SERVER_PORT,
|
153
|
+
self._server_port)
|
154
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MAX_TOKENS,
|
155
|
+
self._kwargs.get("max_output_tokens", -1))
|
156
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_STOP_SEQUENCES,
|
157
|
+
self._kwargs.get("stop", []))
|
158
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TEMPERATURE,
|
159
|
+
self._kwargs.get("temperature", 1.0))
|
160
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TOP_P,
|
161
|
+
self._kwargs.get("top_p", 1.0))
|
162
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_FINISH_REASON,
|
163
|
+
[self._finish_reason])
|
164
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_ID,
|
165
|
+
self._response_id)
|
166
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_MODEL,
|
167
|
+
self._response_model)
|
168
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_USAGE_INPUT_TOKENS,
|
169
|
+
self._input_tokens)
|
170
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_USAGE_OUTPUT_TOKENS,
|
171
|
+
self._output_tokens)
|
172
|
+
self._span.set_attribute(SemanticConvetion.SERVER_ADDRESS,
|
173
|
+
self._server_address)
|
174
|
+
if isinstance(self._llmresponse, str):
|
175
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_OUTPUT_TYPE,
|
176
|
+
"text")
|
177
|
+
else:
|
178
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_OUTPUT_TYPE,
|
179
|
+
"json")
|
180
|
+
|
181
|
+
# Set Span attributes (Extra)
|
182
|
+
self._span.set_attribute(DEPLOYMENT_ENVIRONMENT,
|
183
|
+
environment)
|
184
|
+
self._span.set_attribute(SERVICE_NAME,
|
185
|
+
application_name)
|
186
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_USER,
|
187
|
+
self._kwargs.get("user", ""))
|
188
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_IS_STREAM,
|
189
|
+
True)
|
190
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
|
191
|
+
self._input_tokens + self._output_tokens)
|
192
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
|
193
|
+
cost)
|
194
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_SERVER_TBT,
|
195
|
+
self._tbt)
|
196
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_SERVER_TTFT,
|
197
|
+
self._ttft)
|
198
|
+
self._span.set_attribute(SemanticConvetion.GEN_AI_SDK_VERSION,
|
199
|
+
version)
|
200
|
+
|
201
|
+
if capture_message_content:
|
202
|
+
self._span.add_event(
|
203
|
+
name=SemanticConvetion.GEN_AI_CONTENT_PROMPT_EVENT,
|
204
|
+
attributes={
|
205
|
+
SemanticConvetion.GEN_AI_CONTENT_PROMPT: prompt,
|
206
|
+
},
|
207
|
+
)
|
208
|
+
self._span.add_event(
|
209
|
+
name=SemanticConvetion.GEN_AI_CONTENT_COMPLETION_EVENT,
|
210
|
+
attributes={
|
211
|
+
SemanticConvetion.GEN_AI_CONTENT_COMPLETION: self._llmresponse,
|
212
|
+
},
|
213
|
+
)
|
214
|
+
self._span.set_status(Status(StatusCode.OK))
|
215
|
+
|
216
|
+
if disable_metrics is False:
|
217
|
+
attributes = create_metrics_attributes(
|
218
|
+
service_name=application_name,
|
219
|
+
deployment_environment=environment,
|
220
|
+
operation=SemanticConvetion.GEN_AI_OPERATION_TYPE_CHAT,
|
221
|
+
system=SemanticConvetion.GEN_AI_SYSTEM_OPENAI,
|
222
|
+
request_model=request_model,
|
223
|
+
server_address=self._server_address,
|
224
|
+
server_port=self._server_port,
|
225
|
+
response_model=self._response_model,
|
226
|
+
)
|
227
|
+
|
228
|
+
metrics["genai_client_usage_tokens"].record(
|
229
|
+
self._input_tokens + self._output_tokens, attributes
|
230
|
+
)
|
231
|
+
metrics["genai_client_operation_duration"].record(
|
232
|
+
self._end_time - self._start_time, attributes
|
233
|
+
)
|
234
|
+
metrics["genai_server_tbt"].record(
|
235
|
+
self._tbt, attributes
|
236
|
+
)
|
237
|
+
metrics["genai_server_ttft"].record(
|
238
|
+
self._ttft, attributes
|
239
|
+
)
|
240
|
+
metrics["genai_requests"].add(1, attributes)
|
241
|
+
metrics["genai_completion_tokens"].add(self._output_tokens, attributes)
|
242
|
+
metrics["genai_prompt_tokens"].add(self._input_tokens, attributes)
|
243
|
+
metrics["genai_cost"].record(cost, attributes)
|
244
|
+
|
245
|
+
except Exception as e:
|
246
|
+
handle_exception(self._span, e)
|
247
|
+
logger.error("Error in trace creation: %s", e)
|
248
|
+
finally:
|
249
|
+
self._span.end()
|
250
|
+
raise
|
251
|
+
|
252
|
+
def wrapper(wrapped, instance, args, kwargs):
|
253
|
+
"""
|
254
|
+
Wraps the 'chat.completions' API call to add telemetry.
|
255
|
+
|
256
|
+
This collects metrics such as execution time, cost, and token usage, and handles errors
|
257
|
+
gracefully, adding details to the trace for observability.
|
258
|
+
|
259
|
+
Args:
|
260
|
+
wrapped: The original 'chat.completions' method to be wrapped.
|
261
|
+
instance: The instance of the class where the original method is defined.
|
262
|
+
args: Positional arguments for the 'chat.completions' method.
|
263
|
+
kwargs: Keyword arguments for the 'chat.completions' method.
|
264
|
+
|
265
|
+
Returns:
|
266
|
+
The response from the original 'chat.completions' method.
|
267
|
+
"""
|
268
|
+
|
269
|
+
# Check if streaming is enabled for the API call
|
270
|
+
streaming = kwargs.get("stream", False)
|
271
|
+
server_address, server_port = set_server_address_and_port(instance, "api.openai.com", 443)
|
272
|
+
request_model = kwargs.get("model", "gpt-4o")
|
273
|
+
|
274
|
+
span_name = f"{SemanticConvetion.GEN_AI_OPERATION_TYPE_CHAT} {request_model}"
|
275
|
+
|
276
|
+
# pylint: disable=no-else-return
|
277
|
+
if streaming:
|
278
|
+
# Special handling for streaming response to accommodate the nature of data flow
|
279
|
+
awaited_wrapped = wrapped(*args, **kwargs)
|
280
|
+
span = tracer.start_span(span_name, kind=SpanKind.CLIENT)
|
281
|
+
|
282
|
+
return TracedSyncStream(awaited_wrapped, span, kwargs, server_address, server_port)
|
283
|
+
|
284
|
+
# Handling for non-streaming responses
|
285
|
+
else:
|
286
|
+
with tracer.start_as_current_span(span_name, kind= SpanKind.CLIENT) as span:
|
287
|
+
start_time = time.time()
|
288
|
+
response = wrapped(*args, **kwargs)
|
289
|
+
end_time = time.time()
|
290
|
+
|
291
|
+
response_dict = response_as_dict(response)
|
292
|
+
|
293
|
+
try:
|
294
|
+
try:
|
295
|
+
formatted_messages = extract_and_format_input(kwargs.get('input', ''))
|
296
|
+
prompt = concatenate_all_contents(formatted_messages)
|
297
|
+
except:
|
298
|
+
prompt = kwargs.get('input', '')
|
299
|
+
|
300
|
+
input_tokens = response_dict.get('usage').get('input_tokens')
|
301
|
+
output_tokens = response_dict.get('usage').get('output_tokens')
|
302
|
+
|
303
|
+
# Calculate cost of the operation
|
304
|
+
cost = get_chat_model_cost(request_model,
|
305
|
+
pricing_info, input_tokens,
|
306
|
+
output_tokens)
|
307
|
+
|
308
|
+
# Set base span attribues (OTel Semconv)
|
309
|
+
span.set_attribute(TELEMETRY_SDK_NAME, "openlit")
|
310
|
+
span.set_attribute(SemanticConvetion.GEN_AI_OPERATION,
|
311
|
+
SemanticConvetion.GEN_AI_OPERATION_TYPE_CHAT)
|
312
|
+
span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM,
|
313
|
+
SemanticConvetion.GEN_AI_SYSTEM_OPENAI)
|
314
|
+
span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL,
|
315
|
+
request_model)
|
316
|
+
span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_SEED,
|
317
|
+
kwargs.get("seed", ""))
|
318
|
+
span.set_attribute(SemanticConvetion.SERVER_PORT,
|
319
|
+
server_port)
|
320
|
+
span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MAX_TOKENS,
|
321
|
+
kwargs.get("max_output_tokens", -1))
|
322
|
+
span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_STOP_SEQUENCES,
|
323
|
+
kwargs.get("stop", []))
|
324
|
+
span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TEMPERATURE,
|
325
|
+
str(response_dict.get("temperature", 1.0)))
|
326
|
+
span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_TOP_P,
|
327
|
+
str(response_dict.get("top_p", 1.0)))
|
328
|
+
span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_ID,
|
329
|
+
response_dict.get("id"))
|
330
|
+
span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_MODEL,
|
331
|
+
response_dict.get('model'))
|
332
|
+
span.set_attribute(SemanticConvetion.GEN_AI_USAGE_INPUT_TOKENS,
|
333
|
+
input_tokens)
|
334
|
+
span.set_attribute(SemanticConvetion.GEN_AI_USAGE_OUTPUT_TOKENS,
|
335
|
+
output_tokens)
|
336
|
+
span.set_attribute(SemanticConvetion.SERVER_ADDRESS,
|
337
|
+
server_address)
|
338
|
+
|
339
|
+
# Set base span attribues (Extras)
|
340
|
+
span.set_attribute(DEPLOYMENT_ENVIRONMENT,
|
341
|
+
environment)
|
342
|
+
span.set_attribute(SERVICE_NAME,
|
343
|
+
application_name)
|
344
|
+
span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_USER,
|
345
|
+
kwargs.get("user", ""))
|
346
|
+
span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_IS_STREAM,
|
347
|
+
False)
|
348
|
+
span.set_attribute(SemanticConvetion.GEN_AI_USAGE_TOTAL_TOKENS,
|
349
|
+
input_tokens + output_tokens)
|
350
|
+
span.set_attribute(SemanticConvetion.GEN_AI_USAGE_COST,
|
351
|
+
cost)
|
352
|
+
span.set_attribute(SemanticConvetion.GEN_AI_SERVER_TTFT,
|
353
|
+
end_time - start_time)
|
354
|
+
span.set_attribute(SemanticConvetion.GEN_AI_SDK_VERSION,
|
355
|
+
version)
|
356
|
+
|
357
|
+
if capture_message_content:
|
358
|
+
span.add_event(
|
359
|
+
name=SemanticConvetion.GEN_AI_CONTENT_PROMPT_EVENT,
|
360
|
+
attributes={
|
361
|
+
SemanticConvetion.GEN_AI_CONTENT_PROMPT: prompt,
|
362
|
+
},
|
363
|
+
)
|
364
|
+
|
365
|
+
for i in range(kwargs.get('n',1)):
|
366
|
+
span.set_attribute(SemanticConvetion.GEN_AI_RESPONSE_FINISH_REASON,
|
367
|
+
[response_dict.get('status')])
|
368
|
+
try:
|
369
|
+
llm_response = response_dict.get('output')[i].get('content')[0].get('text','')
|
370
|
+
except:
|
371
|
+
llm_response = ''
|
372
|
+
|
373
|
+
if capture_message_content:
|
374
|
+
span.add_event(
|
375
|
+
name=SemanticConvetion.GEN_AI_CONTENT_COMPLETION_EVENT,
|
376
|
+
attributes={
|
377
|
+
# pylint: disable=line-too-long
|
378
|
+
SemanticConvetion.GEN_AI_CONTENT_COMPLETION: llm_response,
|
379
|
+
},
|
380
|
+
)
|
381
|
+
if kwargs.get('tools'):
|
382
|
+
span.set_attribute(SemanticConvetion.GEN_AI_TOOL_CALLS,
|
383
|
+
str(response_dict.get('tools')))
|
384
|
+
|
385
|
+
if isinstance(llm_response, str):
|
386
|
+
span.set_attribute(SemanticConvetion.GEN_AI_OUTPUT_TYPE,
|
387
|
+
"text")
|
388
|
+
elif llm_response is not None:
|
389
|
+
span.set_attribute(SemanticConvetion.GEN_AI_OUTPUT_TYPE,
|
390
|
+
"json")
|
391
|
+
|
392
|
+
span.set_status(Status(StatusCode.OK))
|
393
|
+
|
394
|
+
if disable_metrics is False:
|
395
|
+
attributes = create_metrics_attributes(
|
396
|
+
service_name=application_name,
|
397
|
+
deployment_environment=environment,
|
398
|
+
operation=SemanticConvetion.GEN_AI_OPERATION_TYPE_CHAT,
|
399
|
+
system=SemanticConvetion.GEN_AI_SYSTEM_OPENAI,
|
400
|
+
request_model=request_model,
|
401
|
+
server_address=server_address,
|
402
|
+
server_port=server_port,
|
403
|
+
response_model=response_dict.get('model'),
|
404
|
+
)
|
405
|
+
|
406
|
+
metrics["genai_client_usage_tokens"].record(
|
407
|
+
input_tokens + output_tokens, attributes
|
408
|
+
)
|
409
|
+
metrics["genai_client_operation_duration"].record(
|
410
|
+
end_time - start_time, attributes
|
411
|
+
)
|
412
|
+
metrics["genai_server_ttft"].record(
|
413
|
+
end_time - start_time, attributes
|
414
|
+
)
|
415
|
+
metrics["genai_requests"].add(1, attributes)
|
416
|
+
metrics["genai_completion_tokens"].add(output_tokens, attributes)
|
417
|
+
metrics["genai_prompt_tokens"].add(input_tokens, attributes)
|
418
|
+
metrics["genai_cost"].record(cost, attributes)
|
419
|
+
|
420
|
+
# Return original response
|
421
|
+
return response
|
422
|
+
|
423
|
+
except Exception as e:
|
424
|
+
handle_exception(span, e)
|
425
|
+
logger.error("Error in trace creation: %s", e)
|
426
|
+
|
427
|
+
# Return original response
|
428
|
+
return response
|
429
|
+
|
430
|
+
return wrapper
|
431
|
+
|
27
432
|
def chat_completions(version, environment, application_name,
|
28
433
|
tracer, pricing_info, capture_message_content, metrics, disable_metrics):
|
29
434
|
"""
|
@@ -0,0 +1,42 @@
|
|
1
|
+
"""Initializer of Auto Instrumentation of OpenAI Agents Functions"""
|
2
|
+
|
3
|
+
from typing import Collection
|
4
|
+
import importlib.metadata
|
5
|
+
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
|
6
|
+
from wrapt import wrap_function_wrapper
|
7
|
+
|
8
|
+
from openlit.instrumentation.openai_agents.openai_agents import (
|
9
|
+
create_agent
|
10
|
+
)
|
11
|
+
|
12
|
+
_instruments = ('openai-agents >= 0.0.3',)
|
13
|
+
|
14
|
+
class OpenAIAgentsInstrumentor(BaseInstrumentor):
|
15
|
+
"""
|
16
|
+
An instrumentor for openai-agents's client library.
|
17
|
+
"""
|
18
|
+
|
19
|
+
def instrumentation_dependencies(self) -> Collection[str]:
|
20
|
+
return _instruments
|
21
|
+
|
22
|
+
def _instrument(self, **kwargs):
|
23
|
+
application_name = kwargs.get('application_name', 'default')
|
24
|
+
environment = kwargs.get('environment', 'default')
|
25
|
+
tracer = kwargs.get('tracer')
|
26
|
+
event_provider = kwargs.get('event_provider')
|
27
|
+
metrics = kwargs.get('metrics_dict')
|
28
|
+
pricing_info = kwargs.get('pricing_info', {})
|
29
|
+
capture_message_content = kwargs.get('capture_message_content', False)
|
30
|
+
disable_metrics = kwargs.get('disable_metrics')
|
31
|
+
version = importlib.metadata.version('openai-agents')
|
32
|
+
|
33
|
+
wrap_function_wrapper(
|
34
|
+
'agents.agent',
|
35
|
+
'Agent.__init__',
|
36
|
+
create_agent(version, environment, application_name,
|
37
|
+
tracer, event_provider, pricing_info, capture_message_content, metrics, disable_metrics),
|
38
|
+
)
|
39
|
+
|
40
|
+
def _uninstrument(self, **kwargs):
|
41
|
+
# Proper uninstrumentation logic to revert patched methods
|
42
|
+
pass
|
@@ -0,0 +1,65 @@
|
|
1
|
+
"""
|
2
|
+
Module for monitoring AG2 API calls.
|
3
|
+
"""
|
4
|
+
|
5
|
+
import logging
|
6
|
+
from opentelemetry.trace import SpanKind, Status, StatusCode
|
7
|
+
from opentelemetry.sdk.resources import SERVICE_NAME, TELEMETRY_SDK_NAME, DEPLOYMENT_ENVIRONMENT
|
8
|
+
from openlit.__helpers import (
|
9
|
+
handle_exception,
|
10
|
+
)
|
11
|
+
from openlit.semcov import SemanticConvetion
|
12
|
+
|
13
|
+
# Initialize logger for logging potential issues and operations
|
14
|
+
logger = logging.getLogger(__name__)
|
15
|
+
|
16
|
+
def set_span_attributes(span, version, operation_name, environment,
|
17
|
+
application_name, server_address, server_port, request_model):
|
18
|
+
"""
|
19
|
+
Set common attributes for the span.
|
20
|
+
"""
|
21
|
+
|
22
|
+
# Set Span attributes (OTel Semconv)
|
23
|
+
span.set_attribute(TELEMETRY_SDK_NAME, 'openlit')
|
24
|
+
span.set_attribute(SemanticConvetion.GEN_AI_OPERATION, operation_name)
|
25
|
+
span.set_attribute(SemanticConvetion.GEN_AI_SYSTEM, SemanticConvetion.GEN_AI_SYSTEM_AG2)
|
26
|
+
span.set_attribute(SemanticConvetion.SERVER_ADDRESS, server_address)
|
27
|
+
span.set_attribute(SemanticConvetion.SERVER_PORT, server_port)
|
28
|
+
span.set_attribute(SemanticConvetion.GEN_AI_REQUEST_MODEL, request_model)
|
29
|
+
|
30
|
+
# Set Span attributes (Extras)
|
31
|
+
span.set_attribute(DEPLOYMENT_ENVIRONMENT, environment)
|
32
|
+
span.set_attribute(SERVICE_NAME, application_name)
|
33
|
+
span.set_attribute(SemanticConvetion.GEN_AI_SDK_VERSION, version)
|
34
|
+
|
35
|
+
def create_agent(version, environment, application_name,
|
36
|
+
tracer, event_provider, pricing_info, capture_message_content, metrics, disable_metrics):
|
37
|
+
"""
|
38
|
+
Generates a telemetry wrapper for GenAI function call
|
39
|
+
"""
|
40
|
+
def wrapper(wrapped, instance, args, kwargs):
|
41
|
+
server_address, server_port = '127.0.0.1', 80
|
42
|
+
|
43
|
+
agent_name = kwargs.get('name', 'openai_agent')
|
44
|
+
span_name = f'{SemanticConvetion.GEN_AI_OPERATION_TYPE_CREATE_AGENT} {agent_name}'
|
45
|
+
|
46
|
+
with tracer.start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
|
47
|
+
try:
|
48
|
+
response = wrapped(*args, **kwargs)
|
49
|
+
|
50
|
+
set_span_attributes(span, version, SemanticConvetion.GEN_AI_OPERATION_TYPE_CREATE_AGENT,
|
51
|
+
environment, application_name, server_address, server_port, kwargs.get('model', 'gpt-4o'))
|
52
|
+
span.set_attribute(SemanticConvetion.GEN_AI_AGENT_NAME, agent_name)
|
53
|
+
|
54
|
+
span.set_attribute(SemanticConvetion.GEN_AI_AGENT_DESCRIPTION, kwargs.get('instructions', ''))
|
55
|
+
|
56
|
+
span.set_status(Status(StatusCode.OK))
|
57
|
+
|
58
|
+
return response
|
59
|
+
|
60
|
+
except Exception as e:
|
61
|
+
handle_exception(span, e)
|
62
|
+
logger.error('Error in trace creation: %s', e)
|
63
|
+
return response
|
64
|
+
|
65
|
+
return wrapper
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.3
|
2
2
|
Name: openlit
|
3
|
-
Version: 1.33.
|
3
|
+
Version: 1.33.16
|
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
|
License: Apache-2.0
|
6
6
|
Keywords: OpenTelemetry,otel,otlp,llm,tracing,openai,anthropic,claude,cohere,llm monitoring,observability,monitoring,gpt,Generative AI,chatGPT,gpu
|
@@ -1,5 +1,5 @@
|
|
1
1
|
openlit/__helpers.py,sha256=9K9nz_RunwtnFeAk591uOJZiY3J88HsYv7T2H8elHWA,10262
|
2
|
-
openlit/__init__.py,sha256=
|
2
|
+
openlit/__init__.py,sha256=iHGwg8XB2DhNVCktU3FLFqubAOiQVQCp1F7L7OHp6cg,23921
|
3
3
|
openlit/evals/__init__.py,sha256=nJe99nuLo1b5rf7pt9U9BCdSDedzbVi2Fj96cgl7msM,380
|
4
4
|
openlit/evals/all.py,sha256=oWrue3PotE-rB5WePG3MRYSA-ro6WivkclSHjYlAqGs,7154
|
5
5
|
openlit/evals/bias_detection.py,sha256=mCdsfK7x1vX7S3psC3g641IMlZ-7df3h-V6eiICj5N8,8154
|
@@ -92,9 +92,11 @@ openlit/instrumentation/ollama/__init__.py,sha256=JjxSqEegmRoRqIVz7ZAq9dLyXPZ2Dq
|
|
92
92
|
openlit/instrumentation/ollama/async_ollama.py,sha256=LhDQPy3wLyNO9JWksUEeCx-DK9oIV3K98Cgwnp4RfKg,6538
|
93
93
|
openlit/instrumentation/ollama/ollama.py,sha256=wVyaX0quoiiCj1J3tyTiQx5Du5CmaWmt9e_lpCr7s6A,6434
|
94
94
|
openlit/instrumentation/ollama/utils.py,sha256=zXsWNqfnZLssrcb-GNbWeZeqTKVzQb1bes8vzgl-gbQ,14549
|
95
|
-
openlit/instrumentation/openai/__init__.py,sha256=
|
96
|
-
openlit/instrumentation/openai/async_openai.py,sha256=
|
97
|
-
openlit/instrumentation/openai/openai.py,sha256=
|
95
|
+
openlit/instrumentation/openai/__init__.py,sha256=FiL4OHDhs957spa3k9sNC_VLt0-txtwbnujQwnevQ5I,5564
|
96
|
+
openlit/instrumentation/openai/async_openai.py,sha256=AMievEMil486B9ibV0Nm54xkxAiHA-XzWWKAGQ4himI,71523
|
97
|
+
openlit/instrumentation/openai/openai.py,sha256=d-I-RyJMNcz1m0vdkEW-l6G0WaUNCavT9BvB_EGEFYg,71266
|
98
|
+
openlit/instrumentation/openai_agents/__init__.py,sha256=tRTSIrUtkXc_lfQnVanXmQLd2Sy9RqBNTHF5FhhZx7o,1530
|
99
|
+
openlit/instrumentation/openai_agents/openai_agents.py,sha256=teVgsn7lIKCGPmdl9RrgPavhdcCXjh3vMeGKskrd2vo,2666
|
98
100
|
openlit/instrumentation/phidata/__init__.py,sha256=tqls5-UI6FzbjxYgq_qqAfALhWJm8dHn2NtgqiQA4f8,1557
|
99
101
|
openlit/instrumentation/phidata/phidata.py,sha256=-BU_g3FpGcttOt-W-QIER5qquCRORob2UFLdaOW3F_s,4819
|
100
102
|
openlit/instrumentation/pinecone/__init__.py,sha256=0guSEPmObaZiOF8yHExpOGY-qW_egHXfZGog3rKGi8M,2596
|
@@ -121,7 +123,7 @@ openlit/otel/events.py,sha256=VrMjTpvnLtYRBHCiFwJojTQqqNpRCxoD4yJYeQrtPsk,3560
|
|
121
123
|
openlit/otel/metrics.py,sha256=Iwx6baEiCZPNqsFf92K5mDWU8are8DOF0uQAuNZsCKg,6826
|
122
124
|
openlit/otel/tracing.py,sha256=tjV2bEbEDPUB1Z46gE-UsJsb04sRdFrfbhIDkxViZc0,3103
|
123
125
|
openlit/semcov/__init__.py,sha256=lM0Y3wMYYmCvfcNGD3k0xSn1XZUiGw-bKgCuwcGsOp8,13302
|
124
|
-
openlit-1.33.
|
125
|
-
openlit-1.33.
|
126
|
-
openlit-1.33.
|
127
|
-
openlit-1.33.
|
126
|
+
openlit-1.33.16.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
|
127
|
+
openlit-1.33.16.dist-info/METADATA,sha256=epse0c3md_EmHgIFIkCpRBNK2xUk7t_e-gQ9O7fyJDY,23471
|
128
|
+
openlit-1.33.16.dist-info/WHEEL,sha256=XbeZDeTWKc1w7CSIyre5aMDU_-PohRwTQceYnisIYYY,88
|
129
|
+
openlit-1.33.16.dist-info/RECORD,,
|
File without changes
|
File without changes
|