openlit 0.0.1__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.
@@ -0,0 +1,335 @@
1
+ # pylint: disable=useless-return, bad-staticmethod-argument, disable=duplicate-code
2
+ """Initializer of Auto Instrumentation of OpenAI Functions"""
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.openai import chat_completions, embedding, finetune
9
+ from openlit.instrumentation.openai.openai import image_generate, image_variatons, audio_create
10
+ from openlit.instrumentation.openai.async_openai import async_chat_completions, async_embedding
11
+ from openlit.instrumentation.openai.async_openai import async_finetune
12
+ from openlit.instrumentation.openai.async_openai import async_image_generate, async_image_variatons
13
+ from openlit.instrumentation.openai.async_openai import async_audio_create
14
+
15
+ from openlit.instrumentation.openai.azure_openai import azure_chat_completions, azure_completions
16
+ from openlit.instrumentation.openai.azure_openai import azure_image_generate, azure_embedding
17
+ from openlit.instrumentation.openai.async_azure_openai import azure_async_chat_completions
18
+ from openlit.instrumentation.openai.async_azure_openai import azure_async_completions
19
+ from openlit.instrumentation.openai.async_azure_openai import azure_async_image_generate
20
+ from openlit.instrumentation.openai.async_azure_openai import azure_async_embedding
21
+
22
+ _instruments = ("openai >= 1.1.1",)
23
+
24
+ class OpenAIInstrumentor(BaseInstrumentor):
25
+ """An instrumentor for OpenAI's client library."""
26
+
27
+ def instrumentation_dependencies(self) -> Collection[str]:
28
+ return _instruments
29
+
30
+ def _instrument(self, **kwargs):
31
+ application_name = kwargs.get("application_name")
32
+ environment = kwargs.get("environment")
33
+ tracer = kwargs.get("tracer")
34
+ metrics = kwargs.get("metrics_dict")
35
+ pricing_info = kwargs.get("pricing_info")
36
+ trace_content = kwargs.get("trace_content")
37
+ disable_metrics = kwargs.get("disable_metrics")
38
+ version = importlib.metadata.version("openai")
39
+
40
+ wrap_function_wrapper(
41
+ "openai.resources.chat.completions",
42
+ "Completions.create",
43
+ chat_wrapper(version, environment, application_name,
44
+ tracer, pricing_info, trace_content,
45
+ metrics, disable_metrics),
46
+ )
47
+
48
+ wrap_function_wrapper(
49
+ "openai.resources.chat.completions",
50
+ "AsyncCompletions.create",
51
+ async_chat_wrapper(version, environment, application_name,
52
+ tracer, pricing_info, trace_content,
53
+ metrics, disable_metrics),
54
+ )
55
+
56
+ wrap_function_wrapper(
57
+ "openai.resources.images",
58
+ "Images.generate",
59
+ image_generate_wrapper(version, environment, application_name,
60
+ tracer, pricing_info, trace_content,
61
+ metrics, disable_metrics),
62
+ )
63
+
64
+ wrap_function_wrapper(
65
+ "openai.resources.images",
66
+ "AsyncImages.generate",
67
+ async_image_generate_wrapper(version, environment, application_name,
68
+ tracer, pricing_info, trace_content,
69
+ metrics, disable_metrics),
70
+ )
71
+
72
+ wrap_function_wrapper(
73
+ "openai.resources.embeddings",
74
+ "Embeddings.create",
75
+ embedding_wrapper(version, environment, application_name,
76
+ tracer, pricing_info, trace_content,
77
+ metrics, disable_metrics),
78
+ )
79
+
80
+ wrap_function_wrapper(
81
+ "openai.resources.embeddings",
82
+ "AsyncEmbeddings.create",
83
+ async_embedding_wrapper(version, environment, application_name,
84
+ tracer, pricing_info, trace_content,
85
+ metrics, disable_metrics),
86
+ )
87
+
88
+ wrap_function_wrapper(
89
+ "openai.resources.completions",
90
+ "Completions.create",
91
+ azure_completions("azure_openai.completions", version, environment, application_name,
92
+ tracer, pricing_info, trace_content,
93
+ metrics, disable_metrics),
94
+ )
95
+
96
+ wrap_function_wrapper(
97
+ "openai.resources.completions",
98
+ "AsyncCompletions.create",
99
+ azure_async_completions("azure_openai.completions", version,
100
+ environment, application_name,
101
+ tracer, pricing_info, trace_content,
102
+ metrics, disable_metrics),
103
+ )
104
+
105
+ wrap_function_wrapper(
106
+ "openai.resources.images",
107
+ "Images.create_variation",
108
+ image_variatons("openai.images.variations", version,
109
+ environment, application_name,
110
+ tracer, pricing_info, trace_content,
111
+ metrics, disable_metrics),
112
+ )
113
+
114
+ wrap_function_wrapper(
115
+ "openai.resources.images",
116
+ "AsyncImages.create_variation",
117
+ async_image_variatons("openai.images.variations", version,
118
+ environment, application_name,
119
+ tracer, pricing_info, trace_content,
120
+ metrics, disable_metrics),
121
+ )
122
+
123
+ wrap_function_wrapper(
124
+ "openai.resources.audio.speech",
125
+ "Speech.create",
126
+ audio_create("openai.audio.speech", version, environment, application_name,
127
+ tracer, pricing_info, trace_content,
128
+ metrics, disable_metrics),
129
+ )
130
+
131
+ wrap_function_wrapper(
132
+ "openai.resources.audio.speech",
133
+ "AsyncSpeech.create",
134
+ async_audio_create("openai.audio.speech", version, environment, application_name,
135
+ tracer, pricing_info, trace_content,
136
+ metrics, disable_metrics),
137
+ )
138
+
139
+ wrap_function_wrapper(
140
+ "openai.resources.fine_tuning.jobs",
141
+ "Jobs.create",
142
+ finetune("openai.audio.speech", version, environment, application_name,
143
+ tracer, pricing_info, trace_content,
144
+ metrics, disable_metrics),
145
+ )
146
+
147
+ wrap_function_wrapper(
148
+ "openai.resources.fine_tuning.jobs",
149
+ "AsyncJobs.create",
150
+ async_finetune("openai.fine_tuning.jo", version, environment, application_name,
151
+ tracer, pricing_info, trace_content,
152
+ metrics, disable_metrics),
153
+ )
154
+
155
+ @staticmethod
156
+ def _uninstrument(self, **kwargs):
157
+ pass
158
+
159
+ def chat_wrapper(version, environment, application_name, tracer, pricing_info, trace_content,
160
+ metrics, disable_metrics):
161
+ """
162
+ Decorator for making a custom wrapper execute conditionally,
163
+ based on whether the instance is for Azure OpenAI or not.
164
+ """
165
+ def wrapper(wrapped, instance, args, kwargs):
166
+ # Default to using the standard OpenAI chat completions
167
+ completion_func = chat_completions("openai.chat.completions", version, environment,
168
+ application_name, tracer, pricing_info, trace_content,
169
+ metrics, disable_metrics)
170
+
171
+ # Check if it's an Azure instance by inspecting `base_url`
172
+ try:
173
+ base_url = getattr(instance, 'base_url', '')
174
+ if 'azure.com' in base_url:
175
+ # Switch to the Azure-specific chat completions logic
176
+ completion_func = azure_chat_completions("azure_openai.chat.completions",
177
+ version, environment, application_name,
178
+ tracer, pricing_info, trace_content,
179
+ metrics, disable_metrics)
180
+ except AttributeError:
181
+ pass # base_url attribute not found, proceed with the default
182
+
183
+ # Execute the selected completion function
184
+ return completion_func(wrapped, instance, args, kwargs)
185
+
186
+ return wrapper
187
+
188
+ def async_chat_wrapper(version, environment, application_name, tracer, pricing_info,
189
+ trace_content, metrics, disable_metrics):
190
+ """
191
+ Decorator for making a custom wrapper execute conditionally,
192
+ based on whether the instance is for Azure OpenAI or not.
193
+ """
194
+ def wrapper(wrapped, instance, args, kwargs):
195
+ # Default to using the standard OpenAI chat completions
196
+ completion_func = async_chat_completions("openai.chat.completions", version, environment,
197
+ application_name, tracer,
198
+ pricing_info, trace_content,
199
+ metrics, disable_metrics)
200
+
201
+ # Check if it's an Azure instance by inspecting `base_url`
202
+ try:
203
+ base_url = getattr(instance, 'base_url', '')
204
+ if 'azure.com' in base_url:
205
+ # Switch to the Azure-specific chat completions logic
206
+ completion_func = azure_async_chat_completions("azure_openai.chat.completions",
207
+ version, environment,
208
+ application_name, tracer,
209
+ pricing_info, trace_content,
210
+ metrics, disable_metrics)
211
+ except AttributeError:
212
+ pass # base_url attribute not found, proceed with the default
213
+
214
+ # Execute the selected completion function
215
+ return completion_func(wrapped, instance, args, kwargs)
216
+
217
+ return wrapper
218
+
219
+ def image_generate_wrapper(version, environment, application_name, tracer, pricing_info,
220
+ trace_content, metrics, disable_metrics):
221
+ """
222
+ Decorator for making a custom wrapper execute conditionally,
223
+ based on whether the instance is for Azure OpenAI or not.
224
+ """
225
+ def wrapper(wrapped, instance, args, kwargs):
226
+ # Default to using the standard OpenAI chat completions
227
+ completion_func = image_generate("openai.images.generate", version, environment,
228
+ application_name, tracer, pricing_info, trace_content,
229
+ metrics, disable_metrics)
230
+
231
+ # Check if it's an Azure instance by inspecting `base_url`
232
+ try:
233
+ base_url = getattr(instance, 'base_url', '')
234
+ if 'azure.com' in base_url:
235
+ # Switch to the Azure-specific chat completions logic
236
+ completion_func = azure_image_generate("azure_openai.images.generate",
237
+ version, environment, application_name,
238
+ tracer, pricing_info, trace_content,
239
+ metrics, disable_metrics)
240
+ except AttributeError:
241
+ pass # base_url attribute not found, proceed with the default
242
+
243
+ # Execute the selected completion function
244
+ return completion_func(wrapped, instance, args, kwargs)
245
+
246
+ return wrapper
247
+
248
+ def async_image_generate_wrapper(version, environment, application_name, tracer,
249
+ pricing_info, trace_content, metrics, disable_metrics):
250
+ """
251
+ Decorator for making a custom wrapper execute conditionally,
252
+ based on whether the instance is for Azure OpenAI or not.
253
+ """
254
+ def wrapper(wrapped, instance, args, kwargs):
255
+ # Default to using the standard OpenAI chat completions
256
+ completion_func = async_image_generate("openai.images.generate", version,
257
+ environment, application_name, tracer,
258
+ pricing_info, trace_content,
259
+ metrics, disable_metrics)
260
+
261
+ # Check if it's an Azure instance by inspecting `base_url`
262
+ try:
263
+ base_url = getattr(instance, 'base_url', '')
264
+ if 'azure.com' in base_url:
265
+ # Switch to the Azure-specific chat completions logic
266
+ completion_func = azure_async_image_generate("azure_openai.images.generate",
267
+ version, environment,
268
+ application_name, tracer,
269
+ pricing_info, trace_content,
270
+ metrics, disable_metrics)
271
+ except AttributeError:
272
+ pass # base_url attribute not found, proceed with the default
273
+
274
+ # Execute the selected completion function
275
+ return completion_func(wrapped, instance, args, kwargs)
276
+
277
+ return wrapper
278
+
279
+ def embedding_wrapper(version, environment, application_name, tracer, pricing_info,
280
+ trace_content, metrics, disable_metrics):
281
+ """
282
+ Decorator for making a custom wrapper execute conditionally,
283
+ based on whether the instance is for Azure OpenAI or not.
284
+ """
285
+ def wrapper(wrapped, instance, args, kwargs):
286
+ # Default to using the standard OpenAI chat completions
287
+ completion_func = embedding("openai.embeddings", version, environment,
288
+ application_name, tracer, pricing_info, trace_content,
289
+ metrics, disable_metrics)
290
+
291
+ # Check if it's an Azure instance by inspecting `base_url`
292
+ try:
293
+ base_url = getattr(instance, 'base_url', '')
294
+ if 'azure.com' in base_url:
295
+ # Switch to the Azure-specific chat completions logic
296
+ completion_func = azure_embedding("azure_openai.embeddings",
297
+ version, environment, application_name,
298
+ tracer, pricing_info, trace_content,
299
+ metrics, disable_metrics)
300
+ except AttributeError:
301
+ pass # base_url attribute not found, proceed with the default
302
+
303
+ # Execute the selected completion function
304
+ return completion_func(wrapped, instance, args, kwargs)
305
+
306
+ return wrapper
307
+
308
+ def async_embedding_wrapper(version, environment, application_name, tracer,
309
+ pricing_info, trace_content, metrics, disable_metrics):
310
+ """
311
+ Decorator for making a custom wrapper execute conditionally,
312
+ based on whether the instance is for Azure OpenAI or not.
313
+ """
314
+ def wrapper(wrapped, instance, args, kwargs):
315
+ # Default to using the standard OpenAI chat completions
316
+ completion_func = async_embedding("openai.embeddings", version, environment,
317
+ application_name, tracer, pricing_info, trace_content,
318
+ metrics, disable_metrics)
319
+
320
+ # Check if it's an Azure instance by inspecting `base_url`
321
+ try:
322
+ base_url = getattr(instance, 'base_url', '')
323
+ if 'azure.com' in base_url:
324
+ # Switch to the Azure-specific chat completions logic
325
+ completion_func = azure_async_embedding("azure_openai.embeddings", version,
326
+ environment, application_name, tracer,
327
+ pricing_info, trace_content,
328
+ metrics, disable_metrics)
329
+ except AttributeError:
330
+ pass # base_url attribute not found, proceed with the default
331
+
332
+ # Execute the selected completion function
333
+ return completion_func(wrapped, instance, args, kwargs)
334
+
335
+ return wrapper