openlayer 0.1.20 → 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.js +6 -5
- package/examples/langchain.mjs +6 -0
- package/examples/openai_monitor.mjs +53 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -95,7 +95,7 @@ class OpenlayerClient {
|
|
|
95
95
|
timestampColumnName: 'timestamp',
|
|
96
96
|
};
|
|
97
97
|
this.openlayerServerUrl = 'https://api.openlayer.com/v1';
|
|
98
|
-
this.version = '0.1.
|
|
98
|
+
this.version = '0.1.0a20';
|
|
99
99
|
this.resolvedQuery = (endpoint, args = {}) => (0, request_1.resolvedQuery)(this.openlayerServerUrl, endpoint, args);
|
|
100
100
|
/**
|
|
101
101
|
* Creates a new inference pipeline in Openlayer or loads an existing one.
|
|
@@ -298,9 +298,10 @@ class OpenAIMonitor {
|
|
|
298
298
|
? undefined
|
|
299
299
|
: (inputCost !== null && inputCost !== void 0 ? inputCost : 0) + (outputCost !== null && outputCost !== void 0 ? outputCost : 0);
|
|
300
300
|
};
|
|
301
|
-
this.formatChatCompletionInput = (messages) => messages.map(({ content, role }, i) => (
|
|
302
|
-
? `{{ message_${i} }}`
|
|
303
|
-
|
|
301
|
+
this.formatChatCompletionInput = (messages) => messages.map(({ content, role }, i) => ({
|
|
302
|
+
content: role === 'user' ? `{{ message_${i} }}` : content,
|
|
303
|
+
role,
|
|
304
|
+
}));
|
|
304
305
|
/**
|
|
305
306
|
* Creates a chat completion using the OpenAI client and streams the result to Openlayer.
|
|
306
307
|
* @param {ChatCompletionCreateParams} body - The parameters for creating a chat completion.
|
|
@@ -324,7 +325,7 @@ class OpenAIMonitor {
|
|
|
324
325
|
const prompt = this.formatChatCompletionInput(body.messages);
|
|
325
326
|
const inputVariableNames = prompt
|
|
326
327
|
.filter(({ role }) => role === 'user')
|
|
327
|
-
.map(({ content }) => content);
|
|
328
|
+
.map(({ content }) => content.replace(/{{\s*|\s*}}/g, ''));
|
|
328
329
|
const inputVariables = body.messages
|
|
329
330
|
.filter(({ role }) => role === 'user')
|
|
330
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
|
+
});
|