1use indexmap::IndexMap;
6use serde::{Deserialize, Serialize};
7use serde_json::{Map, Value};
8
9#[derive(Clone, Debug, Serialize, Deserialize)]
11#[serde(untagged)]
12#[cfg_attr(feature = "protocol-schema", derive(utoipa::ToSchema))]
13#[cfg_attr(feature = "protocol-schema", schema(as = DecisionSystemOneStructuredText))]
14pub enum StructuredText {
15 String(String),
16 Object(Map<String, Value>),
17 Array(Vec<Value>),
18}
19
20impl From<StructuredText> for Value {
21 fn from(text: StructuredText) -> Self {
22 match text {
23 StructuredText::String(text) => Self::String(text),
24 StructuredText::Object(object) => Self::Object(object),
25 StructuredText::Array(array) => Self::Array(array),
26 }
27 }
28}
29
30impl TryFrom<Value> for StructuredText {
31 type Error = &'static str;
32
33 fn try_from(value: Value) -> Result<Self, Self::Error> {
34 match value {
35 Value::String(text) => Ok(Self::String(text)),
36 Value::Object(object) => Ok(Self::Object(object)),
37 Value::Array(array) => Ok(Self::Array(array)),
38 _ => Err("structured text must be a string, object, or array"),
39 }
40 }
41}
42
43#[derive(Clone, Debug, Serialize, Deserialize)]
44#[serde(deny_unknown_fields)]
45#[cfg_attr(feature = "protocol-schema", derive(utoipa::ToSchema))]
46#[cfg_attr(feature = "protocol-schema", schema(as = DecisionSystemOneRequest))]
47pub struct Request {
48 pub model: String,
49 pub state: StructuredText,
50 pub questions: IndexMap<String, Question>,
51 #[serde(default)]
54 pub images: Vec<Value>,
55 #[serde(default)]
58 pub chat_template_kwargs: Map<String, Value>,
59}
60
61#[derive(Clone, Debug, Serialize, Deserialize)]
62#[serde(tag = "type", rename_all = "lowercase", deny_unknown_fields)]
63#[cfg_attr(feature = "protocol-schema", derive(utoipa::ToSchema))]
64#[cfg_attr(feature = "protocol-schema", schema(as = DecisionSystemOneQuestion))]
65pub enum Question {
66 Noul {
67 #[serde(default)]
68 instructions: Option<StructuredText>,
69 #[serde(default)]
70 criteria: Option<NoulCriteria>,
71 },
72 Choice {
73 #[serde(default)]
74 instructions: Option<StructuredText>,
75 #[cfg_attr(feature = "protocol-schema", schema(schema_with = choice_criteria_schema))]
76 criteria: IndexMap<String, Option<StructuredText>>,
77 },
78 Score {
79 #[serde(default)]
80 instructions: Option<StructuredText>,
81 criteria: Vec<StructuredText>,
82 },
83}
84
85#[cfg(feature = "protocol-schema")]
86fn choice_criteria_schema() -> utoipa::openapi::RefOr<utoipa::openapi::schema::Schema> {
87 use utoipa::{
88 ToSchema,
89 openapi::{
90 Ref,
91 schema::{ObjectBuilder, OneOfBuilder, Schema, Type},
92 },
93 };
94
95 ObjectBuilder::new()
96 .additional_properties(Some(Schema::OneOf(
97 OneOfBuilder::new()
98 .item(Ref::from_schema_name(StructuredText::name()))
99 .item(ObjectBuilder::new().schema_type(Type::Null))
100 .build(),
101 )))
102 .into()
103}
104
105#[derive(Clone, Debug, Default, Serialize, Deserialize)]
106#[serde(deny_unknown_fields)]
107#[cfg_attr(feature = "protocol-schema", derive(utoipa::ToSchema))]
108#[cfg_attr(feature = "protocol-schema", schema(as = DecisionSystemOneNoulCriteria))]
109pub struct NoulCriteria {
110 #[serde(default, rename = "true")]
111 pub positive: Option<StructuredText>,
112 #[serde(default, rename = "false")]
113 pub negative: Option<StructuredText>,
114}
115
116#[derive(Clone, Debug, Serialize, Deserialize)]
117#[cfg_attr(feature = "protocol-schema", derive(utoipa::ToSchema))]
118#[cfg_attr(feature = "protocol-schema", schema(as = DecisionSystemOneResponse))]
119pub struct Response {
120 pub model: String,
121 pub answers: IndexMap<String, Answer>,
122 pub usage: Usage,
123}
124
125#[derive(Clone, Debug, Serialize, Deserialize)]
126#[serde(tag = "type", rename_all = "lowercase")]
127#[cfg_attr(feature = "protocol-schema", derive(utoipa::ToSchema))]
128#[cfg_attr(feature = "protocol-schema", schema(as = DecisionSystemOneAnswer))]
129pub enum Answer {
130 Noul {
131 noul: f64,
132 #[serde(skip_serializing_if = "Option::is_none")]
135 x_label_mass: Option<f64>,
136 },
137 Choice {
138 choice: String,
139 probabilities: IndexMap<String, f64>,
140 confidence: f64,
141 #[serde(skip_serializing_if = "Option::is_none")]
144 x_label_mass: Option<f64>,
145 },
146 Score {
147 score: f64,
148 probabilities: IndexMap<String, f64>,
149 legend: IndexMap<String, StructuredText>,
150 confidence: f64,
151 #[serde(skip_serializing_if = "Option::is_none")]
154 x_label_mass: Option<f64>,
155 },
156}
157#[derive(Clone, Debug, Serialize, Deserialize)]
158#[cfg_attr(feature = "protocol-schema", derive(utoipa::ToSchema))]
159#[cfg_attr(feature = "protocol-schema", schema(as = DecisionSystemOneUsage))]
160pub struct Usage {
161 pub input_tokens: u64,
162 pub output_tokens: u64,
163}