๐Ÿ“ฆ Kong / volcano-sdk

๐Ÿ“„ 06-multi-agent.ts ยท 47 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47import { agent, llmOpenAI } from "../dist/volcano-sdk.js";

const llm = llmOpenAI({ 
  apiKey: process.env.OPENAI_API_KEY!, 
  model: "gpt-4o-mini" 
});

// Define specialized agents
const researcher = agent({
  llm,
  name: "researcher",
  description: "Expert at finding facts, data, and research. Use for information gathering."
}).then({ prompt: "Research the topic." }).then({ prompt: "Summarize the research." });

const writer = agent({
  llm,
  name: "writer", 
  description: "Creative writer who crafts engaging narratives. Use for content creation."
}).then({ prompt: "Write content." });

const editor = agent({
  llm,
  name: "editor",
  description: "Polishes content for clarity and grammar. Use for final review."
}).then({ prompt: "Review and polish." });

// Coordinator autonomously delegates to specialists
const results = await agent({ 
  llm,
  timeout: 300  // 5 minutes timeout for multi-agent coordination
})
  .then({
    prompt: "Create a blog post about the James Webb Space Telescope's latest discoveries",
    agents: [researcher, writer, editor]
  })
  .run();

// Use conversational results to get clean output
const blogPost = await results.ask(llm, "Show me the final blog post");
const agentsUsed = await results.ask(llm, "Which agents were used and what did each one do?");

console.log("\n" + blogPost);
console.log("\n" + agentsUsed);

process.exit(0);