Skip to content

Commit 9fea23c

Browse files
committed
👌 IMPROVE: Example updates
1 parent f083e6b commit 9fea23c

File tree

14 files changed

+200
-67
lines changed

14 files changed

+200
-67
lines changed

examples/everything/.env.example

Lines changed: 0 additions & 2 deletions
This file was deleted.

examples/everything/index.ts

Lines changed: 0 additions & 38 deletions
This file was deleted.

examples/everything/package.json

Lines changed: 0 additions & 20 deletions
This file was deleted.

examples/node/.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# These env vars should always be used on the server side and can be called anything.
2+
PIPE_LESS_WORDY=""
3+
LANGBASE_SDK_GENERATE_PIPE=""
4+
LANGBASE_SDK_CHAT_PIPE=""
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Generates a text completion using `generateText()`
3+
*
4+
* @docs: https://langbase.com/docs/langbase-sdk/generate-text
5+
*/
6+
7+
import 'dotenv/config';
8+
import {Pipe} from 'langbase';
9+
10+
(async () => {
11+
console.log('\n============= CHAT PIPE =============');
12+
13+
// Initiate the Pipe.
14+
const myPipe = new Pipe({
15+
apiKey: process.env.LANGBASE_SDK_CHAT_PIPE!,
16+
});
17+
18+
// Message 1: Tell the AI about something.
19+
const userMsg1 = 'My company is ⌘ Langbase.';
20+
let {completion, threadId} = await myPipe.generateText({
21+
messages: [{role: 'user', content: userMsg1}],
22+
chat: true,
23+
});
24+
console.log(`User: `, userMsg1);
25+
console.log(`AI: `, completion);
26+
27+
// Message 2: Ask the AI about what you told in previous message.
28+
const userMsg2 = 'What is the name of my company?';
29+
const {completion: completion2} = await myPipe.generateText({
30+
messages: [{role: 'user', content: userMsg2}],
31+
threadId,
32+
chat: true,
33+
});
34+
console.log(`User: `, userMsg2);
35+
console.log(`AI: `, completion2);
36+
})();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Generates a text completion using `generateText()`
3+
*
4+
* @docs: https://langbase.com/docs/langbase-sdk/generate-text
5+
*/
6+
7+
import 'dotenv/config';
8+
import {Pipe} from 'langbase';
9+
10+
(async () => {
11+
console.log('\n============= GENERATE PIPE =============');
12+
13+
// Initiate the Pipe.
14+
const myPipe = new Pipe({
15+
apiKey: process.env.LANGBASE_SDK_GENERATE_PIPE!,
16+
});
17+
18+
// Generate the text by asking a question.
19+
let {completion} = await myPipe.generateText({
20+
messages: [{role: 'user', content: 'Who is an AI Engineer?'}],
21+
});
22+
console.log(completion);
23+
})();

examples/everything/generate-text.ts renamed to examples/node/examples/pipes/generate-text.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ import 'dotenv/config';
88
import {Pipe} from 'langbase';
99

1010
// 1. Initiate the Pipe.
11-
const pipe = new Pipe({
12-
apiKey: process.env.PIPE_LESS_WORDY!,
11+
const myPipe = new Pipe({
12+
apiKey: process.env.LANGBASE_SDK_GENERATE_PIPE!,
1313
});
1414

1515
// 3. Generate the text by asking a question.
16-
const result = await pipe.generateText({
16+
const {completion} = await myPipe.generateText({
1717
messages: [{role: 'user', content: 'Who is an AI Engineer?'}],
1818
});
1919

2020
// 4. Done: You got the generated completion.
21-
console.log(result.completion);
21+
console.log(completion);
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Generates a text completion using `streamText()`
3+
*
4+
* @docs: https://langbase.com/docs/langbase-sdk/stream-text
5+
*/
6+
import 'dotenv/config';
7+
import type {StreamText} from 'langbase';
8+
import {Pipe, printStreamToStdout} from 'langbase';
9+
10+
// Helper function to print the stream.
11+
// You can write your own — or use printStreamToStdout() from the SDK.
12+
const helperPrintStream = async (stream: StreamText) => {
13+
for await (const chunk of stream) {
14+
// Streaming text part — a single word or several.
15+
const textPart = chunk.choices[0]?.delta?.content || '';
16+
17+
// Demo: Print the stream — you can use it however.
18+
process.stdout.write(textPart);
19+
}
20+
};
21+
22+
// Helper function to print the chat.
23+
const printChat = async ({
24+
userMessage,
25+
stream,
26+
}: {
27+
userMessage: string;
28+
stream: StreamText;
29+
}) => {
30+
console.log(`\n`);
31+
console.log(`User: `, userMessage);
32+
console.log(`AI: `);
33+
await printStreamToStdout(stream);
34+
};
35+
36+
(async () => {
37+
console.log('\n\n============= CHAT PIPE =============');
38+
39+
// Initiate the Pipe.
40+
const myPipe = new Pipe({
41+
apiKey: process.env.LANGBASE_SDK_CHAT_PIPE!,
42+
});
43+
44+
// Message 1: Tell the AI about something.
45+
const userMsg1 = 'My company is ⌘ Langbase.';
46+
let {stream, threadId} = await myPipe.streamText({
47+
messages: [{role: 'user', content: userMsg1}],
48+
chat: true,
49+
});
50+
51+
await printChat({userMessage: userMsg1, stream});
52+
53+
// Message 2: Ask the AI about what you told in previous message.
54+
const userMsg2 = 'What is the name of my company?';
55+
const {stream: stream2} = await myPipe.streamText({
56+
messages: [{role: 'user', content: userMsg2}],
57+
threadId,
58+
chat: true,
59+
});
60+
await printChat({userMessage: userMsg2, stream: stream2});
61+
})();

0 commit comments

Comments
 (0)