-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy path03-evaluate-hotpotqa.rs
More file actions
74 lines (60 loc) · 1.81 KB
/
Copy path03-evaluate-hotpotqa.rs
File metadata and controls
74 lines (60 loc) · 1.81 KB
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
/*
Script to evaluate a typed QA predictor on a HotpotQA sample.
Run with:
```
cargo run --example 03-evaluate-hotpotqa --features dataloaders
```
*/
use anyhow::Result;
use dspy_rs::{
ChatAdapter, DataLoader, Example, LM, MetricOutcome, Predict, Predicted, Signature,
TypedLoadOptions, TypedMetric, average_score, configure, evaluate_trainset, init_tracing,
};
#[derive(Signature, Clone, Debug)]
struct QA {
/// Concisely answer the question, but be accurate.
#[input]
question: String,
#[output(desc = "Answer in less than 5 words.")]
answer: String,
}
struct ExactMatchMetric;
impl TypedMetric<QA, Predict<QA>> for ExactMatchMetric {
async fn evaluate(
&self,
example: &Example<QA>,
prediction: &Predicted<QAOutput>,
) -> Result<MetricOutcome> {
let expected = example.output.answer.trim().to_lowercase();
let actual = prediction.answer.trim().to_lowercase();
Ok(MetricOutcome::score((expected == actual) as u8 as f32))
}
}
#[tokio::main]
async fn main() -> Result<()> {
init_tracing()?;
configure(
LM::builder()
.model("openai:gpt-4o-mini".to_string())
.build()
.await?,
ChatAdapter,
);
let examples = DataLoader::load_hf::<QA>(
"hotpotqa/hotpot_qa",
"fullwiki",
"validation",
true,
TypedLoadOptions::default(),
)?[..64]
.to_vec();
let module = Predict::<QA>::builder()
.instruction("Answer with a short, factual response.")
.build();
let metric = ExactMatchMetric;
let outcomes = evaluate_trainset(&module, &examples, &metric).await?;
let score = average_score(&outcomes);
println!("evaluated {} examples", outcomes.len());
println!("average exact-match score: {score:.3}");
Ok(())
}