Skip to content

Commit d39f27a

Browse files
committed
👌 IMPROVE: Example docs
1 parent 05be989 commit d39f27a

File tree

4 files changed

+60
-5
lines changed

4 files changed

+60
-5
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import 'dotenv/config';
2+
import {Pipe} from 'langbase';
3+
4+
// STREAM: OFF
5+
console.log('STREAM-OFF: generateText()');
6+
7+
// 1. Initiate the Pipe.
8+
const pipeStreamOff = new Pipe({
9+
apiKey: process.env.PIPE_LESS_WORDY!,
10+
});
11+
12+
// 3. Generate the text by asking a question.
13+
const result = await pipeStreamOff.generateText({
14+
messages: [{role: 'user', content: 'Who is an AI Engineer?'}],
15+
});
16+
17+
// 4. Done: You got the generated completion.
18+
console.log(result.completion);

examples/everything/index.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,39 @@ import 'dotenv/config';
22
import {Pipe} from 'langbase';
33

44
// STREAM: OFF
5+
console.log('STREAM-OFF: generateText()');
6+
7+
// 1. Initiate the Pipe.
58
const pipeStreamOff = new Pipe({
69
apiKey: process.env.PIPE_LESS_WORDY!,
710
});
811

12+
// 3. Generate the text by asking a question.
913
const result = await pipeStreamOff.generateText({
1014
messages: [{role: 'user', content: 'Who is an AI Engineer?'}],
1115
});
1216

13-
console.log('STEAM-OFF');
17+
// 4. Done: You got the generated completion.
1418
console.log(result.completion);
1519

1620
// STREAM: ON
21+
console.log('STREAM-ON: streamText()');
22+
23+
// 1. Initiate the Pipe.
1724
const pipeStreaming = new Pipe({
1825
apiKey: process.env.PIPE_LESS_WORDY_STREAM!,
1926
});
2027

28+
// 2. Generate a stream by asking a question
2129
const stream = await pipeStreaming.streamText({
2230
messages: [{role: 'user', content: 'Who is an AI Engineer?'}],
2331
});
2432

25-
console.log('\n');
26-
console.log('STEAM-ON');
33+
// 3. Print the stream
2734
for await (const chunk of stream) {
28-
process.stdout.write(chunk.choices[0]?.delta?.content || '');
35+
// Streaming text part — a single word or several.
36+
const textPart = chunk.choices[0]?.delta?.content || '';
37+
38+
// Demo: Print the stream — you can use it however.
39+
process.stdout.write(textPart);
2940
}

examples/everything/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
"type": "module",
66
"main": "index.js",
77
"scripts": {
8-
"test": "npx tsx index.ts"
8+
"test": "npx tsx index.ts",
9+
"generate-text": "npx tsx generate-text.ts",
10+
"stream-text": "npx tsx stream-text.ts"
911
},
1012
"keywords": [],
1113
"author": "Ahmad Awais <[email protected]> (https://twitter.com/MrAhmadAwais)",

examples/everything/stream-text.ts

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+
// STREAM: ON
5+
console.log('STREAM-ON: streamText()');
6+
7+
// 1. Initiate the Pipe.
8+
const pipeStreaming = new Pipe({
9+
apiKey: process.env.PIPE_LESS_WORDY_STREAM!,
10+
});
11+
12+
// 2. Generate a stream by asking a question
13+
const stream = await pipeStreaming.streamText({
14+
messages: [{role: 'user', content: 'Who is an AI Engineer?'}],
15+
});
16+
17+
// 3. Print the stream
18+
for await (const chunk of stream) {
19+
// Streaming text part — a single word or several.
20+
const textPart = chunk.choices[0]?.delta?.content || '';
21+
22+
// Demo: Print the stream — you can use it however.
23+
process.stdout.write(textPart);
24+
}

0 commit comments

Comments
 (0)