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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
use crate as dspy_rs;
use crate::{
Evaluator, Example, LM, Module, Optimizable, Optimizer, Predict, Prediction, Predictor,
example, get_lm,
};
use anyhow::Result;
use bon::Builder;
use dsrs_macros::Signature;
use futures::future::join_all;
use std::sync::Arc;
use std::{collections::HashMap, future::Future, pin::Pin, sync::LazyLock};
#[Signature]
struct BasicGenerateInstruction {
/// You are an instruction optimizer for large language models. I will give you a ``signature`` of fields (inputs and outputs) in English. Your task is to propose an instruction that will lead a good language model to perform the task well. Don't be afraid to be creative.
#[input(desc = "The initial instructions before optimization")]
pub basic_instruction: String,
#[output(desc = "The improved instructions for the language model")]
pub proposed_instruction: String,
}
#[Signature]
struct GenerateInstructionGivenAttempts {
/// You are an instruction optimizer for large language models. I will give some task instructions I've tried, along with their corresponding validation scores. The instructions are arranged in increasing order based on their scores, where higher scores indicate better quality.
///
/// Your task is to propose a new instruction that will lead a good language model to perform the task even better. Don't be afraid to be creative.
#[input(
desc = "The instructions I've tried, along with their corresponding validation scores"
)]
pub attempted_instructions: Vec<String>,
#[output(desc = "The improved instructions for the language model")]
pub proposed_instruction: String,
}
#[derive(Clone)]
struct Candidate {
pub score: f32,
pub instruction: String,
pub prefix: String,
}
#[derive(Clone)]
struct ProgramStats {
pub results_best: HashMap<String, Vec<f32>>,
pub results_latest: HashMap<String, Vec<f32>>,
pub total_calls: usize,
}
#[derive(Builder)]
pub struct COPRO {
#[builder(default = 10)]
pub breadth: usize,
#[builder(default = 3)]
pub depth: usize,
#[builder(default = 1.4)]
pub init_temperature: f32,
#[builder(default = false)]
pub track_stats: bool,
pub prompt_model: Option<LM>,
}
static BASIC_GENERATOR: LazyLock<Predict> =
LazyLock::new(|| Predict::new(BasicGenerateInstruction::new()));
static REFINEMENT_GENERATOR: LazyLock<Predict> =
LazyLock::new(|| Predict::new(GenerateInstructionGivenAttempts::new()));
impl COPRO {
fn get_output_field_prefix(&self, predictor: &dyn Optimizable) -> String {
// Get the last output field's prefix/desc
let output_fields = predictor.get_signature().output_fields();
if let Some(obj) = output_fields.as_object()
&& let Some((_, field)) = obj.iter().next_back()
&& let Some(desc) = field.get("desc")
{
return desc.as_str().unwrap_or("").to_string();
}
"".to_string()
}
}
impl Optimizer for COPRO {
async fn compile<M: Module + Optimizable + Evaluator>(
&self,
module: &mut M,
trainset: Vec<Example>,
) -> Result<()> {
if self.breadth <= 1 {
return Err(anyhow::anyhow!("Breadth must be greater than 1"));
}
// Collect predictor information first
let predictor_info: Vec<(String, String, String)> = {
let named_predictors = module.parameters();
named_predictors
.iter()
.map(|(name, predictor)| {
let basic_instruction = predictor.get_signature().instruction();
let basic_prefix = self.get_output_field_prefix(*predictor);
(name.clone(), basic_instruction, basic_prefix)
})
.collect()
};
let mut all_candidates: HashMap<String, Vec<(String, String)>> = HashMap::new();
let mut latest_candidates: HashMap<String, Vec<(String, String)>> = HashMap::new();
let mut evaluated_candidates: HashMap<String, HashMap<(String, String), Candidate>> =
HashMap::new();
let mut stats = ProgramStats {
results_best: HashMap::new(),
results_latest: HashMap::new(),
total_calls: 0,
};
// Seed with initial instructions - generate breadth-1 new + 1 original
for (predictor_name, basic_instruction, basic_prefix) in &predictor_info {
let mut candidates = Vec::new();
// Generate new candidates
if self.breadth > 1 {
let mut futures: Vec<Pin<Box<dyn Future<Output = Result<Prediction>> + Send>>> =
Vec::new();
for _ in 0..self.breadth - 1 {
let inst = basic_instruction.clone();
if let Some(mut prompt_model) = self.prompt_model.clone() {
prompt_model.temperature = self.init_temperature;
futures.push(Box::pin(async move {
BASIC_GENERATOR
.forward_with_config(
example! {
"basic_instruction": "input" => inst
},
Arc::new(prompt_model),
)
.await
}));
} else {
futures.push(Box::pin(async move {
BASIC_GENERATOR
.forward_with_config(
example! {
"basic_instruction": "input" => inst
},
Arc::clone(&get_lm()),
)
.await
}));
}
}
let results = join_all(futures).await;
let predictions = results.into_iter().collect::<Result<Vec<_>>>()?;
for pred in predictions {
let instruction = pred
.data
.get("proposed_instruction")
.and_then(|v| v.as_str())
.unwrap_or(basic_instruction)
.to_string();
let prefix = pred
.data
.get("proposed_prefix_for_output_field")
.and_then(|v| v.as_str())
.unwrap_or(basic_prefix)
.to_string();
candidates.push((instruction, prefix));
}
}
candidates.push((basic_instruction.clone(), basic_prefix.clone()));
all_candidates.insert(predictor_name.clone(), candidates.clone());
latest_candidates.insert(predictor_name.clone(), candidates);
evaluated_candidates.insert(predictor_name.clone(), HashMap::new());
if self.track_stats {
stats
.results_best
.insert(predictor_name.clone(), Vec::new());
stats
.results_latest
.insert(predictor_name.clone(), Vec::new());
}
}
// Main optimization loop
for d in 0..self.depth {
println!("Iteration Depth: {}/{}", d + 1, self.depth);
// Evaluate candidates for each predictor
for (p_i, (predictor_name, _, _)) in predictor_info.iter().enumerate() {
// Determine which candidates to evaluate
let candidates_to_eval = if predictor_info.len() > 1 {
// Re-evaluate all candidates when multiple predictors
all_candidates.get(predictor_name).unwrap().clone()
} else {
// Just evaluate latest candidates
latest_candidates.get(predictor_name).unwrap().clone()
};
let mut latest_scores = Vec::new();
for (c_i, (instruction, prefix)) in candidates_to_eval.iter().enumerate() {
// Check if already evaluated
let key = (instruction.clone(), prefix.clone());
let score = if let Some(existing) = evaluated_candidates
.get(predictor_name)
.and_then(|m| m.get(&key))
{
// Skip if already evaluated with same or better score
existing.score
} else {
// Update predictor with candidate
{
let mut module_predictors = module.parameters();
if let Some(predictor) = module_predictors.get_mut(predictor_name) {
predictor.update_signature_instruction(instruction.clone())?;
// Note: We can't update prefix without modifying the signature system
// This would require extending MetaSignature trait
}
}
println!(
"At Depth {}/{}, Evaluating Prompt Candidate #{}/{} for Predictor {} of {}",
d + 1,
self.depth,
c_i + 1,
candidates_to_eval.len(),
p_i + 1,
predictor_info.len()
);
// Evaluate
let score = module.evaluate(trainset.clone()).await;
stats.total_calls += 1;
// Store evaluated candidate
evaluated_candidates
.get_mut(predictor_name)
.unwrap()
.insert(
key,
Candidate {
score,
instruction: instruction.clone(),
prefix: prefix.clone(),
},
);
score
};
// Track latest scores for stats
if candidates_to_eval.len() - self.breadth <= c_i {
latest_scores.push(score);
}
}
// Update to best candidate for this predictor
if let Some(best) = evaluated_candidates.get(predictor_name).and_then(|m| {
m.values()
.max_by(|a, b| a.score.partial_cmp(&b.score).unwrap())
}) {
{
let mut module_predictors = module.parameters();
if let Some(predictor) = module_predictors.get_mut(predictor_name) {
predictor.update_signature_instruction(best.instruction.clone())?;
}
}
println!(
"Updating Predictor {} to best candidate with score {:.3}",
predictor_name, best.score
);
}
// Track stats
if self.track_stats && !latest_scores.is_empty() {
let avg = latest_scores.iter().sum::<f32>() / latest_scores.len() as f32;
stats
.results_latest
.get_mut(predictor_name)
.unwrap()
.push(avg);
// Track best scores
let mut best_scores: Vec<f32> = evaluated_candidates
.get(predictor_name)
.unwrap()
.values()
.map(|c| c.score)
.collect();
best_scores.sort_by(|a, b| b.partial_cmp(a).unwrap());
best_scores.truncate(10);
if !best_scores.is_empty() {
let best_avg = best_scores.iter().sum::<f32>() / best_scores.len() as f32;
stats
.results_best
.get_mut(predictor_name)
.unwrap()
.push(best_avg);
}
}
}
// Skip generation on last iteration
if d == self.depth - 1 {
break;
}
// Generate new candidates based on attempts
let mut new_latest_candidates = HashMap::new();
for (predictor_name, _, _) in &predictor_info {
// Build few-shot examples from best attempts
let mut attempts_list = Vec::new();
let mut best_candidates: Vec<_> = evaluated_candidates
.get(predictor_name)
.unwrap()
.values()
.cloned()
.collect();
best_candidates.sort_by(|a, b| a.score.partial_cmp(&b.score).unwrap());
// Take up to breadth best candidates
let num_examples = std::cmp::min(self.breadth, best_candidates.len());
for (i, candidate) in best_candidates.iter().take(num_examples).enumerate() {
attempts_list.push(format!(
"Instruction #{}: {}",
i + 1,
candidate.instruction
));
attempts_list.push(format!("Prefix #{}: {}", i + 1, candidate.prefix));
attempts_list.push(format!(
"Resulting Score #{}: {:.3}",
i + 1,
candidate.score
));
}
let attempts_str = attempts_list.join("\n");
// Generate new candidates
let results = if let Some(mut prompt_model) = self.prompt_model.clone() {
prompt_model.temperature = self.init_temperature;
let attempts = attempts_str.clone();
REFINEMENT_GENERATOR
.batch_with_config(
(0..self.breadth)
.map(|_| {
example! {
"attempted_instructions": "input" => attempts.clone()
}
})
.collect(),
Arc::new(prompt_model),
)
.await
} else {
let attempts = attempts_str.clone();
REFINEMENT_GENERATOR
.batch_with_config(
(0..self.breadth)
.map(|_| {
example! {
"attempted_instructions": "input" => attempts.clone()
}
})
.collect(),
Arc::clone(&get_lm()),
)
.await
};
if let Ok(predictions) = results {
let mut new_candidates = Vec::new();
for pred in predictions {
// Handle both single and multiple completions
let instructions = if let Some(arr) = pred
.data
.get("proposed_instruction")
.and_then(|v| v.as_array())
{
arr.iter()
.filter_map(|v| v.as_str())
.map(|s| s.to_string())
.collect()
} else if let Some(s) = pred
.data
.get("proposed_instruction")
.and_then(|v| v.as_str())
{
vec![s.to_string()]
} else {
vec![]
};
let prefixes = if let Some(arr) = pred
.data
.get("proposed_prefix_for_output_field")
.and_then(|v| v.as_array())
{
arr.iter()
.filter_map(|v| v.as_str())
.map(|s| s.to_string())
.collect()
} else if let Some(s) = pred
.data
.get("proposed_prefix_for_output_field")
.and_then(|v| v.as_str())
{
vec![s.to_string()]
} else {
vec![]
};
for (inst, pref) in instructions.iter().zip(prefixes.iter()) {
new_candidates.push((inst.clone(), pref.clone()));
}
}
// Add to all candidates
all_candidates
.get_mut(predictor_name)
.unwrap()
.extend(new_candidates.clone());
new_latest_candidates.insert(predictor_name.clone(), new_candidates);
}
}
latest_candidates = new_latest_candidates;
}
// Find best overall candidate and update module
let mut best_overall: Option<(String, Candidate)> = None;
for (predictor_name, candidates_map) in &evaluated_candidates {
if let Some(best) = candidates_map
.values()
.max_by(|a, b| a.score.partial_cmp(&b.score).unwrap())
&& (best_overall.is_none() || best.score > best_overall.as_ref().unwrap().1.score)
{
best_overall = Some((predictor_name.clone(), best.clone()));
}
}
// Update original module with best candidates
if let Some((_, best_candidate)) = best_overall {
let module_predictors = module.parameters();
for (predictor_name, predictor) in module_predictors {
if let Some(best) = evaluated_candidates.get(&predictor_name).and_then(|m| {
m.values()
.max_by(|a, b| a.score.partial_cmp(&b.score).unwrap())
}) {
predictor.update_signature_instruction(best.instruction.clone())?;
}
}
if self.track_stats {
println!("\n=== Optimization Complete ===");
println!("Total calls: {}", stats.total_calls);
println!("Best score: {:.3}", best_candidate.score);
println!("Best instruction: {}", best_candidate.instruction);
if !best_candidate.prefix.is_empty() {
println!("Best prefix: {}", best_candidate.prefix);
}
}
}
Ok(())
}
}