Run Pipe Agent

This example demonstrates how to run a pipe agent with a user message.


Run Pipe Agent Example

Run Pipe Agent Example

import 'dotenv/config';
import { Langbase } from 'langbase';

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

async function main() {
	await createSummaryAgent();

	const response = await langbase.pipes.run({
		stream: false,
		name: 'summary-agent',
		messages: [
			{
				role: 'user',
				content: 'Who is an AI Engineer?',
			},
		],
	});

	console.log('response: ', response.completion);
}

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