Overview
Concrete tool for iterative workflow optimization via LLM-guided graph evolution with experience accumulation and convergence detection.
Description
The AFlowOptimizer class implements an optimization loop that iteratively improves workflow graphs using LLM-generated modifications. Each round samples top-scoring previous rounds, constructs an optimization prompt with experience and operator descriptions, generates a modified graph via the optimizer LLM, and evaluates the new graph against a benchmark. The optimizer maintains experience data, checks for convergence across rounds, and saves optimized graph files in per-round directories.
Usage
- Workflow optimization: Create with a graph directory (containing `graph.py` and `prompt.py`), optimizer and executor LLMs, then call `optimize(benchmark)`.
- Test evaluation: After optimization, call `test(benchmark)` to evaluate the best (or specified) rounds on a test set.
- Multi-benchmark: Supports HotPotQA, GSM8K, MATH, HumanEval, and MBPP benchmarks.
Code Reference
Source Location
- Repository: EvoAgentX
- File: evoagentx/optimizers/aflow_optimizer.py (L30-303)
Signature
class AFlowOptimizer(BaseModule):
question_type: str # "qa", "match", "code"
graph_path: str # directory with graph.py and prompt.py
optimized_path: str = None
initial_round: int = 0
optimizer_llm: BaseLLM = None
executor_llm: BaseLLM = None
operators: List[str] = list(OPERATOR_MAP.keys())
sample: int = 4
max_rounds: int = 20
validation_rounds: int = 5
eval_rounds: int = 3
check_convergence: bool = True
def optimize(self, benchmark: Benchmark)
def test(self, benchmark: Benchmark, test_rounds: Optional[List[int]] = None)
Import
from evoagentx.optimizers.aflow_optimizer import AFlowOptimizer
I/O Contract
Inputs
| Name |
Type |
Required |
Description
|
| question_type |
str |
Yes |
Task type: "qa", "match", or "code"
|
| graph_path |
str |
Yes |
Directory containing graph.py and prompt.py
|
| optimizer_llm |
BaseLLM |
Yes |
LLM for generating optimizations
|
| executor_llm |
BaseLLM |
No |
LLM for executing workflow (defaults to optimizer_llm)
|
| max_rounds |
int |
No |
Max optimization rounds (default: 20)
|
| validation_rounds |
int |
No |
Validation runs per round (default: 5)
|
| benchmark |
Benchmark |
Yes (for optimize/test) |
Benchmark to evaluate against
|
Outputs
| Name |
Type |
Description
|
| optimized graph files |
Files |
Saved per round in optimized_path/round_N/
|
| test returns |
float |
Average test score (np.mean of scores)
|
Usage Examples
Optimize Workflow on HotPotQA
from evoagentx.optimizers.aflow_optimizer import AFlowOptimizer
from evoagentx.models.litellm_model import LiteLLM
from evoagentx.models.model_configs import LiteLLMConfig
from evoagentx.benchmark.hotpotqa import HotPotQA
# 1. Configure LLMs
optimizer_llm = LiteLLM(config=LiteLLMConfig(
model="anthropic/claude-3.5-sonnet", anthropic_key="sk-ant-..."))
executor_llm = LiteLLM(config=LiteLLMConfig(
model="openai/gpt-4o-mini", openai_key="sk-..."))
# 2. Create optimizer
optimizer = AFlowOptimizer(
question_type="qa",
graph_path="./examples/aflow/hotpotqa/",
optimized_path="./optimized/hotpotqa/",
optimizer_llm=optimizer_llm,
executor_llm=executor_llm,
max_rounds=20,
validation_rounds=5,
)
# 3. Load benchmark and optimize
benchmark = HotPotQA(mode="dev")
optimizer.optimize(benchmark=benchmark)
# 4. Test best round
optimizer.test(benchmark=HotPotQA(mode="test"))
Related Pages
Implements Principle
Requires Environment
Uses Heuristic
Page Connections
Double-click a node to navigate. Hold to expand connections.