Run Pipe Agent Chat
This example demonstrates how to maintain a conversational thread with a Langbase pipe agent.
Run Pipe Agent Chat Example
Run Pipe Agent Chat Example
import 'dotenv/config';
import {Langbase} from 'langbase';
const langbase = new Langbase({
apiKey: process.env.LANGBASE_API_KEY!,
});
async function main() {
await createSummaryAgent();
// Message 1: Tell something to the LLM.
const response1 = await langbase.pipes.run({
stream: false,
name: 'summary-agent',
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.pipes.run({
stream: false,
name: 'summary-agent',
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.
}
/**
* Creates a summary agent pipe if it doesn't already exist.
*
* This function checks if a pipe with the name 'summary-agent' exists in the system.
* If the pipe doesn't exist, it creates a new private pipe with a system message
* configuring it as a helpful assistant.
*
* @async
* @returns {Promise<void>} A promise that resolves when the operation is complete
* @throws {Error} Logs any errors encountered during the creation process
*/
async function createSummaryAgent() {
try {
await langbase.pipes.create({
name: 'summary-agent',
upsert: true,
status: 'private',
messages: [
{
role: 'system',
content: 'You are a helpful assistant that help users summarize text.',
},
],
});
} catch (error) {
console.error('Error creating summary agent:', error);
}
}
main();