openlayer 0.1.23 → 0.1.24
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 +1 -3
- package/dist/index.js +24 -29
- package/examples/openai_monitor.mjs +7 -6
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -189,13 +189,11 @@ 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?;
|
|
193
192
|
private openlayerClient;
|
|
194
193
|
private openAIClient;
|
|
195
194
|
private openlayerProjectName;
|
|
196
195
|
private openlayerInferencePipelineName;
|
|
197
196
|
private monitoringOn;
|
|
198
|
-
private project?;
|
|
199
197
|
/**
|
|
200
198
|
* Constructs an OpenAIMonitor instance.
|
|
201
199
|
* @param {OpenAIMonitorConstructorProps} props - The configuration properties for the OpenAI and Openlayer clients.
|
|
@@ -222,7 +220,7 @@ export declare class OpenAIMonitor {
|
|
|
222
220
|
/**
|
|
223
221
|
* Starts monitoring for the OpenAI Monitor instance. If monitoring is already active, a warning is logged.
|
|
224
222
|
*/
|
|
225
|
-
startMonitoring():
|
|
223
|
+
startMonitoring(): void;
|
|
226
224
|
/**
|
|
227
225
|
* Stops monitoring for the OpenAI Monitor instance. If monitoring is not active, a warning is logged.
|
|
228
226
|
*/
|
package/dist/index.js
CHANGED
|
@@ -61,6 +61,10 @@ const OpenAIPricing = {
|
|
|
61
61
|
input: 0.03,
|
|
62
62
|
output: 0.06,
|
|
63
63
|
},
|
|
64
|
+
'gpt-4-0613': {
|
|
65
|
+
input: 0.03,
|
|
66
|
+
output: 0.06,
|
|
67
|
+
},
|
|
64
68
|
'gpt-4-1106-preview': {
|
|
65
69
|
input: 0.01,
|
|
66
70
|
output: 0.03,
|
|
@@ -77,6 +81,10 @@ const OpenAIPricing = {
|
|
|
77
81
|
input: 0.06,
|
|
78
82
|
output: 0.12,
|
|
79
83
|
},
|
|
84
|
+
'gpt-4-32k-0613': {
|
|
85
|
+
input: 0.03,
|
|
86
|
+
output: 0.06,
|
|
87
|
+
},
|
|
80
88
|
};
|
|
81
89
|
class OpenlayerClient {
|
|
82
90
|
/**
|
|
@@ -313,9 +321,8 @@ class OpenAIMonitor {
|
|
|
313
321
|
if (!this.monitoringOn) {
|
|
314
322
|
throw new Error('Monitoring is not active.');
|
|
315
323
|
}
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
}
|
|
324
|
+
const project = yield this.openlayerClient.createProject(this.openlayerProjectName, 'llm-base');
|
|
325
|
+
const inferencePipeline = yield this.openlayerClient.createInferencePipeline(project.id, this.openlayerInferencePipelineName);
|
|
319
326
|
// Start a timer to measure latency
|
|
320
327
|
const startTime = Date.now();
|
|
321
328
|
// Accumulate output for streamed responses
|
|
@@ -352,7 +359,7 @@ class OpenAIMonitor {
|
|
|
352
359
|
}
|
|
353
360
|
const endTime = Date.now();
|
|
354
361
|
const latency = endTime - startTime;
|
|
355
|
-
this.openlayerClient.streamData(Object.assign(Object.assign({ latency, output: streamedOutput, timestamp: startTime }, inputVariablesMap), additionalLogs), config,
|
|
362
|
+
this.openlayerClient.streamData(Object.assign(Object.assign({ latency, output: streamedOutput, timestamp: startTime }, inputVariablesMap), additionalLogs), config, inferencePipeline.id);
|
|
356
363
|
}
|
|
357
364
|
else {
|
|
358
365
|
const nonStreamedResponse = response;
|
|
@@ -368,8 +375,7 @@ class OpenAIMonitor {
|
|
|
368
375
|
throw new Error('No output received from OpenAI.');
|
|
369
376
|
}
|
|
370
377
|
this.openlayerClient.streamData(Object.assign(Object.assign({ cost,
|
|
371
|
-
latency,
|
|
372
|
-
output, timestamp: startTime, tokens }, inputVariablesMap), additionalLogs), config, this.inferencePipeline.id);
|
|
378
|
+
latency, model: nonStreamedResponse.model, output, timestamp: startTime, tokens }, inputVariablesMap), additionalLogs), config, inferencePipeline.id);
|
|
373
379
|
}
|
|
374
380
|
return response;
|
|
375
381
|
});
|
|
@@ -386,12 +392,11 @@ class OpenAIMonitor {
|
|
|
386
392
|
if (!this.monitoringOn) {
|
|
387
393
|
throw new Error('Monitoring is not active.');
|
|
388
394
|
}
|
|
389
|
-
if (typeof this.inferencePipeline === 'undefined') {
|
|
390
|
-
throw new Error('No inference pipeline found.');
|
|
391
|
-
}
|
|
392
395
|
if (!body.prompt) {
|
|
393
396
|
throw new Error('No prompt provided.');
|
|
394
397
|
}
|
|
398
|
+
const project = yield this.openlayerClient.createProject(this.openlayerProjectName, 'llm-base');
|
|
399
|
+
const inferencePipeline = yield this.openlayerClient.createInferencePipeline(project.id, this.openlayerInferencePipelineName);
|
|
395
400
|
// Start a timer to measure latency
|
|
396
401
|
const startTime = Date.now();
|
|
397
402
|
// Accumulate output and tokens data for streamed responses
|
|
@@ -427,7 +432,7 @@ class OpenAIMonitor {
|
|
|
427
432
|
const endTime = Date.now();
|
|
428
433
|
const latency = endTime - startTime;
|
|
429
434
|
const cost = this.cost(streamedModel, streamedInputTokens, streamedOutputTokens);
|
|
430
|
-
this.openlayerClient.streamData(Object.assign({ cost, input: body.prompt, latency, output: streamedOutput, timestamp: startTime, tokens: streamedTokens }, additionalLogs), config,
|
|
435
|
+
this.openlayerClient.streamData(Object.assign({ cost, input: body.prompt, latency, output: streamedOutput, timestamp: startTime, tokens: streamedTokens }, additionalLogs), config, inferencePipeline.id);
|
|
431
436
|
}
|
|
432
437
|
else {
|
|
433
438
|
const nonStreamedResponse = response;
|
|
@@ -438,7 +443,7 @@ class OpenAIMonitor {
|
|
|
438
443
|
const inputTokens = (_z = (_y = nonStreamedResponse.usage) === null || _y === void 0 ? void 0 : _y.prompt_tokens) !== null && _z !== void 0 ? _z : 0;
|
|
439
444
|
const outputTokens = (_1 = (_0 = nonStreamedResponse.usage) === null || _0 === void 0 ? void 0 : _0.completion_tokens) !== null && _1 !== void 0 ? _1 : 0;
|
|
440
445
|
const cost = this.cost(nonStreamedResponse.model, inputTokens, outputTokens);
|
|
441
|
-
this.openlayerClient.streamData(Object.assign({ cost, input: body.prompt, latency, output: nonStreamedResponse.choices[0].text, timestamp: startTime, tokens }, additionalLogs), config,
|
|
446
|
+
this.openlayerClient.streamData(Object.assign({ cost, input: body.prompt, latency, output: nonStreamedResponse.choices[0].text, timestamp: startTime, tokens }, additionalLogs), config, inferencePipeline.id);
|
|
442
447
|
}
|
|
443
448
|
return response;
|
|
444
449
|
});
|
|
@@ -459,33 +464,23 @@ class OpenAIMonitor {
|
|
|
459
464
|
* Starts monitoring for the OpenAI Monitor instance. If monitoring is already active, a warning is logged.
|
|
460
465
|
*/
|
|
461
466
|
startMonitoring() {
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
this.monitoringOn = true;
|
|
469
|
-
this.project = yield this.openlayerClient.createProject(this.openlayerProjectName, 'llm-base');
|
|
470
|
-
if (typeof this.project !== 'undefined') {
|
|
471
|
-
this.inferencePipeline =
|
|
472
|
-
yield this.openlayerClient.createInferencePipeline(this.project.id, this.openlayerInferencePipelineName);
|
|
473
|
-
}
|
|
474
|
-
console.info('Monitor started');
|
|
475
|
-
});
|
|
467
|
+
if (this.monitoringOn) {
|
|
468
|
+
console.warn('Monitoring is already on!');
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
this.monitoringOn = true;
|
|
472
|
+
console.info('Monitoring started.');
|
|
476
473
|
}
|
|
477
474
|
/**
|
|
478
475
|
* Stops monitoring for the OpenAI Monitor instance. If monitoring is not active, a warning is logged.
|
|
479
476
|
*/
|
|
480
477
|
stopMonitoring() {
|
|
481
478
|
if (!this.monitoringOn) {
|
|
482
|
-
console.warn('
|
|
479
|
+
console.warn('Monitoring is not active.');
|
|
483
480
|
return;
|
|
484
481
|
}
|
|
485
482
|
this.monitoringOn = false;
|
|
486
|
-
|
|
487
|
-
this.inferencePipeline = undefined;
|
|
488
|
-
console.info('Monitor stopped.');
|
|
483
|
+
console.info('Monitoring stopped.');
|
|
489
484
|
}
|
|
490
485
|
}
|
|
491
486
|
exports.OpenAIMonitor = OpenAIMonitor;
|
|
@@ -5,14 +5,13 @@
|
|
|
5
5
|
import { OpenAIMonitor } from 'openlayer';
|
|
6
6
|
|
|
7
7
|
const monitor = new OpenAIMonitor({
|
|
8
|
-
openAiApiKey: '
|
|
9
|
-
openlayerApiKey: '
|
|
8
|
+
openAiApiKey: 'YOUR_OPENAI_API_KEY',
|
|
9
|
+
openlayerApiKey: 'YOUR_OPENLAYER_API_KEY',
|
|
10
10
|
openlayerInferencePipelineName: 'production',
|
|
11
|
-
openlayerProjectName: '
|
|
12
|
-
openlayerServerUrl: 'http://localhost:8080/v1',
|
|
11
|
+
openlayerProjectName: 'YOUR_OPENLAYER_PROJECT_NAME',
|
|
13
12
|
});
|
|
14
13
|
|
|
15
|
-
|
|
14
|
+
monitor.startMonitoring();
|
|
16
15
|
|
|
17
16
|
const inputs = [
|
|
18
17
|
{
|
|
@@ -37,7 +36,7 @@ for (let i = 0; i < inputs.length; i++) {
|
|
|
37
36
|
messages: [
|
|
38
37
|
{
|
|
39
38
|
content: systemMessage,
|
|
40
|
-
role: '
|
|
39
|
+
role: 'system',
|
|
41
40
|
},
|
|
42
41
|
{
|
|
43
42
|
content: userMessage,
|
|
@@ -53,3 +52,5 @@ for (let i = 0; i < inputs.length; i++) {
|
|
|
53
52
|
}
|
|
54
53
|
);
|
|
55
54
|
}
|
|
55
|
+
|
|
56
|
+
monitor.stopMonitoring();
|