openlayer 0.1.24 → 0.1.26
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/index.d.ts +3 -1
- package/dist/index.js +38 -18
- package/examples/openai_monitor.mjs +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -189,11 +189,13 @@ export declare class OpenlayerClient {
|
|
|
189
189
|
streamData: (data: StreamingData, config: StreamingDataConfig, inferencePipelineId: string) => Promise<void>;
|
|
190
190
|
}
|
|
191
191
|
export declare class OpenAIMonitor {
|
|
192
|
+
private inferencePipeline?;
|
|
192
193
|
private openlayerClient;
|
|
193
194
|
private openAIClient;
|
|
194
195
|
private openlayerProjectName;
|
|
195
196
|
private openlayerInferencePipelineName;
|
|
196
197
|
private monitoringOn;
|
|
198
|
+
private project?;
|
|
197
199
|
/**
|
|
198
200
|
* Constructs an OpenAIMonitor instance.
|
|
199
201
|
* @param {OpenAIMonitorConstructorProps} props - The configuration properties for the OpenAI and Openlayer clients.
|
|
@@ -220,7 +222,7 @@ export declare class OpenAIMonitor {
|
|
|
220
222
|
/**
|
|
221
223
|
* Starts monitoring for the OpenAI Monitor instance. If monitoring is already active, a warning is logged.
|
|
222
224
|
*/
|
|
223
|
-
startMonitoring(): void
|
|
225
|
+
startMonitoring(): Promise<void>;
|
|
224
226
|
/**
|
|
225
227
|
* Stops monitoring for the OpenAI Monitor instance. If monitoring is not active, a warning is logged.
|
|
226
228
|
*/
|
package/dist/index.js
CHANGED
|
@@ -30,8 +30,12 @@ const OpenAIPricing = {
|
|
|
30
30
|
output: 0.002,
|
|
31
31
|
},
|
|
32
32
|
'gpt-3.5-turbo': {
|
|
33
|
-
input: 0.
|
|
34
|
-
output: 0.
|
|
33
|
+
input: 0.0005,
|
|
34
|
+
output: 0.0015,
|
|
35
|
+
},
|
|
36
|
+
'gpt-3.5-turbo-0125': {
|
|
37
|
+
input: 0.0005,
|
|
38
|
+
output: 0.0015,
|
|
35
39
|
},
|
|
36
40
|
'gpt-3.5-turbo-0301': {
|
|
37
41
|
input: 0.0015,
|
|
@@ -57,6 +61,10 @@ const OpenAIPricing = {
|
|
|
57
61
|
input: 0.03,
|
|
58
62
|
output: 0.06,
|
|
59
63
|
},
|
|
64
|
+
'gpt-4-0125-preview': {
|
|
65
|
+
input: 0.01,
|
|
66
|
+
output: 0.03,
|
|
67
|
+
},
|
|
60
68
|
'gpt-4-0314': {
|
|
61
69
|
input: 0.03,
|
|
62
70
|
output: 0.06,
|
|
@@ -321,8 +329,9 @@ class OpenAIMonitor {
|
|
|
321
329
|
if (!this.monitoringOn) {
|
|
322
330
|
throw new Error('Monitoring is not active.');
|
|
323
331
|
}
|
|
324
|
-
|
|
325
|
-
|
|
332
|
+
if (typeof this.inferencePipeline === 'undefined') {
|
|
333
|
+
throw new Error('No inference pipeline found.');
|
|
334
|
+
}
|
|
326
335
|
// Start a timer to measure latency
|
|
327
336
|
const startTime = Date.now();
|
|
328
337
|
// Accumulate output for streamed responses
|
|
@@ -359,7 +368,7 @@ class OpenAIMonitor {
|
|
|
359
368
|
}
|
|
360
369
|
const endTime = Date.now();
|
|
361
370
|
const latency = endTime - startTime;
|
|
362
|
-
this.openlayerClient.streamData(Object.assign(Object.assign({ latency, output: streamedOutput, timestamp: startTime }, inputVariablesMap), additionalLogs), config, inferencePipeline.id);
|
|
371
|
+
this.openlayerClient.streamData(Object.assign(Object.assign({ latency, output: streamedOutput, timestamp: startTime }, inputVariablesMap), additionalLogs), config, this.inferencePipeline.id);
|
|
363
372
|
}
|
|
364
373
|
else {
|
|
365
374
|
const nonStreamedResponse = response;
|
|
@@ -375,7 +384,7 @@ class OpenAIMonitor {
|
|
|
375
384
|
throw new Error('No output received from OpenAI.');
|
|
376
385
|
}
|
|
377
386
|
this.openlayerClient.streamData(Object.assign(Object.assign({ cost,
|
|
378
|
-
latency, model: nonStreamedResponse.model, output, timestamp: startTime, tokens }, inputVariablesMap), additionalLogs), config, inferencePipeline.id);
|
|
387
|
+
latency, model: nonStreamedResponse.model, output, timestamp: startTime, tokens }, inputVariablesMap), additionalLogs), config, this.inferencePipeline.id);
|
|
379
388
|
}
|
|
380
389
|
return response;
|
|
381
390
|
});
|
|
@@ -392,11 +401,12 @@ class OpenAIMonitor {
|
|
|
392
401
|
if (!this.monitoringOn) {
|
|
393
402
|
throw new Error('Monitoring is not active.');
|
|
394
403
|
}
|
|
404
|
+
if (typeof this.inferencePipeline === 'undefined') {
|
|
405
|
+
throw new Error('No inference pipeline found.');
|
|
406
|
+
}
|
|
395
407
|
if (!body.prompt) {
|
|
396
408
|
throw new Error('No prompt provided.');
|
|
397
409
|
}
|
|
398
|
-
const project = yield this.openlayerClient.createProject(this.openlayerProjectName, 'llm-base');
|
|
399
|
-
const inferencePipeline = yield this.openlayerClient.createInferencePipeline(project.id, this.openlayerInferencePipelineName);
|
|
400
410
|
// Start a timer to measure latency
|
|
401
411
|
const startTime = Date.now();
|
|
402
412
|
// Accumulate output and tokens data for streamed responses
|
|
@@ -432,7 +442,7 @@ class OpenAIMonitor {
|
|
|
432
442
|
const endTime = Date.now();
|
|
433
443
|
const latency = endTime - startTime;
|
|
434
444
|
const cost = this.cost(streamedModel, streamedInputTokens, streamedOutputTokens);
|
|
435
|
-
this.openlayerClient.streamData(Object.assign({ cost, input: body.prompt, latency, output: streamedOutput, timestamp: startTime, tokens: streamedTokens }, additionalLogs), config, inferencePipeline.id);
|
|
445
|
+
this.openlayerClient.streamData(Object.assign({ cost, input: body.prompt, latency, output: streamedOutput, timestamp: startTime, tokens: streamedTokens }, additionalLogs), config, this.inferencePipeline.id);
|
|
436
446
|
}
|
|
437
447
|
else {
|
|
438
448
|
const nonStreamedResponse = response;
|
|
@@ -443,7 +453,7 @@ class OpenAIMonitor {
|
|
|
443
453
|
const inputTokens = (_z = (_y = nonStreamedResponse.usage) === null || _y === void 0 ? void 0 : _y.prompt_tokens) !== null && _z !== void 0 ? _z : 0;
|
|
444
454
|
const outputTokens = (_1 = (_0 = nonStreamedResponse.usage) === null || _0 === void 0 ? void 0 : _0.completion_tokens) !== null && _1 !== void 0 ? _1 : 0;
|
|
445
455
|
const cost = this.cost(nonStreamedResponse.model, inputTokens, outputTokens);
|
|
446
|
-
this.openlayerClient.streamData(Object.assign({ cost, input: body.prompt, latency, output: nonStreamedResponse.choices[0].text, timestamp: startTime, tokens }, additionalLogs), config, inferencePipeline.id);
|
|
456
|
+
this.openlayerClient.streamData(Object.assign({ cost, input: body.prompt, latency, output: nonStreamedResponse.choices[0].text, timestamp: startTime, tokens }, additionalLogs), config, this.inferencePipeline.id);
|
|
447
457
|
}
|
|
448
458
|
return response;
|
|
449
459
|
});
|
|
@@ -464,23 +474,33 @@ class OpenAIMonitor {
|
|
|
464
474
|
* Starts monitoring for the OpenAI Monitor instance. If monitoring is already active, a warning is logged.
|
|
465
475
|
*/
|
|
466
476
|
startMonitoring() {
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
477
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
478
|
+
if (this.monitoringOn) {
|
|
479
|
+
console.warn('Monitor is already on.');
|
|
480
|
+
return;
|
|
481
|
+
}
|
|
482
|
+
console.info('Starting monitor: creating or loading an Openlayer project and inference pipeline...');
|
|
483
|
+
this.monitoringOn = true;
|
|
484
|
+
this.project = yield this.openlayerClient.createProject(this.openlayerProjectName, 'llm-base');
|
|
485
|
+
if (typeof this.project !== 'undefined') {
|
|
486
|
+
this.inferencePipeline =
|
|
487
|
+
yield this.openlayerClient.createInferencePipeline(this.project.id, this.openlayerInferencePipelineName);
|
|
488
|
+
}
|
|
489
|
+
console.info('Monitor started');
|
|
490
|
+
});
|
|
473
491
|
}
|
|
474
492
|
/**
|
|
475
493
|
* Stops monitoring for the OpenAI Monitor instance. If monitoring is not active, a warning is logged.
|
|
476
494
|
*/
|
|
477
495
|
stopMonitoring() {
|
|
478
496
|
if (!this.monitoringOn) {
|
|
479
|
-
console.warn('
|
|
497
|
+
console.warn('Monitor is not active.');
|
|
480
498
|
return;
|
|
481
499
|
}
|
|
482
500
|
this.monitoringOn = false;
|
|
483
|
-
|
|
501
|
+
this.project = undefined;
|
|
502
|
+
this.inferencePipeline = undefined;
|
|
503
|
+
console.info('Monitor stopped.');
|
|
484
504
|
}
|
|
485
505
|
}
|
|
486
506
|
exports.OpenAIMonitor = OpenAIMonitor;
|