How foundationagents/aflow works
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.