@arizeai/openinference-instrumentation-openai 0.2.0 → 0.3.0

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.
@@ -40,6 +40,19 @@ import { isTracingSuppressed } from "@opentelemetry/core";
40
40
 
41
41
  const MODULE_NAME = "openai";
42
42
 
43
+ /**
44
+ * Flag to check if the openai module has been patched
45
+ * Note: This is a fallback in case the module is made immutable (e.x. Deno, webpack, etc.)
46
+ */
47
+ let _isOpenInferencePatched = false;
48
+
49
+ /**
50
+ * function to check if instrumentation is enabled / disabled
51
+ */
52
+ export function isPatched() {
53
+ return _isOpenInferencePatched;
54
+ }
55
+
43
56
  /**
44
57
  * Resolves the execution context for the current span
45
58
  * If tracing is suppressed, the span is dropped and the current context is returned
@@ -93,7 +106,7 @@ export class OpenAIInstrumentation extends InstrumentationBase<typeof openai> {
93
106
  moduleVersion?: string,
94
107
  ) {
95
108
  diag.debug(`Applying patch for ${MODULE_NAME}@${moduleVersion}`);
96
- if (module?.openInferencePatched) {
109
+ if (module?.openInferencePatched || _isOpenInferencePatched) {
97
110
  return module;
98
111
  }
99
112
  // eslint-disable-next-line @typescript-eslint/no-this-alias
@@ -318,16 +331,35 @@ export class OpenAIInstrumentation extends InstrumentationBase<typeof openai> {
318
331
  },
319
332
  );
320
333
 
321
- module.openInferencePatched = true;
334
+ _isOpenInferencePatched = true;
335
+ try {
336
+ // This can fail if the module is made immutable via the runtime or bundler
337
+ module.openInferencePatched = true;
338
+ } catch (e) {
339
+ diag.warn(`Failed to set ${MODULE_NAME} patched flag on the module`, e);
340
+ }
341
+
322
342
  return module;
323
343
  }
324
344
  /**
325
345
  * Un-patches the OpenAI module's chat completions API
326
346
  */
327
- private unpatch(moduleExports: typeof openai, moduleVersion?: string) {
347
+ private unpatch(
348
+ moduleExports: typeof openai & { openInferencePatched?: boolean },
349
+ moduleVersion?: string,
350
+ ) {
328
351
  diag.debug(`Removing patch for ${MODULE_NAME}@${moduleVersion}`);
329
352
  this._unwrap(moduleExports.OpenAI.Chat.Completions.prototype, "create");
353
+ this._unwrap(moduleExports.OpenAI.Completions.prototype, "create");
330
354
  this._unwrap(moduleExports.OpenAI.Embeddings.prototype, "create");
355
+
356
+ _isOpenInferencePatched = false;
357
+ try {
358
+ // This can fail if the module is made immutable via the runtime or bundler
359
+ moduleExports.openInferencePatched = false;
360
+ } catch (e) {
361
+ diag.warn(`Failed to unset ${MODULE_NAME} patched flag on the module`, e);
362
+ }
331
363
  }
332
364
  }
333
365
 
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  // this is an auto-generated file. see js/scripts/version-update.js
2
- export const VERSION = "0.2.0";
2
+ export const VERSION = "0.3.0";