1use rig_agent::completion::StructuredOutputError;
4use rig_core::completion::Usage;
5use rig_core::error::BoxError;
6use rig_core::wasm_compat::{WasmBoxedFuture, WasmCompatSend, WasmCompatSync};
7use serde::{Deserialize, Serialize};
8
9use crate::{Case, Trial};
10
11#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
13#[non_exhaustive]
14pub struct Score {
15 pub evaluator: String,
18 pub value: f64,
20 pub passed: bool,
22 #[serde(default, skip_serializing_if = "Option::is_none")]
25 pub label: Option<String>,
26 #[serde(default, skip_serializing_if = "Option::is_none")]
28 pub explanation: Option<String>,
29 #[serde(default, skip_serializing_if = "is_false")]
33 pub not_applicable: bool,
34 #[serde(default, skip_serializing_if = "Option::is_none")]
37 pub usage: Option<Usage>,
38}
39
40fn is_false(value: &bool) -> bool {
41 !value
42}
43
44impl Score {
45 pub fn new(evaluator: impl Into<String>, value: f64, passed: bool) -> Self {
47 Self {
48 evaluator: evaluator.into(),
49 value: clamp(value),
50 passed,
51 label: None,
52 explanation: None,
53 not_applicable: false,
54 usage: None,
55 }
56 }
57
58 pub fn not_applicable(evaluator: impl Into<String>, explanation: impl Into<String>) -> Self {
61 Self {
62 not_applicable: true,
63 ..Self::new(evaluator, 0.0, false)
64 .with_label("n/a")
65 .with_explanation(explanation)
66 }
67 }
68
69 pub fn pass(evaluator: impl Into<String>) -> Self {
71 Self::new(evaluator, 1.0, true)
72 }
73
74 pub fn fail(evaluator: impl Into<String>, explanation: impl Into<String>) -> Self {
76 Self::new(evaluator, 0.0, false).with_explanation(explanation)
77 }
78
79 #[must_use]
81 pub fn with_label(mut self, label: impl Into<String>) -> Self {
82 self.label = Some(label.into());
83 self
84 }
85
86 #[must_use]
88 pub fn with_explanation(mut self, explanation: impl Into<String>) -> Self {
89 self.explanation = Some(explanation.into());
90 self
91 }
92
93 #[must_use]
95 pub fn with_usage(mut self, usage: Usage) -> Self {
96 self.usage = Some(usage);
97 self
98 }
99}
100
101pub(crate) fn clamp(value: f64) -> f64 {
103 if value.is_nan() {
104 0.0
105 } else {
106 value.clamp(0.0, 1.0)
107 }
108}
109
110#[derive(Debug, thiserror::Error)]
113#[non_exhaustive]
114pub enum EvalError {
115 #[error("case {case:?} has no {field} to compare with")]
117 MissingReference {
118 case: String,
120 field: &'static str,
122 },
123 #[error("the judge returned no verdict: {0}")]
126 Judge(#[from] StructuredOutputError),
127 #[error(transparent)]
129 Other(BoxError),
130}
131
132pub trait Evaluator: WasmCompatSend + WasmCompatSync {
134 fn name(&self) -> &str;
137
138 fn evaluate<'a>(
141 &'a self,
142 case: &'a Case,
143 trial: &'a Trial,
144 ) -> WasmBoxedFuture<'a, Result<Score, EvalError>>;
145
146 fn pass_cutoff(&self) -> Option<f64> {
151 None
152 }
153
154 fn named(self, name: impl Into<String>) -> Named<Self>
157 where
158 Self: Sized,
159 {
160 Named {
161 name: name.into(),
162 inner: self,
163 }
164 }
165}
166
167pub struct Named<E> {
169 name: String,
170 inner: E,
171}
172
173impl<E: Evaluator> Evaluator for Named<E> {
174 fn name(&self) -> &str {
175 &self.name
176 }
177
178 fn evaluate<'a>(
179 &'a self,
180 case: &'a Case,
181 trial: &'a Trial,
182 ) -> WasmBoxedFuture<'a, Result<Score, EvalError>> {
183 self.inner.evaluate(case, trial)
184 }
185
186 fn pass_cutoff(&self) -> Option<f64> {
187 self.inner.pass_cutoff()
188 }
189}