Skip to main content

synaptic_eval/
evaluator.rs

1use async_trait::async_trait;
2use serde::{Deserialize, Serialize};
3use synaptic_core::SynapticError;
4
5/// Result of a single evaluation.
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct EvalResult {
8    /// Score between 0.0 and 1.0.
9    pub score: f64,
10    /// Whether the evaluation passed.
11    pub passed: bool,
12    /// Optional reasoning for the result.
13    pub reasoning: Option<String>,
14}
15
16impl EvalResult {
17    /// Create a passing result with score 1.0.
18    pub fn pass() -> Self {
19        Self {
20            score: 1.0,
21            passed: true,
22            reasoning: None,
23        }
24    }
25
26    /// Create a failing result with score 0.0.
27    pub fn fail() -> Self {
28        Self {
29            score: 0.0,
30            passed: false,
31            reasoning: None,
32        }
33    }
34
35    /// Create a result with a specific score. Passes if score >= 0.5.
36    pub fn with_score(score: f64) -> Self {
37        Self {
38            score,
39            passed: score >= 0.5,
40            reasoning: None,
41        }
42    }
43
44    /// Attach reasoning to this result.
45    pub fn with_reasoning(mut self, reasoning: impl Into<String>) -> Self {
46        self.reasoning = Some(reasoning.into());
47        self
48    }
49}
50
51/// Trait for evaluating predictions against references.
52#[async_trait]
53pub trait Evaluator: Send + Sync {
54    /// Evaluate a prediction against a reference, given the original input.
55    async fn evaluate(
56        &self,
57        prediction: &str,
58        reference: &str,
59        input: &str,
60    ) -> Result<EvalResult, SynapticError>;
61}