1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
mod private
{
use crate::error::Result;
use crate::environment::XaiEnvironment;
use crate::client::Client;
use crate::components::models::{ Model, ListModelsResponse };
/// Models API accessor.
///
/// Provides methods for listing and retrieving model information.
///
/// # Examples
///
/// ```no_run
/// use api_xai::{ Client, XaiEnvironmentImpl, Secret, ClientApiAccessors };
///
/// # async fn example() -> Result< (), Box< dyn std::error::Error > > {
/// let secret = Secret::load_with_fallbacks( "XAI_API_KEY" )?;
/// let env = XaiEnvironmentImpl::new( secret )?;
/// let client = Client::build( env )?;
///
/// let models = client.models().list().await?;
/// for model in models.data {
/// println!( "Model : {}", model.id );
/// }
/// # Ok( () )
/// # }
/// ```
#[ derive( Debug ) ]
pub struct Models< 'a, E >
where
E : XaiEnvironment + Send + Sync + 'static,
{
client : &'a Client< E >,
}
impl< 'a, E > Models< 'a, E >
where
E : XaiEnvironment + Send + Sync + 'static,
{
/// Creates a new Models API accessor.
///
/// Typically not called directly - use `client.models()` instead.
///
/// # Arguments
///
/// * `client` - Reference to the client
pub fn new( client : &'a Client< E > ) -> Self
{
Self { client }
}
/// Lists all available models.
///
/// Retrieves a list of all models accessible with the current API key.
///
/// # Errors
///
/// Returns errors for network failures or API errors.
///
/// # Examples
///
/// ```no_run
/// # use api_xai::{ Client, XaiEnvironmentImpl, Secret, ClientApiAccessors };
/// # async fn example() -> Result< (), Box< dyn std::error::Error > > {
/// # let secret = Secret::load_with_fallbacks( "XAI_API_KEY" )?;
/// # let env = XaiEnvironmentImpl::new( secret )?;
/// # let client = Client::build( env )?;
/// let response = client.models().list().await?;
///
/// println!( "Available models:" );
/// for model in response.data {
/// println!( " - {} (created : {})", model.id, model.created );
/// }
/// # Ok( () )
/// # }
/// ```
pub async fn list( &self ) -> Result< ListModelsResponse >
{
self.client.get( "models" ).await
}
/// Retrieves information about a specific model.
///
/// # Arguments
///
/// * `model_id` - Model identifier (e.g., "grok-2-1212", "grok-4")
///
/// # Errors
///
/// Returns errors for network failures, API errors, or if the model is not found.
///
/// # Examples
///
/// ```no_run
/// # use api_xai::{ Client, XaiEnvironmentImpl, Secret, ClientApiAccessors };
/// # async fn example() -> Result< (), Box< dyn std::error::Error > > {
/// # let secret = Secret::load_with_fallbacks( "XAI_API_KEY" )?;
/// # let env = XaiEnvironmentImpl::new( secret )?;
/// # let client = Client::build( env )?;
/// let model = client.models().get( "grok-2-1212" ).await?;
///
/// println!( "Model : {}", model.id );
/// println!( "Owned by : {}", model.owned_by );
/// println!( "Created : {}", model.created );
/// # Ok( () )
/// # }
/// ```
pub async fn get( &self, model_id : &str ) -> Result< Model >
{
let path = format!( "models/{model_id}" );
self.client.get( &path ).await
}
}
}
crate::mod_interface!
{
exposed use
{
Models,
};
}