Graph + 子智能体
结合 Graph 工作流与 Sub-Agent 协调,实现复杂的层级化任务分配。
快速开始#
克隆完整示例#
最简单的开始方式是克隆完整的示例仓库:
git clone https://github.com/astreus-ai/examples
cd examples/graph-sub-agents
npm install或仅安装包#
如果你更喜欢从零开始构建:
npm install @astreus-ai/astreus环境配置#
# .env
OPENAI_API_KEY=sk-your-openai-api-key-here
ANTHROPIC_API_KEY=your-anthropic-api-key-here
DB_URL=sqlite://./astreus.dbGraph + Sub-Agent 集成#
import { Agent, Graph } from '@astreus-ai/astreus';
// Create specialized sub-agents
const researcher = await Agent.create({
name: 'ResearchSpecialist',
model: 'gpt-6-astra',
systemPrompt: 'You conduct thorough research and gather comprehensive information.',
knowledge: true,
memory: true
});
const analyst = await Agent.create({
name: 'DataAnalyst',
model: 'gpt-6-astra',
systemPrompt: 'You analyze data and provide actionable insights.',
useTools: true
});
const writer = await Agent.create({
name: 'ContentWriter',
model: 'claude-sonnet-5',
systemPrompt: 'You create compelling, well-structured content.',
vision: true
});
// Create main coordinator agent with sub-agents
const coordinator = await Agent.create({
name: 'ProjectCoordinator',
model: 'gpt-6-astra',
systemPrompt: 'You coordinate complex projects using specialized sub-agents.',
subAgents: [researcher, analyst, writer]
});
// Create graph with sub-agent awareness
const projectGraph = new Graph({
name: 'Market Analysis Project',
subAgentAware: true,
maxConcurrency: 2
}, coordinator);
// Add tasks that leverage sub-agents
const researchTask = projectGraph.addTaskNode({
name: 'Market Research',
prompt: 'Research the AI healthcare market, including key players, market size, and growth trends',
useSubAgents: true,
subAgentDelegation: 'auto'
});
const analysisTask = projectGraph.addTaskNode({
name: 'Data Analysis',
prompt: 'Analyze the research data and identify key opportunities and challenges',
dependencies: [researchTask],
useSubAgents: true,
subAgentCoordination: 'parallel'
});
const reportTask = projectGraph.addTaskNode({
name: 'Executive Report',
prompt: 'Create a comprehensive executive report with recommendations',
dependencies: [analysisTask],
useSubAgents: true
});
// Execute the graph with intelligent sub-agent coordination
const result = await projectGraph.run();
// Display results
console.log('Project completed:', result.success);
console.log(`Tasks completed: ${result.completedNodes}/${result.completedNodes + result.failedNodes}`);
console.log(`Duration: ${result.duration}ms`);
// Display task results
if (result.results) {
console.log('\nTask Results:');
for (const [nodeId, nodeResult] of Object.entries(result.results)) {
console.log(`\n${nodeId}:`, nodeResult);
}
}
// Get the final report from the last task
const finalReport = result.results?.[reportTask];
if (finalReport) {
console.log('\n=== Final Executive Report ===');
console.log(finalReport);
}运行示例#
如果你克隆了仓库:
npm run dev如果你从零开始构建,请创建一个 index.ts 文件并写入上面的代码,然后运行:
npx tsx index.ts仓库#
完整示例可在 GitHub 上获取:examples/graph-sub-agents