assay_core/
metrics_api.rs1use crate::model::{Expected, LlmResponse, TestCase};
2use async_trait::async_trait;
3
4#[derive(Debug, Clone)]
5pub struct MetricResult {
6 pub score: f64,
7 pub passed: bool,
8 pub unstable: bool,
9 pub details: serde_json::Value,
10}
11
12impl MetricResult {
13 pub fn pass(score: f64) -> Self {
14 Self {
15 score,
16 passed: true,
17 unstable: false,
18 details: serde_json::json!({}),
19 }
20 }
21 pub fn fail(score: f64, msg: &str) -> Self {
22 Self {
23 score,
24 passed: false,
25 unstable: false,
26 details: serde_json::json!({"message": msg}),
27 }
28 }
29 pub fn unstable(score: f64, msg: &str) -> Self {
30 Self {
31 score,
32 passed: false,
33 unstable: true,
34 details: serde_json::json!({"message": msg}),
35 }
36 }
37}
38
39#[async_trait]
40pub trait Metric: Send + Sync {
41 fn name(&self) -> &'static str;
42 async fn evaluate(
43 &self,
44 tc: &TestCase,
45 expected: &Expected,
46 resp: &LlmResponse,
47 ) -> anyhow::Result<MetricResult>;
48}