openlayer 0.1.19 → 0.1.21
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 -0
- package/dist/index.js +7 -5
- package/examples/langchain.mjs +6 -0
- package/examples/openai_monitor.mjs +53 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -32,6 +32,10 @@ export interface StreamingData {
|
|
|
32
32
|
* Configuration settings for uploading chat completion data to Openlayer.
|
|
33
33
|
*/
|
|
34
34
|
interface StreamingDataConfig {
|
|
35
|
+
/**
|
|
36
|
+
* The name of the column that stores the request cost data. Can be null.
|
|
37
|
+
*/
|
|
38
|
+
costColumnName: string | null;
|
|
35
39
|
/**
|
|
36
40
|
* The name of the column that stores the ground truth data. Can be null.
|
|
37
41
|
*/
|
package/dist/index.js
CHANGED
|
@@ -86,6 +86,7 @@ class OpenlayerClient {
|
|
|
86
86
|
*/
|
|
87
87
|
constructor({ openlayerApiKey, openlayerServerUrl, }) {
|
|
88
88
|
this.defaultConfig = {
|
|
89
|
+
costColumnName: 'cost',
|
|
89
90
|
groundTruthColumnName: null,
|
|
90
91
|
inferenceIdColumnName: 'id',
|
|
91
92
|
latencyColumnName: 'latency',
|
|
@@ -94,7 +95,7 @@ class OpenlayerClient {
|
|
|
94
95
|
timestampColumnName: 'timestamp',
|
|
95
96
|
};
|
|
96
97
|
this.openlayerServerUrl = 'https://api.openlayer.com/v1';
|
|
97
|
-
this.version = '0.1.
|
|
98
|
+
this.version = '0.1.0a20';
|
|
98
99
|
this.resolvedQuery = (endpoint, args = {}) => (0, request_1.resolvedQuery)(this.openlayerServerUrl, endpoint, args);
|
|
99
100
|
/**
|
|
100
101
|
* Creates a new inference pipeline in Openlayer or loads an existing one.
|
|
@@ -297,9 +298,10 @@ class OpenAIMonitor {
|
|
|
297
298
|
? undefined
|
|
298
299
|
: (inputCost !== null && inputCost !== void 0 ? inputCost : 0) + (outputCost !== null && outputCost !== void 0 ? outputCost : 0);
|
|
299
300
|
};
|
|
300
|
-
this.formatChatCompletionInput = (messages) => messages.map(({ content, role }, i) => (
|
|
301
|
-
? `{{ message_${i} }}`
|
|
302
|
-
|
|
301
|
+
this.formatChatCompletionInput = (messages) => messages.map(({ content, role }, i) => ({
|
|
302
|
+
content: role === 'user' ? `{{ message_${i} }}` : content,
|
|
303
|
+
role,
|
|
304
|
+
}));
|
|
303
305
|
/**
|
|
304
306
|
* Creates a chat completion using the OpenAI client and streams the result to Openlayer.
|
|
305
307
|
* @param {ChatCompletionCreateParams} body - The parameters for creating a chat completion.
|
|
@@ -323,7 +325,7 @@ class OpenAIMonitor {
|
|
|
323
325
|
const prompt = this.formatChatCompletionInput(body.messages);
|
|
324
326
|
const inputVariableNames = prompt
|
|
325
327
|
.filter(({ role }) => role === 'user')
|
|
326
|
-
.map(({ content }) => content);
|
|
328
|
+
.map(({ content }) => content.replace(/{{\s*|\s*}}/g, ''));
|
|
327
329
|
const inputVariables = body.messages
|
|
328
330
|
.filter(({ role }) => role === 'user')
|
|
329
331
|
.map(({ content }) => content);
|
package/examples/langchain.mjs
CHANGED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* This example shows how to use Openlayer to monitor your OpenAI workflows.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { OpenAIMonitor } from 'openlayer';
|
|
6
|
+
|
|
7
|
+
const monitor = new OpenAIMonitor({
|
|
8
|
+
openAiApiKey: 'YOUR_OPENAI_API_KEY',
|
|
9
|
+
openlayerApiKey: 'YOUR_OPENLAYER_API_KEY',
|
|
10
|
+
openlayerProjectName: 'YOUR_PROJECT_NAME',
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
monitor.startMonitoring();
|
|
14
|
+
|
|
15
|
+
const inputs = [
|
|
16
|
+
{
|
|
17
|
+
promptVersion: 'v1',
|
|
18
|
+
systemMessage:
|
|
19
|
+
'You are an all-knowing assistant. Answer questions thoughtfully.',
|
|
20
|
+
userMessage: 'What is the meaning of life?',
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
promptVersion: 'v2',
|
|
24
|
+
systemMessage: 'Be as creative as you can!',
|
|
25
|
+
userMessage:
|
|
26
|
+
'What would be a good name for a company that makes colorful socks?',
|
|
27
|
+
},
|
|
28
|
+
];
|
|
29
|
+
|
|
30
|
+
inputs.map(async ({ promptVersion, systemMessage, userMessage }) => {
|
|
31
|
+
// Stream the results to Openlayer
|
|
32
|
+
const chatCompletion = await monitor.createChatCompletion(
|
|
33
|
+
{
|
|
34
|
+
messages: [
|
|
35
|
+
{
|
|
36
|
+
content: systemMessage,
|
|
37
|
+
role: 'assistant',
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
content: userMessage,
|
|
41
|
+
role: 'user',
|
|
42
|
+
},
|
|
43
|
+
],
|
|
44
|
+
model: 'gpt-3.5-turbo',
|
|
45
|
+
},
|
|
46
|
+
undefined,
|
|
47
|
+
{
|
|
48
|
+
// Add any additional columns you want to track here
|
|
49
|
+
promptVersion,
|
|
50
|
+
}
|
|
51
|
+
);
|
|
52
|
+
console.log(chatCompletion);
|
|
53
|
+
});
|