Expand description
Agent 执行模式
提供 Chain(链式)和 Parallel(并行)模式的 Agent 执行支持
§架构
┌─────────────────────────────────────────────────────────────────────────┐
│ Agent 执行模式 │
├─────────────────────────────────────────────────────────────────────────┤
│ │
│ Chain (链式模式) │
│ ┌─────┐ ┌─────┐ ┌─────┐ ┌─────┐ │
│ │Agent│───▶│Agent│───▶│Agent│───▶│Agent│ │
│ │ 1 │ │ 2 │ │ 3 │ │ N │ │
│ └─────┘ └─────┘ └─────┘ └─────┘ │
│ input output output output │
│ =input =input =final │
│ │
│ Parallel (并行模式) │
│ ┌─────┐ │
│ ┌─▶│Agent│──┐ │
│ │ │ 1 │ │ │
│ │ └─────┘ │ │
│ ┌─────┐ │ ┌─────┐ │ ┌──────────┐ ┌─────┐ │
│ │Input│──┼─▶│Agent│──┼─▶│Aggregator│───▶│Output│ │
│ └─────┘ │ │ 2 │ │ └──────────┘ └─────┘ │
│ │ └─────┘ │ │
│ │ ┌─────┐ │ │
│ └─▶│Agent│──┘ │
│ │ N │ │
│ └─────┘ │
│ │
└─────────────────────────────────────────────────────────────────────────┘§示例
§Chain 模式
ⓘ
use mofa_foundation::react::{ChainAgent, ReActAgent};
// 创建链式 Agent
let chain = ChainAgent::new()
.add("researcher", researcher_agent)
.add("writer", writer_agent)
.add("editor", editor_agent)
.with_transform(|prev_output, _next_name| {
format!("Based on this: {}\n\nPlease continue.", prev_output)
});
let result = chain.run("Write an article about Rust").await?;§Parallel 模式
ⓘ
use mofa_foundation::react::{ParallelAgent, AggregationStrategy};
// 创建并行 Agent
let parallel = ParallelAgent::new()
.add("analyst1", analyst1_agent)
.add("analyst2", analyst2_agent)
.add("analyst3", analyst3_agent)
.with_aggregation(AggregationStrategy::LLMSummarize(summarizer_agent));
let result = parallel.run("Analyze market trends").await?;Structs§
- Agent
Output - Agent 输出
- Chain
Agent - 链式 Agent 执行模式
- Chain
Result - 链式执行结果
- Chain
Step Result - 链式执行步骤结果
- MapReduce
Agent - MapReduce Agent
- MapReduce
Result - MapReduce 执行结果
- MapStep
Result - Map 步骤结果
- Parallel
Agent - 并行 Agent 执行模式
- Parallel
Result - 并行执行结果
- Parallel
Step Result - 并行执行步骤结果
Enums§
- Agent
Output Metadata - Agent 输出元数据
- Agent
Unit - Agent 执行单元
- Aggregation
Strategy - 聚合策略
Functions§
- chain_
agents - 创建简单的链式 Agent
- parallel_
agents - 创建简单的并行 Agent
- parallel_
agents_ with_ summarizer - 创建带 LLM 聚合的并行 Agent