Skip to main content

fauna_api/parrot/
routing.rs

1use std::collections::HashMap;
2use std::ops::Deref;
3
4use serde::de::DeserializeOwned;
5use serde::{Deserialize, Deserializer, Serialize};
6use serde_json::Value;
7
8use super::generative::{Decision, Embedding, Image, Speech, Text, Transcription, Video, Voice};
9use crate::authentification::CredentialsOrApiKey;
10use crate::error::Result;
11use crate::http::{client, encode, ok};
12
13/// One provider's settings in a garden.
14#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
15#[serde(rename_all = "camelCase")]
16pub struct ProviderConfig {
17    #[serde(default, skip_serializing_if = "Option::is_none")]
18    pub api_key: Option<String>,
19    #[serde(default, skip_serializing_if = "Option::is_none")]
20    pub system_prompt: Option<String>,
21    #[serde(default, skip_serializing_if = "Option::is_none")]
22    pub temperature: Option<f64>,
23}
24
25#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
26pub struct Garden {
27    #[serde(default)]
28    pub providers: HashMap<String, ProviderConfig>,
29    #[serde(default)]
30    pub memories: u32,
31}
32
33/// `Partial<Garden>` in the TypeScript SDK.
34#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
35pub struct PartialGarden {
36    #[serde(default, skip_serializing_if = "Option::is_none")]
37    pub providers: Option<HashMap<String, ProviderConfig>>,
38    #[serde(default, skip_serializing_if = "Option::is_none")]
39    pub memories: Option<u32>,
40}
41
42/// `Partial<Garden> & { provider?, providerConfig? }` in the TypeScript SDK.
43#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
44#[serde(rename_all = "camelCase")]
45pub struct GardenUpdate {
46    #[serde(flatten)]
47    pub garden: PartialGarden,
48    #[serde(default, skip_serializing_if = "Option::is_none")]
49    pub provider: Option<String>,
50    #[serde(default, skip_serializing_if = "Option::is_none")]
51    pub provider_config: Option<ProviderConfig>,
52}
53
54impl Deref for GardenUpdate {
55    type Target = PartialGarden;
56    fn deref(&self) -> &PartialGarden {
57        &self.garden
58    }
59}
60
61/// `Text | Embedding | Image | Video | Speech | Transcription | Voice | Decision`
62/// in the TypeScript SDK: any Parrot model.
63#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
64#[serde(untagged)]
65pub enum TextOrEmbeddingOrImageOrVideoOrSpeechOrTranscriptionOrVoiceOrDecision {
66    Text(Text),
67    Embedding(Embedding),
68    Image(Image),
69    Video(Video),
70    Speech(Speech),
71    Transcription(Transcription),
72    Voice(Voice),
73    Decision(Decision),
74}
75
76impl TextOrEmbeddingOrImageOrVideoOrSpeechOrTranscriptionOrVoiceOrDecision {
77    fn as_str(self) -> &'static str {
78        match self {
79            Self::Text(model) => model.as_str(),
80            Self::Embedding(model) => model.as_str(),
81            Self::Image(model) => model.as_str(),
82            Self::Video(model) => model.as_str(),
83            Self::Speech(model) => model.as_str(),
84            Self::Transcription(model) => model.as_str(),
85            Self::Voice(model) => model.as_str(),
86            Self::Decision(model) => model.as_str(),
87        }
88    }
89}
90
91macro_rules! any_model {
92    ($($variant:ident),*) => {
93        $(
94            impl From<$variant> for TextOrEmbeddingOrImageOrVideoOrSpeechOrTranscriptionOrVoiceOrDecision {
95                fn from(model: $variant) -> Self {
96                    Self::$variant(model)
97                }
98            }
99        )*
100    };
101}
102
103any_model!(
104    Text,
105    Embedding,
106    Image,
107    Video,
108    Speech,
109    Transcription,
110    Voice,
111    Decision
112);
113
114/// `{ inputPer1M?, outputPer1M?, perUnit?, unit? }` in the TypeScript SDK.
115#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
116pub struct Price {
117    #[serde(
118        default,
119        rename = "inputPer1M",
120        skip_serializing_if = "Option::is_none"
121    )]
122    pub input_per_1m: Option<f64>,
123    #[serde(
124        default,
125        rename = "outputPer1M",
126        skip_serializing_if = "Option::is_none"
127    )]
128    pub output_per_1m: Option<f64>,
129    #[serde(default, rename = "perUnit", skip_serializing_if = "Option::is_none")]
130    pub per_unit: Option<f64>,
131    #[serde(default, skip_serializing_if = "Option::is_none")]
132    pub unit: Option<String>,
133}
134
135pub async fn get_price(
136    model: impl Into<TextOrEmbeddingOrImageOrVideoOrSpeechOrTranscriptionOrVoiceOrDecision>,
137) -> Result<Price> {
138    let response = client()
139        .get(format!(
140            "{}/routing/price/{}",
141            super::get_url(),
142            encode(model.into().as_str())
143        ))
144        .send()
145        .await?;
146    Ok(ok(response).await?.json().await?)
147}
148
149pub async fn get_dimensions(model: Embedding) -> Result<u32> {
150    let response = client()
151        .get(format!(
152            "{}/routing/dimensions/{}",
153            super::get_url(),
154            encode(model.as_str())
155        ))
156        .send()
157        .await?;
158    let text = ok(response).await?.text().await?;
159    Ok(text.trim().parse::<f64>().unwrap_or(f64::NAN) as u32)
160}
161
162#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
163#[serde(rename_all = "camelCase")]
164pub struct CatalogModel {
165    pub model: String,
166    pub name: String,
167    pub provider: String,
168    #[serde(rename = "baseURL")]
169    pub base_url: String,
170    pub icon: String,
171    #[serde(
172        default,
173        rename = "inputPer1M",
174        skip_serializing_if = "Option::is_none"
175    )]
176    pub input_per_1m: Option<f64>,
177    #[serde(
178        default,
179        rename = "outputPer1M",
180        skip_serializing_if = "Option::is_none"
181    )]
182    pub output_per_1m: Option<f64>,
183    #[serde(
184        default,
185        rename = "cachedInputPer1M",
186        skip_serializing_if = "Option::is_none"
187    )]
188    pub cached_input_per_1m: Option<f64>,
189    #[serde(default, skip_serializing_if = "Option::is_none")]
190    pub per_unit: Option<f64>,
191    #[serde(default, skip_serializing_if = "Option::is_none")]
192    pub unit: Option<String>,
193    #[serde(default, skip_serializing_if = "Option::is_none")]
194    pub kind: Option<String>,
195    #[serde(default, skip_serializing_if = "Option::is_none")]
196    pub dimensions: Option<u32>,
197    #[serde(default, skip_serializing_if = "Option::is_none")]
198    pub reasoning: Option<Vec<String>>,
199    /// Decision models: image input and the limits the model accepts.
200    #[serde(default, skip_serializing_if = "Option::is_none")]
201    pub images: Option<bool>,
202    #[serde(default, skip_serializing_if = "Option::is_none")]
203    pub max_questions: Option<u32>,
204    #[serde(default, skip_serializing_if = "Option::is_none")]
205    pub max_options: Option<u32>,
206    #[serde(default, skip_serializing_if = "Option::is_none")]
207    pub input_context_window: Option<u64>,
208}
209
210/// Every model Parrot offers, by family.
211#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
212pub struct Catalog {
213    #[serde(default)]
214    pub text: Vec<CatalogModel>,
215    #[serde(default)]
216    pub embedding: Vec<CatalogModel>,
217    #[serde(default)]
218    pub image: Vec<CatalogModel>,
219    #[serde(default)]
220    pub video: Vec<CatalogModel>,
221    #[serde(default)]
222    pub speech: Vec<CatalogModel>,
223    #[serde(default)]
224    pub transcription: Vec<CatalogModel>,
225    #[serde(default)]
226    pub voice: Vec<CatalogModel>,
227    #[serde(default)]
228    pub decision: Vec<CatalogModel>,
229}
230
231pub async fn get_all() -> Result<Catalog> {
232    let response = client()
233        .get(format!("{}/routing/all", super::get_url()))
234        .header("Content-Type", "application/json")
235        .send()
236        .await?;
237    Ok(ok(response).await?.json().await?)
238}
239
240/// A model id this SDK knows; one it doesn't reads as unset.
241fn known<'de, D: Deserializer<'de>, T: DeserializeOwned>(
242    deserializer: D,
243) -> std::result::Result<Option<T>, D::Error> {
244    Ok(serde_json::from_value(Value::deserialize(deserializer)?).ok())
245}
246
247/// `{ model, reasoning? }` in the TypeScript SDK.
248#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
249pub struct LastTextModel {
250    pub model: Text,
251    #[serde(default, skip_serializing_if = "Option::is_none")]
252    pub reasoning: Option<String>,
253}
254
255/// The models the caller used last, by family.
256#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
257pub struct LastUsedModels {
258    #[serde(
259        default,
260        deserialize_with = "known",
261        skip_serializing_if = "Option::is_none"
262    )]
263    pub text: Option<LastTextModel>,
264    #[serde(
265        default,
266        deserialize_with = "known",
267        skip_serializing_if = "Option::is_none"
268    )]
269    pub embedding: Option<Embedding>,
270    #[serde(
271        default,
272        deserialize_with = "known",
273        skip_serializing_if = "Option::is_none"
274    )]
275    pub image: Option<Image>,
276    #[serde(
277        default,
278        deserialize_with = "known",
279        skip_serializing_if = "Option::is_none"
280    )]
281    pub video: Option<Video>,
282    #[serde(
283        default,
284        deserialize_with = "known",
285        skip_serializing_if = "Option::is_none"
286    )]
287    pub speech: Option<Speech>,
288    #[serde(
289        default,
290        deserialize_with = "known",
291        skip_serializing_if = "Option::is_none"
292    )]
293    pub transcription: Option<Transcription>,
294}
295
296pub async fn get_last_used_models(authorization: &CredentialsOrApiKey) -> Result<LastUsedModels> {
297    let response = client()
298        .get(format!("{}/routing/last", super::get_url()))
299        .header(
300            "Authorization",
301            match authorization {
302                CredentialsOrApiKey::Credentials(credentials) => {
303                    format!("Token {}", credentials.token)
304                }
305                CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
306            },
307        )
308        .send()
309        .await?;
310    Ok(ok(response).await?.json().await?)
311}
312
313/// `{ input, output }` in the TypeScript SDK: text tokens spent.
314#[derive(Debug, Clone, Copy, Default, PartialEq, Serialize, Deserialize)]
315pub struct TokenCost {
316    #[serde(default)]
317    pub input: f64,
318    #[serde(default)]
319    pub output: f64,
320}
321
322/// `{ cost }` in the TypeScript SDK.
323#[derive(Debug, Clone, Copy, Default, PartialEq, Serialize, Deserialize)]
324pub struct Cost {
325    #[serde(default)]
326    pub cost: f64,
327}
328
329/// The caller's usage by family: `(model, day, spend)` rows.
330#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
331pub struct ModelUsage {
332    #[serde(default)]
333    pub text: Vec<(String, String, TokenCost)>,
334    #[serde(default)]
335    pub embedding: Vec<(String, String, Cost)>,
336    #[serde(default)]
337    pub image: Vec<(String, String, Cost)>,
338    #[serde(default)]
339    pub video: Vec<(String, String, Cost)>,
340    #[serde(default)]
341    pub speech: Vec<(String, String, Cost)>,
342    #[serde(default)]
343    pub transcription: Vec<(String, String, Cost)>,
344    #[serde(default)]
345    pub voice: Vec<(String, String, Cost)>,
346    #[serde(default)]
347    pub decision: Vec<(String, String, Cost)>,
348}
349
350pub async fn get_usage(authorization: &CredentialsOrApiKey) -> Result<ModelUsage> {
351    let response = client()
352        .get(format!("{}/routing/usage", super::get_url()))
353        .header(
354            "Authorization",
355            match authorization {
356                CredentialsOrApiKey::Credentials(credentials) => {
357                    format!("Token {}", credentials.token)
358                }
359                CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
360            },
361        )
362        .send()
363        .await?;
364    Ok(ok(response).await?.json().await?)
365}
366
367pub async fn get_garden(authorization: &CredentialsOrApiKey) -> Result<Garden> {
368    let response = client()
369        .get(format!("{}/routing/garden", super::get_url()))
370        .header(
371            "Authorization",
372            match authorization {
373                CredentialsOrApiKey::Credentials(credentials) => {
374                    format!("Token {}", credentials.token)
375                }
376                CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
377            },
378        )
379        .send()
380        .await?;
381    Ok(ok(response).await?.json().await?)
382}
383
384pub async fn set_garden(authorization: &CredentialsOrApiKey, update: &GardenUpdate) -> Result<()> {
385    let response = client()
386        .put(format!("{}/routing/garden", super::get_url()))
387        .header(
388            "Authorization",
389            match authorization {
390                CredentialsOrApiKey::Credentials(credentials) => {
391                    format!("Token {}", credentials.token)
392                }
393                CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
394            },
395        )
396        .json(update)
397        .send()
398        .await?;
399    ok(response).await?;
400    Ok(())
401}