openlayer 0.1.24 → 0.1.25
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 +28 -16
- 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
|
@@ -321,8 +321,9 @@ class OpenAIMonitor {
|
|
|
321
321
|
if (!this.monitoringOn) {
|
|
322
322
|
throw new Error('Monitoring is not active.');
|
|
323
323
|
}
|
|
324
|
-
|
|
325
|
-
|
|
324
|
+
if (typeof this.inferencePipeline === 'undefined') {
|
|
325
|
+
throw new Error('No inference pipeline found.');
|
|
326
|
+
}
|
|
326
327
|
// Start a timer to measure latency
|
|
327
328
|
const startTime = Date.now();
|
|
328
329
|
// Accumulate output for streamed responses
|
|
@@ -359,7 +360,7 @@ class OpenAIMonitor {
|
|
|
359
360
|
}
|
|
360
361
|
const endTime = Date.now();
|
|
361
362
|
const latency = endTime - startTime;
|
|
362
|
-
this.openlayerClient.streamData(Object.assign(Object.assign({ latency, output: streamedOutput, timestamp: startTime }, inputVariablesMap), additionalLogs), config, inferencePipeline.id);
|
|
363
|
+
this.openlayerClient.streamData(Object.assign(Object.assign({ latency, output: streamedOutput, timestamp: startTime }, inputVariablesMap), additionalLogs), config, this.inferencePipeline.id);
|
|
363
364
|
}
|
|
364
365
|
else {
|
|
365
366
|
const nonStreamedResponse = response;
|
|
@@ -375,7 +376,7 @@ class OpenAIMonitor {
|
|
|
375
376
|
throw new Error('No output received from OpenAI.');
|
|
376
377
|
}
|
|
377
378
|
this.openlayerClient.streamData(Object.assign(Object.assign({ cost,
|
|
378
|
-
latency, model: nonStreamedResponse.model, output, timestamp: startTime, tokens }, inputVariablesMap), additionalLogs), config, inferencePipeline.id);
|
|
379
|
+
latency, model: nonStreamedResponse.model, output, timestamp: startTime, tokens }, inputVariablesMap), additionalLogs), config, this.inferencePipeline.id);
|
|
379
380
|
}
|
|
380
381
|
return response;
|
|
381
382
|
});
|
|
@@ -392,11 +393,12 @@ class OpenAIMonitor {
|
|
|
392
393
|
if (!this.monitoringOn) {
|
|
393
394
|
throw new Error('Monitoring is not active.');
|
|
394
395
|
}
|
|
396
|
+
if (typeof this.inferencePipeline === 'undefined') {
|
|
397
|
+
throw new Error('No inference pipeline found.');
|
|
398
|
+
}
|
|
395
399
|
if (!body.prompt) {
|
|
396
400
|
throw new Error('No prompt provided.');
|
|
397
401
|
}
|
|
398
|
-
const project = yield this.openlayerClient.createProject(this.openlayerProjectName, 'llm-base');
|
|
399
|
-
const inferencePipeline = yield this.openlayerClient.createInferencePipeline(project.id, this.openlayerInferencePipelineName);
|
|
400
402
|
// Start a timer to measure latency
|
|
401
403
|
const startTime = Date.now();
|
|
402
404
|
// Accumulate output and tokens data for streamed responses
|
|
@@ -432,7 +434,7 @@ class OpenAIMonitor {
|
|
|
432
434
|
const endTime = Date.now();
|
|
433
435
|
const latency = endTime - startTime;
|
|
434
436
|
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);
|
|
437
|
+
this.openlayerClient.streamData(Object.assign({ cost, input: body.prompt, latency, output: streamedOutput, timestamp: startTime, tokens: streamedTokens }, additionalLogs), config, this.inferencePipeline.id);
|
|
436
438
|
}
|
|
437
439
|
else {
|
|
438
440
|
const nonStreamedResponse = response;
|
|
@@ -443,7 +445,7 @@ class OpenAIMonitor {
|
|
|
443
445
|
const inputTokens = (_z = (_y = nonStreamedResponse.usage) === null || _y === void 0 ? void 0 : _y.prompt_tokens) !== null && _z !== void 0 ? _z : 0;
|
|
444
446
|
const outputTokens = (_1 = (_0 = nonStreamedResponse.usage) === null || _0 === void 0 ? void 0 : _0.completion_tokens) !== null && _1 !== void 0 ? _1 : 0;
|
|
445
447
|
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);
|
|
448
|
+
this.openlayerClient.streamData(Object.assign({ cost, input: body.prompt, latency, output: nonStreamedResponse.choices[0].text, timestamp: startTime, tokens }, additionalLogs), config, this.inferencePipeline.id);
|
|
447
449
|
}
|
|
448
450
|
return response;
|
|
449
451
|
});
|
|
@@ -464,23 +466,33 @@ class OpenAIMonitor {
|
|
|
464
466
|
* Starts monitoring for the OpenAI Monitor instance. If monitoring is already active, a warning is logged.
|
|
465
467
|
*/
|
|
466
468
|
startMonitoring() {
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
469
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
470
|
+
if (this.monitoringOn) {
|
|
471
|
+
console.warn('Monitor is already on.');
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
474
|
+
console.info('Starting monitor: creating or loading an Openlayer project and inference pipeline...');
|
|
475
|
+
this.monitoringOn = true;
|
|
476
|
+
this.project = yield this.openlayerClient.createProject(this.openlayerProjectName, 'llm-base');
|
|
477
|
+
if (typeof this.project !== 'undefined') {
|
|
478
|
+
this.inferencePipeline =
|
|
479
|
+
yield this.openlayerClient.createInferencePipeline(this.project.id, this.openlayerInferencePipelineName);
|
|
480
|
+
}
|
|
481
|
+
console.info('Monitor started');
|
|
482
|
+
});
|
|
473
483
|
}
|
|
474
484
|
/**
|
|
475
485
|
* Stops monitoring for the OpenAI Monitor instance. If monitoring is not active, a warning is logged.
|
|
476
486
|
*/
|
|
477
487
|
stopMonitoring() {
|
|
478
488
|
if (!this.monitoringOn) {
|
|
479
|
-
console.warn('
|
|
489
|
+
console.warn('Monitor is not active.');
|
|
480
490
|
return;
|
|
481
491
|
}
|
|
482
492
|
this.monitoringOn = false;
|
|
483
|
-
|
|
493
|
+
this.project = undefined;
|
|
494
|
+
this.inferencePipeline = undefined;
|
|
495
|
+
console.info('Monitor stopped.');
|
|
484
496
|
}
|
|
485
497
|
}
|
|
486
498
|
exports.OpenAIMonitor = OpenAIMonitor;
|