use std::collections::HashSet;
use std::time::Instant;
use futures::StreamExt;
use tracing::Instrument;
use crate::evaluator::clamp;
use crate::{Case, Dataset, Evaluator, Report, Score, Sink, SinkError, Target, Trial, TrialResult};
pub struct Experiment {
name: String,
dataset: Dataset,
target: Option<Box<dyn Target>>,
evaluators: Vec<Box<dyn Evaluator>>,
repetitions: usize,
concurrency: usize,
thresholds: Vec<(String, f64)>,
sinks: Vec<Box<dyn Sink>>,
}
impl Experiment {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
dataset: Dataset::default(),
target: None,
evaluators: Vec::new(),
repetitions: 1,
concurrency: 4,
thresholds: Vec::new(),
sinks: Vec::new(),
}
}
#[must_use]
pub fn dataset(mut self, dataset: impl Into<Dataset>) -> Self {
self.dataset = dataset.into();
self
}
#[must_use]
pub fn target(mut self, target: impl Target + 'static) -> Self {
self.target = Some(Box::new(target));
self
}
#[must_use]
pub fn evaluator(mut self, evaluator: impl Evaluator + 'static) -> Self {
self.evaluators.push(Box::new(evaluator));
self
}
#[must_use]
pub fn repetitions(mut self, repetitions: usize) -> Self {
self.repetitions = repetitions;
self
}
#[must_use]
pub fn concurrency(mut self, concurrency: usize) -> Self {
self.concurrency = concurrency;
self
}
#[must_use]
pub fn threshold(mut self, evaluator: impl Into<String>, min_pass_rate: f64) -> Self {
self.thresholds.push((evaluator.into(), min_pass_rate));
self
}
#[must_use]
pub fn sink(mut self, sink: impl Sink + 'static) -> Self {
self.sinks.push(Box::new(sink));
self
}
pub async fn run(&self) -> Result<Report, ExperimentError> {
let target = self.check()?;
let span = tracing::info_span!("eval.experiment", eval.experiment = %self.name);
let mut runs = Vec::with_capacity(self.dataset.cases.len() * self.repetitions);
for (index, case) in self.dataset.cases.iter().enumerate() {
for repetition in 1..=self.repetitions {
runs.push(self.trial(target, index, case, repetition, &span));
}
}
let mut trials: Vec<(usize, TrialResult)> = futures::stream::iter(runs)
.buffer_unordered(self.concurrency.max(1))
.collect()
.instrument(span.clone())
.await;
trials.sort_by_key(|(index, result)| (*index, result.trial.repetition));
let evaluators: Vec<String> = self
.evaluators
.iter()
.map(|e| e.name().to_owned())
.collect();
let report = Report::new(
self.name.clone(),
self.repetitions,
trials.into_iter().map(|(_, result)| result).collect(),
&evaluators,
&self.thresholds,
);
for sink in &self.sinks {
if let Err(source) = sink.record(&report).await {
return Err(ExperimentError::Sink {
source,
report: Box::new(report),
});
}
}
Ok(report)
}
fn check(&self) -> Result<&dyn Target, ExperimentError> {
let target = self.target.as_deref().ok_or(ExperimentError::NoTarget)?;
if self.dataset.cases.is_empty() {
return Err(ExperimentError::EmptyDataset);
}
if self.evaluators.is_empty() {
return Err(ExperimentError::NoEvaluators);
}
if self.repetitions == 0 {
return Err(ExperimentError::NoRepetitions);
}
let mut seen = HashSet::new();
if let Some(case) = self
.dataset
.cases
.iter()
.find(|case| !seen.insert(&case.id))
{
return Err(ExperimentError::DuplicateCase(case.id.clone()));
}
let mut seen = HashSet::new();
if let Some(evaluator) = self.evaluators.iter().find(|e| !seen.insert(e.name())) {
return Err(ExperimentError::DuplicateEvaluator(
evaluator.name().to_owned(),
));
}
for (evaluator, min) in &self.thresholds {
if !seen.contains(evaluator.as_str()) {
return Err(ExperimentError::UnknownThreshold(evaluator.clone()));
}
if !(0.0..=1.0).contains(min) {
return Err(ExperimentError::InvalidThreshold {
evaluator: evaluator.clone(),
min_pass_rate: *min,
});
}
}
Ok(target)
}
async fn trial(
&self,
target: &dyn Target,
index: usize,
case: &Case,
repetition: usize,
parent: &tracing::Span,
) -> (usize, TrialResult) {
let span = tracing::info_span!(
parent: parent,
"eval.trial",
eval.experiment = %self.name,
eval.case = %case.id,
eval.repetition = repetition as i64,
);
async move {
let started = Instant::now();
let ran = target.run(case).await;
let mut trial = Trial::new(case.id.clone(), repetition, "");
trial.latency = started.elapsed();
match ran {
Ok(output) => {
trial.output = output.output;
trial.tool_calls = output.tool_calls;
trial.usage = output.usage;
trial.session_id = output.session_id;
}
Err(error) => trial.error = Some(error.to_string()),
}
let mut scores = Vec::with_capacity(self.evaluators.len());
for evaluator in &self.evaluators {
let name = evaluator.name();
let mut score = evaluator
.evaluate(case, &trial)
.await
.unwrap_or_else(|error| {
Score::fail(name, error.to_string()).with_label("error")
});
score.evaluator = name.to_owned();
score.value = clamp(score.value);
let label =
score
.label
.as_deref()
.unwrap_or(if score.passed { "pass" } else { "fail" });
tracing::info!(
gen_ai.evaluation.name = %score.evaluator,
gen_ai.evaluation.score.value = score.value,
gen_ai.evaluation.score.label = %label,
gen_ai.evaluation.explanation = score.explanation.as_deref(),
"gen_ai.evaluation.result"
);
scores.push(score);
}
(index, TrialResult { trial, scores })
}
.instrument(span)
.await
}
}
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum ExperimentError {
#[error("the experiment has no target")]
NoTarget,
#[error("the dataset has no cases")]
EmptyDataset,
#[error("the experiment has no evaluators")]
NoEvaluators,
#[error("repetitions must be at least 1")]
NoRepetitions,
#[error("two cases have the id {0:?}")]
DuplicateCase(String),
#[error("two evaluators are named {0:?}; rename one with named()")]
DuplicateEvaluator(String),
#[error("a threshold names {0:?}, which is not an evaluator of the experiment")]
UnknownThreshold(String),
#[error("the threshold of {evaluator:?} is {min_pass_rate}; it must be from 0 to 1")]
InvalidThreshold {
evaluator: String,
min_pass_rate: f64,
},
#[error("a sink failed: {source}")]
Sink {
source: SinkError,
report: Box<Report>,
},
}