rig/providers/together/
client.rs

1use super::{M2_BERT_80M_8K_RETRIEVAL, completion::CompletionModel, embedding::EmbeddingModel};
2use crate::client::{EmbeddingsClient, ProviderClient, impl_conversion_traits};
3use rig::client::CompletionClient;
4
5// ================================================================
6// Together AI Client
7// ================================================================
8const TOGETHER_AI_BASE_URL: &str = "https://api.together.xyz";
9
10#[derive(Clone)]
11pub struct Client {
12    base_url: String,
13    default_headers: reqwest::header::HeaderMap,
14    api_key: String,
15    http_client: reqwest::Client,
16}
17
18impl std::fmt::Debug for Client {
19    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20        f.debug_struct("Client")
21            .field("base_url", &self.base_url)
22            .field("http_client", &self.http_client)
23            .field("default_headers", &self.default_headers)
24            .field("api_key", &"<REDACTED>")
25            .finish()
26    }
27}
28
29impl Client {
30    /// Create a new Together AI client with the given API key.
31    pub fn new(api_key: &str) -> Self {
32        Self::from_url(api_key, TOGETHER_AI_BASE_URL)
33    }
34
35    fn from_url(api_key: &str, base_url: &str) -> Self {
36        let mut default_headers = reqwest::header::HeaderMap::new();
37        default_headers.insert(
38            reqwest::header::CONTENT_TYPE,
39            "application/json".parse().unwrap(),
40        );
41        Self {
42            base_url: base_url.to_string(),
43            api_key: api_key.to_string(),
44            default_headers,
45            http_client: reqwest::Client::builder()
46                .build()
47                .expect("Together AI reqwest client should build"),
48        }
49    }
50
51    /// Use your own `reqwest::Client`.
52    /// The required headers will be automatically attached upon trying to make a request.
53    pub fn with_custom_client(mut self, client: reqwest::Client) -> Self {
54        self.http_client = client;
55
56        self
57    }
58
59    pub fn post(&self, path: &str) -> reqwest::RequestBuilder {
60        let url = format!("{}/{}", self.base_url, path).replace("//", "/");
61
62        tracing::debug!("POST {}", url);
63        self.http_client
64            .post(url)
65            .bearer_auth(&self.api_key)
66            .headers(self.default_headers.clone())
67    }
68}
69
70impl ProviderClient for Client {
71    /// Create a new Together AI client from the `TOGETHER_API_KEY` environment variable.
72    /// Panics if the environment variable is not set.
73    fn from_env() -> Self {
74        let api_key = std::env::var("TOGETHER_API_KEY").expect("TOGETHER_API_KEY not set");
75        Self::new(&api_key)
76    }
77}
78
79impl CompletionClient for Client {
80    type CompletionModel = CompletionModel;
81
82    /// Create a completion model with the given name.
83    fn completion_model(&self, model: &str) -> CompletionModel {
84        CompletionModel::new(self.clone(), model)
85    }
86}
87
88impl EmbeddingsClient for Client {
89    type EmbeddingModel = EmbeddingModel;
90
91    /// Create an embedding model with the given name.
92    /// Note: default embedding dimension of 0 will be used if model is not known.
93    /// If this is the case, it's better to use function `embedding_model_with_ndims`
94    ///
95    /// # Example
96    /// ```
97    /// use rig::providers::together_ai::{Client, self};
98    ///
99    /// // Initialize the Together AI client
100    /// let together_ai = Client::new("your-together-ai-api-key");
101    ///
102    /// let embedding_model = together_ai.embedding_model(together_ai::embedding::EMBEDDING_V1);
103    /// ```
104    fn embedding_model(&self, model: &str) -> EmbeddingModel {
105        let ndims = match model {
106            M2_BERT_80M_8K_RETRIEVAL => 8192,
107            _ => 0,
108        };
109        EmbeddingModel::new(self.clone(), model, ndims)
110    }
111
112    /// Create an embedding model with the given name and the number of dimensions in the embedding
113    /// generated by the model.
114    ///
115    /// # Example
116    /// ```
117    /// use rig::providers::together_ai::{Client, self};
118    ///
119    /// // Initialize the Together AI client
120    /// let together_ai = Client::new("your-together-ai-api-key");
121    ///
122    /// let embedding_model = together_ai.embedding_model_with_ndims("model-unknown-to-rig", 1024);
123    /// ```
124    fn embedding_model_with_ndims(&self, model: &str, ndims: usize) -> EmbeddingModel {
125        EmbeddingModel::new(self.clone(), model, ndims)
126    }
127}
128
129impl_conversion_traits!(AsTranscription, AsImageGeneration, AsAudioGeneration for Client);
130
131pub mod together_ai_api_types {
132    use serde::Deserialize;
133
134    impl ApiErrorResponse {
135        pub fn message(&self) -> String {
136            format!("Code `{}`: {}", self.code, self.error)
137        }
138    }
139
140    #[derive(Debug, Deserialize)]
141    pub struct ApiErrorResponse {
142        pub error: String,
143        pub code: String,
144    }
145
146    #[derive(Debug, Deserialize)]
147    #[serde(untagged)]
148    pub enum ApiResponse<T> {
149        Ok(T),
150        Error(ApiErrorResponse),
151    }
152}