Skip to main content

rig/providers/together/
client.rs

1use crate::{
2    client::{
3        self, BearerAuth, Capabilities, Capable, Nothing, Provider, ProviderBuilder, ProviderClient,
4    },
5    http_client,
6};
7
8// ================================================================
9// Together AI Client
10// ================================================================
11const TOGETHER_AI_BASE_URL: &str = "https://api.together.xyz";
12
13#[derive(Debug, Default, Clone, Copy)]
14pub struct TogetherExt;
15#[derive(Debug, Default, Clone, Copy)]
16pub struct TogetherExtBuilder;
17
18type TogetherApiKey = BearerAuth;
19
20pub type Client<H = reqwest::Client> = client::Client<TogetherExt, H>;
21pub type ClientBuilder<H = reqwest::Client> =
22    client::ClientBuilder<TogetherExtBuilder, TogetherApiKey, H>;
23
24impl Provider for TogetherExt {
25    type Builder = TogetherExtBuilder;
26
27    const VERIFY_PATH: &'static str = "/models";
28
29    fn build<H>(
30        _: &client::ClientBuilder<Self::Builder, TogetherApiKey, H>,
31    ) -> http_client::Result<Self> {
32        Ok(Self)
33    }
34}
35
36impl<H> Capabilities<H> for TogetherExt {
37    type Completion = Capable<super::CompletionModel<H>>;
38    type Embeddings = Capable<super::EmbeddingModel<H>>;
39
40    type Transcription = Nothing;
41    type ModelListing = Nothing;
42    #[cfg(feature = "image")]
43    type ImageGeneration = Nothing;
44    #[cfg(feature = "audio")]
45    type AudioGeneration = Nothing;
46}
47
48impl ProviderBuilder for TogetherExtBuilder {
49    type Output = TogetherExt;
50    type ApiKey = TogetherApiKey;
51
52    const BASE_URL: &'static str = TOGETHER_AI_BASE_URL;
53}
54
55impl ProviderClient for Client {
56    type Input = String;
57
58    /// Create a new Together AI client from the `TOGETHER_API_KEY` environment variable.
59    /// Panics if the environment variable is not set.
60    fn from_env() -> Self {
61        let api_key = std::env::var("TOGETHER_API_KEY").expect("TOGETHER_API_KEY not set");
62        Self::new(&api_key).unwrap()
63    }
64
65    fn from_val(input: Self::Input) -> Self {
66        Self::new(&input).unwrap()
67    }
68}
69
70pub mod together_ai_api_types {
71    use serde::Deserialize;
72
73    impl ApiErrorResponse {
74        pub fn message(&self) -> String {
75            format!("Code `{}`: {}", self.code, self.error)
76        }
77    }
78
79    #[derive(Debug, Deserialize)]
80    pub struct ApiErrorResponse {
81        pub error: String,
82        pub code: String,
83    }
84
85    #[derive(Debug, Deserialize)]
86    #[serde(untagged)]
87    pub enum ApiResponse<T> {
88        Ok(T),
89        Error(ApiErrorResponse),
90    }
91}
92#[cfg(test)]
93mod tests {
94    #[test]
95    fn test_client_initialization() {
96        let _client: crate::providers::together::Client =
97            crate::providers::together::Client::new("dummy-key").expect("Client::new() failed");
98        let _client_from_builder: crate::providers::together::Client =
99            crate::providers::together::Client::builder()
100                .api_key("dummy-key")
101                .build()
102                .expect("Client::builder() failed");
103    }
104}