openlayer 0.1.7 → 0.1.9
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 -2
- package/dist/index.js +36 -37
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -133,7 +133,6 @@ declare class OpenlayerClient {
|
|
|
133
133
|
private version;
|
|
134
134
|
constructor({ openlayerApiKey, openlayerServerUrl, }: OpenlayerClientConstructorProps);
|
|
135
135
|
private resolvedQuery;
|
|
136
|
-
private uploadToInferencePipeline;
|
|
137
136
|
/**
|
|
138
137
|
* Uploads data to Openlayer. This function takes ChatCompletionData and an optional
|
|
139
138
|
* ChatCompletionConfig, then uploads the data to the specified Openlayer Inference Pipeline.
|
|
@@ -142,7 +141,7 @@ declare class OpenlayerClient {
|
|
|
142
141
|
* @throws Throws an error if Openlayer API key or project name are not set.
|
|
143
142
|
* @returns A promise that resolves when the data has been successfully uploaded.
|
|
144
143
|
*/
|
|
145
|
-
|
|
144
|
+
streamData: (data: ChatCompletionData, config: ChatCompletionConfig, inferencePipelineId: string) => Promise<void>;
|
|
146
145
|
/**
|
|
147
146
|
* Creates a new inference pipeline in Openlayer, or loads an existing one if it already exists.
|
|
148
147
|
* @param name The name of the inference pipeline.
|
package/dist/index.js
CHANGED
|
@@ -25,29 +25,6 @@ class OpenlayerClient {
|
|
|
25
25
|
this.openlayerServerUrl = 'https://api.openlayer.com/v1';
|
|
26
26
|
this.version = '0.1.0a16';
|
|
27
27
|
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
28
|
/**
|
|
52
29
|
* Uploads data to Openlayer. This function takes ChatCompletionData and an optional
|
|
53
30
|
* ChatCompletionConfig, then uploads the data to the specified Openlayer Inference Pipeline.
|
|
@@ -56,17 +33,35 @@ class OpenlayerClient {
|
|
|
56
33
|
* @throws Throws an error if Openlayer API key or project name are not set.
|
|
57
34
|
* @returns A promise that resolves when the data has been successfully uploaded.
|
|
58
35
|
*/
|
|
59
|
-
this.
|
|
36
|
+
this.streamData = (data, config, inferencePipelineId) => __awaiter(this, void 0, void 0, function* () {
|
|
37
|
+
var _a;
|
|
60
38
|
if (!this.openlayerApiKey) {
|
|
61
|
-
throw new Error('Openlayer API key are required for
|
|
39
|
+
throw new Error('Openlayer API key are required for streaming data.');
|
|
62
40
|
}
|
|
63
41
|
try {
|
|
64
|
-
const
|
|
65
|
-
const
|
|
66
|
-
yield
|
|
42
|
+
const dataStreamEndpoint = `/inference-pipelines/${inferencePipelineId}/data-stream`;
|
|
43
|
+
const dataStreamQuery = this.resolvedQuery(dataStreamEndpoint);
|
|
44
|
+
const response = yield fetch(dataStreamQuery, {
|
|
45
|
+
body: JSON.stringify({
|
|
46
|
+
config,
|
|
47
|
+
rows: [
|
|
48
|
+
Object.assign(Object.assign({}, data), { id: (0, uuid_1.v4)(), timestamp: Math.round(((_a = data.timestamp) !== null && _a !== void 0 ? _a : Date.now()) / 1000) }),
|
|
49
|
+
],
|
|
50
|
+
}),
|
|
51
|
+
headers: {
|
|
52
|
+
Authorization: `Bearer ${this.openlayerApiKey}`,
|
|
53
|
+
'Content-Type': 'application/json',
|
|
54
|
+
},
|
|
55
|
+
method: 'POST',
|
|
56
|
+
});
|
|
57
|
+
if (!response.ok) {
|
|
58
|
+
console.error('Error making POST request:', response.status);
|
|
59
|
+
throw new Error(`Error: ${response.status}`);
|
|
60
|
+
}
|
|
61
|
+
return yield response.json();
|
|
67
62
|
}
|
|
68
63
|
catch (error) {
|
|
69
|
-
console.error('Error
|
|
64
|
+
console.error('Error streaming data to Openlayer:', error);
|
|
70
65
|
throw error;
|
|
71
66
|
}
|
|
72
67
|
});
|
|
@@ -235,6 +230,8 @@ class OpenAIMonitor {
|
|
|
235
230
|
if (!this.monitoringOn) {
|
|
236
231
|
throw new Error('Monitoring is not active.');
|
|
237
232
|
}
|
|
233
|
+
const project = yield this.openlayerClient.createProject(this.openlayerProjectName, 'llm-base');
|
|
234
|
+
const inferencePipeline = yield this.openlayerClient.createInferencePipeline(project.id, this.openlayerInferencePipelineName);
|
|
238
235
|
// Start a timer to measure latency
|
|
239
236
|
const startTime = Date.now();
|
|
240
237
|
// Accumulate output for streamed responses
|
|
@@ -260,12 +257,12 @@ class OpenAIMonitor {
|
|
|
260
257
|
}
|
|
261
258
|
const endTime = Date.now();
|
|
262
259
|
const latency = endTime - startTime;
|
|
263
|
-
this.openlayerClient.
|
|
260
|
+
this.openlayerClient.streamData({
|
|
264
261
|
input: this.formatChatCompletionInput(body.messages),
|
|
265
262
|
latency,
|
|
266
263
|
output: outputData,
|
|
267
264
|
timestamp: startTime,
|
|
268
|
-
}, this.openlayerDefaultDataConfig,
|
|
265
|
+
}, this.openlayerDefaultDataConfig, inferencePipeline.id);
|
|
269
266
|
}
|
|
270
267
|
else {
|
|
271
268
|
const nonStreamedResponse = response;
|
|
@@ -276,13 +273,13 @@ class OpenAIMonitor {
|
|
|
276
273
|
if (typeof output !== 'string') {
|
|
277
274
|
throw new Error('No output received from OpenAI.');
|
|
278
275
|
}
|
|
279
|
-
this.openlayerClient.
|
|
276
|
+
this.openlayerClient.streamData({
|
|
280
277
|
input: this.formatChatCompletionInput(body.messages),
|
|
281
278
|
latency,
|
|
282
279
|
output,
|
|
283
280
|
timestamp: startTime,
|
|
284
281
|
tokens: (_e = (_d = nonStreamedResponse.usage) === null || _d === void 0 ? void 0 : _d.total_tokens) !== null && _e !== void 0 ? _e : 0,
|
|
285
|
-
}, this.openlayerDefaultDataConfig,
|
|
282
|
+
}, this.openlayerDefaultDataConfig, inferencePipeline.id);
|
|
286
283
|
}
|
|
287
284
|
return response;
|
|
288
285
|
});
|
|
@@ -303,6 +300,8 @@ class OpenAIMonitor {
|
|
|
303
300
|
if (!body.prompt) {
|
|
304
301
|
throw new Error('No prompt provided.');
|
|
305
302
|
}
|
|
303
|
+
const project = yield this.openlayerClient.createProject(this.openlayerProjectName, 'llm-base');
|
|
304
|
+
const inferencePipeline = yield this.openlayerClient.createInferencePipeline(project.id, this.openlayerInferencePipelineName);
|
|
306
305
|
// Start a timer to measure latency
|
|
307
306
|
const startTime = Date.now();
|
|
308
307
|
// Accumulate output and tokens data for streamed responses
|
|
@@ -330,26 +329,26 @@ class OpenAIMonitor {
|
|
|
330
329
|
}
|
|
331
330
|
const endTime = Date.now();
|
|
332
331
|
const latency = endTime - startTime;
|
|
333
|
-
this.openlayerClient.
|
|
332
|
+
this.openlayerClient.streamData({
|
|
334
333
|
input: body.prompt,
|
|
335
334
|
latency,
|
|
336
335
|
output: outputData,
|
|
337
336
|
timestamp: startTime,
|
|
338
337
|
tokens: tokensData,
|
|
339
|
-
}, this.openlayerDefaultDataConfig,
|
|
338
|
+
}, this.openlayerDefaultDataConfig, inferencePipeline.id);
|
|
340
339
|
}
|
|
341
340
|
else {
|
|
342
341
|
const nonStreamedResponse = response;
|
|
343
342
|
// Handle regular (non-streamed) response
|
|
344
343
|
const endTime = Date.now();
|
|
345
344
|
const latency = endTime - startTime;
|
|
346
|
-
this.openlayerClient.
|
|
345
|
+
this.openlayerClient.streamData({
|
|
347
346
|
input: body.prompt,
|
|
348
347
|
latency,
|
|
349
348
|
output: nonStreamedResponse.choices[0].text,
|
|
350
349
|
timestamp: startTime,
|
|
351
350
|
tokens: (_o = (_m = nonStreamedResponse.usage) === null || _m === void 0 ? void 0 : _m.total_tokens) !== null && _o !== void 0 ? _o : 0,
|
|
352
|
-
}, this.openlayerDefaultDataConfig,
|
|
351
|
+
}, this.openlayerDefaultDataConfig, inferencePipeline.id);
|
|
353
352
|
}
|
|
354
353
|
return response;
|
|
355
354
|
});
|