openlayer 0.1.8 → 0.1.10
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 +4 -5
- package/dist/index.js +47 -48
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -127,13 +127,13 @@ type OpenlayerSampleVolumeGraph = {
|
|
|
127
127
|
weekly: OpenlayerSampleVolumeGraphBucket;
|
|
128
128
|
};
|
|
129
129
|
type OpenlayerTaskType = 'llm-base' | 'tabular-classification' | 'tabular-regression' | 'text-classification';
|
|
130
|
-
declare class OpenlayerClient {
|
|
130
|
+
export declare class OpenlayerClient {
|
|
131
131
|
private openlayerApiKey?;
|
|
132
|
+
private openlayerDefaultDataConfig;
|
|
132
133
|
private openlayerServerUrl;
|
|
133
134
|
private version;
|
|
134
135
|
constructor({ openlayerApiKey, openlayerServerUrl, }: OpenlayerClientConstructorProps);
|
|
135
136
|
private resolvedQuery;
|
|
136
|
-
private uploadToInferencePipeline;
|
|
137
137
|
/**
|
|
138
138
|
* Uploads data to Openlayer. This function takes ChatCompletionData and an optional
|
|
139
139
|
* ChatCompletionConfig, then uploads the data to the specified Openlayer Inference Pipeline.
|
|
@@ -142,7 +142,7 @@ declare class OpenlayerClient {
|
|
|
142
142
|
* @throws Throws an error if Openlayer API key or project name are not set.
|
|
143
143
|
* @returns A promise that resolves when the data has been successfully uploaded.
|
|
144
144
|
*/
|
|
145
|
-
|
|
145
|
+
streamData: (data: ChatCompletionData, inferencePipelineId: string) => Promise<void>;
|
|
146
146
|
/**
|
|
147
147
|
* Creates a new inference pipeline in Openlayer, or loads an existing one if it already exists.
|
|
148
148
|
* @param name The name of the inference pipeline.
|
|
@@ -173,7 +173,6 @@ declare class OpenlayerClient {
|
|
|
173
173
|
export declare class OpenAIMonitor {
|
|
174
174
|
private openlayerClient;
|
|
175
175
|
private openAIClient;
|
|
176
|
-
private openlayerDefaultDataConfig;
|
|
177
176
|
private openlayerProjectName;
|
|
178
177
|
private openlayerInferencePipelineName;
|
|
179
178
|
private monitoringOn;
|
|
@@ -206,4 +205,4 @@ export declare class OpenAIMonitor {
|
|
|
206
205
|
*/
|
|
207
206
|
stopMonitoring(): void;
|
|
208
207
|
}
|
|
209
|
-
export
|
|
208
|
+
export {};
|
package/dist/index.js
CHANGED
|
@@ -16,38 +16,24 @@ var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
|
16
16
|
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
17
17
|
};
|
|
18
18
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
|
-
exports.OpenAIMonitor = void 0;
|
|
19
|
+
exports.OpenAIMonitor = exports.OpenlayerClient = void 0;
|
|
20
20
|
const openai_1 = require("openai");
|
|
21
21
|
const uuid_1 = require("uuid");
|
|
22
22
|
const request_1 = require("./utils/request");
|
|
23
23
|
class OpenlayerClient {
|
|
24
24
|
constructor({ openlayerApiKey, openlayerServerUrl, }) {
|
|
25
|
+
this.openlayerDefaultDataConfig = {
|
|
26
|
+
groundTruthColumnName: null,
|
|
27
|
+
inferenceIdColumnName: 'id',
|
|
28
|
+
inputVariableNames: ['input'],
|
|
29
|
+
latencyColumnName: 'latency',
|
|
30
|
+
numOfTokenColumnName: 'tokens',
|
|
31
|
+
outputColumnName: 'output',
|
|
32
|
+
timestampColumnName: 'timestamp',
|
|
33
|
+
};
|
|
25
34
|
this.openlayerServerUrl = 'https://api.openlayer.com/v1';
|
|
26
35
|
this.version = '0.1.0a16';
|
|
27
36
|
this.resolvedQuery = (endpoint, args = {}) => (0, request_1.resolvedQuery)(this.openlayerServerUrl, endpoint, args);
|
|
28
|
-
this.uploadToInferencePipeline = (inferencePipelineId, data, config) => __awaiter(this, void 0, void 0, function* () {
|
|
29
|
-
var _a;
|
|
30
|
-
const dataStreamEndpoint = `/inference-pipelines/${inferencePipelineId}/data-stream`;
|
|
31
|
-
const dataStreamQuery = this.resolvedQuery(dataStreamEndpoint);
|
|
32
|
-
const response = yield fetch(dataStreamQuery, {
|
|
33
|
-
body: JSON.stringify({
|
|
34
|
-
config,
|
|
35
|
-
rows: [
|
|
36
|
-
Object.assign(Object.assign({}, data), { id: (0, uuid_1.v4)(), timestamp: Math.round(((_a = data.timestamp) !== null && _a !== void 0 ? _a : Date.now()) / 1000) }),
|
|
37
|
-
],
|
|
38
|
-
}),
|
|
39
|
-
headers: {
|
|
40
|
-
Authorization: `Bearer ${this.openlayerApiKey}`,
|
|
41
|
-
'Content-Type': 'application/json',
|
|
42
|
-
},
|
|
43
|
-
method: 'POST',
|
|
44
|
-
});
|
|
45
|
-
if (!response.ok) {
|
|
46
|
-
console.error('Error making POST request:', response.status);
|
|
47
|
-
throw new Error(`Error: ${response.status}`);
|
|
48
|
-
}
|
|
49
|
-
return yield response.json();
|
|
50
|
-
});
|
|
51
37
|
/**
|
|
52
38
|
* Uploads data to Openlayer. This function takes ChatCompletionData and an optional
|
|
53
39
|
* ChatCompletionConfig, then uploads the data to the specified Openlayer Inference Pipeline.
|
|
@@ -56,17 +42,35 @@ class OpenlayerClient {
|
|
|
56
42
|
* @throws Throws an error if Openlayer API key or project name are not set.
|
|
57
43
|
* @returns A promise that resolves when the data has been successfully uploaded.
|
|
58
44
|
*/
|
|
59
|
-
this.
|
|
45
|
+
this.streamData = (data, inferencePipelineId) => __awaiter(this, void 0, void 0, function* () {
|
|
46
|
+
var _a;
|
|
60
47
|
if (!this.openlayerApiKey) {
|
|
61
|
-
throw new Error('Openlayer API key are required for
|
|
48
|
+
throw new Error('Openlayer API key are required for streaming data.');
|
|
62
49
|
}
|
|
63
50
|
try {
|
|
64
|
-
const
|
|
65
|
-
const
|
|
66
|
-
yield
|
|
51
|
+
const dataStreamEndpoint = `/inference-pipelines/${inferencePipelineId}/data-stream`;
|
|
52
|
+
const dataStreamQuery = this.resolvedQuery(dataStreamEndpoint);
|
|
53
|
+
const response = yield fetch(dataStreamQuery, {
|
|
54
|
+
body: JSON.stringify({
|
|
55
|
+
config: this.openlayerDefaultDataConfig,
|
|
56
|
+
rows: [
|
|
57
|
+
Object.assign(Object.assign({}, data), { id: (0, uuid_1.v4)(), timestamp: Math.round(((_a = data.timestamp) !== null && _a !== void 0 ? _a : Date.now()) / 1000) }),
|
|
58
|
+
],
|
|
59
|
+
}),
|
|
60
|
+
headers: {
|
|
61
|
+
Authorization: `Bearer ${this.openlayerApiKey}`,
|
|
62
|
+
'Content-Type': 'application/json',
|
|
63
|
+
},
|
|
64
|
+
method: 'POST',
|
|
65
|
+
});
|
|
66
|
+
if (!response.ok) {
|
|
67
|
+
console.error('Error making POST request:', response.status);
|
|
68
|
+
throw new Error(`Error: ${response.status}`);
|
|
69
|
+
}
|
|
70
|
+
return yield response.json();
|
|
67
71
|
}
|
|
68
72
|
catch (error) {
|
|
69
|
-
console.error('Error
|
|
73
|
+
console.error('Error streaming data to Openlayer:', error);
|
|
70
74
|
throw error;
|
|
71
75
|
}
|
|
72
76
|
});
|
|
@@ -203,17 +207,9 @@ class OpenlayerClient {
|
|
|
203
207
|
}
|
|
204
208
|
}
|
|
205
209
|
}
|
|
210
|
+
exports.OpenlayerClient = OpenlayerClient;
|
|
206
211
|
class OpenAIMonitor {
|
|
207
212
|
constructor({ openAiApiKey, openlayerApiKey, openlayerProjectName, openlayerInferencePipelineName, openlayerServerUrl, }) {
|
|
208
|
-
this.openlayerDefaultDataConfig = {
|
|
209
|
-
groundTruthColumnName: null,
|
|
210
|
-
inferenceIdColumnName: 'id',
|
|
211
|
-
inputVariableNames: ['input'],
|
|
212
|
-
latencyColumnName: 'latency',
|
|
213
|
-
numOfTokenColumnName: 'tokens',
|
|
214
|
-
outputColumnName: 'output',
|
|
215
|
-
timestampColumnName: 'timestamp',
|
|
216
|
-
};
|
|
217
213
|
this.openlayerInferencePipelineName = 'production';
|
|
218
214
|
this.monitoringOn = false;
|
|
219
215
|
this.formatChatCompletionInput = (messages) => messages
|
|
@@ -235,6 +231,8 @@ class OpenAIMonitor {
|
|
|
235
231
|
if (!this.monitoringOn) {
|
|
236
232
|
throw new Error('Monitoring is not active.');
|
|
237
233
|
}
|
|
234
|
+
const project = yield this.openlayerClient.createProject(this.openlayerProjectName, 'llm-base');
|
|
235
|
+
const inferencePipeline = yield this.openlayerClient.createInferencePipeline(project.id, this.openlayerInferencePipelineName);
|
|
238
236
|
// Start a timer to measure latency
|
|
239
237
|
const startTime = Date.now();
|
|
240
238
|
// Accumulate output for streamed responses
|
|
@@ -260,12 +258,12 @@ class OpenAIMonitor {
|
|
|
260
258
|
}
|
|
261
259
|
const endTime = Date.now();
|
|
262
260
|
const latency = endTime - startTime;
|
|
263
|
-
this.openlayerClient.
|
|
261
|
+
this.openlayerClient.streamData({
|
|
264
262
|
input: this.formatChatCompletionInput(body.messages),
|
|
265
263
|
latency,
|
|
266
264
|
output: outputData,
|
|
267
265
|
timestamp: startTime,
|
|
268
|
-
},
|
|
266
|
+
}, inferencePipeline.id);
|
|
269
267
|
}
|
|
270
268
|
else {
|
|
271
269
|
const nonStreamedResponse = response;
|
|
@@ -276,13 +274,13 @@ class OpenAIMonitor {
|
|
|
276
274
|
if (typeof output !== 'string') {
|
|
277
275
|
throw new Error('No output received from OpenAI.');
|
|
278
276
|
}
|
|
279
|
-
this.openlayerClient.
|
|
277
|
+
this.openlayerClient.streamData({
|
|
280
278
|
input: this.formatChatCompletionInput(body.messages),
|
|
281
279
|
latency,
|
|
282
280
|
output,
|
|
283
281
|
timestamp: startTime,
|
|
284
282
|
tokens: (_e = (_d = nonStreamedResponse.usage) === null || _d === void 0 ? void 0 : _d.total_tokens) !== null && _e !== void 0 ? _e : 0,
|
|
285
|
-
},
|
|
283
|
+
}, inferencePipeline.id);
|
|
286
284
|
}
|
|
287
285
|
return response;
|
|
288
286
|
});
|
|
@@ -303,6 +301,8 @@ class OpenAIMonitor {
|
|
|
303
301
|
if (!body.prompt) {
|
|
304
302
|
throw new Error('No prompt provided.');
|
|
305
303
|
}
|
|
304
|
+
const project = yield this.openlayerClient.createProject(this.openlayerProjectName, 'llm-base');
|
|
305
|
+
const inferencePipeline = yield this.openlayerClient.createInferencePipeline(project.id, this.openlayerInferencePipelineName);
|
|
306
306
|
// Start a timer to measure latency
|
|
307
307
|
const startTime = Date.now();
|
|
308
308
|
// Accumulate output and tokens data for streamed responses
|
|
@@ -330,26 +330,26 @@ class OpenAIMonitor {
|
|
|
330
330
|
}
|
|
331
331
|
const endTime = Date.now();
|
|
332
332
|
const latency = endTime - startTime;
|
|
333
|
-
this.openlayerClient.
|
|
333
|
+
this.openlayerClient.streamData({
|
|
334
334
|
input: body.prompt,
|
|
335
335
|
latency,
|
|
336
336
|
output: outputData,
|
|
337
337
|
timestamp: startTime,
|
|
338
338
|
tokens: tokensData,
|
|
339
|
-
},
|
|
339
|
+
}, inferencePipeline.id);
|
|
340
340
|
}
|
|
341
341
|
else {
|
|
342
342
|
const nonStreamedResponse = response;
|
|
343
343
|
// Handle regular (non-streamed) response
|
|
344
344
|
const endTime = Date.now();
|
|
345
345
|
const latency = endTime - startTime;
|
|
346
|
-
this.openlayerClient.
|
|
346
|
+
this.openlayerClient.streamData({
|
|
347
347
|
input: body.prompt,
|
|
348
348
|
latency,
|
|
349
349
|
output: nonStreamedResponse.choices[0].text,
|
|
350
350
|
timestamp: startTime,
|
|
351
351
|
tokens: (_o = (_m = nonStreamedResponse.usage) === null || _m === void 0 ? void 0 : _m.total_tokens) !== null && _o !== void 0 ? _o : 0,
|
|
352
|
-
},
|
|
352
|
+
}, inferencePipeline.id);
|
|
353
353
|
}
|
|
354
354
|
return response;
|
|
355
355
|
});
|
|
@@ -390,4 +390,3 @@ class OpenAIMonitor {
|
|
|
390
390
|
}
|
|
391
391
|
}
|
|
392
392
|
exports.OpenAIMonitor = OpenAIMonitor;
|
|
393
|
-
exports.default = OpenlayerClient;
|