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
27impl<H> Capabilities<H> for XAiExt {
28    type Completion = Capable<super::completion::CompletionModel<H>>;
29
30    type Embeddings = Nothing;
31    type Transcription = Nothing;
32    type ModelListing = Nothing;
33    #[cfg(feature = "image")]
34    type ImageGeneration = Nothing;
35    #[cfg(feature = "audio")]
36    type AudioGeneration = Nothing;
37}
38
39impl DebugExt for XAiExt {}
40
41impl ProviderBuilder for XAiExtBuilder {
42    type Extension<H>
43        = XAiExt
44    where
45        H: http_client::HttpClientExt;
46    type ApiKey = XAiApiKey;
47
48    const BASE_URL: &'static str = XAI_BASE_URL;
49
50    fn build<H>(
51        _builder: &client::ClientBuilder<Self, Self::ApiKey, H>,
52    ) -> http_client::Result<Self::Extension<H>>
53    where
54        H: http_client::HttpClientExt,
55    {
56        Ok(XAiExt)
57    }
58}
59
60impl ProviderClient for Client {
61    type Input = String;
62
63    /// Create a new xAI client from the `XAI_API_KEY` environment variable.
64    /// Panics if the environment variable is not set.
65    fn from_env() -> Self {
66        let api_key = std::env::var("XAI_API_KEY").expect("XAI_API_KEY not set");
67        Self::new(&api_key).unwrap()
68    }
69
70    fn from_val(input: Self::Input) -> Self {
71        Self::new(&input).unwrap()
72    }
73}
74#[cfg(test)]
75mod tests {
76    #[test]
77    fn test_client_initialization() {
78        let _client_from_builder = crate::providers::xai::Client::builder()
79            .api_key("dummy-key")
80            .build()
81            .expect("Client::builder() failed");
82    }
83}