Skip to content

Commit

Permalink
📦 NEW: Examples
Browse files Browse the repository at this point in the history
  • Loading branch information
msaaddev committed Nov 26, 2024
1 parent 32d83aa commit 83d7ddd
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
32 changes: 32 additions & 0 deletions examples/nodejs/examples/pipes/pipe.run.chat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'dotenv/config';
import {Langbase} from 'langbase';

const langbase = new Langbase({
apiKey: process.env.LANGBASE_API_KEY!,
});

async function main() {
// Message 1: Tell something to the LLM.
const response1 = await langbase.pipe.run({
name: 'summary',
messages: [{role: 'user', content: 'My company is called Langbase'}],
});

console.log(response1.completion);

// Message 2: Ask something about the first message.
// Continue the conversation in the same thread by sending
// `threadId` from the second message onwards.
const response2 = await langbase.pipe.run({
name: 'summary',
threadId: response1.threadId!,
messages: [{role: 'user', content: 'Tell me the name of my company?'}],
});

console.log(response2.completion);
// You'll see any LLM will know the company is `Langbase`
// since it's the same chat thread. This is how you can
// continue a conversation in the same thread.
}

main();
35 changes: 35 additions & 0 deletions examples/nodejs/examples/pipes/pipe.run.stream.chat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import 'dotenv/config';
import {Langbase, getRunner} from 'langbase';

const langbase = new Langbase({
apiKey: process.env.LANGBASE_API_KEY!,
});

// Message 1: Tell something to the LLM.
const response1 = await langbase.pipe.run({
name: 'summary',
stream: true,
messages: [{role: 'user', content: 'My company is called Langbase'}],
});

const runner1 = getRunner(response1.stream);

runner1.on('content', content => {
process.stdout.write(content);
});

// Message 2: Ask something about the first message.
// Continue the conversation in the same thread by sending
// `threadId` from the second message onwards.
const response2 = await langbase.pipe.run({
name: 'summary',
stream: true,
threadId: response1.threadId!,
messages: [{role: 'user', content: 'Tell me the name of my company?'}],
});

const runner2 = getRunner(response2.stream);

runner2.on('content', content => {
process.stdout.write(content);
});
2 changes: 2 additions & 0 deletions examples/nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"scripts": {
"generate-text": "npx tsx ./examples/pipes/generate-text.ts",
"pipe.create": "npx tsx ./examples/pipes/pipe.create.ts",
"pipe.run.chat": "npx tsx ./examples/pipes/pipe.run.chat.ts",
"pipe.run.stream.chat": "npx tsx ./examples/pipes/pipe.run.stream.chat.ts",
"memory.create": "npx tsx ./examples/memory/memory.create.ts",
"memory.list": "npx tsx ./examples/memory/memory.list.ts",
"memory.delete": "npx tsx ./examples/memory/memory.delete.ts",
Expand Down

0 comments on commit 83d7ddd

Please sign in to comment.