🧮 优化器接口¶
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
Optimizes the prompt templates and system prompts in a workflow using TextGrad. For more information, 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[str | dict[str, str]]], dataset: Benchmark, use_answers: bool = True)
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[SequentialWorkFlowGraph] = 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
|
SequentialWorkFlowGraph
|
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[SequentialWorkFlowGraph] = None, ignore: List[str] = [])
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
|
SequantialWorkFlowGraph
|
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: SequentialWorkFlowGraph, metrics: dict)
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
.