Skip to content

Commit d00a840

Browse files
committed
👌 IMPROVE: examples
1 parent 8745524 commit d00a840

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import 'dotenv/config';
2+
import {Pipe} from '../../pipes/pipes';
3+
4+
const myGeneratePipe = async () => {
5+
console.log('\n============= GENERATE PIPE =============');
6+
7+
// Initiate the Pipe.
8+
const myPipe = new Pipe({
9+
apiKey: process.env.LANGBASE_SDK_GENERATE_PIPE!,
10+
});
11+
12+
// Generate the text by asking a question.
13+
let {completion} = await myPipe.generateText({
14+
messages: [{role: 'user', content: 'Who is an AI Engineer?'}],
15+
});
16+
console.log(completion);
17+
};
18+
19+
const myChatPipe = async () => {
20+
console.log('\n============= CHAT PIPE =============');
21+
22+
// Initiate the Pipe.
23+
const myPipe = new Pipe({
24+
apiKey: process.env.LANGBASE_SDK_CHAT_PIPE!,
25+
});
26+
27+
// Message 1: Tell the AI about something.
28+
const userMsg1 = 'My company is ⌘ Langbase.';
29+
let {completion, threadId} = await myPipe.generateText({
30+
messages: [{role: 'user', content: userMsg1}],
31+
chat: true,
32+
});
33+
console.log(`User: `, userMsg1);
34+
console.log(`AI: `, completion);
35+
36+
// Message 2: Ask the AI about what you told in previous message.
37+
const userMsg2 = 'What is the name of my company?';
38+
const {completion: completion2} = await myPipe.generateText({
39+
messages: [{role: 'user', content: userMsg2}],
40+
threadId,
41+
chat: true,
42+
});
43+
console.log(`User: `, userMsg2);
44+
console.log(`AI: `, completion2);
45+
};
46+
47+
(async () => {
48+
await myGeneratePipe();
49+
await myChatPipe();
50+
})();
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import 'dotenv/config';
2+
import {Pipe, printStreamToStdout, StreamText} from '../../pipes/pipes';
3+
4+
// You can write your own — or use printStreamToStdout() from the SDK.
5+
const helperPrintStream = async (stream: StreamText) => {
6+
for await (const chunk of stream) {
7+
// Streaming text part — a single word or several.
8+
const textPart = chunk.choices[0]?.delta?.content || '';
9+
10+
// Demo: Print the stream — you can use it however.
11+
process.stdout.write(textPart);
12+
}
13+
};
14+
15+
const printChat = async ({
16+
userMessage,
17+
stream,
18+
}: {
19+
userMessage: string;
20+
stream: StreamText;
21+
}) => {
22+
console.log(`\n`);
23+
console.log(`User: `, userMessage);
24+
console.log(`AI: `);
25+
await printStreamToStdout(stream);
26+
};
27+
28+
const myGeneratePipe = async () => {
29+
console.log('\n============= GENERATE PIPE =============');
30+
31+
// Initiate the Pipe.
32+
const myPipe = new Pipe({
33+
apiKey: process.env.LANGBASE_SDK_GENERATE_PIPE!,
34+
});
35+
36+
// Generate the text by asking a question.
37+
const userMsg = 'Who is an AI Engineer?';
38+
let {stream} = await myPipe.streamText({
39+
messages: [{role: 'user', content: userMsg}],
40+
});
41+
42+
// Print.
43+
await printChat({userMessage: userMsg, stream});
44+
};
45+
46+
const myChatPipe = async () => {
47+
console.log('\n\n============= CHAT PIPE =============');
48+
49+
// Initiate the Pipe.
50+
const myPipe = new Pipe({
51+
apiKey: process.env.LANGBASE_SDK_CHAT_PIPE!,
52+
});
53+
54+
// Message 1: Tell the AI about something.
55+
const userMsg1 = 'My company is ⌘ Langbase.';
56+
let {stream, threadId} = await myPipe.streamText({
57+
messages: [{role: 'user', content: userMsg1}],
58+
chat: true,
59+
});
60+
61+
await printChat({userMessage: userMsg1, stream});
62+
63+
// Message 2: Ask the AI about what you told in previous message.
64+
const userMsg2 = 'What is the name of my company?';
65+
const {stream: stream2} = await myPipe.streamText({
66+
messages: [{role: 'user', content: userMsg2}],
67+
threadId,
68+
chat: true,
69+
});
70+
await printChat({userMessage: userMsg2, stream: stream2});
71+
};
72+
73+
(async () => {
74+
await myGeneratePipe();
75+
await myChatPipe();
76+
})();

0 commit comments

Comments
 (0)