Optimizable

Trait Optimizable 

Source
pub trait Optimizable {
    // Required method
    fn parameters(&mut self) -> IndexMap<String, &mut dyn Optimizable>;

    // Provided methods
    fn get_signature(&self) -> &dyn MetaSignature { ... }
    fn update_signature_instruction(
        &mut self,
        instruction: String,
    ) -> Result<()> { ... }
}

Required Methods§

Source

fn parameters(&mut self) -> IndexMap<String, &mut dyn Optimizable>

Provided Methods§

Source

fn get_signature(&self) -> &dyn MetaSignature

Examples found in repository?
examples/04-optimize-hotpotqa.rs (line 83)
60async fn main() -> anyhow::Result<()> {
61    configure(
62        LM::builder()
63            .model("openai:gpt-4o-mini".to_string())
64            .build()
65            .await
66            .unwrap(),
67        ChatAdapter {},
68    );
69
70    let examples = DataLoader::load_hf(
71        "hotpotqa/hotpot_qa",
72        vec!["question".to_string()],
73        vec!["answer".to_string()],
74        "fullwiki",
75        "validation",
76        true,
77    )?[..10]
78        .to_vec();
79
80    let mut rater = QARater::builder().build();
81    let optimizer = COPRO::builder().breadth(10).depth(1).build();
82
83    println!("Rater: {:?}", rater.answerer.get_signature().instruction());
84
85    optimizer.compile(&mut rater, examples.clone()).await?;
86
87    println!("Rater: {:?}", rater.answerer.get_signature().instruction());
88
89    Ok(())
90}
More examples
Hide additional examples
examples/08-optimize-mipro.rs (line 112)
84async fn main() -> Result<()> {
85    println!("=== MIPROv2 Optimizer Example ===\n");
86
87    // Configure the LM
88    configure(LM::default(), ChatAdapter);
89
90    // Load training data from HuggingFace
91    println!("Loading training data from HuggingFace...");
92    let train_examples = DataLoader::load_hf(
93        "hotpotqa/hotpot_qa",
94        vec!["question".to_string()],
95        vec!["answer".to_string()],
96        "fullwiki",
97        "validation",
98        true,
99    )?;
100
101    // Use a small subset for faster optimization
102    let train_subset = train_examples[..15].to_vec();
103    println!("Using {} training examples\n", train_subset.len());
104
105    // Create the module
106    let mut qa_module = SimpleQA::builder().build();
107
108    // Show initial instruction
109    println!("Initial instruction:");
110    println!(
111        "  \"{}\"\n",
112        qa_module.answerer.get_signature().instruction()
113    );
114
115    // Test baseline performance
116    println!("Evaluating baseline performance...");
117    let baseline_score = qa_module.evaluate(train_subset[..5].to_vec()).await;
118    println!("Baseline score: {:.3}\n", baseline_score);
119
120    // Create MIPROv2 optimizer
121    let optimizer = MIPROv2::builder()
122        .num_candidates(8) // Generate 8 candidate prompts
123        .num_trials(15) // Run 15 evaluation trials
124        .minibatch_size(10) // Evaluate on 10 examples per candidate
125        .temperature(1.0) // Temperature for prompt generation
126        .track_stats(true) // Display detailed statistics
127        .build();
128
129    // Optimize the module
130    println!("Starting MIPROv2 optimization...");
131    println!("This will:");
132    println!("  1. Generate execution traces");
133    println!("  2. Create a program description using LLM");
134    println!("  3. Generate {} candidate prompts with best practices", 8);
135    println!("  4. Evaluate each candidate");
136    println!("  5. Select and apply the best prompt\n");
137
138    optimizer
139        .compile(&mut qa_module, train_subset.clone())
140        .await?;
141
142    // Show optimized instruction
143    println!("\nOptimized instruction:");
144    println!(
145        "  \"{}\"\n",
146        qa_module.answerer.get_signature().instruction()
147    );
148
149    // Test optimized performance
150    println!("Evaluating optimized performance...");
151    let optimized_score = qa_module.evaluate(train_subset[..5].to_vec()).await;
152    println!("Optimized score: {:.3}", optimized_score);
153
154    // Show improvement
155    let improvement = ((optimized_score - baseline_score) / baseline_score) * 100.0;
156    println!(
157        "\n✓ Improvement: {:.1}% ({:.3} -> {:.3})",
158        improvement, baseline_score, optimized_score
159    );
160
161    // Test on a new example
162    println!("\n--- Testing on a new example ---");
163    let test_example = example! {
164        "question": "input" => "What is the capital of France?",
165    };
166
167    let result = qa_module.forward(test_example).await?;
168    println!("Question: What is the capital of France?");
169    println!("Answer: {}", result.get("answer", None));
170
171    println!("\n=== Example Complete ===");
172    Ok(())
173}
Source

fn update_signature_instruction(&mut self, instruction: String) -> Result<()>

Examples found in repository?
examples/02-module-iteration-and-updation.rs (line 96)
91async fn main() {
92    // Single module test
93    let mut qa_rater = QARater::builder().build();
94    for (name, param) in qa_rater.parameters() {
95        param
96            .update_signature_instruction("Updated instruction for ".to_string() + &name)
97            .unwrap();
98    }
99    println!(
100        "single.answerer -> {}",
101        qa_rater.answerer.signature.instruction()
102    );
103    println!(
104        "single.rater    -> {}",
105        qa_rater.rater.signature.instruction()
106    );
107
108    // Nested module test
109    let mut nested = NestedModule::builder().build();
110    for (name, param) in nested.parameters() {
111        param
112            .update_signature_instruction("Deep updated: ".to_string() + &name)
113            .unwrap();
114    }
115
116    // Show nested updates (module-in-module)
117    println!(
118        "nested.qa_outer.answerer -> {}",
119        nested.qa_outer.answerer.signature.instruction()
120    );
121    println!(
122        "nested.qa_outer.rater    -> {}",
123        nested.qa_outer.rater.signature.instruction()
124    );
125    println!(
126        "nested.qa_inner.answerer -> {}",
127        nested.qa_inner.answerer.signature.instruction()
128    );
129    println!(
130        "nested.qa_inner.rater    -> {}",
131        nested.qa_inner.rater.signature.instruction()
132    );
133    println!(
134        "nested.extra    -> {}",
135        nested.extra.signature.instruction()
136    );
137}

Implementors§