🧮 Optimizers¶
evoagentx.optimizers ¶
SEWOptimizer ¶
Bases: Optimizer
Source code in evoagentx/core/module.py
step ¶
step(**kwargs) -> Union[SequentialWorkFlowGraph, ActionGraph]
Take a step of optimization and return the optimized graph.
Source code in evoagentx/optimizers/sew_optimizer.py
evaluate ¶
evaluate(dataset: Benchmark, eval_mode: str = 'test', graph: Optional[Union[SequentialWorkFlowGraph, ActionGraph]] = None, indices: Optional[List[int]] = None, sample_k: Optional[int] = None, **kwargs) -> dict
Evaluate the workflow. If graph
is provided, use the provided graph for evaluation. Otherwise, use the graph in the optimizer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset
|
Benchmark
|
The dataset to evaluate the workflow on. |
required |
eval_mode
|
str
|
The evaluation mode. Choices: ["test", "dev", "train"]. |
'test'
|
graph
|
Union[WorkFlowGraph, ActionGraph]
|
The graph to evaluate. If not provided, use the graph in the optimizer. |
None
|
indices
|
List[int]
|
The indices of the data to evaluate the workflow on. |
None
|
sample_k
|
int
|
The number of data to evaluate the workflow on. If provided, a random sample of size |
None
|
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
The metrics of the workflow evaluation. |
Source code in evoagentx/optimizers/sew_optimizer.py
AFlowOptimizer ¶
Bases: BaseModule
AFlow Optimizer for workflow optimization.
This optimizer iteratively improves workflows through multiple rounds of optimization using large language models. It evaluates workflow performance, identifies improvement opportunities, and applies optimizations based on experience and convergence metrics.
Attributes:
Name | Type | Description |
---|---|---|
question_type |
str
|
Type of task to optimize for (e.g., qa, match, code) |
graph_path |
str
|
Path to the workflow graph directory (must contain graph.py and prompt.py) |
optimized_path |
str
|
Path to save optimized workflows (defaults to graph_path) |
initial_round |
int
|
Starting round number for optimization |
optimizer_llm |
BaseLLM
|
LLM used for generating optimizations |
executor_llm |
BaseLLM
|
LLM used for executing the workflow |
operators |
List[str]
|
List of operators available for optimization |
sample |
int
|
Number of rounds to sample from for optimization |
max_rounds |
int
|
Maximum number of optimization rounds to perform |
validation_rounds |
int
|
Number of validation runs per optimization round |
eval_rounds |
int
|
Number of evaluation runs for test mode |
check_convergence |
bool
|
Whether to check for optimization convergence |
Source code in evoagentx/core/module.py
optimize ¶
Run the optimization process on the workflow.
Performs multiple rounds of optimization, evaluating each round against the benchmark and checking for convergence. Continues until convergence is detected or the maximum number of rounds is reached.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
benchmark
|
Benchmark
|
The benchmark to evaluate the workflow against |
required |
Source code in evoagentx/optimizers/aflow_optimizer.py
test ¶
Run the test evaluation on optimized workflows.
Evaluates specified rounds (or the best round if none specified) against the benchmark multiple times and logs the results.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
benchmark
|
Benchmark
|
The benchmark to evaluate against |
required |
test_rounds
|
List[int]
|
Specific round numbers to test, or None to use the best round |
None
|
Source code in evoagentx/optimizers/aflow_optimizer.py
TextGradOptimizer ¶
Bases: BaseModule
Uses TextGrad to optimize agents' system prompts and instructions in a multi-agent workflow. For more information on TextGrad, see https://github.com/zou-group/textgrad.
Source code in evoagentx/core/module.py
optimize ¶
Optimizes self.graph using dataset
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset
|
Benchmark
|
The dataset to use for optimization. |
required |
use_answers
|
bool
|
Whether to use the answers (labels) in the training set for optimization.
If False, |
True
|
seed
|
Optional[int]
|
The random seed to use for shuffling the data. |
None
|
Source code in evoagentx/optimizers/textgrad_optimizer.py
step ¶
step(inputs: list[dict[str, str]], labels: Optional[list[Union[str, dict[str, str]]]], dataset: Benchmark, use_answers: bool = True) -> None
Performs one optimization step using a batch of data.
Source code in evoagentx/optimizers/textgrad_optimizer.py
forward ¶
Returns the final output from the workflow.
Source code in evoagentx/optimizers/textgrad_optimizer.py
evaluate ¶
evaluate(dataset: Benchmark, eval_mode: str = 'dev', graph: Optional[WorkFlowGraph] = None, indices: Optional[List[int]] = None, sample_k: Optional[int] = None, **kwargs) -> dict
Evaluate the workflow. If graph
is provided, use the provided graph for evaluation. Otherwise, use the graph in the optimizer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset
|
Benchmark
|
The dataset to evaluate the workflow on. |
required |
eval_mode
|
str
|
The evaluation mode. Choices: ["test", "dev", "train"]. |
'dev'
|
graph
|
WorkFlowGraph
|
The graph to evaluate. If not provided, use the graph in the optimizer. |
None
|
indices
|
List[int]
|
The indices of the data to evaluate the workflow on. |
None
|
sample_k
|
int
|
The number of data to evaluate the workflow on. If provided, a random sample of size |
None
|
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
The metrics of the workflow evaluation. |
Source code in evoagentx/optimizers/textgrad_optimizer.py
save ¶
save(path: str, graph: Optional[WorkFlowGraph] = None, ignore: List[str] = []) -> None
Save the workflow graph containing the optimized prompts to a file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
The path to save the workflow graph. |
required |
graph
|
WorkFlowGraph
|
The graph to save. If not provided, use the graph in the optimizer. |
None
|
ignore
|
List[str]
|
The keys to ignore when saving the workflow graph. |
[]
|
Source code in evoagentx/optimizers/textgrad_optimizer.py
log_snapshot ¶
log_snapshot(graph: WorkFlowGraph, metrics: dict) -> None
Log the snapshot of the workflow.
Source code in evoagentx/optimizers/textgrad_optimizer.py
restore_best_graph ¶
Restore the best graph from the snapshot and set it to self.graph
.
Source code in evoagentx/optimizers/textgrad_optimizer.py
MiproOptimizer ¶
MiproOptimizer(registry: ParamRegistry, program: Callable, optimizer_llm: BaseLLM, evaluator: Optional[Callable] = None, eval_rounds: Optional[int] = 1, metric_threshold: Optional[float] = None, max_bootstrapped_demos: int = 4, max_labeled_demos: int = 4, auto: Optional[Literal['light', 'medium', 'heavy']] = 'medium', max_steps: int = None, num_candidates: Optional[int] = None, num_threads: Optional[int] = None, max_errors: int = 10, seed: int = 9, init_temperature: float = 0.5, track_stats: bool = True, save_path: Optional[str] = None, minibatch: bool = True, minibatch_size: int = 35, minibatch_full_eval_steps: int = 5, program_aware_proposer: bool = True, data_aware_proposer: bool = True, view_data_batch_size: int = 10, tip_aware_proposer: bool = True, fewshot_aware_proposer: bool = True, requires_permission_to_run: bool = False, provide_traceback: Optional[bool] = None, verbose: bool = False, **kwargs)
Bases: BaseOptimizer
, MIPROv2
Base MiproOptimizer class that supports plug-and-play usage.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
registry
|
ParamRegistry
|
a ParamRegistry object that contains the parameters to optimize. |
required |
program
|
Callable
|
a program to optimize. Must be a callable object with save(path) and load(path) methods. |
required |
optimizer_llm
|
BaseLLM
|
a language model to use for optimization. |
required |
evaluator
|
Optional[Callable]
|
a function that evaluates the performance of the program.
Required to have a |
None
|
eval_rounds
|
Optional[int]
|
number of rounds to evaluate the program. Defaults to 1. |
1
|
metric_threshold
|
Optional[float]
|
threshold for the metric score. If provided, only examples with scores above this threshold will be used as demonstrations. If not provided, examples with scores above 0 will be used as demonstrations. |
None
|
max_bootstrapped_demos
|
int
|
maximum number of bootstrapped demonstrations to use. Defaults to 4. |
4
|
max_labeled_demos
|
int
|
maximum number of labeled demonstrations to use. Defaults to 4. |
4
|
auto
|
Optional[Literal['light', 'medium', 'heavy']]
|
automatic configuration mode. If set, will override num_candidates and max_steps. "light": n=6, val_size=100; "medium": n=12, val_size=300; "heavy": n=18, val_size=1000. Defaults to "medium". |
'medium'
|
max_steps
|
int
|
maximum number of optimization steps. Required if auto is None. |
None
|
num_candidates
|
Optional[int]
|
number of candidates to generate for each optimization step. Required if auto is None. |
None
|
num_threads
|
Optional[int]
|
number of threads to use for parallel evaluation. If None, will use single thread. Only used if evaluator is not provided. |
None
|
max_errors
|
int
|
maximum number of errors allowed during evaluation before stopping. Defaults to 10. |
10
|
seed
|
int
|
random seed for reproducibility. Defaults to 9. |
9
|
init_temperature
|
float
|
initial temperature for instruction generation. Defaults to 0.5. |
0.5
|
track_stats
|
bool
|
whether to track optimization statistics. Defaults to True. |
True
|
save_path
|
Optional[str]
|
path to save optimization results. If None, results will not be saved. |
None
|
minibatch
|
bool
|
whether to use minibatch evaluation during optimization. Defaults to True. |
True
|
minibatch_size
|
int
|
size of minibatch for evaluation. Defaults to 35. |
35
|
minibatch_full_eval_steps
|
int
|
number of minibatch steps between full evaluations. Defaults to 5. |
5
|
program_aware_proposer
|
bool
|
whether to use program-aware instruction proposer. Defaults to True. |
True
|
data_aware_proposer
|
bool
|
whether to use data-aware instruction proposer. Defaults to True. |
True
|
view_data_batch_size
|
int
|
batch size for viewing data during instruction proposal. Defaults to 10. |
10
|
tip_aware_proposer
|
bool
|
whether to use tip-aware instruction proposer. Defaults to True. |
True
|
fewshot_aware_proposer
|
bool
|
whether to use fewshot-aware instruction proposer. Defaults to True. |
True
|
requires_permission_to_run
|
bool
|
whether to require user permission before running optimization. Defaults to False. |
False
|
provide_traceback
|
Optional[bool]
|
whether to provide traceback for evaluation errors. If None, will use default setting. |
None
|
**kwargs
|
additional keyword arguments to pass to the evaluator. |
{}
|
Raises:
Type | Description |
---|---|
TypeError
|
If program is not callable or evaluator doesn't return float |
ValueError
|
If program doesn't have required methods (save and load) or if evaluator doesn't have required methods |
Source code in evoagentx/optimizers/mipro_optimizer.py
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 |
|
optimize ¶
Optimize the program using the Mipro algorithm.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset
|
Benchmark
|
a Benchmark object that contains the training and validation data. |
required |
metric_name
|
Optional[str]
|
the name of the metric to use for optimization. Only used when |
None
|
**kwargs
|
additional keyword arguments to pass to the evaluator. |
{}
|
Source code in evoagentx/optimizers/mipro_optimizer.py
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 |
|
WorkFlowMiproOptimizer ¶
WorkFlowMiproOptimizer(graph: WorkFlowGraph, evaluator: Evaluator, optimizer_llm: Optional[BaseLLM] = None, **kwargs)
Bases: MiproOptimizer
MiproOptimizer tailored for workflow graphs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
graph
|
WorkFlowGraph
|
the workflow graph to optimize. |
required |
evaluator
|
Evaluator
|
the evaluator to use for the optimization. |
required |
optimizer_llm
|
BaseLLM
|
the LLM to use for the optimization. If None, will use the LLM model in the evaluator. |
None
|
**kwargs
|
additional keyword arguments to pass to the MiproOptimizer. Available options: - metric_threshold (Optional[int]): threshold for the metric score. If provided, only examples with scores above this threshold will be used as demonstrations. - max_bootstrapped_demos (int): maximum number of bootstrapped demonstrations to use. Defaults to 4. - max_labeled_demos (int): maximum number of labeled demonstrations to use. Defaults to 4. - auto (Optional[Literal["light", "medium", "heavy"]]): automatic configuration mode. If set, will override num_candidates and max_steps. "light": n=6, val_size=100; "medium": n=12, val_size=300; "heavy": n=18, val_size=1000. Defaults to "medium". - max_steps (int): maximum number of optimization steps. Required if auto is None. - num_candidates (Optional[int]): number of candidates to generate for each optimization step. Required if auto is None. - num_threads (Optional[int]): number of threads to use for parallel evaluation. If None, will use single thread. - max_errors (int): maximum number of errors allowed during evaluation before stopping. Defaults to 10. - seed (int): random seed for reproducibility. Defaults to 9. - init_temperature (float): initial temperature for instruction generation. Defaults to 0.5. - track_stats (bool): whether to track optimization statistics. Defaults to True. - save_path (Optional[str]): path to save optimization results. If None, results will not be saved. - minibatch (bool): whether to use minibatch evaluation during optimization. Defaults to True. - minibatch_size (int): size of minibatch for evaluation. Defaults to 35. - minibatch_full_eval_steps (int): number of minibatch steps between full evaluations. Defaults to 5. - program_aware_proposer (bool): whether to use program-aware instruction proposer. Defaults to True. - data_aware_proposer (bool): whether to use data-aware instruction proposer. Defaults to True. - view_data_batch_size (int): batch size for viewing data during instruction proposal. Defaults to 10. - tip_aware_proposer (bool): whether to use tip-aware instruction proposer. Defaults to True. - fewshot_aware_proposer (bool): whether to use fewshot-aware instruction proposer. Defaults to True. - requires_permission_to_run (bool): whether to require user permission before running optimization. Defaults to False. - provide_traceback (Optional[bool]): whether to provide traceback for evaluation errors. If None, will use default setting. |
{}
|
Source code in evoagentx/optimizers/mipro_optimizer.py
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 |
|