portkey_sdk/model/
models.rs

1//! Models API data structures.
2//!
3//! This module contains data models for listing available models through Portkey.
4
5use serde::{Deserialize, Serialize};
6
7/// Sort field for models.
8#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default)]
9#[serde(rename_all = "lowercase")]
10pub enum ModelSortField {
11    /// Sort by model name
12    #[default]
13    Name,
14    /// Sort by provider
15    Provider,
16    /// Sort by AI service
17    #[serde(rename = "ai_service")]
18    AiService,
19}
20
21/// Sort order for models.
22#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default)]
23#[serde(rename_all = "lowercase")]
24pub enum SortOrder {
25    /// Ascending order
26    #[default]
27    Asc,
28    /// Descending order
29    Desc,
30}
31
32/// Parameters for listing models.
33///
34/// # Example
35///
36/// ```
37/// use portkey_sdk::model::{ListModelsParams, ModelSortField, SortOrder};
38///
39/// let params = ListModelsParams {
40///     ai_service: Some("openai".to_string()),
41///     provider: Some("openai".to_string()),
42///     limit: Some(10),
43///     offset: Some(0),
44///     sort: Some(ModelSortField::Name),
45///     order: Some(SortOrder::Asc),
46/// };
47/// ```
48#[derive(Debug, Clone, Default, Serialize, Deserialize)]
49pub struct ListModelsParams {
50    /// Filter models by the AI service (e.g., 'openai', 'anthropic').
51    #[serde(skip_serializing_if = "Option::is_none")]
52    pub ai_service: Option<String>,
53
54    /// Filter models by the provider.
55    #[serde(skip_serializing_if = "Option::is_none")]
56    pub provider: Option<String>,
57
58    /// The maximum number of models to return.
59    #[serde(skip_serializing_if = "Option::is_none")]
60    pub limit: Option<i32>,
61
62    /// The number of models to skip before starting to collect the result set.
63    #[serde(skip_serializing_if = "Option::is_none")]
64    pub offset: Option<i32>,
65
66    /// The field to sort the results by.
67    #[serde(skip_serializing_if = "Option::is_none")]
68    pub sort: Option<ModelSortField>,
69
70    /// The order to sort the results in.
71    #[serde(skip_serializing_if = "Option::is_none")]
72    pub order: Option<SortOrder>,
73}
74
75/// Describes a model offering that can be used with the API.
76#[derive(Debug, Clone, Serialize, Deserialize)]
77pub struct Model {
78    /// The model identifier, which can be referenced in the API endpoints.
79    pub id: String,
80
81    /// The Unix timestamp (in seconds) when the model was created.
82    pub created: i64,
83
84    /// The object type, which is always "model".
85    pub object: String,
86
87    /// The organization that owns the model.
88    pub owned_by: String,
89}
90
91/// Response from listing models.
92///
93/// # Example
94///
95/// ```
96/// use portkey_sdk::model::ListModelsResponse;
97///
98/// // This would typically be deserialized from the API response
99/// ```
100#[derive(Debug, Clone, Serialize, Deserialize)]
101pub struct ListModelsResponse {
102    /// The object type, which is always "list".
103    pub object: String,
104
105    /// Array of model objects.
106    pub data: Vec<Model>,
107}
108
109#[cfg(test)]
110mod tests {
111    use super::*;
112
113    #[test]
114    fn test_model_sort_field() {
115        let field = ModelSortField::Name;
116        let json = serde_json::to_string(&field).unwrap();
117        assert_eq!(json, "\"name\"");
118
119        let field = ModelSortField::AiService;
120        let json = serde_json::to_string(&field).unwrap();
121        assert_eq!(json, "\"ai_service\"");
122    }
123
124    #[test]
125    fn test_sort_order() {
126        let order = SortOrder::Asc;
127        let json = serde_json::to_string(&order).unwrap();
128        assert_eq!(json, "\"asc\"");
129
130        let order = SortOrder::Desc;
131        let json = serde_json::to_string(&order).unwrap();
132        assert_eq!(json, "\"desc\"");
133    }
134
135    #[test]
136    fn test_list_models_params() {
137        let params = ListModelsParams {
138            provider: Some("openai".to_string()),
139            limit: Some(10),
140            ..Default::default()
141        };
142
143        assert_eq!(params.provider, Some("openai".to_string()));
144        assert_eq!(params.limit, Some(10));
145    }
146}