Skip to content

Commit

Permalink
👌 IMPROVE: Example docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmadawais committed Jul 24, 2024
1 parent 05be989 commit d39f27a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 5 deletions.
18 changes: 18 additions & 0 deletions examples/everything/generate-text.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'dotenv/config';
import {Pipe} from 'langbase';

// STREAM: OFF
console.log('STREAM-OFF: generateText()');

// 1. Initiate the Pipe.
const pipeStreamOff = new Pipe({
apiKey: process.env.PIPE_LESS_WORDY!,
});

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

// 4. Done: You got the generated completion.
console.log(result.completion);
19 changes: 15 additions & 4 deletions examples/everything/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,39 @@ import 'dotenv/config';
import {Pipe} from 'langbase';

// STREAM: OFF
console.log('STREAM-OFF: generateText()');

// 1. Initiate the Pipe.
const pipeStreamOff = new Pipe({
apiKey: process.env.PIPE_LESS_WORDY!,
});

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

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

// STREAM: ON
console.log('STREAM-ON: streamText()');

// 1. Initiate the Pipe.
const pipeStreaming = new Pipe({
apiKey: process.env.PIPE_LESS_WORDY_STREAM!,
});

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

console.log('\n');
console.log('STEAM-ON');
// 3. Print the stream
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content || '');
// Streaming text part — a single word or several.
const textPart = chunk.choices[0]?.delta?.content || '';

// Demo: Print the stream — you can use it however.
process.stdout.write(textPart);
}
4 changes: 3 additions & 1 deletion examples/everything/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"type": "module",
"main": "index.js",
"scripts": {
"test": "npx tsx index.ts"
"test": "npx tsx index.ts",
"generate-text": "npx tsx generate-text.ts",
"stream-text": "npx tsx stream-text.ts"
},
"keywords": [],
"author": "Ahmad Awais <[email protected]> (https://twitter.com/MrAhmadAwais)",
Expand Down
24 changes: 24 additions & 0 deletions examples/everything/stream-text.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'dotenv/config';
import {Pipe} from 'langbase';

// STREAM: ON
console.log('STREAM-ON: streamText()');

// 1. Initiate the Pipe.
const pipeStreaming = new Pipe({
apiKey: process.env.PIPE_LESS_WORDY_STREAM!,
});

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

// 3. Print the stream
for await (const chunk of stream) {
// Streaming text part — a single word or several.
const textPart = chunk.choices[0]?.delta?.content || '';

// Demo: Print the stream — you can use it however.
process.stdout.write(textPart);
}

0 comments on commit d39f27a

Please sign in to comment.