@arizeai/openinference-instrumentation-openai 0.5.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.
@@ -20,6 +20,8 @@ import {
20
20
  SemanticConventions,
21
21
  OpenInferenceSpanKind,
22
22
  MimeType,
23
+ LLMSystem,
24
+ LLMProvider,
23
25
  } from "@arizeai/openinference-semantic-conventions";
24
26
  import {
25
27
  ChatCompletion,
@@ -38,7 +40,11 @@ import {
38
40
  import { assertUnreachable, isString } from "./typeUtils";
39
41
  import { isTracingSuppressed } from "@opentelemetry/core";
40
42
 
41
- import { safelyJSONStringify } from "@arizeai/openinference-core";
43
+ import {
44
+ OITracer,
45
+ safelyJSONStringify,
46
+ TraceConfigOptions,
47
+ } from "@arizeai/openinference-core";
42
48
 
43
49
  const MODULE_NAME = "openai";
44
50
 
@@ -72,13 +78,34 @@ function getExecContext(span: Span) {
72
78
  }
73
79
  return execContext;
74
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
+ */
75
86
  export class OpenAIInstrumentation extends InstrumentationBase<typeof openai> {
76
- 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
+ } = {}) {
77
103
  super(
78
104
  "@arizeai/openinference-instrumentation-openai",
79
105
  VERSION,
80
- Object.assign({}, config),
106
+ Object.assign({}, instrumentationConfig),
81
107
  );
108
+ this.oiTracer = new OITracer({ tracer: this.tracer, traceConfig });
82
109
  }
83
110
 
84
111
  protected init(): InstrumentationModuleDefinition<typeof openai> {
@@ -129,7 +156,7 @@ export class OpenAIInstrumentation extends InstrumentationBase<typeof openai> {
129
156
  ) {
130
157
  const body = args[0];
131
158
  const { messages: _messages, ...invocationParameters } = body;
132
- const span = instrumentation.tracer.startSpan(
159
+ const span = instrumentation.oiTracer.startSpan(
133
160
  `OpenAI Chat Completions`,
134
161
  {
135
162
  kind: SpanKind.INTERNAL,
@@ -141,6 +168,8 @@ export class OpenAIInstrumentation extends InstrumentationBase<typeof openai> {
141
168
  [SemanticConventions.INPUT_MIME_TYPE]: MimeType.JSON,
142
169
  [SemanticConventions.LLM_INVOCATION_PARAMETERS]:
143
170
  JSON.stringify(invocationParameters),
171
+ [SemanticConventions.LLM_SYSTEM]: LLMSystem.OPENAI,
172
+ [SemanticConventions.LLM_PROVIDER]: LLMProvider.OPENAI,
144
173
  ...getLLMInputMessagesAttributes(body),
145
174
  ...getLLMToolsJSONSchema(body),
146
175
  },
@@ -212,17 +241,22 @@ export class OpenAIInstrumentation extends InstrumentationBase<typeof openai> {
212
241
  ) {
213
242
  const body = args[0];
214
243
  const { prompt: _prompt, ...invocationParameters } = body;
215
- const span = instrumentation.tracer.startSpan(`OpenAI Completions`, {
216
- kind: SpanKind.INTERNAL,
217
- attributes: {
218
- [SemanticConventions.OPENINFERENCE_SPAN_KIND]:
219
- OpenInferenceSpanKind.LLM,
220
- [SemanticConventions.LLM_MODEL_NAME]: body.model,
221
- [SemanticConventions.LLM_INVOCATION_PARAMETERS]:
222
- JSON.stringify(invocationParameters),
223
- ...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
+ },
224
258
  },
225
- });
259
+ );
226
260
  const execContext = getExecContext(span);
227
261
 
228
262
  const execPromise = safeExecuteInTheMiddle<
@@ -281,7 +315,7 @@ export class OpenAIInstrumentation extends InstrumentationBase<typeof openai> {
281
315
  const body = args[0];
282
316
  const { input } = body;
283
317
  const isStringInput = typeof input === "string";
284
- const span = instrumentation.tracer.startSpan(`OpenAI Embeddings`, {
318
+ const span = instrumentation.oiTracer.startSpan(`OpenAI Embeddings`, {
285
319
  kind: SpanKind.INTERNAL,
286
320
  attributes: {
287
321
  [SemanticConventions.OPENINFERENCE_SPAN_KIND]:
@@ -339,7 +373,7 @@ export class OpenAIInstrumentation extends InstrumentationBase<typeof openai> {
339
373
  // This can fail if the module is made immutable via the runtime or bundler
340
374
  module.openInferencePatched = true;
341
375
  } catch (e) {
342
- 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);
343
377
  }
344
378
 
345
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.5.0";
2
+ export const VERSION = "1.0.0";