Multi Agent

This example demonstrates how to create an multi agentic workflow chaining multiple agents where one agent's output feeds into another.


Multi Agent Example

List Pipe Agents Example

// Basic example to demonstrate how to feed the output of an agent as an input to another agent.

import dotenv from "dotenv";
import { Langbase } from "langbase";

dotenv.config();

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

async function main() {
    await createSummaryAgent();

    // First agent: Summarize text
    const summarizeAgent = async (text: string) => {
        const response = await langbase.pipes.run({
            stream: false,
            name: "summary-agent",
            messages: [
                {
                    role: "user",
                    content: `Summarize the following text: ${text}`
                }
            ]
        });
        return response.completion;
    };

    await createGenerateQuestionsAgent();
    // Second agent: Generate questions
    const questionsAgent = async (summary: string) => {
        const response = await langbase.pipes.run({
            stream: false,
            name: "generate-questions-agent",
            messages: [
                {
                    role: "user",
                    content: `Generate 3 questions based on this summary: ${summary}`
                }
            ]
        });
        return response.completion;
    };

    // Router agent: Orchestrate the flow
    const workflow = async (inputText: string) => {
        const summary = await summarizeAgent(inputText);
        const questions = await questionsAgent(summary);
        return { summary, questions };
    };

    // Example usage
    const inputText = "Artificial intelligence (AI) is intelligence demonstrated by machines, as opposed to natural intelligence displayed by humans. AI research has been defined as the field of study of intelligent agents, which refers to any system that perceives its environment and takes actions that maximize its chance of achieving its goals.";

    const result = await workflow(inputText);
    console.log("Summary:", result.summary);
    console.log("Questions:", result.questions);
}


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


/**
 * Creates a questions agent pipe if it doesn't already exist.
 *
 * This function checks if a pipe with the name 'questions-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 createGenerateQuestionsAgent() {
	try {
	    await langbase.pipes.create({
			name: 'generate-questions-agent',
			upsert: true,
			status: 'private',
			messages: [
				{
					role: 'system',
					content: 'You are a helpful assistant that help user to generate questions based on the text.',
				}
			]
		});
	} catch (error) {
		console.error('Error creating questions agent:', error);
	}
}

main();