latent.optimize.base¶
Optimizer protocol and shared data models for prompt optimization.
Classes¶
ExperimentResult¶
Result of a single optimization experiment.
OptimizationReport¶
Full result of an optimization run with summary metadata.
OptimizedPrompt¶
Serializable optimized prompt artifact.
Optimizer¶
Protocol for prompt optimizers.
Functions¶
apply_optimized_prompt¶
Apply an optimized prompt to an agent in-place.
For Judge: sets prompt_template. For ReActAgent: sets system_prompt.
OptimizedPrompt.few_shot_demos is not applied — a bootstrapped
demo set does not reach Judge.few_shot, so a DSPy run that produced
demos still needs them wired by hand.
An artifact with no system_prompt (DSPy writes only prompt_template)
falls back to the template as the system prompt, so applying it to a plain
ReActAgent is not a silent no-op. That fallback is skipped for an agent that
owns a prompt_template: there the template is the row prompt, rendered
per row with .format(), so installing it as the system prompt would send
the model its own unrendered {placeholders} as instructions.
The prompt_template attribute must therefore exist by the time this
runs. Judge declares it on the class for exactly that reason — see
Judge.prompt_template.
Methods¶
OptimizedPrompt.from_best¶
Extract the best prompt from an OptimizationResult.
OptimizedPrompt.from_file¶
Load from JSON file.
OptimizedPrompt.render_prompt¶
Render prompt_template with variable substitution using string.Template.
OptimizedPrompt.save¶
Save to JSON file.
Optimizer.optimize¶
optimize(train_data: list[dict[str, Any]], metric: Any, max_iterations: int = 10) -> OptimizationResult
Run optimization and return the best prompt.