dsir 0.1.1

DSPy-shaped library for programming language models in Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
//! Experiment authoring, runs, comparison, and promotion.

use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

use anyhow::{Context, Result, anyhow, bail};
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::core::DynPredictor;
use crate::data::example::Example as RawExample;
use crate::evaluate::{FieldExactMatch, average_score, evaluate_dyn_trainset};
use crate::graph::{GraphModule, GraphState, StrategyFactory, StrategyKind, StructuralOptimizer};
use crate::optimizer::{BootstrapFewShot, COPRO, GEPA, MIPROv2};
use crate::predictors::{DynModule, DynPredict};
use crate::trace;
use crate::{CallMetadata, DynSignature, Predicted};

use super::{LocalRegistry, RunRecord};

/// Optimizer choice for a lab run.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum LabOptimizer {
    LabeledFewShot {
        k: usize,
    },
    BootstrapFewShot {
        max_bootstrapped_demos: usize,
        max_labeled_demos: usize,
        metric_threshold: f32,
    },
    Copro {
        breadth: usize,
        depth: usize,
    },
    Mipro,
    Gepa {
        num_iterations: usize,
    },
    Structural,
}

/// Offline-friendly Facet module: optimizers mutate `predictor` state, but
/// `forward` echoes prompt/question → answer without calling an LM.
#[derive(facet::Facet)]
#[facet(crate = facet)]
pub struct EchoModule {
    pub predictor: DynPredict,
}

impl EchoModule {
    pub fn from_signature(signature: &DynSignature, strategy: StrategyKind) -> Result<Self> {
        Ok(Self {
            predictor: StrategyFactory::create_predict_for_kind(signature, strategy)?,
        })
    }
}

impl DynModule for EchoModule {
    async fn forward(
        &self,
        input: RawExample,
    ) -> Result<Predicted<RawExample>, crate::PredictError> {
        let mut data = input.data.clone();
        if let Some(prompt) = data.get("prompt").cloned() {
            data.insert("answer".into(), prompt);
        } else if let Some(q) = data.get("question").cloned() {
            data.insert("answer".into(), q);
        }
        Ok(Predicted::new(
            RawExample::new(data, input.input_keys, vec!["answer".into()]),
            CallMetadata::default(),
        ))
    }
}

enum ProgramSlot {
    Live(GraphModule),
    Echo(EchoModule),
}

impl ProgramSlot {
    fn predictor_mut(&mut self) -> &mut DynPredict {
        match self {
            Self::Live(m) => &mut m.predictor,
            Self::Echo(m) => &mut m.predictor,
        }
    }
}

/// In-memory + filesystem-backed experiment harness.
pub struct Lab {
    pub registry: LocalRegistry,
    programs: BTreeMap<String, ProgramSlot>,
    signatures: BTreeMap<String, DynSignature>,
    datasets: BTreeMap<String, Vec<RawExample>>,
    promoted: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompareRow {
    pub run_id: String,
    pub optimizer: String,
    pub avg_train: f32,
    pub avg_val: f32,
}

impl Lab {
    pub fn open(workdir: impl AsRef<Path>) -> Result<Self> {
        let registry = LocalRegistry::open(workdir)?;
        Ok(Self {
            registry,
            programs: BTreeMap::new(),
            signatures: BTreeMap::new(),
            datasets: BTreeMap::new(),
            promoted: None,
        })
    }

    pub fn author(
        &mut self,
        program_id: impl Into<String>,
        signature: DynSignature,
        strategy: StrategyKind,
    ) -> Result<String> {
        let program_id = program_id.into();
        let module = GraphModule::from_signature(&signature, strategy)?;
        self.signatures.insert(program_id.clone(), signature);
        self.programs
            .insert(program_id.clone(), ProgramSlot::Live(module));
        self.registry.write_json(
            &format!("programs/{program_id}.meta.json"),
            &serde_json::json!({
                "id": program_id,
                "strategy": strategy,
            }),
        )?;
        Ok(program_id)
    }

    /// Author an offline echo program (no LM required for optimize/execute).
    pub fn author_echo(
        &mut self,
        program_id: impl Into<String>,
        signature: DynSignature,
        strategy: StrategyKind,
    ) -> Result<String> {
        let program_id = program_id.into();
        let module = EchoModule::from_signature(&signature, strategy)?;
        self.signatures.insert(program_id.clone(), signature);
        self.programs
            .insert(program_id.clone(), ProgramSlot::Echo(module));
        self.registry.write_json(
            &format!("programs/{program_id}.meta.json"),
            &serde_json::json!({
                "id": program_id,
                "strategy": strategy,
                "echo": true,
            }),
        )?;
        Ok(program_id)
    }

    pub fn author_from_json(
        &mut self,
        program_id: impl Into<String>,
        json: &str,
        strategy: StrategyKind,
    ) -> Result<String> {
        let signature = DynSignature::from_json_str(json)?;
        self.author(program_id, signature, strategy)
    }

    pub fn author_from_dsl(
        &mut self,
        program_id: impl Into<String>,
        dsl: &str,
        strategy: StrategyKind,
    ) -> Result<String> {
        let signature = DynSignature::from_dsl(dsl)?;
        self.author(program_id, signature, strategy)
    }

    pub fn load_dataset_json(
        &mut self,
        dataset_id: impl Into<String>,
        rows: Vec<Value>,
        input_keys: Vec<String>,
        output_keys: Vec<String>,
    ) -> Result<String> {
        let dataset_id = dataset_id.into();
        let mut examples = Vec::new();
        for row in rows {
            let obj = row
                .as_object()
                .ok_or_else(|| anyhow!("dataset row must be a JSON object"))?;
            let data = obj.iter().map(|(k, v)| (k.clone(), v.clone())).collect();
            examples.push(RawExample::new(
                data,
                input_keys.clone(),
                output_keys.clone(),
            ));
        }
        self.registry
            .write_json(&format!("datasets/{dataset_id}.json"), &examples)?;
        self.datasets.insert(dataset_id.clone(), examples);
        Ok(dataset_id)
    }

    pub fn split_dataset(
        &self,
        dataset_id: &str,
        train_n: usize,
    ) -> Result<(Vec<RawExample>, Vec<RawExample>)> {
        let data = self
            .datasets
            .get(dataset_id)
            .ok_or_else(|| anyhow!("unknown dataset `{dataset_id}`"))?;
        let train = data.iter().take(train_n).cloned().collect::<Vec<_>>();
        let val = data.iter().skip(train_n).cloned().collect::<Vec<_>>();
        Ok((train, val))
    }

    pub async fn execute(
        &self,
        program_id: &str,
        input: RawExample,
    ) -> Result<Predicted<RawExample>> {
        match self
            .programs
            .get(program_id)
            .ok_or_else(|| anyhow!("unknown program `{program_id}`"))?
        {
            ProgramSlot::Live(m) => m.call(input).await.map_err(|e| anyhow!("{e}")),
            ProgramSlot::Echo(m) => m.call(input).await.map_err(|e| anyhow!("{e}")),
        }
    }

    pub async fn execute_traced(
        &self,
        program_id: &str,
        input: RawExample,
    ) -> Result<(Predicted<RawExample>, trace::Graph)> {
        match self
            .programs
            .get(program_id)
            .ok_or_else(|| anyhow!("unknown program `{program_id}`"))?
        {
            ProgramSlot::Live(m) => {
                let (result, graph) = trace::trace(|| async { m.call(input).await }).await;
                Ok((result.map_err(|e| anyhow!("{e}"))?, graph))
            }
            ProgramSlot::Echo(m) => {
                let (result, graph) = trace::trace(|| async { m.call(input).await }).await;
                Ok((result.map_err(|e| anyhow!("{e}"))?, graph))
            }
        }
    }

    pub async fn optimize(
        &mut self,
        program_id: &str,
        dataset_id: &str,
        train_n: usize,
        optimizer: LabOptimizer,
        metric_field: &str,
    ) -> Result<RunRecord> {
        let (train, val) = self.split_dataset(dataset_id, train_n)?;
        let metric = FieldExactMatch::new(metric_field);
        let feedback_metric = FieldExactMatch::new(metric_field).with_feedback();
        let optimizer_name = match &optimizer {
            LabOptimizer::LabeledFewShot { .. } => "labeled_fewshot",
            LabOptimizer::BootstrapFewShot { .. } => "bootstrap_fewshot",
            LabOptimizer::Copro { .. } => "copro",
            LabOptimizer::Mipro => "mipro",
            LabOptimizer::Gepa { .. } => "gepa",
            LabOptimizer::Structural => "structural",
        }
        .to_string();

        if matches!(optimizer, LabOptimizer::Structural) {
            let sig = self
                .signatures
                .get(program_id)
                .ok_or_else(|| anyhow!("missing signature for `{program_id}`"))?
                .clone();
            match self
                .programs
                .get_mut(program_id)
                .ok_or_else(|| anyhow!("unknown program `{program_id}`"))?
            {
                ProgramSlot::Live(module) => {
                    StructuralOptimizer::new()
                        .compile(&sig, module, train.clone(), &metric)
                        .await?;
                }
                ProgramSlot::Echo(_) => {
                    bail!("structural optimizer requires a live GraphModule program");
                }
            }
        }

        let (avg_train, avg_val) = match self
            .programs
            .get_mut(program_id)
            .ok_or_else(|| anyhow!("unknown program `{program_id}`"))?
        {
            ProgramSlot::Live(module) => {
                run_optimizer(&optimizer, module, train.clone(), &metric, &feedback_metric)
                    .await?;
                let avg_train =
                    average_score(&evaluate_dyn_trainset(module, &train, &metric).await?);
                let avg_val = if val.is_empty() {
                    0.0
                } else {
                    average_score(&evaluate_dyn_trainset(module, &val, &metric).await?)
                };
                (avg_train, avg_val)
            }
            ProgramSlot::Echo(module) => {
                run_optimizer(&optimizer, module, train.clone(), &metric, &feedback_metric)
                    .await?;
                let avg_train =
                    average_score(&evaluate_dyn_trainset(module, &train, &metric).await?);
                let avg_val = if val.is_empty() {
                    0.0
                } else {
                    average_score(&evaluate_dyn_trainset(module, &val, &metric).await?)
                };
                (avg_train, avg_val)
            }
        };

        let module_predictor_state = {
            let slot = self
                .programs
                .get_mut(program_id)
                .ok_or_else(|| anyhow!("unknown program `{program_id}`"))?;
            DynPredictor::dump_state(slot.predictor_mut())
        };

        let mut state = GraphState::default();
        state
            .predictors
            .insert("predictor".into(), module_predictor_state);
        let run = self.registry.record_run(RunRecord {
            id: String::new(),
            program_id: program_id.to_string(),
            dataset_id: dataset_id.to_string(),
            optimizer: optimizer_name,
            avg_train,
            avg_val,
            artifact: String::new(),
        })?;
        self.registry
            .save_artifact(&run.id, &state)
            .context("save run artifact")?;
        Ok(run)
    }

    pub fn compare(&self, run_ids: &[String]) -> Result<Vec<CompareRow>> {
        let mut rows = Vec::new();
        for id in run_ids {
            let run = self.registry.get_run(id)?;
            rows.push(CompareRow {
                run_id: run.id,
                optimizer: run.optimizer,
                avg_train: run.avg_train,
                avg_val: run.avg_val,
            });
        }
        rows.sort_by(|a, b| {
            b.avg_val
                .partial_cmp(&a.avg_val)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        Ok(rows)
    }

    pub fn promote(&mut self, run_id: &str, min_val: f32) -> Result<PathBuf> {
        let run = self.registry.get_run(run_id)?;
        if run.avg_val < min_val {
            bail!(
                "run `{run_id}` val score {:.3} below promotion threshold {min_val}",
                run.avg_val
            );
        }
        let path = self.registry.promote(&run)?;
        let state: GraphState = self.registry.load_artifact(&run.id)?;
        if let Some(slot) = self.programs.get_mut(&run.program_id) {
            if let Some(pred_state) = state.predictors.get("predictor") {
                DynPredictor::load_state(slot.predictor_mut(), pred_state.clone())?;
            }
        }
        self.promoted = Some(run.program_id);
        Ok(path)
    }

    pub fn promoted_program_id(&self) -> Option<&str> {
        self.promoted.as_deref()
    }
}

async fn run_optimizer<M>(
    optimizer: &LabOptimizer,
    module: &mut M,
    train: Vec<RawExample>,
    metric: &FieldExactMatch,
    feedback_metric: &FieldExactMatch,
) -> Result<()>
where
    M: DynModule + for<'a> crate::Facet<'a>,
{
    match optimizer {
        LabOptimizer::LabeledFewShot { k } => {
            crate::LabeledFewShot { k: *k }
                .compile_dyn(module, train)
                .await?;
        }
        LabOptimizer::BootstrapFewShot {
            max_bootstrapped_demos,
            max_labeled_demos,
            metric_threshold,
        } => {
            BootstrapFewShot {
                max_bootstrapped_demos: *max_bootstrapped_demos,
                max_labeled_demos: *max_labeled_demos,
                metric_threshold: *metric_threshold,
            }
            .compile_dyn(module, train, metric)
            .await?;
        }
        LabOptimizer::Copro { breadth, depth } => {
            COPRO::builder()
                .breadth(*breadth)
                .depth(*depth)
                .build()
                .compile_dyn(module, train, metric)
                .await?;
        }
        LabOptimizer::Mipro => {
            MIPROv2::builder()
                .build()
                .compile_dyn(module, train, metric)
                .await?;
        }
        LabOptimizer::Gepa { num_iterations } => {
            GEPA::builder()
                .num_iterations(*num_iterations)
                .build()
                .compile_dyn(module, train, feedback_metric)
                .await?;
        }
        LabOptimizer::Structural => {}
    }
    Ok(())
}