Run Pipe Agent Streaming Chat

This example demonstrates how to use streaming responses with a Langbase pipe agent while maintaining conversation context.


Run Pipe Agent Streaming Chat Example

Run Pipe Agent Streaming Chat Example

import 'dotenv/config';
import {Langbase, getRunner} 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: true,
		name: 'summary-agent',
		messages: [
			{
				role: 'user', 
				content: 'My company is called Langbase'
			}
		]
	});

	const runner = await getRunner(response1.stream);
	for await (const chunk of runner) {
		if (chunk.choices[0]?.delta?.content) {
			process.stdout.write(chunk.choices[0].delta.content);
		}
	}
	console.log("\n");


	const threadId = response1.threadId;

	// 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: true,
		name: 'summary-agent',
		threadId: 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);
	});
}

/**
 * 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();