Skip to main content

Module patterns

Module patterns 

Source
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§

AgentOutput
Agent 输出
ChainAgent
链式 Agent 执行模式
ChainResult
链式执行结果
ChainStepResult
链式执行步骤结果
MapReduceAgent
MapReduce Agent
MapReduceResult
MapReduce 执行结果
MapStepResult
Map 步骤结果
ParallelAgent
并行 Agent 执行模式
ParallelResult
并行执行结果
ParallelStepResult
并行执行步骤结果

Enums§

AgentOutputMetadata
Agent 输出元数据
AgentUnit
Agent 执行单元
AggregationStrategy
聚合策略

Functions§

chain_agents
创建简单的链式 Agent
parallel_agents
创建简单的并行 Agent
parallel_agents_with_summarizer
创建带 LLM 聚合的并行 Agent