@arizeai/openinference-instrumentation-openai 0.1.4 → 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
@@ -75,6 +88,16 @@ export class OpenAIInstrumentation extends InstrumentationBase<typeof openai> {
75
88
  );
76
89
  return module;
77
90
  }
91
+
92
+ /**
93
+ * Manually instruments the OpenAI module. This is needed when the module is not loaded via require (commonjs)
94
+ * @param {openai} module
95
+ */
96
+ manuallyInstrument(module: typeof openai) {
97
+ diag.debug(`Manually instrumenting ${MODULE_NAME}`);
98
+ this.patch(module);
99
+ }
100
+
78
101
  /**
79
102
  * Patches the OpenAI module
80
103
  */
@@ -83,7 +106,7 @@ export class OpenAIInstrumentation extends InstrumentationBase<typeof openai> {
83
106
  moduleVersion?: string,
84
107
  ) {
85
108
  diag.debug(`Applying patch for ${MODULE_NAME}@${moduleVersion}`);
86
- if (module?.openInferencePatched) {
109
+ if (module?.openInferencePatched || _isOpenInferencePatched) {
87
110
  return module;
88
111
  }
89
112
  // eslint-disable-next-line @typescript-eslint/no-this-alias
@@ -308,16 +331,35 @@ export class OpenAIInstrumentation extends InstrumentationBase<typeof openai> {
308
331
  },
309
332
  );
310
333
 
311
- 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
+
312
342
  return module;
313
343
  }
314
344
  /**
315
345
  * Un-patches the OpenAI module's chat completions API
316
346
  */
317
- private unpatch(moduleExports: typeof openai, moduleVersion?: string) {
347
+ private unpatch(
348
+ moduleExports: typeof openai & { openInferencePatched?: boolean },
349
+ moduleVersion?: string,
350
+ ) {
318
351
  diag.debug(`Removing patch for ${MODULE_NAME}@${moduleVersion}`);
319
352
  this._unwrap(moduleExports.OpenAI.Chat.Completions.prototype, "create");
353
+ this._unwrap(moduleExports.OpenAI.Completions.prototype, "create");
320
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
+ }
321
363
  }
322
364
  }
323
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.1.4";
2
+ export const VERSION = "0.3.0";