Skip to main content

rig_typesafeai/
types.rs

1//! TypeSafe's JSON protocol. Prefer the typed question constructors for application code.
2use serde::{Deserialize, Serialize};
3use serde_json::Value;
4use std::collections::BTreeMap;
5
6/// Model-independent input to an evaluation. [`crate::Jev`] supplies the model
7/// from its configuration when encoding the full HTTP request.
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct Request {
10    /// The shared state evaluated independently by every question.
11    pub state: Value,
12    /// Serialized question object. Keeping the JSON intact preserves duplicate
13    /// keys for validation and allows named structs to supply the shape.
14    pub questions: Box<serde_json::value::RawValue>,
15}
16
17/// A question as represented by the HTTP API.
18#[derive(Debug, Clone, Serialize, Deserialize)]
19#[serde(tag = "type", rename_all = "lowercase")]
20pub enum Question {
21    Choice {
22        instructions: Value,
23        criteria: BTreeMap<String, Option<Value>>,
24    },
25    Score {
26        instructions: Value,
27        criteria: Vec<Value>,
28    },
29    Noul {
30        instructions: Value,
31        #[serde(skip_serializing_if = "Option::is_none")]
32        criteria: Option<BTreeMap<String, Value>>,
33    },
34}
35
36/// An answer as represented by the HTTP API.
37#[derive(Debug, Clone, Serialize, Deserialize)]
38#[serde(tag = "type", rename_all = "lowercase")]
39pub enum Answer {
40    Choice {
41        choice: String,
42        probabilities: BTreeMap<String, f64>,
43        confidence: f64,
44    },
45    Score {
46        score: f64,
47        probabilities: BTreeMap<String, f64>,
48        legend: BTreeMap<String, Value>,
49        confidence: f64,
50    },
51    Noul {
52        noul: f64,
53    },
54}
55
56/// Rig token accounting; counters absent from Jev remain unknown.
57pub use rig_core::completion::Usage;
58
59/// Raw response, including the transport's request identifier.
60#[derive(Debug, Clone, Serialize, Deserialize)]
61pub struct Response {
62    /// The model identifier returned by the provider.
63    pub model: String,
64    /// Answer object kept intact for validation and typed Serde decoding.
65    pub answers: Box<serde_json::value::RawValue>,
66    #[serde(default)]
67    /// Token accounting when reported by the provider.
68    pub usage: Option<Usage>,
69    #[serde(skip)]
70    /// Transport request identifier for diagnostics.
71    pub provider_request_id: Option<String>,
72    #[serde(skip)]
73    /// The whole reply document, including fields this type does not model.
74    pub raw: Value,
75}