Skip to content

Commit 6abc12f

Browse files
authored
Create multi-agent.ts
Add example for multi-agent workflow
1 parent 1092a58 commit 6abc12f

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Basic example to demonstrate how to feed the output of an agent as an input to another agent.
2+
3+
import dotenv from "dotenv";
4+
import { Langbase } from "langbase";
5+
6+
dotenv.config();
7+
8+
const langbase = new Langbase({
9+
apiKey: process.env.LANGBASE_API_KEY!,
10+
});
11+
12+
async function main() {
13+
// First agent: Summarize text
14+
const summarizeAgent = async (text: string) => {
15+
const response = await langbase.pipe.run({
16+
stream: false,
17+
name: "summarize",
18+
messages: [
19+
{
20+
role: "system",
21+
content: `You are a helpful assistant that summarizes text.`,
22+
},
23+
{
24+
role: "user",
25+
content: `Summarize the following text: ${text}`,
26+
},
27+
],
28+
});
29+
return response.completion;
30+
};
31+
32+
// Second agent: Generate questions
33+
const questionsAgent = async (summary: string) => {
34+
const response = await langbase.pipe.run({
35+
stream: false,
36+
name: "generate-questions",
37+
messages: [
38+
{
39+
role: "system",
40+
content: `You are a helpful assistant that generates questions.`,
41+
},
42+
{
43+
role: "user",
44+
content: `Generate 3 questions based on this summary: ${summary}`,
45+
},
46+
],
47+
});
48+
return response.completion;
49+
};
50+
51+
// Router agent: Orchestrate the flow
52+
const workflow = async (inputText: string) => {
53+
const summary = await summarizeAgent(inputText);
54+
const questions = await questionsAgent(summary);
55+
return { summary, questions };
56+
};
57+
58+
// Example usage
59+
const inputText =
60+
"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.";
61+
62+
const result = await workflow(inputText);
63+
console.log("Summary:", result.summary);
64+
console.log("Questions:", result.questions);
65+
}
66+
67+
main().catch(console.error);

0 commit comments

Comments
 (0)