async_openai/
uploads.rs

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