openlayer 0.1.17 → 0.1.19

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 CHANGED
@@ -143,14 +143,6 @@ export declare class OpenlayerClient {
143
143
  */
144
144
  constructor({ openlayerApiKey, openlayerServerUrl, }: OpenlayerClientConstructorProps);
145
145
  private resolvedQuery;
146
- /**
147
- * Streams data to the Openlayer inference pipeline.
148
- * @param {StreamingData} data - The chat completion data to be streamed.
149
- * @param {string} inferencePipelineId - The ID of the Openlayer inference pipeline to which data is streamed.
150
- * @returns {Promise<void>} A promise that resolves when the data has been successfully streamed.
151
- * @throws {Error} Throws an error if the Openlayer API key is not set or an error occurs in the streaming process.
152
- */
153
- streamData: (data: StreamingData, config: StreamingDataConfig, inferencePipelineId: string) => Promise<void>;
154
146
  /**
155
147
  * Creates a new inference pipeline in Openlayer or loads an existing one.
156
148
  * @param {string} projectId - The ID of the project containing the inference pipeline.
@@ -183,6 +175,14 @@ export declare class OpenlayerClient {
183
175
  * @throws {Error} Throws an error if the project is not found.
184
176
  */
185
177
  loadProject: (name: string) => Promise<OpenlayerProject>;
178
+ /**
179
+ * Streams data to the Openlayer inference pipeline.
180
+ * @param {StreamingData} data - The chat completion data to be streamed.
181
+ * @param {string} inferencePipelineId - The ID of the Openlayer inference pipeline to which data is streamed.
182
+ * @returns {Promise<void>} A promise that resolves when the data has been successfully streamed.
183
+ * @throws {Error} Throws an error if the Openlayer API key is not set or an error occurs in the streaming process.
184
+ */
185
+ streamData: (data: StreamingData, config: StreamingDataConfig, inferencePipelineId: string) => Promise<void>;
186
186
  }
187
187
  export declare class OpenAIMonitor {
188
188
  private openlayerClient;
package/dist/index.js CHANGED
@@ -96,45 +96,6 @@ class OpenlayerClient {
96
96
  this.openlayerServerUrl = 'https://api.openlayer.com/v1';
97
97
  this.version = '0.1.0a16';
98
98
  this.resolvedQuery = (endpoint, args = {}) => (0, request_1.resolvedQuery)(this.openlayerServerUrl, endpoint, args);
99
- /**
100
- * Streams data to the Openlayer inference pipeline.
101
- * @param {StreamingData} data - The chat completion data to be streamed.
102
- * @param {string} inferencePipelineId - The ID of the Openlayer inference pipeline to which data is streamed.
103
- * @returns {Promise<void>} A promise that resolves when the data has been successfully streamed.
104
- * @throws {Error} Throws an error if the Openlayer API key is not set or an error occurs in the streaming process.
105
- */
106
- this.streamData = (data, config, inferencePipelineId) => __awaiter(this, void 0, void 0, function* () {
107
- var _a;
108
- if (!this.openlayerApiKey) {
109
- throw new Error('Openlayer API key are required for streaming data.');
110
- }
111
- try {
112
- const dataStreamEndpoint = `/inference-pipelines/${inferencePipelineId}/data-stream`;
113
- const dataStreamQuery = this.resolvedQuery(dataStreamEndpoint);
114
- const response = yield fetch(dataStreamQuery, {
115
- body: JSON.stringify({
116
- config,
117
- rows: [
118
- Object.assign(Object.assign({}, data), { id: (0, uuid_1.v4)(), timestamp: Math.round(((_a = data.timestamp) !== null && _a !== void 0 ? _a : Date.now()) / 1000) }),
119
- ],
120
- }),
121
- headers: {
122
- Authorization: `Bearer ${this.openlayerApiKey}`,
123
- 'Content-Type': 'application/json',
124
- },
125
- method: 'POST',
126
- });
127
- if (!response.ok) {
128
- console.error('Error making POST request:', response.status);
129
- throw new Error(`Error: ${response.status}`);
130
- }
131
- return yield response.json();
132
- }
133
- catch (error) {
134
- console.error('Error streaming data to Openlayer:', error);
135
- throw error;
136
- }
137
- });
138
99
  /**
139
100
  * Creates a new inference pipeline in Openlayer or loads an existing one.
140
101
  * @param {string} projectId - The ID of the project containing the inference pipeline.
@@ -146,7 +107,7 @@ class OpenlayerClient {
146
107
  try {
147
108
  return yield this.loadInferencePipeline(projectId, name);
148
109
  }
149
- catch (_b) {
110
+ catch (_a) {
150
111
  const createInferencePipelineEndpoint = `/projects/${projectId}/inference-pipelines`;
151
112
  const createInferencePipelineQuery = this.resolvedQuery(createInferencePipelineEndpoint, { version: this.version });
152
113
  const createInferencePipelineResponse = yield fetch(createInferencePipelineQuery, {
@@ -179,7 +140,7 @@ class OpenlayerClient {
179
140
  try {
180
141
  return yield this.loadProject(name);
181
142
  }
182
- catch (_c) {
143
+ catch (_b) {
183
144
  const projectsEndpoint = '/projects';
184
145
  const projectsQuery = this.resolvedQuery(projectsEndpoint);
185
146
  const response = yield fetch(projectsQuery, {
@@ -267,6 +228,45 @@ class OpenlayerClient {
267
228
  }
268
229
  return project;
269
230
  });
231
+ /**
232
+ * Streams data to the Openlayer inference pipeline.
233
+ * @param {StreamingData} data - The chat completion data to be streamed.
234
+ * @param {string} inferencePipelineId - The ID of the Openlayer inference pipeline to which data is streamed.
235
+ * @returns {Promise<void>} A promise that resolves when the data has been successfully streamed.
236
+ * @throws {Error} Throws an error if the Openlayer API key is not set or an error occurs in the streaming process.
237
+ */
238
+ this.streamData = (data, config, inferencePipelineId) => __awaiter(this, void 0, void 0, function* () {
239
+ var _c;
240
+ if (!this.openlayerApiKey) {
241
+ throw new Error('Openlayer API key are required for streaming data.');
242
+ }
243
+ try {
244
+ const dataStreamEndpoint = `/inference-pipelines/${inferencePipelineId}/data-stream`;
245
+ const dataStreamQuery = this.resolvedQuery(dataStreamEndpoint);
246
+ const response = yield fetch(dataStreamQuery, {
247
+ body: JSON.stringify({
248
+ config,
249
+ rows: [
250
+ Object.assign(Object.assign({}, data), { id: (0, uuid_1.v4)(), timestamp: Math.round(((_c = data.timestamp) !== null && _c !== void 0 ? _c : Date.now()) / 1000) }),
251
+ ],
252
+ }),
253
+ headers: {
254
+ Authorization: `Bearer ${this.openlayerApiKey}`,
255
+ 'Content-Type': 'application/json',
256
+ },
257
+ method: 'POST',
258
+ });
259
+ if (!response.ok) {
260
+ console.error('Error making POST request:', response.status);
261
+ throw new Error(`Error: ${response.status}`);
262
+ }
263
+ return yield response.json();
264
+ }
265
+ catch (error) {
266
+ console.error('Error streaming data to Openlayer:', error);
267
+ throw error;
268
+ }
269
+ });
270
270
  this.openlayerApiKey = openlayerApiKey;
271
271
  if (openlayerServerUrl) {
272
272
  this.openlayerServerUrl = openlayerServerUrl;
@@ -381,7 +381,7 @@ class OpenAIMonitor {
381
381
  */
382
382
  this.createCompletion = (body, options, additionalLogs) => __awaiter(this, void 0, void 0, function* () {
383
383
  var _m, e_2, _o, _p;
384
- var _q, _r, _s, _t, _u, _v, _w, _x;
384
+ var _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1;
385
385
  if (!this.monitoringOn) {
386
386
  throw new Error('Monitoring is not active.');
387
387
  }
@@ -393,41 +393,48 @@ class OpenAIMonitor {
393
393
  // Start a timer to measure latency
394
394
  const startTime = Date.now();
395
395
  // Accumulate output and tokens data for streamed responses
396
+ let streamedModel = body.model;
396
397
  let streamedOutput = '';
397
398
  let streamedTokens = 0;
399
+ let streamedInputTokens = 0;
400
+ let streamedOutputTokens = 0;
398
401
  const response = yield this.openAIClient.completions.create(body, options);
399
402
  const config = Object.assign(Object.assign({}, this.openlayerClient.defaultConfig), { inputVariableNames: ['input'] });
400
403
  if (body.stream) {
401
404
  const streamedResponse = response;
402
405
  try {
403
- for (var _y = true, streamedResponse_2 = __asyncValues(streamedResponse), streamedResponse_2_1; streamedResponse_2_1 = yield streamedResponse_2.next(), _m = streamedResponse_2_1.done, !_m; _y = true) {
406
+ for (var _2 = true, streamedResponse_2 = __asyncValues(streamedResponse), streamedResponse_2_1; streamedResponse_2_1 = yield streamedResponse_2.next(), _m = streamedResponse_2_1.done, !_m; _2 = true) {
404
407
  _p = streamedResponse_2_1.value;
405
- _y = false;
408
+ _2 = false;
406
409
  const chunk = _p;
407
410
  // Process each chunk - for example, accumulate input data
411
+ streamedModel = chunk.model;
408
412
  streamedOutput += chunk.choices[0].text.trim();
409
413
  streamedTokens += (_r = (_q = chunk.usage) === null || _q === void 0 ? void 0 : _q.total_tokens) !== null && _r !== void 0 ? _r : 0;
414
+ streamedInputTokens += (_t = (_s = chunk.usage) === null || _s === void 0 ? void 0 : _s.prompt_tokens) !== null && _t !== void 0 ? _t : 0;
415
+ streamedOutputTokens += (_v = (_u = chunk.usage) === null || _u === void 0 ? void 0 : _u.completion_tokens) !== null && _v !== void 0 ? _v : 0;
410
416
  }
411
417
  }
412
418
  catch (e_2_1) { e_2 = { error: e_2_1 }; }
413
419
  finally {
414
420
  try {
415
- if (!_y && !_m && (_o = streamedResponse_2.return)) yield _o.call(streamedResponse_2);
421
+ if (!_2 && !_m && (_o = streamedResponse_2.return)) yield _o.call(streamedResponse_2);
416
422
  }
417
423
  finally { if (e_2) throw e_2.error; }
418
424
  }
419
425
  const endTime = Date.now();
420
426
  const latency = endTime - startTime;
421
- this.openlayerClient.streamData(Object.assign({ input: body.prompt, latency, output: streamedOutput, timestamp: startTime, tokens: streamedTokens }, additionalLogs), config, inferencePipeline.id);
427
+ const cost = this.cost(streamedModel, streamedInputTokens, streamedOutputTokens);
428
+ this.openlayerClient.streamData(Object.assign({ cost, input: body.prompt, latency, output: streamedOutput, timestamp: startTime, tokens: streamedTokens }, additionalLogs), config, inferencePipeline.id);
422
429
  }
423
430
  else {
424
431
  const nonStreamedResponse = response;
425
432
  // Handle regular (non-streamed) response
426
433
  const endTime = Date.now();
427
434
  const latency = endTime - startTime;
428
- const tokens = (_t = (_s = nonStreamedResponse.usage) === null || _s === void 0 ? void 0 : _s.total_tokens) !== null && _t !== void 0 ? _t : 0;
429
- const inputTokens = (_v = (_u = nonStreamedResponse.usage) === null || _u === void 0 ? void 0 : _u.prompt_tokens) !== null && _v !== void 0 ? _v : 0;
430
- const outputTokens = (_x = (_w = nonStreamedResponse.usage) === null || _w === void 0 ? void 0 : _w.completion_tokens) !== null && _x !== void 0 ? _x : 0;
435
+ const tokens = (_x = (_w = nonStreamedResponse.usage) === null || _w === void 0 ? void 0 : _w.total_tokens) !== null && _x !== void 0 ? _x : 0;
436
+ const inputTokens = (_z = (_y = nonStreamedResponse.usage) === null || _y === void 0 ? void 0 : _y.prompt_tokens) !== null && _z !== void 0 ? _z : 0;
437
+ const outputTokens = (_1 = (_0 = nonStreamedResponse.usage) === null || _0 === void 0 ? void 0 : _0.completion_tokens) !== null && _1 !== void 0 ? _1 : 0;
431
438
  const cost = this.cost(nonStreamedResponse.model, inputTokens, outputTokens);
432
439
  this.openlayerClient.streamData(Object.assign({ cost, input: body.prompt, latency, output: nonStreamedResponse.choices[0].text, timestamp: startTime, tokens }, additionalLogs), config, inferencePipeline.id);
433
440
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openlayer",
3
- "version": "0.1.17",
3
+ "version": "0.1.19",
4
4
  "description": "The Openlayer TypeScript client",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",