pub struct DataLoader;Implementations§
Source§impl DataLoader
impl DataLoader
pub fn load_json( path: &str, lines: bool, input_keys: Vec<String>, output_keys: Vec<String>, ) -> Result<Vec<Example>>
pub fn save_json(path: &str, examples: Vec<Example>, lines: bool) -> Result<()>
pub fn load_csv( path: &str, delimiter: char, input_keys: Vec<String>, output_keys: Vec<String>, has_headers: bool, ) -> Result<Vec<Example>>
pub fn save_csv( path: &str, examples: Vec<Example>, delimiter: char, ) -> Result<()>
pub fn load_parquet( path: &str, input_keys: Vec<String>, output_keys: Vec<String>, ) -> Result<Vec<Example>>
Sourcepub fn load_hf(
dataset_id: &str,
input_keys: Vec<String>,
output_keys: Vec<String>,
subset: &str,
split: &str,
verbose: bool,
) -> Result<Vec<Example>>
pub fn load_hf( dataset_id: &str, input_keys: Vec<String>, output_keys: Vec<String>, subset: &str, split: &str, verbose: bool, ) -> Result<Vec<Example>>
Examples found in repository?
examples/03-evaluate-hotpotqa.rs (lines 73-80)
64async fn main() -> anyhow::Result<()> {
65 configure(
66 LM::builder()
67 .model("openai:gpt-4o-mini".to_string())
68 .build()
69 .await?,
70 ChatAdapter {},
71 );
72
73 let examples = DataLoader::load_hf(
74 "hotpotqa/hotpot_qa",
75 vec!["question".to_string()],
76 vec!["answer".to_string()],
77 "fullwiki",
78 "validation",
79 true,
80 )?[..128]
81 .to_vec();
82
83 let evaluator = QARater::builder().build();
84 let metric = evaluator.evaluate(examples).await;
85
86 println!("Metric: {metric}");
87 Ok(())
88}More examples
examples/04-optimize-hotpotqa.rs (lines 70-77)
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}examples/08-optimize-mipro.rs (lines 92-99)
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}Auto Trait Implementations§
impl Freeze for DataLoader
impl RefUnwindSafe for DataLoader
impl Send for DataLoader
impl Sync for DataLoader
impl Unpin for DataLoader
impl UnsafeUnpin for DataLoader
impl UnwindSafe for DataLoader
Blanket Implementations§
impl<T> Allocation for T
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more