@arizeai/openinference-instrumentation-openai 0.6.0 → 1.0.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,7 +40,11 @@ import {
40
40
  import { assertUnreachable, isString } from "./typeUtils";
41
41
  import { isTracingSuppressed } from "@opentelemetry/core";
42
42
 
43
- import { safelyJSONStringify } from "@arizeai/openinference-core";
43
+ import {
44
+ OITracer,
45
+ safelyJSONStringify,
46
+ TraceConfigOptions,
47
+ } from "@arizeai/openinference-core";
44
48
 
45
49
  const MODULE_NAME = "openai";
46
50
 
@@ -74,13 +78,34 @@ function getExecContext(span: Span) {
74
78
  }
75
79
  return execContext;
76
80
  }
81
+ /**
82
+ * An auto instrumentation class for OpenAI that creates {@link https://github.com/Arize-ai/openinference/blob/main/spec/semantic_conventions.md|OpenInference} Compliant spans for the OpenAI API
83
+ * @param instrumentationConfig The config for the instrumentation @see {@link InstrumentationConfig}
84
+ * @param traceConfig The OpenInference trace configuration. Can be used to mask or redact sensitive information on spans. @see {@link TraceConfigOptions}
85
+ */
77
86
  export class OpenAIInstrumentation extends InstrumentationBase<typeof openai> {
78
- constructor(config?: InstrumentationConfig) {
87
+ private oiTracer: OITracer;
88
+ constructor({
89
+ instrumentationConfig,
90
+ traceConfig,
91
+ }: {
92
+ /**
93
+ * The config for the instrumentation
94
+ * @see {@link InstrumentationConfig}
95
+ */
96
+ instrumentationConfig?: InstrumentationConfig;
97
+ /**
98
+ * The OpenInference trace configuration. Can be used to mask or redact sensitive information on spans.
99
+ * @see {@link TraceConfigOptions}
100
+ */
101
+ traceConfig?: TraceConfigOptions;
102
+ } = {}) {
79
103
  super(
80
104
  "@arizeai/openinference-instrumentation-openai",
81
105
  VERSION,
82
- Object.assign({}, config),
106
+ Object.assign({}, instrumentationConfig),
83
107
  );
108
+ this.oiTracer = new OITracer({ tracer: this.tracer, traceConfig });
84
109
  }
85
110
 
86
111
  protected init(): InstrumentationModuleDefinition<typeof openai> {
@@ -131,7 +156,7 @@ export class OpenAIInstrumentation extends InstrumentationBase<typeof openai> {
131
156
  ) {
132
157
  const body = args[0];
133
158
  const { messages: _messages, ...invocationParameters } = body;
134
- const span = instrumentation.tracer.startSpan(
159
+ const span = instrumentation.oiTracer.startSpan(
135
160
  `OpenAI Chat Completions`,
136
161
  {
137
162
  kind: SpanKind.INTERNAL,
@@ -216,19 +241,22 @@ export class OpenAIInstrumentation extends InstrumentationBase<typeof openai> {
216
241
  ) {
217
242
  const body = args[0];
218
243
  const { prompt: _prompt, ...invocationParameters } = body;
219
- const span = instrumentation.tracer.startSpan(`OpenAI Completions`, {
220
- kind: SpanKind.INTERNAL,
221
- attributes: {
222
- [SemanticConventions.OPENINFERENCE_SPAN_KIND]:
223
- OpenInferenceSpanKind.LLM,
224
- [SemanticConventions.LLM_MODEL_NAME]: body.model,
225
- [SemanticConventions.LLM_INVOCATION_PARAMETERS]:
226
- JSON.stringify(invocationParameters),
227
- [SemanticConventions.LLM_SYSTEM]: LLMSystem.OPENAI,
228
- [SemanticConventions.LLM_PROVIDER]: LLMProvider.OPENAI,
229
- ...getCompletionInputValueAndMimeType(body),
244
+ const span = instrumentation.oiTracer.startSpan(
245
+ `OpenAI Completions`,
246
+ {
247
+ kind: SpanKind.INTERNAL,
248
+ attributes: {
249
+ [SemanticConventions.OPENINFERENCE_SPAN_KIND]:
250
+ OpenInferenceSpanKind.LLM,
251
+ [SemanticConventions.LLM_MODEL_NAME]: body.model,
252
+ [SemanticConventions.LLM_INVOCATION_PARAMETERS]:
253
+ JSON.stringify(invocationParameters),
254
+ [SemanticConventions.LLM_SYSTEM]: LLMSystem.OPENAI,
255
+ [SemanticConventions.LLM_PROVIDER]: LLMProvider.OPENAI,
256
+ ...getCompletionInputValueAndMimeType(body),
257
+ },
230
258
  },
231
- });
259
+ );
232
260
  const execContext = getExecContext(span);
233
261
 
234
262
  const execPromise = safeExecuteInTheMiddle<
@@ -287,7 +315,7 @@ export class OpenAIInstrumentation extends InstrumentationBase<typeof openai> {
287
315
  const body = args[0];
288
316
  const { input } = body;
289
317
  const isStringInput = typeof input === "string";
290
- const span = instrumentation.tracer.startSpan(`OpenAI Embeddings`, {
318
+ const span = instrumentation.oiTracer.startSpan(`OpenAI Embeddings`, {
291
319
  kind: SpanKind.INTERNAL,
292
320
  attributes: {
293
321
  [SemanticConventions.OPENINFERENCE_SPAN_KIND]:
@@ -345,7 +373,7 @@ export class OpenAIInstrumentation extends InstrumentationBase<typeof openai> {
345
373
  // This can fail if the module is made immutable via the runtime or bundler
346
374
  module.openInferencePatched = true;
347
375
  } catch (e) {
348
- diag.warn(`Failed to set ${MODULE_NAME} patched flag on the module`, e);
376
+ diag.debug(`Failed to set ${MODULE_NAME} patched flag on the module`, e);
349
377
  }
350
378
 
351
379
  return module;
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.6.0";
2
+ export const VERSION = "1.0.0";