Skip to content

Commit ea34fed

Browse files
committed
📦 NEW: pipe.run node example
1 parent 3a0f11a commit ea34fed

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import 'dotenv/config';
2+
import {getRunner, Pipe} from 'langbase';
3+
4+
const pipe = new Pipe({
5+
apiKey: process.env.LANGBASE_API_KEY!,
6+
name: 'summary',
7+
});
8+
9+
async function main() {
10+
const userMsg = 'Who is an AI Engineer?';
11+
12+
// Get readable stream
13+
const {stream, threadId, rawResponse} = await pipe.run({
14+
messages: [{role: 'user', content: userMsg}],
15+
stream: true,
16+
rawResponse: true,
17+
});
18+
19+
// Convert the stream to a stream runner.
20+
const runner = getRunner(stream);
21+
22+
// Method 1: Using event listeners
23+
runner.on('connect', () => {
24+
console.log('Stream started.\n');
25+
});
26+
27+
runner.on('content', content => {
28+
process.stdout.write(content);
29+
});
30+
31+
runner.on('end', () => {
32+
console.log('\nStream ended.');
33+
});
34+
35+
runner.on('error', error => {
36+
console.error('Error:', error);
37+
});
38+
}
39+
40+
main();
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import 'dotenv/config';
2+
import {Pipe} from 'langbase';
3+
4+
const pipe = new Pipe({
5+
apiKey: process.env.LANGBASE_API_KEY!,
6+
name: 'summary',
7+
});
8+
9+
async function main() {
10+
const userMsg = 'Who is an AI Engineer?';
11+
12+
const response = await pipe.run({
13+
messages: [
14+
{
15+
role: 'user',
16+
content: userMsg,
17+
},
18+
],
19+
stream: false,
20+
});
21+
console.log('response: ', response);
22+
}
23+
24+
main();

examples/nodejs/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
"main": "index.js",
88
"scripts": {
99
"generate-text": "npx tsx ./examples/pipes/generate-text.ts",
10+
"pipe.run": "npx tsx ./examples/pipes/pipe.run.ts",
11+
"pipe.run.stream": "npx tsx ./examples/pipes/pipe.run.stream.ts",
1012
"generate-text-generate-pipe": "npx tsx ./examples/pipes/generate-text-generate-pipe.ts",
1113
"generate-text-chat-pipe": "npx tsx ./examples/pipes/generate-text-chat-pipe.ts",
1214
"stream-text": "npx tsx ./examples/pipes/stream-text.ts",

0 commit comments

Comments
 (0)