openlayer 0.1.11 → 0.1.13
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/README.md +35 -0
- package/dist/index.d.ts +57 -45
- package/dist/index.js +66 -56
- package/examples/langchain.mjs +45 -0
- package/package.json +1 -1
- package/static/logo.png +0 -0
package/README.md
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
<div align="left">
|
|
2
|
+
<img src="static/logo.png"><br>
|
|
3
|
+
</div>
|
|
4
|
+
|
|
5
|
+
# Openlayer | JavaScript/TypeScript Library
|
|
6
|
+
|
|
7
|
+
[](https://badge.fury.io/js/openlayer)
|
|
8
|
+
|
|
9
|
+
## What is it?
|
|
10
|
+
|
|
11
|
+
Openlayer is a debugging workspace for ML & Data Science. Openlayer combines and builds upon SOTA techniques in explainability, model and dataset versioning, synthetic data generation, data-centric testing and much more to form a powerful, **unified platform for model development**.
|
|
12
|
+
|
|
13
|
+
👉 [Join our Discord community!](https://discord.gg/t6wS2g6MMB) We'd love to meet you and help you get started with Openlayer!
|
|
14
|
+
|
|
15
|
+
This is the official JavaScript/TypeScript library for interacting with the Openlayer platform. Navigate [here](https://docs.openlayer.com) for a quickstart guide and for in-depth tutorials.
|
|
16
|
+
|
|
17
|
+
## Main Features
|
|
18
|
+
|
|
19
|
+
This library's primary function is to enable you to easily package your models and datasets and add them to your Openlayer account.
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
|
|
23
|
+
Install with npm
|
|
24
|
+
|
|
25
|
+
```console
|
|
26
|
+
npm i openlayer
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Documentation
|
|
30
|
+
|
|
31
|
+
The official documentation for this library can be found [here](https://docs.openlayer.com).
|
|
32
|
+
|
|
33
|
+
## Contributing
|
|
34
|
+
|
|
35
|
+
All contributions, bug reports, bug fixes, documentation improvements, enhancements, and ideas are welcome! Just send us a message on [Discord](https://discord.gg/t6wS2g6MMB).
|
package/dist/index.d.ts
CHANGED
|
@@ -1,16 +1,12 @@
|
|
|
1
1
|
import { RequestOptions } from 'openai/core';
|
|
2
|
-
import { ChatCompletion, ChatCompletionChunk, ChatCompletionCreateParams, Completion, CompletionCreateParams } from 'openai/resources';
|
|
2
|
+
import { ChatCompletion, ChatCompletionChunk, ChatCompletionCreateParams, ChatCompletionMessageParam, Completion, CompletionCreateParams } from 'openai/resources';
|
|
3
3
|
import { Stream } from 'openai/streaming';
|
|
4
4
|
/**
|
|
5
5
|
* Represents the data structure for a chat completion.
|
|
6
|
+
* Object keys represent a column name and the values represent the column value.
|
|
6
7
|
*/
|
|
7
|
-
export interface
|
|
8
|
-
[
|
|
9
|
-
/**
|
|
10
|
-
* The input data for the chat completion. It can be a string, an array of strings,
|
|
11
|
-
* or a nested array of numbers.
|
|
12
|
-
*/
|
|
13
|
-
input: string | string[] | number[] | number[][];
|
|
8
|
+
export interface StreamingData {
|
|
9
|
+
[columnName: string]: any;
|
|
14
10
|
/**
|
|
15
11
|
* The latency of the chat completion in milliseconds. Optional.
|
|
16
12
|
*/
|
|
@@ -31,7 +27,7 @@ export interface ChatCompletionData {
|
|
|
31
27
|
/**
|
|
32
28
|
* Configuration settings for uploading chat completion data to Openlayer.
|
|
33
29
|
*/
|
|
34
|
-
|
|
30
|
+
interface StreamingDataConfig {
|
|
35
31
|
/**
|
|
36
32
|
* The name of the column that stores the ground truth data. Can be null.
|
|
37
33
|
*/
|
|
@@ -43,7 +39,7 @@ export interface ChatCompletionConfig {
|
|
|
43
39
|
/**
|
|
44
40
|
* An array of names for input variable columns. Can be null.
|
|
45
41
|
*/
|
|
46
|
-
inputVariableNames
|
|
42
|
+
inputVariableNames?: string[] | null;
|
|
47
43
|
/**
|
|
48
44
|
* The name of the column that stores latency data. Can be null.
|
|
49
45
|
*/
|
|
@@ -56,6 +52,10 @@ export interface ChatCompletionConfig {
|
|
|
56
52
|
* The name of the column that stores output data. Can be null.
|
|
57
53
|
*/
|
|
58
54
|
outputColumnName: string | null;
|
|
55
|
+
/**
|
|
56
|
+
* The full prompt history for the chat completion.
|
|
57
|
+
*/
|
|
58
|
+
prompt?: ChatCompletionMessageParam[];
|
|
59
59
|
/**
|
|
60
60
|
* The name of the column that stores timestamp data. Can be null.
|
|
61
61
|
*/
|
|
@@ -129,45 +129,55 @@ type OpenlayerSampleVolumeGraph = {
|
|
|
129
129
|
type OpenlayerTaskType = 'llm-base' | 'tabular-classification' | 'tabular-regression' | 'text-classification';
|
|
130
130
|
export declare class OpenlayerClient {
|
|
131
131
|
private openlayerApiKey?;
|
|
132
|
-
|
|
132
|
+
defaultConfig: StreamingDataConfig;
|
|
133
133
|
private openlayerServerUrl;
|
|
134
134
|
private version;
|
|
135
|
+
/**
|
|
136
|
+
* Constructs an OpenlayerClient instance.
|
|
137
|
+
* @param {OpenlayerClientConstructorProps} props - The config for the Openlayer client. The API key is required.
|
|
138
|
+
* @throws {Error} Throws an error if the Openlayer API key is not provided.
|
|
139
|
+
*/
|
|
135
140
|
constructor({ openlayerApiKey, openlayerServerUrl, }: OpenlayerClientConstructorProps);
|
|
136
141
|
private resolvedQuery;
|
|
137
142
|
/**
|
|
138
|
-
*
|
|
139
|
-
*
|
|
140
|
-
* @param
|
|
141
|
-
* @
|
|
142
|
-
* @throws Throws an error if Openlayer API key or
|
|
143
|
-
* @returns A promise that resolves when the data has been successfully uploaded.
|
|
143
|
+
* Streams data to the Openlayer inference pipeline.
|
|
144
|
+
* @param {StreamingData} data - The chat completion data to be streamed.
|
|
145
|
+
* @param {string} inferencePipelineId - The ID of the Openlayer inference pipeline to which data is streamed.
|
|
146
|
+
* @returns {Promise<void>} A promise that resolves when the data has been successfully streamed.
|
|
147
|
+
* @throws {Error} Throws an error if the Openlayer API key is not set or an error occurs in the streaming process.
|
|
144
148
|
*/
|
|
145
|
-
streamData: (data:
|
|
149
|
+
streamData: (data: StreamingData, config: StreamingDataConfig, inferencePipelineId: string) => Promise<void>;
|
|
146
150
|
/**
|
|
147
|
-
* Creates a new inference pipeline in Openlayer
|
|
148
|
-
* @param
|
|
149
|
-
* @param
|
|
150
|
-
* @
|
|
151
|
-
* @
|
|
151
|
+
* Creates a new inference pipeline in Openlayer or loads an existing one.
|
|
152
|
+
* @param {string} projectId - The ID of the project containing the inference pipeline.
|
|
153
|
+
* @param {string} [name='production'] - The name of the inference pipeline, defaults to 'production'.
|
|
154
|
+
* @returns {Promise<OpenlayerInferencePipeline>} A promise that resolves to an OpenlayerInferencePipeline object.
|
|
155
|
+
* @throws {Error} Throws an error if the inference pipeline cannot be created or found.
|
|
152
156
|
*/
|
|
153
157
|
createInferencePipeline: (projectId: string, name?: string) => Promise<OpenlayerInferencePipeline>;
|
|
154
158
|
/**
|
|
155
|
-
* Creates a new project in Openlayer
|
|
156
|
-
* @param name The name of the project.
|
|
157
|
-
* @param taskType The type of task associated with the project.
|
|
158
|
-
* @param description Optional description of the project.
|
|
159
|
-
* @
|
|
160
|
-
* @
|
|
159
|
+
* Creates a new project in Openlayer or loads an existing one.
|
|
160
|
+
* @param {string} name - The name of the project.
|
|
161
|
+
* @param {OpenlayerTaskType} taskType - The type of task associated with the project.
|
|
162
|
+
* @param {string} [description] - Optional description of the project.
|
|
163
|
+
* @returns {Promise<OpenlayerProject>} A promise that resolves to an OpenlayerProject object.
|
|
164
|
+
* @throws {Error} Throws an error if the project cannot be created or found.
|
|
161
165
|
*/
|
|
162
166
|
createProject: (name: string, taskType: OpenlayerTaskType, description?: string) => Promise<OpenlayerProject>;
|
|
163
167
|
/**
|
|
164
168
|
* Loads an existing inference pipeline from Openlayer based on its name and project ID.
|
|
165
|
-
* @param
|
|
166
|
-
* @param
|
|
167
|
-
* @
|
|
168
|
-
* @
|
|
169
|
+
* @param {string} projectId - The ID of the project containing the inference pipeline.
|
|
170
|
+
* @param {string} [name='production'] - The name of the inference pipeline, defaults to 'production'.
|
|
171
|
+
* @returns {Promise<OpenlayerInferencePipeline>} A promise that resolves to an OpenlayerInferencePipeline object.
|
|
172
|
+
* @throws {Error} Throws an error if the inference pipeline is not found.
|
|
169
173
|
*/
|
|
170
174
|
loadInferencePipeline: (projectId: string, name?: string) => Promise<OpenlayerInferencePipeline>;
|
|
175
|
+
/**
|
|
176
|
+
* Loads an existing project from Openlayer based on its name.
|
|
177
|
+
* @param {string} name - The name of the project.
|
|
178
|
+
* @returns {Promise<OpenlayerProject>} A promise that resolves to an OpenlayerProject object.
|
|
179
|
+
* @throws {Error} Throws an error if the project is not found.
|
|
180
|
+
*/
|
|
171
181
|
loadProject: (name: string) => Promise<OpenlayerProject>;
|
|
172
182
|
}
|
|
173
183
|
export declare class OpenAIMonitor {
|
|
@@ -176,24 +186,26 @@ export declare class OpenAIMonitor {
|
|
|
176
186
|
private openlayerProjectName;
|
|
177
187
|
private openlayerInferencePipelineName;
|
|
178
188
|
private monitoringOn;
|
|
189
|
+
/**
|
|
190
|
+
* Constructs an OpenAIMonitor instance.
|
|
191
|
+
* @param {OpenAIMonitorConstructorProps} props - The configuration properties for the OpenAI and Openlayer clients.
|
|
192
|
+
*/
|
|
179
193
|
constructor({ openAiApiKey, openlayerApiKey, openlayerProjectName, openlayerInferencePipelineName, openlayerServerUrl, }: OpenAIMonitorConstructorProps);
|
|
180
194
|
private formatChatCompletionInput;
|
|
181
195
|
/**
|
|
182
|
-
* Creates a
|
|
183
|
-
*
|
|
184
|
-
* @param
|
|
185
|
-
* @
|
|
186
|
-
* @throws Throws an error if monitoring is not active or if
|
|
187
|
-
* @returns A promise that resolves to a ChatCompletion or a Stream of ChatCompletionChunks.
|
|
196
|
+
* Creates a chat completion using the OpenAI client and streams the result to Openlayer.
|
|
197
|
+
* @param {ChatCompletionCreateParams} body - The parameters for creating a chat completion.
|
|
198
|
+
* @param {RequestOptions} [options] - Optional request options.
|
|
199
|
+
* @returns {Promise<ChatCompletion | Stream<ChatCompletionChunk>>} Promise of a ChatCompletion or a Stream
|
|
200
|
+
* @throws {Error} Throws an error if monitoring is not active or if no output is received from OpenAI.
|
|
188
201
|
*/
|
|
189
202
|
createChatCompletion: (body: ChatCompletionCreateParams, options?: RequestOptions) => Promise<ChatCompletion | Stream<ChatCompletionChunk>>;
|
|
190
203
|
/**
|
|
191
|
-
* Creates a
|
|
192
|
-
*
|
|
193
|
-
* @param
|
|
194
|
-
* @
|
|
195
|
-
* @throws Throws an error if monitoring is not active or if no prompt is provided.
|
|
196
|
-
* @returns A promise that resolves to a Completion or a Stream of Completions.
|
|
204
|
+
* Creates a completion using the OpenAI client and streams the result to Openlayer.
|
|
205
|
+
* @param {CompletionCreateParams} body - The parameters for creating a completion.
|
|
206
|
+
* @param {RequestOptions} [options] - Optional request options.
|
|
207
|
+
* @returns {Promise<Completion | Stream<Completion>>} Promise that resolves to a Completion or a Stream.
|
|
208
|
+
* @throws {Error} Throws an error if monitoring is not active or if no prompt is provided.
|
|
197
209
|
*/
|
|
198
210
|
createCompletion: (body: CompletionCreateParams, options?: RequestOptions) => Promise<Completion | Stream<Completion>>;
|
|
199
211
|
/**
|
package/dist/index.js
CHANGED
|
@@ -21,11 +21,15 @@ 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
|
+
/**
|
|
25
|
+
* Constructs an OpenlayerClient instance.
|
|
26
|
+
* @param {OpenlayerClientConstructorProps} props - The config for the Openlayer client. The API key is required.
|
|
27
|
+
* @throws {Error} Throws an error if the Openlayer API key is not provided.
|
|
28
|
+
*/
|
|
24
29
|
constructor({ openlayerApiKey, openlayerServerUrl, }) {
|
|
25
|
-
this.
|
|
30
|
+
this.defaultConfig = {
|
|
26
31
|
groundTruthColumnName: null,
|
|
27
32
|
inferenceIdColumnName: 'id',
|
|
28
|
-
inputVariableNames: ['input'],
|
|
29
33
|
latencyColumnName: 'latency',
|
|
30
34
|
numOfTokenColumnName: 'tokens',
|
|
31
35
|
outputColumnName: 'output',
|
|
@@ -35,14 +39,13 @@ class OpenlayerClient {
|
|
|
35
39
|
this.version = '0.1.0a16';
|
|
36
40
|
this.resolvedQuery = (endpoint, args = {}) => (0, request_1.resolvedQuery)(this.openlayerServerUrl, endpoint, args);
|
|
37
41
|
/**
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
* @param
|
|
41
|
-
* @
|
|
42
|
-
* @throws Throws an error if Openlayer API key or
|
|
43
|
-
* @returns A promise that resolves when the data has been successfully uploaded.
|
|
42
|
+
* Streams data to the Openlayer inference pipeline.
|
|
43
|
+
* @param {StreamingData} data - The chat completion data to be streamed.
|
|
44
|
+
* @param {string} inferencePipelineId - The ID of the Openlayer inference pipeline to which data is streamed.
|
|
45
|
+
* @returns {Promise<void>} A promise that resolves when the data has been successfully streamed.
|
|
46
|
+
* @throws {Error} Throws an error if the Openlayer API key is not set or an error occurs in the streaming process.
|
|
44
47
|
*/
|
|
45
|
-
this.streamData = (data, inferencePipelineId) => __awaiter(this, void 0, void 0, function* () {
|
|
48
|
+
this.streamData = (data, config, inferencePipelineId) => __awaiter(this, void 0, void 0, function* () {
|
|
46
49
|
var _a;
|
|
47
50
|
if (!this.openlayerApiKey) {
|
|
48
51
|
throw new Error('Openlayer API key are required for streaming data.');
|
|
@@ -52,7 +55,7 @@ class OpenlayerClient {
|
|
|
52
55
|
const dataStreamQuery = this.resolvedQuery(dataStreamEndpoint);
|
|
53
56
|
const response = yield fetch(dataStreamQuery, {
|
|
54
57
|
body: JSON.stringify({
|
|
55
|
-
config
|
|
58
|
+
config,
|
|
56
59
|
rows: [
|
|
57
60
|
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
61
|
],
|
|
@@ -75,11 +78,11 @@ class OpenlayerClient {
|
|
|
75
78
|
}
|
|
76
79
|
});
|
|
77
80
|
/**
|
|
78
|
-
* Creates a new inference pipeline in Openlayer
|
|
79
|
-
* @param
|
|
80
|
-
* @param
|
|
81
|
-
* @
|
|
82
|
-
* @
|
|
81
|
+
* Creates a new inference pipeline in Openlayer or loads an existing one.
|
|
82
|
+
* @param {string} projectId - The ID of the project containing the inference pipeline.
|
|
83
|
+
* @param {string} [name='production'] - The name of the inference pipeline, defaults to 'production'.
|
|
84
|
+
* @returns {Promise<OpenlayerInferencePipeline>} A promise that resolves to an OpenlayerInferencePipeline object.
|
|
85
|
+
* @throws {Error} Throws an error if the inference pipeline cannot be created or found.
|
|
83
86
|
*/
|
|
84
87
|
this.createInferencePipeline = (projectId, name = 'production') => __awaiter(this, void 0, void 0, function* () {
|
|
85
88
|
try {
|
|
@@ -107,12 +110,12 @@ class OpenlayerClient {
|
|
|
107
110
|
}
|
|
108
111
|
});
|
|
109
112
|
/**
|
|
110
|
-
* Creates a new project in Openlayer
|
|
111
|
-
* @param name The name of the project.
|
|
112
|
-
* @param taskType The type of task associated with the project.
|
|
113
|
-
* @param description Optional description of the project.
|
|
114
|
-
* @
|
|
115
|
-
* @
|
|
113
|
+
* Creates a new project in Openlayer or loads an existing one.
|
|
114
|
+
* @param {string} name - The name of the project.
|
|
115
|
+
* @param {OpenlayerTaskType} taskType - The type of task associated with the project.
|
|
116
|
+
* @param {string} [description] - Optional description of the project.
|
|
117
|
+
* @returns {Promise<OpenlayerProject>} A promise that resolves to an OpenlayerProject object.
|
|
118
|
+
* @throws {Error} Throws an error if the project cannot be created or found.
|
|
116
119
|
*/
|
|
117
120
|
this.createProject = (name, taskType, description) => __awaiter(this, void 0, void 0, function* () {
|
|
118
121
|
try {
|
|
@@ -146,10 +149,10 @@ class OpenlayerClient {
|
|
|
146
149
|
});
|
|
147
150
|
/**
|
|
148
151
|
* Loads an existing inference pipeline from Openlayer based on its name and project ID.
|
|
149
|
-
* @param
|
|
150
|
-
* @param
|
|
151
|
-
* @
|
|
152
|
-
* @
|
|
152
|
+
* @param {string} projectId - The ID of the project containing the inference pipeline.
|
|
153
|
+
* @param {string} [name='production'] - The name of the inference pipeline, defaults to 'production'.
|
|
154
|
+
* @returns {Promise<OpenlayerInferencePipeline>} A promise that resolves to an OpenlayerInferencePipeline object.
|
|
155
|
+
* @throws {Error} Throws an error if the inference pipeline is not found.
|
|
153
156
|
*/
|
|
154
157
|
this.loadInferencePipeline = (projectId, name = 'production') => __awaiter(this, void 0, void 0, function* () {
|
|
155
158
|
const inferencePipelineEndpoint = `/projects/${projectId}/inference-pipelines`;
|
|
@@ -174,6 +177,12 @@ class OpenlayerClient {
|
|
|
174
177
|
}
|
|
175
178
|
return inferencePipeline;
|
|
176
179
|
});
|
|
180
|
+
/**
|
|
181
|
+
* Loads an existing project from Openlayer based on its name.
|
|
182
|
+
* @param {string} name - The name of the project.
|
|
183
|
+
* @returns {Promise<OpenlayerProject>} A promise that resolves to an OpenlayerProject object.
|
|
184
|
+
* @throws {Error} Throws an error if the project is not found.
|
|
185
|
+
*/
|
|
177
186
|
this.loadProject = (name) => __awaiter(this, void 0, void 0, function* () {
|
|
178
187
|
const projectsEndpoint = '/projects';
|
|
179
188
|
const projectsQueryParameters = {
|
|
@@ -209,21 +218,22 @@ class OpenlayerClient {
|
|
|
209
218
|
}
|
|
210
219
|
exports.OpenlayerClient = OpenlayerClient;
|
|
211
220
|
class OpenAIMonitor {
|
|
221
|
+
/**
|
|
222
|
+
* Constructs an OpenAIMonitor instance.
|
|
223
|
+
* @param {OpenAIMonitorConstructorProps} props - The configuration properties for the OpenAI and Openlayer clients.
|
|
224
|
+
*/
|
|
212
225
|
constructor({ openAiApiKey, openlayerApiKey, openlayerProjectName, openlayerInferencePipelineName, openlayerServerUrl, }) {
|
|
213
226
|
this.openlayerInferencePipelineName = 'production';
|
|
214
227
|
this.monitoringOn = false;
|
|
215
|
-
this.formatChatCompletionInput = (messages) => messages
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
.join('\n')
|
|
219
|
-
.trim();
|
|
228
|
+
this.formatChatCompletionInput = (messages) => messages.map(({ content, role }, i) => (role === 'user'
|
|
229
|
+
? `{{ message_${i} }}`
|
|
230
|
+
: content));
|
|
220
231
|
/**
|
|
221
|
-
* Creates a
|
|
222
|
-
*
|
|
223
|
-
* @param
|
|
224
|
-
* @
|
|
225
|
-
* @throws Throws an error if monitoring is not active or if
|
|
226
|
-
* @returns A promise that resolves to a ChatCompletion or a Stream of ChatCompletionChunks.
|
|
232
|
+
* Creates a chat completion using the OpenAI client and streams the result to Openlayer.
|
|
233
|
+
* @param {ChatCompletionCreateParams} body - The parameters for creating a chat completion.
|
|
234
|
+
* @param {RequestOptions} [options] - Optional request options.
|
|
235
|
+
* @returns {Promise<ChatCompletion | Stream<ChatCompletionChunk>>} Promise of a ChatCompletion or a Stream
|
|
236
|
+
* @throws {Error} Throws an error if monitoring is not active or if no output is received from OpenAI.
|
|
227
237
|
*/
|
|
228
238
|
this.createChatCompletion = (body, options) => __awaiter(this, void 0, void 0, function* () {
|
|
229
239
|
var _a, e_1, _b, _c;
|
|
@@ -238,6 +248,15 @@ class OpenAIMonitor {
|
|
|
238
248
|
// Accumulate output for streamed responses
|
|
239
249
|
let outputData = '';
|
|
240
250
|
const response = yield this.openAIClient.chat.completions.create(body, options);
|
|
251
|
+
const prompt = this.formatChatCompletionInput(body.messages);
|
|
252
|
+
const inputVariableNames = prompt
|
|
253
|
+
.filter(({ role }) => role === 'user')
|
|
254
|
+
.map(({ content }) => content);
|
|
255
|
+
const inputVariables = body.messages
|
|
256
|
+
.filter(({ role }) => role === 'user')
|
|
257
|
+
.map(({ content }) => content);
|
|
258
|
+
const inputVariablesMap = inputVariableNames.reduce((acc, name, i) => (Object.assign(Object.assign({}, acc), { [name]: inputVariables[i] })), {});
|
|
259
|
+
const config = Object.assign(Object.assign({}, this.openlayerClient.defaultConfig), { inputVariableNames });
|
|
241
260
|
if (body.stream) {
|
|
242
261
|
const streamedResponse = response;
|
|
243
262
|
try {
|
|
@@ -258,12 +277,7 @@ class OpenAIMonitor {
|
|
|
258
277
|
}
|
|
259
278
|
const endTime = Date.now();
|
|
260
279
|
const latency = endTime - startTime;
|
|
261
|
-
this.openlayerClient.streamData({
|
|
262
|
-
input: this.formatChatCompletionInput(body.messages),
|
|
263
|
-
latency,
|
|
264
|
-
output: outputData,
|
|
265
|
-
timestamp: startTime,
|
|
266
|
-
}, inferencePipeline.id);
|
|
280
|
+
this.openlayerClient.streamData(Object.assign({ latency, output: outputData, prompt, timestamp: startTime }, inputVariablesMap), config, inferencePipeline.id);
|
|
267
281
|
}
|
|
268
282
|
else {
|
|
269
283
|
const nonStreamedResponse = response;
|
|
@@ -274,23 +288,18 @@ class OpenAIMonitor {
|
|
|
274
288
|
if (typeof output !== 'string') {
|
|
275
289
|
throw new Error('No output received from OpenAI.');
|
|
276
290
|
}
|
|
277
|
-
this.openlayerClient.streamData({
|
|
278
|
-
input: this.formatChatCompletionInput(body.messages),
|
|
279
|
-
latency,
|
|
291
|
+
this.openlayerClient.streamData(Object.assign({ latency,
|
|
280
292
|
output,
|
|
281
|
-
timestamp: startTime,
|
|
282
|
-
tokens: (_e = (_d = nonStreamedResponse.usage) === null || _d === void 0 ? void 0 : _d.total_tokens) !== null && _e !== void 0 ? _e : 0,
|
|
283
|
-
}, inferencePipeline.id);
|
|
293
|
+
prompt, timestamp: startTime, tokens: (_e = (_d = nonStreamedResponse.usage) === null || _d === void 0 ? void 0 : _d.total_tokens) !== null && _e !== void 0 ? _e : 0 }, inputVariablesMap), config, inferencePipeline.id);
|
|
284
294
|
}
|
|
285
295
|
return response;
|
|
286
296
|
});
|
|
287
297
|
/**
|
|
288
|
-
* Creates a
|
|
289
|
-
*
|
|
290
|
-
* @param
|
|
291
|
-
* @
|
|
292
|
-
* @throws Throws an error if monitoring is not active or if no prompt is provided.
|
|
293
|
-
* @returns A promise that resolves to a Completion or a Stream of Completions.
|
|
298
|
+
* Creates a completion using the OpenAI client and streams the result to Openlayer.
|
|
299
|
+
* @param {CompletionCreateParams} body - The parameters for creating a completion.
|
|
300
|
+
* @param {RequestOptions} [options] - Optional request options.
|
|
301
|
+
* @returns {Promise<Completion | Stream<Completion>>} Promise that resolves to a Completion or a Stream.
|
|
302
|
+
* @throws {Error} Throws an error if monitoring is not active or if no prompt is provided.
|
|
294
303
|
*/
|
|
295
304
|
this.createCompletion = (body, options) => __awaiter(this, void 0, void 0, function* () {
|
|
296
305
|
var _g, e_2, _h, _j;
|
|
@@ -309,6 +318,7 @@ class OpenAIMonitor {
|
|
|
309
318
|
let outputData = '';
|
|
310
319
|
let tokensData = 0;
|
|
311
320
|
const response = yield this.openAIClient.completions.create(body, options);
|
|
321
|
+
const config = Object.assign(Object.assign({}, this.openlayerClient.defaultConfig), { inputVariableNames: ['input'] });
|
|
312
322
|
if (body.stream) {
|
|
313
323
|
const streamedResponse = response;
|
|
314
324
|
try {
|
|
@@ -336,7 +346,7 @@ class OpenAIMonitor {
|
|
|
336
346
|
output: outputData,
|
|
337
347
|
timestamp: startTime,
|
|
338
348
|
tokens: tokensData,
|
|
339
|
-
}, inferencePipeline.id);
|
|
349
|
+
}, config, inferencePipeline.id);
|
|
340
350
|
}
|
|
341
351
|
else {
|
|
342
352
|
const nonStreamedResponse = response;
|
|
@@ -349,7 +359,7 @@ class OpenAIMonitor {
|
|
|
349
359
|
output: nonStreamedResponse.choices[0].text,
|
|
350
360
|
timestamp: startTime,
|
|
351
361
|
tokens: (_o = (_m = nonStreamedResponse.usage) === null || _m === void 0 ? void 0 : _m.total_tokens) !== null && _o !== void 0 ? _o : 0,
|
|
352
|
-
}, inferencePipeline.id);
|
|
362
|
+
}, config, inferencePipeline.id);
|
|
353
363
|
}
|
|
354
364
|
return response;
|
|
355
365
|
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* This example shows how to use Openlayer to monitor your LangChain workflows.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { ChatOpenAI } from 'langchain/chat_models/openai';
|
|
6
|
+
import { OpenlayerClient } from 'openlayer';
|
|
7
|
+
|
|
8
|
+
// Instantiate the Openlayer client with your API key
|
|
9
|
+
const openlayer = new OpenlayerClient({
|
|
10
|
+
openlayerApiKey: 'YOUR_OPENLAYER_API_KEY',
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
// Create or load your project
|
|
14
|
+
const project = await openlayer.createProject('YOUR_PROJECT_NAME', 'llm-base');
|
|
15
|
+
|
|
16
|
+
/*
|
|
17
|
+
* Create or load an inference pipeline from your project.
|
|
18
|
+
* If no name is specified, it will default to 'production'
|
|
19
|
+
*/
|
|
20
|
+
const inferencePipeline = await openlayer.createInferencePipeline(project.id);
|
|
21
|
+
const chatModel = new ChatOpenAI();
|
|
22
|
+
const inputs = [
|
|
23
|
+
'What is the meaning of life?',
|
|
24
|
+
'What would be a good name for a company that makes colorful socks?',
|
|
25
|
+
];
|
|
26
|
+
|
|
27
|
+
await Promise.all(
|
|
28
|
+
inputs.map(async (input) => {
|
|
29
|
+
// Call the LLM
|
|
30
|
+
const output = await chatModel.predict(input);
|
|
31
|
+
|
|
32
|
+
// Stream the results to Openlayer
|
|
33
|
+
await openlayer.streamData(
|
|
34
|
+
{
|
|
35
|
+
input,
|
|
36
|
+
output,
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
...openlayer.defaultConfig,
|
|
40
|
+
inputVariableNames: ['input'],
|
|
41
|
+
},
|
|
42
|
+
inferencePipeline.id
|
|
43
|
+
);
|
|
44
|
+
})
|
|
45
|
+
);
|
package/package.json
CHANGED
package/static/logo.png
ADDED
|
Binary file
|