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    #[cfg(feature = "image")]
39    type ImageGeneration = Nothing;
40    #[cfg(feature = "audio")]
41    type AudioGeneration = Nothing;
42}
43
44impl DebugExt for XAiExt {}
45
46impl ProviderBuilder for XAiExtBuilder {
47    type Output = XAiExt;
48    type ApiKey = XAiApiKey;
49
50    const BASE_URL: &'static str = XAI_BASE_URL;
51}
52
53impl ProviderClient for Client {
54    type Input = String;
55
56    /// Create a new xAI client from the `XAI_API_KEY` environment variable.
57    /// Panics if the environment variable is not set.
58    fn from_env() -> Self {
59        let api_key = std::env::var("XAI_API_KEY").expect("XAI_API_KEY not set");
60        Self::new(&api_key).unwrap()
61    }
62
63    fn from_val(input: Self::Input) -> Self {
64        Self::new(&input).unwrap()
65    }
66}