lm_studio_api/chat/
schema.rs

1 use crate::prelude::*;
2
3/// Response format JSON schema
4#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)]
5pub struct JsonSchema {
6    pub name: String,
7    #[serde(rename = "schema")]
8    pub schemes: Schemes,
9    pub strict: bool,
10}
11
12
13/// Response format schemes
14#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)]
15pub enum Schemes {
16    #[serde(rename = "oneOf")]
17    OneOf(Vec<Schema>),
18
19    #[serde(rename = "anyOf")]
20    AnyOf(Vec<Schema>),
21
22    #[serde(rename = "allOf")]
23    AllOf(Vec<Schema>),
24}
25
26
27/// Response format schema kind
28#[derive(Debug, Display, Clone, Serialize, Deserialize, Eq, PartialEq)]
29#[serde(rename_all = "lowercase")]
30pub enum SchemaKind {
31    Object,
32    Array,
33    String,
34    Integer,
35    Float,
36    Boolean,
37    Null,
38}
39
40
41/// Response format schema
42#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)]
43pub struct Schema {
44    #[serde(rename = "type")]
45    pub kind: SchemaKind,
46    pub title: String,
47    pub description: String,
48    #[serde(skip_serializing_if = "Option::is_none")]
49    pub format: Option<String>,
50    #[serde(skip_serializing_if = "Option::is_none")]
51    pub properties: Option<HashMap<String, Box<Schema>>>,
52    #[serde(default, skip_serializing_if = "Vec::is_empty")]
53    pub required: Vec<String>,
54}
55
56impl ::std::default::Default for Schema {
57    fn default() -> Self {
58        Self {
59            kind: SchemaKind::String,
60            title: str!(),
61            description: str!(),
62            format: None,
63            properties: None,
64            required: vec![],
65        }
66    }
67}
68
69impl Schema {
70    /// Creates an object scheme
71    pub fn object<S: Into<String>>(name: S, descr: S, props: HashMap<S, Schema>) -> Self {
72        Self {
73            kind: SchemaKind::Object,
74            title: name.into(),
75            description: descr.into(),
76            format: None,
77            properties: Some(
78                props.into_iter()
79                    .map(|(key, val)| (key.into(), Box::new(val)))
80                    .collect::<HashMap<_, _>>()
81            ),
82            required: vec![],
83        }
84    }
85
86    /// Creates an array scheme
87    pub fn array<S: Into<String>>(name: S, descr: S) -> Self {
88        Self {
89            kind: SchemaKind::Array,
90            title: name.into(),
91            description: descr.into(),
92            format: None,
93            properties: None,
94            required: vec![],
95        }
96    }
97    
98    /// Creates a string scheme
99    pub fn string<S: Into<String>>(descr: S, format: Option<S>) -> Self {
100        Self {
101            kind: SchemaKind::String,
102            title: "".into(),
103            description: descr.into(),
104            format: format.map(|s| s.into()),
105            properties: None,
106            required: vec![],
107        }
108    }
109    
110    /// Creates an integer scheme
111    pub fn integer<S: Into<String>>(descr: S) -> Self {
112        Self {
113            kind: SchemaKind::Integer,
114            title: "".into(),
115            description: descr.into(),
116            format: None,
117            properties: None,
118            required: vec![],
119        }
120    }
121    
122    /// Creates a float scheme
123    pub fn float<S: Into<String>>(descr: S) -> Self {
124        Self {
125            kind: SchemaKind::Float,
126            title: "".into(),
127            description: descr.into(),
128            format: None,
129            properties: None,
130            required: vec![],
131        }
132    }
133    
134    /// Creates a boolean scheme
135    pub fn boolean<S: Into<String>>(descr: S) -> Self {
136        Self {
137            kind: SchemaKind::Boolean,
138            title: "".into(),
139            description: descr.into(),
140            format: None,
141            properties: None,
142            required: vec![],
143        }
144    }
145    
146    /// Creates a null scheme
147    pub fn null<S: Into<String>>(descr: S) -> Self {
148        Self {
149            kind: SchemaKind::Null,
150            title: "".into(),
151            description: descr.into(),
152            format: None,
153            properties: None,
154            required: vec![],
155        }
156    }
157}