pub mod bootstrap;
pub mod labeled;
pub mod mipro;
use std::{fmt, sync::Arc};
use async_trait::async_trait;
pub use bootstrap::BootstrapFewShot;
pub use labeled::LabeledFewShot;
pub use mipro::{AutoMode, MIPROv2, MiproCompileRequest, MiproConfig, MiproDeps};
use typesayer_types::error::Result;
use crate::{context::Context, example::Example, module::Module, prediction::Prediction};
pub type MetricFn = Arc<dyn Fn(&Example, &Prediction) -> f64 + Send + Sync>;
pub type ProgressFn = Arc<dyn Fn(&Progress) + Send + Sync>;
#[derive(Clone)]
pub struct Progress {
pub phase: String,
pub step: usize,
pub total: usize,
pub message: String,
pub best_score: Option<f64>,
}
impl fmt::Display for Progress {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.total > 0 {
write!(
f,
"[{} {}/{}] {}",
self.phase, self.step, self.total, self.message
)?;
} else {
write!(f, "[{}] {}", self.phase, self.message)?;
}
if let Some(score) = self.best_score {
write!(f, " (best: {:.1}%)", score * 100.0)?;
}
Ok(())
}
}
#[cfg(test)]
#[must_use]
pub fn no_progress() -> ProgressFn {
Arc::new(|_| {})
}
pub struct CompileRequest<'a> {
pub module: &'a mut dyn Module,
pub trainset: &'a [Example],
pub ctx: &'a Context,
pub teacher_ctx: Option<&'a Context>,
pub valset: Option<&'a [Example]>,
pub progress: &'a ProgressFn,
}
#[async_trait]
pub trait Optimizer: Send + Sync {
async fn compile(&self, args: CompileRequest<'_>) -> Result<()>;
}