Skip to main content

rig/providers/xai/
client.rs

1use crate::{
2    client::{
3        self, BearerAuth, Capabilities, Capable, DebugExt, Nothing, Provider, ProviderBuilder,
4        ProviderClient,
5    },
6    http_client,
7};
8
9#[derive(Debug, Default, Clone, Copy)]
10pub struct XAiExt;
11#[derive(Debug, Default, Clone, Copy)]
12pub struct XAiExtBuilder;
13
14type XAiApiKey = BearerAuth;
15
16pub type Client<H = reqwest::Client> = client::Client<XAiExt, H>;
17pub type ClientBuilder<H = reqwest::Client> = client::ClientBuilder<XAiExtBuilder, XAiApiKey, H>;
18
19const XAI_BASE_URL: &str = "https://api.x.ai";
20
21impl Provider for XAiExt {
22    type Builder = XAiExtBuilder;
23
24    const VERIFY_PATH: &'static str = "/v1/api-key";
25
26    fn build<H>(
27        _: &client::ClientBuilder<Self::Builder, XAiApiKey, H>,
28    ) -> http_client::Result<Self> {
29        Ok(Self)
30    }
31}
32
33impl<H> Capabilities<H> for XAiExt {
34    type Completion = Capable<super::completion::CompletionModel<H>>;
35
36    type Embeddings = Nothing;
37    type Transcription = Nothing;
38    type ModelListing = Nothing;
39    #[cfg(feature = "image")]
40    type ImageGeneration = Nothing;
41    #[cfg(feature = "audio")]
42    type AudioGeneration = Nothing;
43}
44
45impl DebugExt for XAiExt {}
46
47impl ProviderBuilder for XAiExtBuilder {
48    type Output = XAiExt;
49    type ApiKey = XAiApiKey;
50
51    const BASE_URL: &'static str = XAI_BASE_URL;
52}
53
54impl ProviderClient for Client {
55    type Input = String;
56
57    /// Create a new xAI client from the `XAI_API_KEY` environment variable.
58    /// Panics if the environment variable is not set.
59    fn from_env() -> Self {
60        let api_key = std::env::var("XAI_API_KEY").expect("XAI_API_KEY not set");
61        Self::new(&api_key).unwrap()
62    }
63
64    fn from_val(input: Self::Input) -> Self {
65        Self::new(&input).unwrap()
66    }
67}
68#[cfg(test)]
69mod tests {
70    #[test]
71    fn test_client_initialization() {
72        let _client_from_builder: crate::providers::xai::Client =
73            crate::providers::xai::Client::builder()
74                .api_key("dummy-key")
75                .build()
76                .expect("Client::builder() failed");
77    }
78}