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
//! A one-shot structured judgment: one model request whose system prompt a
//! typed template renders, whose input is the text being judged, and whose
//! answer is decoded into the caller's type from the model's structured
//! output. Subagent routing asks the fast model this way, and an embedder's
//! own checks use the same path.
use aither_core::LanguageModel;
use aither_core::llm::{GenerateError, model::Parameters, oneshot};
use schemars::JsonSchema;
use serde::de::DeserializeOwned;
#[cfg(test)]
mod tests;
/// Why a judgment produced no answer.
#[derive(Debug, thiserror::Error)]
pub enum JudgmentError<E: core::error::Error + 'static> {
/// The system prompt's template did not render.
#[error("the judgment's system prompt did not render: {0}")]
Render(#[from] askama::Error),
/// The model failed, or answered something the answer type does not
/// decode.
#[error("the model gave no judgment: {0}")]
Model(#[source] GenerateError<E>),
}
/// Asks `model` one question: `system` rendered as the system prompt,
/// `input` as the user message, sent with `parameters`, and answered as a
/// `T` from the model's structured output.
///
/// # Errors
///
/// Returns [`JudgmentError::Render`] when `system` does not render and
/// [`JudgmentError::Model`] when the model fails or its answer does not
/// decode into `T`.
pub fn judge<'a, T, M, S, I>(
model: &'a M,
parameters: Parameters,
system: &S,
input: I,
) -> impl Future<Output = Result<T, JudgmentError<M::Error>>> + Send + use<'a, T, M, S, I>
where
T: JsonSchema + DeserializeOwned + 'static,
M: LanguageModel,
S: askama::Template,
I: Into<String>,
{
// Rendered before the request starts, so the future holds no template.
let request = system
.render()
.map(|system| oneshot(system, input).with_parameters(parameters));
async move { model.generate(request?).await.map_err(JudgmentError::Model) }
}