dspy_rs/core/
module.rs

1use anyhow::Result;
2use futures::future::join_all;
3use indexmap::IndexMap;
4use kdam::tqdm;
5
6use crate::{Example, Prediction, core::MetaSignature};
7
8#[allow(async_fn_in_trait)]
9pub trait Module: Send + Sync {
10    async fn forward(&self, inputs: Example) -> Result<Prediction>;
11
12    async fn batch(
13        &self,
14        inputs: Vec<Example>,
15        max_concurrency: usize,
16        display_progress: bool,
17    ) -> Result<Vec<Prediction>> {
18        let batches = inputs.chunks(max_concurrency).collect::<Vec<_>>();
19        let mut predictions = Vec::new();
20
21        for batch in tqdm!(
22            batches.iter(),
23            desc = "Processing Batch",
24            disable = !display_progress
25        ) {
26            let futures: Vec<_> = batch
27                .iter()
28                .map(|example| self.forward(example.clone()))
29                .collect();
30
31            predictions.extend(
32                join_all(futures)
33                    .await
34                    .into_iter()
35                    .map(|prediction| prediction.unwrap())
36                    .collect::<Vec<_>>(),
37            );
38        }
39
40        Ok(predictions)
41    }
42}
43
44#[allow(unused_variables)]
45pub trait Optimizable {
46    fn get_signature(&self) -> &dyn MetaSignature {
47        todo!()
48    }
49
50    fn parameters(&mut self) -> IndexMap<String, &mut dyn Optimizable>;
51
52    fn update_signature_instruction(&mut self, instruction: String) -> anyhow::Result<()> {
53        todo!()
54    }
55}