pub mod bootstrap;
pub mod copro;
pub mod dyn_compile;
pub mod gepa;
pub mod mipro;
pub mod pareto;
pub mod propose;
pub use bootstrap::*;
pub use copro::*;
pub use gepa::*;
pub use mipro::*;
pub use pareto::*;
pub use propose::propose_instructions_with_hint;
use anyhow::Result;
use anyhow::anyhow;
use std::ops::ControlFlow;
use crate::core::{DynPredictor, visit_named_predictors_mut};
use crate::evaluate::{MetricOutcome, TypedMetric, evaluate_trainset};
use crate::predictors::Example;
use crate::{Facet, Module, Signature};
#[allow(async_fn_in_trait)]
pub trait Optimizer {
type Report;
async fn compile<S, M, MT>(
&self,
module: &mut M,
trainset: Vec<Example<S>>,
metric: &MT,
) -> Result<Self::Report>
where
S: Signature,
S::Input: Clone,
M: Module<Input = S::Input> + for<'a> Facet<'a>,
MT: TypedMetric<S, M>;
}
pub(crate) async fn evaluate_module_with_metric<S, M, MT>(
module: &M,
trainset: &[Example<S>],
metric: &MT,
) -> Result<Vec<MetricOutcome>>
where
S: Signature,
S::Input: Clone,
M: Module<Input = S::Input>,
MT: TypedMetric<S, M>,
{
evaluate_trainset(module, trainset, metric).await
}
pub(crate) fn predictor_names<M>(module: &mut M) -> Result<Vec<String>>
where
M: for<'a> Facet<'a>,
{
let mut names = Vec::new();
visit_named_predictors_mut(module, |name, _predictor| {
names.push(name.to_string());
ControlFlow::Continue(())
})?;
Ok(names)
}
pub(crate) fn with_named_predictor<M, R, F>(module: &mut M, predictor_name: &str, f: F) -> Result<R>
where
M: for<'a> Facet<'a>,
F: FnOnce(&mut dyn DynPredictor) -> Result<R>,
{
let mut apply = Some(f);
let mut result = None;
visit_named_predictors_mut(module, |name, predictor| {
if name != predictor_name {
return ControlFlow::Continue(());
}
let f = apply.take().expect("selector closure should only run once");
result = Some(f(predictor));
ControlFlow::Break(())
})?;
result.unwrap_or_else(|| Err(anyhow!("predictor `{predictor_name}` not found")))
}