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