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::billing::Rate;
11use crate::error::Result;
12use crate::http::{client, encode, ok};
13
14#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
16#[serde(rename_all = "camelCase")]
17pub struct ProviderConfig {
18 #[serde(default, skip_serializing_if = "Option::is_none")]
19 pub api_key: Option<String>,
20 #[serde(default, skip_serializing_if = "Option::is_none")]
21 pub system_prompt: Option<String>,
22 #[serde(default, skip_serializing_if = "Option::is_none")]
23 pub temperature: Option<f64>,
24}
25
26#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
27pub struct Garden {
28 #[serde(default)]
29 pub providers: HashMap<String, ProviderConfig>,
30 #[serde(default)]
31 pub memories: u32,
32}
33
34#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
36pub struct PartialGarden {
37 #[serde(default, skip_serializing_if = "Option::is_none")]
38 pub providers: Option<HashMap<String, ProviderConfig>>,
39 #[serde(default, skip_serializing_if = "Option::is_none")]
40 pub memories: Option<u32>,
41}
42
43#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
45#[serde(rename_all = "camelCase")]
46pub struct GardenUpdate {
47 #[serde(flatten)]
48 pub garden: PartialGarden,
49 #[serde(default, skip_serializing_if = "Option::is_none")]
50 pub provider: Option<String>,
51 #[serde(default, skip_serializing_if = "Option::is_none")]
52 pub provider_config: Option<ProviderConfig>,
53}
54
55impl Deref for GardenUpdate {
56 type Target = PartialGarden;
57 fn deref(&self) -> &PartialGarden {
58 &self.garden
59 }
60}
61
62#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
64pub struct LongContext {
65 pub above: f64,
66 pub rates: Vec<Rate>,
67}
68
69#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
72#[serde(rename_all = "lowercase")]
73pub enum ModelKind {
74 Text,
75 Embedding,
76 Decision,
77 Image,
78 Video,
79 Speech,
80 Transcription,
81 Voice,
82}
83
84#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
87#[serde(rename_all = "camelCase")]
88pub struct ModelPricing {
89 pub model: String,
90 pub kind: ModelKind,
91 pub rates: Vec<Rate>,
92 #[serde(default, skip_serializing_if = "Option::is_none")]
93 pub long_context: Option<LongContext>,
94}
95
96#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
98pub struct Pricing {
99 pub models: Vec<ModelPricing>,
100}
101
102pub async fn pricing() -> Result<Pricing> {
104 let response = client()
105 .get(format!("{}/parrot/pricing", super::get_url()))
106 .send()
107 .await?;
108 Ok(ok(response).await?.json().await?)
109}
110
111pub async fn get_dimensions(model: Embedding) -> Result<u32> {
112 let response = client()
113 .get(format!(
114 "{}/routing/dimensions/{}",
115 super::get_url(),
116 encode(model.as_str())
117 ))
118 .send()
119 .await?;
120 let text = ok(response).await?.text().await?;
121 Ok(text.trim().parse::<f64>().unwrap_or(f64::NAN) as u32)
122}
123
124#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
125#[serde(rename_all = "camelCase")]
126pub struct CatalogModel {
127 pub model: String,
128 pub name: String,
129 pub provider: String,
130 #[serde(rename = "baseURL")]
131 pub base_url: String,
132 pub icon: String,
133 #[serde(default, skip_serializing_if = "Option::is_none")]
134 pub kind: Option<String>,
135 #[serde(default, skip_serializing_if = "Option::is_none")]
136 pub dimensions: Option<u32>,
137 #[serde(default, skip_serializing_if = "Option::is_none")]
138 pub reasoning: Option<Vec<String>>,
139 #[serde(default, skip_serializing_if = "Option::is_none")]
141 pub demo: Option<bool>,
142 #[serde(default, skip_serializing_if = "Option::is_none")]
144 pub images: Option<bool>,
145 #[serde(default, skip_serializing_if = "Option::is_none")]
146 pub max_questions: Option<u32>,
147 #[serde(default, skip_serializing_if = "Option::is_none")]
148 pub max_options: Option<u32>,
149 #[serde(default, skip_serializing_if = "Option::is_none")]
150 pub input_context_window: Option<u64>,
151}
152
153#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
155pub struct Catalog {
156 #[serde(default)]
157 pub text: Vec<CatalogModel>,
158 #[serde(default)]
159 pub embedding: Vec<CatalogModel>,
160 #[serde(default)]
161 pub image: Vec<CatalogModel>,
162 #[serde(default)]
163 pub video: Vec<CatalogModel>,
164 #[serde(default)]
165 pub speech: Vec<CatalogModel>,
166 #[serde(default)]
167 pub transcription: Vec<CatalogModel>,
168 #[serde(default)]
169 pub voice: Vec<CatalogModel>,
170 #[serde(default)]
171 pub decision: Vec<CatalogModel>,
172}
173
174pub async fn get_all() -> Result<Catalog> {
175 let response = client()
176 .get(format!("{}/routing/all", super::get_url()))
177 .header("Content-Type", "application/json")
178 .send()
179 .await?;
180 Ok(ok(response).await?.json().await?)
181}
182
183fn known<'de, D: Deserializer<'de>, T: DeserializeOwned>(
185 deserializer: D,
186) -> std::result::Result<Option<T>, D::Error> {
187 Ok(serde_json::from_value(Value::deserialize(deserializer)?).ok())
188}
189
190#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
192pub struct LastTextModel {
193 pub model: Text,
194 #[serde(default, skip_serializing_if = "Option::is_none")]
195 pub reasoning: Option<String>,
196}
197
198#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
202pub struct LastUsedModels {
203 #[serde(
204 default,
205 deserialize_with = "known",
206 skip_serializing_if = "Option::is_none"
207 )]
208 pub text: Option<LastTextModel>,
209 #[serde(
210 default,
211 deserialize_with = "known",
212 skip_serializing_if = "Option::is_none"
213 )]
214 pub embedding: Option<Embedding>,
215 #[serde(
216 default,
217 deserialize_with = "known",
218 skip_serializing_if = "Option::is_none"
219 )]
220 pub image: Option<Image>,
221 #[serde(
222 default,
223 deserialize_with = "known",
224 skip_serializing_if = "Option::is_none"
225 )]
226 pub video: Option<Video>,
227 #[serde(
228 default,
229 deserialize_with = "known",
230 skip_serializing_if = "Option::is_none"
231 )]
232 pub speech: Option<Speech>,
233 #[serde(
234 default,
235 deserialize_with = "known",
236 skip_serializing_if = "Option::is_none"
237 )]
238 pub transcription: Option<Transcription>,
239 #[serde(
240 default,
241 deserialize_with = "known",
242 skip_serializing_if = "Option::is_none"
243 )]
244 pub voice: Option<Voice>,
245 #[serde(
246 default,
247 deserialize_with = "known",
248 skip_serializing_if = "Option::is_none"
249 )]
250 pub decision: Option<Decision>,
251}
252
253pub async fn get_last_used_models(authorization: &CredentialsOrApiKey) -> Result<LastUsedModels> {
257 let response = client()
258 .get(format!("{}/routing/last", super::get_url()))
259 .header(
260 "Authorization",
261 match authorization {
262 CredentialsOrApiKey::Credentials(credentials) => {
263 format!("Token {}", credentials.token)
264 }
265 CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
266 },
267 )
268 .send()
269 .await?;
270 Ok(ok(response).await?.json().await?)
271}
272
273pub async fn set_last_used_models(
277 authorization: &CredentialsOrApiKey,
278 models: &LastUsedModels,
279) -> Result<()> {
280 let response = client()
281 .put(format!("{}/routing/last", super::get_url()))
282 .header(
283 "Authorization",
284 match authorization {
285 CredentialsOrApiKey::Credentials(credentials) => {
286 format!("Token {}", credentials.token)
287 }
288 CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
289 },
290 )
291 .json(models)
292 .send()
293 .await?;
294 ok(response).await?;
295 Ok(())
296}
297
298#[derive(Debug, Clone, Copy, Default, PartialEq, Serialize, Deserialize)]
300pub struct TokenCost {
301 #[serde(default)]
302 pub input: f64,
303 #[serde(default)]
304 pub output: f64,
305}
306
307#[derive(Debug, Clone, Copy, Default, PartialEq, Serialize, Deserialize)]
309pub struct Cost {
310 #[serde(default)]
311 pub cost: f64,
312}
313
314#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
316pub struct ModelUsage {
317 #[serde(default)]
318 pub text: Vec<(String, String, TokenCost)>,
319 #[serde(default)]
320 pub embedding: Vec<(String, String, Cost)>,
321 #[serde(default)]
322 pub image: Vec<(String, String, Cost)>,
323 #[serde(default)]
324 pub video: Vec<(String, String, Cost)>,
325 #[serde(default)]
326 pub speech: Vec<(String, String, Cost)>,
327 #[serde(default)]
328 pub transcription: Vec<(String, String, Cost)>,
329 #[serde(default)]
330 pub voice: Vec<(String, String, Cost)>,
331 #[serde(default)]
332 pub decision: Vec<(String, String, Cost)>,
333}
334
335pub async fn get_usage(authorization: &CredentialsOrApiKey) -> Result<ModelUsage> {
336 let response = client()
337 .get(format!("{}/routing/usage", super::get_url()))
338 .header(
339 "Authorization",
340 match authorization {
341 CredentialsOrApiKey::Credentials(credentials) => {
342 format!("Token {}", credentials.token)
343 }
344 CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
345 },
346 )
347 .send()
348 .await?;
349 Ok(ok(response).await?.json().await?)
350}
351
352pub async fn get_garden(authorization: &CredentialsOrApiKey) -> Result<Garden> {
353 let response = client()
354 .get(format!("{}/routing/garden", super::get_url()))
355 .header(
356 "Authorization",
357 match authorization {
358 CredentialsOrApiKey::Credentials(credentials) => {
359 format!("Token {}", credentials.token)
360 }
361 CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
362 },
363 )
364 .send()
365 .await?;
366 Ok(ok(response).await?.json().await?)
367}
368
369pub async fn set_garden(authorization: &CredentialsOrApiKey, update: &GardenUpdate) -> Result<()> {
370 let response = client()
371 .put(format!("{}/routing/garden", super::get_url()))
372 .header(
373 "Authorization",
374 match authorization {
375 CredentialsOrApiKey::Credentials(credentials) => {
376 format!("Token {}", credentials.token)
377 }
378 CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
379 },
380 )
381 .json(update)
382 .send()
383 .await?;
384 ok(response).await?;
385 Ok(())
386}