async_openai_alt/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 pub async fn create(&self, request: CreateUploadRequest) -> Result<Upload, OpenAIError> {
32 self.client.post("/uploads", request).await
33 }
34
35 /// Adds a [Part](https://platform.openai.com/docs/api-reference/uploads/part-object) to an
36 /// [Upload](https://platform.openai.com/docs/api-reference/uploads/object) object.
37 /// A Part represents a chunk of bytes from the file you are trying to upload.
38 ///
39 /// Each Part can be at most 64 MB, and you can add Parts until you hit the Upload maximum of 8 GB.
40 ///
41 /// It is possible to add multiple Parts in parallel. You can decide the intended order of the Parts
42 /// when you [complete the Upload](https://platform.openai.com/docs/api-reference/uploads/complete).
43 pub async fn add_part(
44 &self,
45 upload_id: &str,
46 request: AddUploadPartRequest,
47 ) -> Result<UploadPart, OpenAIError> {
48 self.client
49 .post_form(&format!("/uploads/{upload_id}/parts"), request)
50 .await
51 }
52
53 /// Completes the [Upload](https://platform.openai.com/docs/api-reference/uploads/object).
54 ///
55 /// Within the returned Upload object, there is a nested [File](https://platform.openai.com/docs/api-reference/files/object)
56 /// object that is ready to use in the rest of the platform.
57 ///
58 /// You can specify the order of the Parts by passing in an ordered list of the Part IDs.
59 ///
60 /// The number of bytes uploaded upon completion must match the number of bytes initially specified
61 /// when creating the Upload object. No Parts may be added after an Upload is completed.
62 pub async fn complete(
63 &self,
64 upload_id: &str,
65 request: CompleteUploadRequest,
66 ) -> Result<Upload, OpenAIError> {
67 self.client
68 .post(&format!("/uploads/{upload_id}/complete"), request)
69 .await
70 }
71
72 /// Cancels the Upload. No Parts may be added after an Upload is cancelled.
73 pub async fn cancel(&self, upload_id: &str) -> Result<Upload, OpenAIError> {
74 self.client
75 .post(
76 &format!("/uploads/{upload_id}/cancel"),
77 serde_json::json!({}),
78 )
79 .await
80 }
81}