@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.
- package/dist/esm/instrumentation.d.ts +19 -1
- package/dist/esm/instrumentation.d.ts.map +1 -1
- package/dist/esm/instrumentation.js +15 -7
- package/dist/esm/instrumentation.js.map +1 -1
- package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/esm/version.d.ts +1 -1
- package/dist/esm/version.js +1 -1
- package/dist/esnext/instrumentation.d.ts +19 -1
- package/dist/esnext/instrumentation.d.ts.map +1 -1
- package/dist/esnext/instrumentation.js +13 -7
- package/dist/esnext/instrumentation.js.map +1 -1
- package/dist/esnext/tsconfig.esnext.tsbuildinfo +1 -1
- package/dist/esnext/version.d.ts +1 -1
- package/dist/esnext/version.js +1 -1
- package/dist/src/instrumentation.d.ts +19 -1
- package/dist/src/instrumentation.d.ts.map +1 -1
- package/dist/src/instrumentation.js +12 -6
- package/dist/src/instrumentation.js.map +1 -1
- package/dist/src/version.d.ts +1 -1
- package/dist/src/version.js +1 -1
- package/dist/test/openai.test.js +141 -5
- package/dist/test/openai.test.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -3
- package/src/instrumentation.ts +46 -18
- package/src/version.ts +1 -1
package/src/instrumentation.ts
CHANGED
|
@@ -40,7 +40,11 @@ import {
|
|
|
40
40
|
import { assertUnreachable, isString } from "./typeUtils";
|
|
41
41
|
import { isTracingSuppressed } from "@opentelemetry/core";
|
|
42
42
|
|
|
43
|
-
import {
|
|
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
|
-
|
|
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({},
|
|
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.
|
|
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.
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
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.
|
|
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.
|
|
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.
|
|
2
|
+
export const VERSION = "1.0.0";
|