async_openai/
uploads.rs

1use crate::{
2    config::Config,
3    error::OpenAIError,
4    types::{AddUploadPartRequest, CompleteUploadRequest, CreateUploadRequest, Upload, UploadPart},
5    Client,
6};
7
8/// Allows you to upload large files in multiple parts.
9pub struct Uploads<'c, C: Config> {
10    client: &'c Client<C>,
11}
12
13impl<'c, C: Config> Uploads<'c, C> {
14    pub fn new(client: &'c Client<C>) -> Self {
15        Self { client }
16    }
17
18    /// Creates an intermediate [Upload](https://platform.openai.com/docs/api-reference/uploads/object) object that
19    /// you can add [Parts](https://platform.openai.com/docs/api-reference/uploads/part-object) to. Currently,
20    /// an Upload can accept at most 8 GB in total and expires after an hour after you create it.
21    ///            
22    /// Once you complete the Upload, we will create a [File](https://platform.openai.com/docs/api-reference/files/object)
23    /// object that contains all the parts you uploaded. This File is usable in the rest of our platform as a regular File object.
24    ///            
25    /// For certain `purpose`s, the correct `mime_type` must be specified. Please refer to documentation for the
26    /// supported MIME types for your use case:
27    /// - [Assistants](https://platform.openai.com/docs/assistants/tools/file-search/supported-files)
28    ///
29    /// For guidance on the proper filename extensions for each purpose, please follow the documentation on
30    /// [creating a File](https://platform.openai.com/docs/api-reference/files/create).
31    #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
32    pub async fn create(&self, request: CreateUploadRequest) -> Result<Upload, OpenAIError> {
33        self.client.post("/uploads", request).await
34    }
35
36    /// Adds a [Part](https://platform.openai.com/docs/api-reference/uploads/part-object) to an
37    /// [Upload](https://platform.openai.com/docs/api-reference/uploads/object) object.
38    /// A Part represents a chunk of bytes from the file you are trying to upload.
39    ///
40    /// Each Part can be at most 64 MB, and you can add Parts until you hit the Upload maximum of 8 GB.
41    ///
42    /// It is possible to add multiple Parts in parallel. You can decide the intended order of the Parts
43    /// when you [complete the Upload](https://platform.openai.com/docs/api-reference/uploads/complete).
44    #[crate::byot(
45        T0 = std::fmt::Display,
46        T1 = Clone,
47        R = serde::de::DeserializeOwned,
48        where_clause =  "reqwest::multipart::Form: crate::traits::AsyncTryFrom<T1, Error = OpenAIError>")]
49    pub async fn add_part(
50        &self,
51        upload_id: &str,
52        request: AddUploadPartRequest,
53    ) -> Result<UploadPart, OpenAIError> {
54        self.client
55            .post_form(&format!("/uploads/{upload_id}/parts"), request)
56            .await
57    }
58
59    /// Completes the [Upload](https://platform.openai.com/docs/api-reference/uploads/object).
60    ///
61    /// Within the returned Upload object, there is a nested [File](https://platform.openai.com/docs/api-reference/files/object)
62    /// object that is ready to use in the rest of the platform.
63    ///
64    /// You can specify the order of the Parts by passing in an ordered list of the Part IDs.
65    ///
66    /// The number of bytes uploaded upon completion must match the number of bytes initially specified
67    /// when creating the Upload object. No Parts may be added after an Upload is completed.
68
69    #[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)]
70    pub async fn complete(
71        &self,
72        upload_id: &str,
73        request: CompleteUploadRequest,
74    ) -> Result<Upload, OpenAIError> {
75        self.client
76            .post(&format!("/uploads/{upload_id}/complete"), request)
77            .await
78    }
79
80    /// Cancels the Upload. No Parts may be added after an Upload is cancelled.
81    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
82    pub async fn cancel(&self, upload_id: &str) -> Result<Upload, OpenAIError> {
83        self.client
84            .post(
85                &format!("/uploads/{upload_id}/cancel"),
86                serde_json::json!({}),
87            )
88            .await
89    }
90}