Skip to main content

jev_client/
models.rs

1//! The body returned by `GET /v1/models`.
2
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5
6/// The model names an account can send in a request's `model` field.
7///
8/// The endpoint currently lists aliases only. A versioned id such as `jev-1.13.0` is accepted by
9/// the API whether or not it appears here.
10#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
11#[non_exhaustive]
12pub struct ModelList {
13    /// One entry per model or alias.
14    #[serde(default)]
15    pub models: Vec<ModelCard>,
16}
17
18impl ModelList {
19    /// A list of the given models.
20    #[must_use]
21    pub const fn new(models: Vec<ModelCard>) -> Self {
22        Self { models }
23    }
24}
25
26/// One model or alias, as listed by the API.
27#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
28#[non_exhaustive]
29pub struct ModelCard {
30    /// The model id or alias, as accepted by a request's `model` field.
31    pub name: String,
32
33    /// What the model is for.
34    #[serde(default, skip_serializing_if = "Option::is_none")]
35    pub description: Option<String>,
36
37    /// When the model or alias was released, as the RFC 3339 timestamp the API sent.
38    ///
39    /// Kept as text so that a change of format upstream cannot fail the whole listing.
40    #[serde(default, skip_serializing_if = "Option::is_none")]
41    pub release_date: Option<String>,
42}
43
44impl ModelCard {
45    /// A model card with only a name.
46    #[must_use]
47    pub fn new(name: impl Into<String>) -> Self {
48        Self {
49            name: name.into(),
50            description: None,
51            release_date: None,
52        }
53    }
54}
55
56#[cfg(test)]
57mod tests {
58    use serde_json::json;
59
60    use super::{ModelCard, ModelList};
61
62    #[test]
63    fn tolerates_missing_and_unknown_fields() {
64        let json = json!({
65            "models": [{ "name": "jev-latest", "context_length": 64000 }],
66            "next_page": null
67        });
68
69        let list: ModelList = serde_json::from_value(json).unwrap();
70
71        assert_eq!(list, ModelList::new(vec![ModelCard::new("jev-latest")]));
72    }
73
74    #[test]
75    fn an_empty_body_is_an_empty_list() {
76        let list: ModelList = serde_json::from_value(json!({})).unwrap();
77
78        assert!(list.models.is_empty());
79    }
80}