-
Notifications
You must be signed in to change notification settings - Fork 323
/
Copy pathsdk.js
446 lines (358 loc) · 12 KB
/
sdk.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
'use strict'
const { SPAN_KIND, OUTPUT_VALUE, INPUT_VALUE } = require('./constants/tags')
const {
getFunctionArguments,
validateKind
} = require('./util')
const { isTrue, isError } = require('../util')
const { storage } = require('./storage')
const Span = require('../opentracing/span')
const tracerVersion = require('../../../../package.json').version
const logger = require('../log')
const telemetry = require('./telemetry')
const LLMObsTagger = require('./tagger')
// communicating with writer
const { channel } = require('dc-polyfill')
const evalMetricAppendCh = channel('llmobs:eval-metric:append')
const flushCh = channel('llmobs:writers:flush')
const NoopLLMObs = require('./noop')
class LLMObs extends NoopLLMObs {
constructor (tracer, llmobsModule, config) {
super(tracer)
this._config = config
this._llmobsModule = llmobsModule
this._tagger = new LLMObsTagger(config)
}
get enabled () {
return this._config.llmobs.enabled
}
enable (options = {}) {
if (this.enabled) {
logger.debug('LLMObs is already enabled.')
return
}
logger.debug('Enabling LLMObs')
const { mlApp, agentlessEnabled } = options
const { DD_LLMOBS_ENABLED } = process.env
const llmobsConfig = {
mlApp,
agentlessEnabled
}
const enabled = DD_LLMOBS_ENABLED == null || isTrue(DD_LLMOBS_ENABLED)
if (!enabled) {
logger.debug('LLMObs.enable() called when DD_LLMOBS_ENABLED is false. No action taken.')
return
}
this._config.llmobs.enabled = true
this._config.configure({ ...this._config, llmobs: llmobsConfig })
// configure writers and channel subscribers
this._llmobsModule.enable(this._config)
}
disable () {
if (!this.enabled) {
logger.debug('LLMObs is already disabled.')
return
}
logger.debug('Disabling LLMObs')
this._config.llmobs.enabled = false
// disable writers and channel subscribers
this._llmobsModule.disable()
}
trace (options = {}, fn) {
if (typeof options === 'function') {
fn = options
options = {}
}
const kind = validateKind(options.kind) // will throw if kind is undefined or not an expected kind
telemetry.incrementLLMObsSpanStartCount({ autoinstrumented: false, kind })
// name is required for spans generated with `trace`
// while `kind` is required, this should never throw (as otherwise it would have thrown above)
const name = options.name || kind
if (!name) {
throw new Error('No span name provided for `trace`.')
}
const {
spanOptions,
...llmobsOptions
} = this._extractOptions(options)
if (fn.length > 1) {
return this._tracer.trace(name, spanOptions, (span, cb) =>
this._activate(span, { kind, ...llmobsOptions }, () => fn(span, cb))
)
}
return this._tracer.trace(name, spanOptions, span =>
this._activate(span, { kind, ...llmobsOptions }, () => fn(span))
)
}
wrap (options = {}, fn) {
if (typeof options === 'function') {
fn = options
options = {}
}
const kind = validateKind(options.kind) // will throw if kind is undefined or not an expected kind
let name = options.name || (fn?.name ? fn.name : undefined) || kind
if (!name) {
logger.warn('No span name provided for `wrap`. Defaulting to "unnamed-anonymous-function".')
name = 'unnamed-anonymous-function'
}
const {
spanOptions,
...llmobsOptions
} = this._extractOptions(options)
const llmobs = this
function wrapped () {
telemetry.incrementLLMObsSpanStartCount({ autoinstrumented: false, kind })
const span = llmobs._tracer.scope().active()
const fnArgs = arguments
const lastArgId = fnArgs.length - 1
const cb = fnArgs[lastArgId]
const hasCallback = typeof cb === 'function'
if (hasCallback) {
const scopeBoundCb = llmobs._bind(cb)
fnArgs[lastArgId] = function () {
// it is standard practice to follow the callback signature (err, result)
// however, we try to parse the arguments to determine if the first argument is an error
// if it is not, and is not undefined, we will use that for the output value
const maybeError = arguments[0]
const maybeResult = arguments[1]
llmobs._autoAnnotate(
span,
kind,
getFunctionArguments(fn, fnArgs),
isError(maybeError) || maybeError == null ? maybeResult : maybeError
)
return scopeBoundCb.apply(this, arguments)
}
}
try {
const result = llmobs._activate(span, { kind, ...llmobsOptions }, () => fn.apply(this, fnArgs))
if (result && typeof result.then === 'function') {
return result.then(
value => {
if (!hasCallback) {
llmobs._autoAnnotate(span, kind, getFunctionArguments(fn, fnArgs), value)
}
return value
},
err => {
llmobs._autoAnnotate(span, kind, getFunctionArguments(fn, fnArgs))
throw err
}
)
}
// it is possible to return a value and have a callback
// however, since the span finishes when the callback is called, it is possible that
// the callback is called before the function returns (although unlikely)
// we do not want to throw for "annotating a finished span" in this case
if (!hasCallback) {
llmobs._autoAnnotate(span, kind, getFunctionArguments(fn, fnArgs), result)
}
return result
} catch (e) {
llmobs._autoAnnotate(span, kind, getFunctionArguments(fn, fnArgs))
throw e
}
}
return this._tracer.wrap(name, spanOptions, wrapped)
}
annotate (span, options) {
if (!this.enabled) return
if (!span) {
span = this._active()
}
if ((span && !options) && !(span instanceof Span)) {
options = span
span = this._active()
}
if (!span) {
throw new Error('No span provided and no active LLMObs-generated span found')
}
if (!options) {
throw new Error('No options provided for annotation.')
}
if (!LLMObsTagger.tagMap.has(span)) {
throw new Error('Span must be an LLMObs-generated span')
}
if (span._duration !== undefined) {
throw new Error('Cannot annotate a finished span')
}
const spanKind = LLMObsTagger.tagMap.get(span)[SPAN_KIND]
if (!spanKind) {
throw new Error('LLMObs span must have a span kind specified')
}
const { inputData, outputData, metadata, metrics, tags } = options
if (inputData || outputData) {
if (spanKind === 'llm') {
this._tagger.tagLLMIO(span, inputData, outputData)
} else if (spanKind === 'embedding') {
this._tagger.tagEmbeddingIO(span, inputData, outputData)
} else if (spanKind === 'retrieval') {
this._tagger.tagRetrievalIO(span, inputData, outputData)
} else {
this._tagger.tagTextIO(span, inputData, outputData)
}
}
if (metadata) {
this._tagger.tagMetadata(span, metadata)
}
if (metrics) {
this._tagger.tagMetrics(span, metrics)
}
if (tags) {
this._tagger.tagSpanTags(span, tags)
}
}
exportSpan (span) {
span = span || this._active()
if (!span) {
throw new Error('No span provided and no active LLMObs-generated span found')
}
if (!(span instanceof Span)) {
throw new Error('Span must be a valid Span object.')
}
if (!LLMObsTagger.tagMap.has(span)) {
throw new Error('Span must be an LLMObs-generated span')
}
try {
return {
traceId: span.context().toTraceId(true),
spanId: span.context().toSpanId()
}
} catch {
logger.warn('Faild to export span. Span must be a valid Span object.')
}
}
submitEvaluation (llmobsSpanContext, options = {}) {
if (!this.enabled) return
if (!this._config.apiKey) {
throw new Error(
'DD_API_KEY is required for sending evaluation metrics. Evaluation metric data will not be sent.\n' +
'Ensure this configuration is set before running your application.'
)
}
const { traceId, spanId } = llmobsSpanContext
if (!traceId || !spanId) {
throw new Error(
'spanId and traceId must both be specified for the given evaluation metric to be submitted.'
)
}
const mlApp = options.mlApp || this._config.llmobs.mlApp
if (!mlApp) {
throw new Error(
'ML App name is required for sending evaluation metrics. Evaluation metric data will not be sent.'
)
}
const timestampMs = options.timestampMs || Date.now()
if (typeof timestampMs !== 'number' || timestampMs < 0) {
throw new Error('timestampMs must be a non-negative integer. Evaluation metric data will not be sent')
}
const { label, value, tags } = options
const metricType = options.metricType?.toLowerCase()
if (!label) {
throw new Error('label must be the specified name of the evaluation metric')
}
if (!metricType || !['categorical', 'score'].includes(metricType)) {
throw new Error('metricType must be one of "categorical" or "score"')
}
if (metricType === 'categorical' && typeof value !== 'string') {
throw new Error('value must be a string for a categorical metric.')
}
if (metricType === 'score' && typeof value !== 'number') {
throw new Error('value must be a number for a score metric.')
}
const evaluationTags = {
'ddtrace.version': tracerVersion,
ml_app: mlApp
}
if (tags) {
for (const key in tags) {
const tag = tags[key]
if (typeof tag === 'string') {
evaluationTags[key] = tag
} else if (typeof tag.toString === 'function') {
evaluationTags[key] = tag.toString()
} else if (tag == null) {
evaluationTags[key] = Object.prototype.toString.call(tag)
} else {
// should be a rare case
// every object in JS has a toString, otherwise every primitive has its own toString
// null and undefined are handled above
throw new Error('Failed to parse tags. Tags for evaluation metrics must be strings')
}
}
}
const payload = {
span_id: spanId,
trace_id: traceId,
label,
metric_type: metricType,
ml_app: mlApp,
[`${metricType}_value`]: value,
timestamp_ms: timestampMs,
tags: Object.entries(evaluationTags).map(([key, value]) => `${key}:${value}`)
}
evalMetricAppendCh.publish(payload)
}
flush () {
if (!this.enabled) return
flushCh.publish()
}
_autoAnnotate (span, kind, input, output) {
const annotations = {}
if (input && !['llm', 'embedding'].includes(kind) && !LLMObsTagger.tagMap.get(span)?.[INPUT_VALUE]) {
annotations.inputData = input
}
if (output && !['llm', 'retrieval'].includes(kind) && !LLMObsTagger.tagMap.get(span)?.[OUTPUT_VALUE]) {
annotations.outputData = output
}
this.annotate(span, annotations)
}
_active () {
const store = storage.getStore()
return store?.span
}
_activate (span, options, fn) {
const parent = this._active()
if (this.enabled) storage.enterWith({ span })
if (options) {
this._tagger.registerLLMObsSpan(span, {
...options,
parent
})
}
try {
return fn()
} finally {
if (this.enabled) storage.enterWith({ span: parent })
}
}
// bind function to active LLMObs span
_bind (fn) {
if (typeof fn !== 'function') return fn
const llmobs = this
const activeSpan = llmobs._active()
const bound = function () {
return llmobs._activate(activeSpan, null, () => {
return fn.apply(this, arguments)
})
}
return bound
}
_extractOptions (options) {
const {
modelName,
modelProvider,
sessionId,
mlApp,
...spanOptions
} = options
return {
mlApp,
modelName,
modelProvider,
sessionId,
spanOptions
}
}
}
module.exports = LLMObs