dspy_rs/evaluate/
evaluator.rs1use crate::core::Module;
2use crate::data::{example::Example, prediction::Prediction};
3use futures::future::join_all;
4
5#[allow(async_fn_in_trait)]
6pub trait Evaluator: Module {
7 const MAX_CONCURRENCY: usize = 32;
8 const DISPLAY_PROGRESS: bool = true;
9
10 async fn metric(&self, example: &Example, prediction: &Prediction) -> f32;
11
12 async fn evaluate(&self, examples: Vec<Example>) -> f32 {
13 let predictions = self
14 .batch(
15 examples.clone(),
16 Self::MAX_CONCURRENCY,
17 Self::DISPLAY_PROGRESS,
18 )
19 .await
20 .unwrap();
21
22 let futures: Vec<_> = examples
23 .iter()
24 .zip(predictions.iter())
25 .map(|(example, prediction)| {
26 let prediction = prediction.clone();
27 async move { self.metric(example, &prediction).await }
28 })
29 .collect();
30
31 let metrics = join_all(futures).await;
32 metrics.iter().sum::<f32>() / examples.len() as f32
33 }
34}