Skip to main content

mistral_openapi_client/apis/
audio_transcriptions_api.rs

1/*
2 * Mistral AI API
3 *
4 * Our Chat Completion and Embeddings APIs specification. Create your account on [La Plateforme](https://console.mistral.ai) to get access and read the [docs](https://docs.mistral.ai) to learn how to use it.
5 *
6 * The version of the OpenAPI document: 1.0.0
7 * 
8 * Generated by: https://openapi-generator.tech
9 */
10
11
12use reqwest;
13use serde::{Deserialize, Serialize, de::Error as _};
14use crate::{apis::ResponseContent, models};
15use super::{Error, configuration, ContentType};
16
17
18/// struct for typed errors of method [`audio_api_v1_transcriptions_post`]
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum AudioApiV1TranscriptionsPostError {
22    UnknownValue(serde_json::Value),
23}
24
25/// struct for typed errors of method [`audio_api_v1_transcriptions_post_stream`]
26#[derive(Debug, Clone, Serialize, Deserialize)]
27#[serde(untagged)]
28pub enum AudioApiV1TranscriptionsPostStreamError {
29    UnknownValue(serde_json::Value),
30}
31
32
33pub async fn audio_api_v1_transcriptions_post(configuration: &configuration::Configuration, model: &str, file: Option<std::path::PathBuf>, file_url: Option<&str>, file_id: Option<&str>, language: Option<&str>, temperature: Option<f64>, stream: Option<bool>, diarize: Option<bool>, context_bias: Option<Vec<String>>, timestamp_granularities: Option<Vec<models::TimestampGranularity>>) -> Result<models::TranscriptionResponse, Error<AudioApiV1TranscriptionsPostError>> {
34    // add a prefix to parameters to efficiently prevent name collisions
35    let p_form_model = model;
36    let p_form_file = file;
37    let p_form_file_url = file_url;
38    let p_form_file_id = file_id;
39    let p_form_language = language;
40    let p_form_temperature = temperature;
41    let p_form_stream = stream;
42    let p_form_diarize = diarize;
43    let p_form_context_bias = context_bias;
44    let p_form_timestamp_granularities = timestamp_granularities;
45
46    let uri_str = format!("{}/v1/audio/transcriptions", configuration.base_path);
47    let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
48
49    if let Some(ref user_agent) = configuration.user_agent {
50        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
51    }
52    if let Some(ref token) = configuration.bearer_access_token {
53        req_builder = req_builder.bearer_auth(token.to_owned());
54    };
55    let mut multipart_form = reqwest::multipart::Form::new();
56    multipart_form = multipart_form.text("model", p_form_model.to_string());
57    if let Some(param_value) = p_form_file {
58        multipart_form = multipart_form.text("file", serde_json::to_string(&param_value)?);
59    }
60    if let Some(param_value) = p_form_file_url {
61        multipart_form = multipart_form.text("file_url", param_value.to_string());
62    }
63    if let Some(param_value) = p_form_file_id {
64        multipart_form = multipart_form.text("file_id", param_value.to_string());
65    }
66    if let Some(param_value) = p_form_language {
67        multipart_form = multipart_form.text("language", param_value.to_string());
68    }
69    if let Some(param_value) = p_form_temperature {
70        multipart_form = multipart_form.text("temperature", param_value.to_string());
71    }
72    if let Some(param_value) = p_form_stream {
73        multipart_form = multipart_form.text("stream", param_value.to_string());
74    }
75    if let Some(param_value) = p_form_diarize {
76        multipart_form = multipart_form.text("diarize", param_value.to_string());
77    }
78    if let Some(param_value) = p_form_context_bias {
79        multipart_form = multipart_form.text("context_bias", serde_json::to_string(&param_value)?);
80    }
81    if let Some(param_value) = p_form_timestamp_granularities {
82        multipart_form = multipart_form.text("timestamp_granularities", serde_json::to_string(&param_value)?);
83    }
84    req_builder = req_builder.multipart(multipart_form);
85
86    let req = req_builder.build()?;
87    let resp = configuration.client.execute(req).await?;
88
89    let status = resp.status();
90    let content_type = resp
91        .headers()
92        .get("content-type")
93        .and_then(|v| v.to_str().ok())
94        .unwrap_or("application/octet-stream");
95    let content_type = super::ContentType::from(content_type);
96
97    if !status.is_client_error() && !status.is_server_error() {
98        let content = resp.text().await?;
99        match content_type {
100            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
101            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::TranscriptionResponse`"))),
102            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::TranscriptionResponse`")))),
103        }
104    } else {
105        let content = resp.text().await?;
106        let entity: Option<AudioApiV1TranscriptionsPostError> = serde_json::from_str(&content).ok();
107        Err(Error::ResponseError(ResponseContent { status, content, entity }))
108    }
109}
110
111pub async fn audio_api_v1_transcriptions_post_stream(configuration: &configuration::Configuration, model: &str, file: Option<std::path::PathBuf>, file_url: Option<&str>, file_id: Option<&str>, language: Option<&str>, temperature: Option<f64>, stream: Option<bool>, diarize: Option<bool>, context_bias: Option<Vec<String>>, timestamp_granularities: Option<Vec<models::TimestampGranularity>>) -> Result<models::TranscriptionStreamEvents, Error<AudioApiV1TranscriptionsPostStreamError>> {
112    // add a prefix to parameters to efficiently prevent name collisions
113    let p_form_model = model;
114    let p_form_file = file;
115    let p_form_file_url = file_url;
116    let p_form_file_id = file_id;
117    let p_form_language = language;
118    let p_form_temperature = temperature;
119    let p_form_stream = stream;
120    let p_form_diarize = diarize;
121    let p_form_context_bias = context_bias;
122    let p_form_timestamp_granularities = timestamp_granularities;
123
124    let uri_str = format!("{}/v1/audio/transcriptions#stream", configuration.base_path);
125    let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
126
127    if let Some(ref user_agent) = configuration.user_agent {
128        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
129    }
130    if let Some(ref token) = configuration.bearer_access_token {
131        req_builder = req_builder.bearer_auth(token.to_owned());
132    };
133    let mut multipart_form = reqwest::multipart::Form::new();
134    multipart_form = multipart_form.text("model", p_form_model.to_string());
135    if let Some(param_value) = p_form_file {
136        multipart_form = multipart_form.text("file", serde_json::to_string(&param_value)?);
137    }
138    if let Some(param_value) = p_form_file_url {
139        multipart_form = multipart_form.text("file_url", param_value.to_string());
140    }
141    if let Some(param_value) = p_form_file_id {
142        multipart_form = multipart_form.text("file_id", param_value.to_string());
143    }
144    if let Some(param_value) = p_form_language {
145        multipart_form = multipart_form.text("language", param_value.to_string());
146    }
147    if let Some(param_value) = p_form_temperature {
148        multipart_form = multipart_form.text("temperature", param_value.to_string());
149    }
150    if let Some(param_value) = p_form_stream {
151        multipart_form = multipart_form.text("stream", param_value.to_string());
152    }
153    if let Some(param_value) = p_form_diarize {
154        multipart_form = multipart_form.text("diarize", param_value.to_string());
155    }
156    if let Some(param_value) = p_form_context_bias {
157        multipart_form = multipart_form.text("context_bias", serde_json::to_string(&param_value)?);
158    }
159    if let Some(param_value) = p_form_timestamp_granularities {
160        multipart_form = multipart_form.text("timestamp_granularities", serde_json::to_string(&param_value)?);
161    }
162    req_builder = req_builder.multipart(multipart_form);
163
164    let req = req_builder.build()?;
165    let resp = configuration.client.execute(req).await?;
166
167    let status = resp.status();
168    let content_type = resp
169        .headers()
170        .get("content-type")
171        .and_then(|v| v.to_str().ok())
172        .unwrap_or("application/octet-stream");
173    let content_type = super::ContentType::from(content_type);
174
175    if !status.is_client_error() && !status.is_server_error() {
176        let content = resp.text().await?;
177        match content_type {
178            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
179            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::TranscriptionStreamEvents`"))),
180            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::TranscriptionStreamEvents`")))),
181        }
182    } else {
183        let content = resp.text().await?;
184        let entity: Option<AudioApiV1TranscriptionsPostStreamError> = serde_json::from_str(&content).ok();
185        Err(Error::ResponseError(ResponseContent { status, content, entity }))
186    }
187}
188