pub struct PromptOptimizer { /* private fields */ }Expand description
Iteratively improves an agent’s system instructions using an optimizer LLM and an evaluation set.
The optimizer runs a loop of evaluate → propose improvements → apply best →
repeat, logging progress via tracing at each iteration.
Implementations§
Source§impl PromptOptimizer
impl PromptOptimizer
Sourcepub fn new(
optimizer_llm: Arc<dyn Llm>,
evaluator: Evaluator,
config: OptimizerConfig,
) -> Self
pub fn new( optimizer_llm: Arc<dyn Llm>, evaluator: Evaluator, config: OptimizerConfig, ) -> Self
Create a new prompt optimizer.
§Arguments
optimizer_llm- The LLM used to propose instruction improvements (separate from the agent’s own LLM).evaluator- The evaluator used to score the agent against the eval set.config- Optimization configuration (max iterations, target threshold, output path).
Sourcepub async fn optimize(
&self,
agent: Arc<dyn Agent>,
eval_set: &EvalSet,
) -> Result<OptimizationResult>
pub async fn optimize( &self, agent: Arc<dyn Agent>, eval_set: &EvalSet, ) -> Result<OptimizationResult>
Evaluates agent against the eval set.
An Arc<dyn Agent> carries neither its instruction nor a way to rebuild it, so this
method can only score the agent as given: when the baseline meets the threshold it
writes the agent’s description to the output file and returns; otherwise it fails,
since it cannot rewrite the instruction. optimize_candidates
is the optimization loop.
§Errors
Returns EvalError::ConfigError when the eval set has no cases or the baseline is
below the target threshold, or the evaluator’s error.
Sourcepub async fn optimize_candidates(
&self,
candidates: &dyn AgentCandidates,
eval_set: &EvalSet,
) -> Result<OptimizationResult>
pub async fn optimize_candidates( &self, candidates: &dyn AgentCandidates, eval_set: &EvalSet, ) -> Result<OptimizationResult>
Runs the optimization loop over candidates.
Evaluates the agent built from the starting instruction, asks the optimizer LLM for an improved instruction, evaluates the agent built from it, keeps the best, and repeats until the target threshold is met or max iterations are exhausted. On completion, writes the best-performing instruction to the configured output file.
§Errors
Returns EvalError::ConfigError when the eval set has no cases, the candidate’s
build error, the evaluator’s error, or EvalError::ExecutionError when the
optimizer LLM fails or answers nothing.