多Agent系统设计模式
设计多Agent系统有多种方式,但我们常见两种广泛适用的模式:- 管理者模式(Agent作为工具):中央管理者/编排器将专业子Agent作为工具调用,并保持对话控制权。
- 交接模式:对等Agent将控制权移交给接管对话的专业Agent。这是去中心化的。
构建协作式Agent系统
npm install @llamaindex/workflow @llamaindex/openai llamaindex zod
import { agent } from '@llamaindex/workflow';
import { OpenAI } from '@llamaindex/openai';
import { createMemory } from 'llamaindex';
const llm = new OpenAI({
model: process.env.OPENAI_MODEL || 'gpt-4o-mini',
apiKey: process.env.OPENAI_API_KEY,
baseURL: process.env.OPENAI_BASE_URL
});
const translatorAgent = agent({
llm,
name: 'TranslatorAgent',
description: '将获取的内容翻译为目标语言,同时保留结构',
systemPrompt: `你是TranslatorAgent。翻译提供的内容。
保留标题、列表、表格、代码块、引用和链接。
如果已经是目标语言,则原样返回。仅输出Markdown。`
});
const summarizerAgent = agent({
llm,
name: 'SummarizerAgent',
description: '提取实体和事实;生成结构化内容和思维导图',
systemPrompt: '你是SummarizerAgent。从翻译后的文章中提取关键实体和事实(带引用),构建主题大纲。',
memory: createMemory({ tokenLimit: 100 * 1024 })
});
此页面对您有帮助吗?