dspy_rs/predictors/
predict.rs

1use indexmap::IndexMap;
2use std::sync::Arc;
3
4use crate::core::{MetaSignature, Optimizable};
5use crate::{ChatAdapter, Example, GLOBAL_SETTINGS, LM, Prediction, adapter::Adapter};
6
7pub struct Predict {
8    pub signature: Box<dyn MetaSignature>,
9}
10
11impl Predict {
12    pub fn new(signature: impl MetaSignature + 'static) -> Self {
13        Self {
14            signature: Box::new(signature),
15        }
16    }
17}
18
19impl super::Predictor for Predict {
20    async fn forward(&self, inputs: Example) -> anyhow::Result<Prediction> {
21        let (adapter, lm) = {
22            let guard = GLOBAL_SETTINGS.read().unwrap();
23            let settings = guard.as_ref().unwrap();
24            (settings.adapter.clone(), Arc::clone(&settings.lm))
25        }; // guard is dropped here
26        adapter.call(lm, self.signature.as_ref(), inputs).await
27    }
28
29    async fn forward_with_config(
30        &self,
31        inputs: Example,
32        lm: Arc<LM>,
33    ) -> anyhow::Result<Prediction> {
34        ChatAdapter.call(lm, self.signature.as_ref(), inputs).await
35    }
36}
37
38impl Optimizable for Predict {
39    fn get_signature(&self) -> &dyn MetaSignature {
40        self.signature.as_ref()
41    }
42
43    fn parameters(&mut self) -> IndexMap<String, &mut dyn Optimizable> {
44        IndexMap::new()
45    }
46
47    fn update_signature_instruction(&mut self, instruction: String) -> anyhow::Result<()> {
48        let _ = self.signature.update_instruction(instruction);
49        Ok(())
50    }
51}