# foundationagents/aflow architecture

> Architecture diagram of the GitHub repository foundationagents/aflow, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/foundationagents/aflow
- Repository: https://github.com/foundationagents/aflow

## Overview

##### Project Type and Purpose
- AFlow is a Python‐based research framework for automatic Agentic Workflow Generation using Monte Carlo Tree Search over LLM‐defined workflows. There’s no frontend UI; it’s a CLI‐driven tool/library combining data ingestion, workflow synthesis, evaluation, and optimization.

##### File Structure
- Top‐level entry points: `run.py`, `run_baseline.py` (CLI drivers).
- `config/` holds YAML templates for LLM and optimization parameters.
- `data/` contains scripts to download raw datasets.
- `benchmarks/` implements dataset‐specific evaluation logic.
- `scripts/` provides supporting modules (LLM wrappers, operators, prompt templates, optimizer utilities, evaluator logic, workflow definitions).
- `workspace/` houses per‐dataset working folders, holding templates, logs, intermediate graphs, results.

A. CLI & Orchestration
- run.py / run_baseline.py: parse args, load `config/config2.yaml`, bootstrap optimizer & evaluator.
B. Configuration Loader
- YAML Parser: reads LLM credentials, optimizer settings, dataset selection.
C. Data Layer
- `data/download_data.py`: retrieves and stores raw datasets.
- `benchmarks/`: wraps datasets into a common interface (`BaseBenchmark`).
D. Workflow Engine
- `scripts/workflow.py`: defines the Workflow abstraction (graph of Nodes).
- `scripts/prompts/` & `scripts/operators.py`: define Operator templates and prompt patterns.
- `metagpt_core/action_nodes/action_node.py` (imported): low‐level LLM call interface.
E. Optimizer (MCTS)
- `scripts/optimizer.py`: Monte Carlo Tree Search variant controlling workflow generation.
- `scripts/optimizer_utils/`: utility modules for convergence checks, data handling, evaluation wrappers, graph operations.
F. Evaluator
- `scripts/evaluator.py`: runs candidate workflows on benchmark tasks, returns performance metrics.
G. Workspace & Persistence
- `workspace/<Dataset>/workflows`: stores JSON templates, processed experience, logs, graphs per round.
- Final optimized workflows written out to `optimized_path` as JSON/Python code.
H. External Services
- LLM Provider API (e.g., OpenAI GPT): invoked by Node/Operator.
- Optionally cloud storage for datasets or results.

1. CLI → Config Loader: pass runtime args, load YAML.
2. CLI → Data Layer: invoke download if first run.
3. CLI → Optimizer: start MCTS loop.
4. Optimizer ↔ Workflow Engine: generates candidate workflows (graphs) using Operators + Nodes.
5. Workflow Engine → LLM API: each Node invokes the LLM to perform generation/format/review.
6. Optimizer → Evaluator: pass workflows to be scored.
7. Evaluator → Benchmarks: load test cases, run workflows, compute scores.
8. Evaluator → Optimizer: return metrics for MCTS update.
9. Optimizer → Workspace: log intermediate workflows, prompt traces, performance.
10. CLI → Workspace: at completion, dump optimal workflows and final reports.

##### Architectural Patterns and Principles
- Modular layering: CLI layer, config layer, data benchmarks layer, workflow synthesis layer, optimization layer, evaluation layer, persistence layer.
- Graph‐based representation: workflows are directed graphs of Nodes/Operators.
- Monte Carlo Tree Search: optimizer uses MCTS pattern (selection, expansion, simulation, backpropagation).
- Template‐based extensibility: prompt and operator definitions are JSON/Python templates.
- Separation of concerns: benchmarks isolate dataset logic; optimizer and evaluator are decoupled via interfaces.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Subgraphs for layers
    subgraph "CLI & Orchestration"
        CLI1["run.py"] 
        CLI2["run_baseline.py"]
    end

    subgraph "Configuration Loader"
        CONFIG["YAML Parser\n(config/config2.example.yaml)"]
    end

    subgraph "Data Layer"
        DL1["download_data.py"]
        DL2["Benchmark Interface\n(benchmark.py)"]
        DL3["Benchmark Implementations\n(amc.py, gpqa.py, gsm8k.py, ...)"]
    end

    subgraph "Workflow Engine"
        WF["workflow.py"]
        OPS1["operators.py"]
        OPS2["operator_an.py"]
        PROMPT1["prompt.py"]
        PROMPT2["optimize_prompt.py"]
    end

    subgraph "Optimizer (MCTS)"
        OPT["optimizer.py"]
        OPTU["optimizer_utils\n(convergence_utils.py, data_utils.py,\n evaluation_utils.py, experience_utils.py,\n graph_utils.py)"]
    end

    subgraph "Evaluator"
        EVAL["evaluator.py"]
        EVALB["Benchmark Logic\n(amc.py, gpqa.py, gsm8k.py, ...)"]
    end

    subgraph "Workspace & Persistence"
        WS1["workflows\n(workspace/<Dataset>/workflows)"]
        WS2["round artifacts\n(.../workflows/round_1/{graph.py, log.json, prompt.py})"]
        WS3["templates\n(.../workflows/template/{operator.json, operator.py, op_prompt.py})"]
    end

    subgraph "External Services"
        LLM["LLM Provider API"] 
        CLOUD["Cloud Storage (optional)"]
    end

    %% Connections
    CLI1 -->|"load config"| CONFIG
    CLI2 -->|"load config"| CONFIG

    CLI1 -->|"download data"| DL1
    CLI2 -->|"download data"| DL1

    CLI1 -->|"start MCTS"| OPT
    CLI2 -->|"start MCTS"| OPT

    CLI1 -->|"dump final workflows"| WS1
    CLI2 -->|"dump final workflows"| WS1

    OPT -->|"generate candidates"| WF
    WF -->|"supports operators/templates"| OPS1
    WF -->|"supports operators/templates"| OPS2
    WF -->|"uses prompts"| PROMPT1
    WF -->|"uses prompts"| PROMPT2
    WF -->|"invoke LLM"| LLM

    OPT -->|"evaluate"| EVAL
    EVAL -->|"load benchmarks"| EVALB
    EVAL -->|"return metrics"| OPT

    OPT -->|"log intermediates"| WS2
    OPT -->|"store templates"| WS3

    %% Data Layer to Evaluator
    EVAL -->|"uses benchmark interface"| DL2
    DL2 -->|"implements"| DL3

    %% Data Layer download flow
    DL1 -->|"stores raw data"| WS1

    %% Optional cloud
    DL1 -->|"optional upload/download"| CLOUD

    %% Click Events
    click CLI1 "https://github.com/foundationagents/aflow/blob/main/run.py"
    click CLI2 "https://github.com/foundationagents/aflow/blob/main/run_baseline.py"
    click CONFIG "https://github.com/foundationagents/aflow/blob/main/config/config2.example.yaml"
    click DL1 "https://github.com/foundationagents/aflow/blob/main/data/download_data.py"
    click DL2 "https://github.com/foundationagents/aflow/blob/main/benchmarks/benchmark.py"
    click DL3 "https://github.com/foundationagents/aflow/tree/main/benchmarks/"
    click WF "https://github.com/foundationagents/aflow/blob/main/scripts/workflow.py"
    click OPS1 "https://github.com/foundationagents/aflow/blob/main/scripts/operators.py"
    click OPS2 "https://github.com/foundationagents/aflow/blob/main/scripts/operator_an.py"
    click PROMPT1 "https://github.com/foundationagents/aflow/blob/main/scripts/prompts/prompt.py"
    click PROMPT2 "https://github.com/foundationagents/aflow/blob/main/scripts/prompts/optimize_prompt.py"
    click OPT "https://github.com/foundationagents/aflow/blob/main/scripts/optimizer.py"
    click OPTU "https://github.com/foundationagents/aflow/tree/main/scripts/optimizer_utils/"
    click EVAL "https://github.com/foundationagents/aflow/blob/main/scripts/evaluator.py"
    click EVALB "https://github.com/foundationagents/aflow/tree/main/benchmarks/"
    click WS1 "https://github.com/foundationagents/aflow/tree/main/workspace/<Dataset>/workflows"
    click WS2 "https://github.com/foundationagents/aflow/tree/main/workspace/<Dataset>/workflows/round_1/"
    click WS3 "https://github.com/foundationagents/aflow/tree/main/workspace/<Dataset>/workflows/template/"

    %% Styles
    classDef orchestration fill:#D0E8FF,stroke:#0366d6,stroke-width:1px;
    classDef config fill:#D0F0C0,stroke:#228B22,stroke-width:1px;
    classDef data fill:#FFF5BA,stroke:#DAA520,stroke-width:1px;
    classDef core fill:#E0FFE0,stroke:#32CD32,stroke-width:1px;
    classDef persistence fill:#FFF0F5,stroke:#C71585,stroke-width:1px;
    classDef external fill:#D3D3D3,stroke:#696969,stroke-width:1px,stroke-dasharray: 5 5;

    class CLI1,CLI2 orchestration
    class CONFIG config
    class DL1,DL2,DL3 data
    class WF,OPS1,OPS2,PROMPT1,PROMPT2,OPT,EVAL core
    class OPTU core
    class WS1,WS2,WS3 persistence
    class LLM,CLOUD external
```
