async_openai/types/uploads/upload.rs
1use crate::{error::OpenAIError, types::files::FileExpirationAfter};
2use derive_builder::Builder;
3use serde::{Deserialize, Serialize};
4
5use crate::types::{files::OpenAIFile, InputSource};
6
7/// Request to create an upload object that can accept byte chunks in the form of Parts.
8#[derive(Clone, Serialize, Default, Debug, Deserialize, Builder, PartialEq)]
9#[builder(name = "CreateUploadRequestArgs")]
10#[builder(pattern = "mutable")]
11#[builder(setter(into, strip_option), default)]
12#[builder(derive(Debug))]
13#[builder(build_fn(error = "OpenAIError"))]
14pub struct CreateUploadRequest {
15 /// The name of the file to upload.
16 pub filename: String,
17
18 /// The intended purpose of the uploaded file.
19 ///
20 /// See the [documentation on File purposes](https://platform.openai.com/docs/api-reference/files/create#files-create-purpose).
21 pub purpose: UploadPurpose,
22
23 /// The number of bytes in the file you are uploading.
24 pub bytes: u64,
25
26 /// The MIME type of the file.
27 ///
28 /// This must fall within the supported MIME types for your file purpose. See the supported MIME
29 /// types for assistants and vision.
30 pub mime_type: String,
31
32 /// The expiration policy for a file. By default, files with `purpose=batch` expire after 30 days and all
33 /// other files are persisted until they are manually deleted.
34 pub expires_after: Option<FileExpirationAfter>,
35}
36
37/// The intended purpose of the uploaded file.
38#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
39pub enum UploadPurpose {
40 /// For use with Assistants and Message files
41 #[serde(rename = "assistants")]
42 Assistants,
43 /// For Assistants image file inputs
44 #[serde(rename = "vision")]
45 Vision,
46 /// For use with the Batch API
47 #[serde(rename = "batch")]
48 Batch,
49 /// For use with Fine-tuning
50 #[default]
51 #[serde(rename = "fine-tune")]
52 FineTune,
53}
54
55/// The Upload object can accept byte chunks in the form of Parts.
56#[derive(Debug, Serialize, Deserialize)]
57pub struct Upload {
58 /// The Upload unique identifier, which can be referenced in API endpoints
59 pub id: String,
60
61 /// The Unix timestamp (in seconds) for when the Upload was created
62 pub created_at: u32,
63
64 /// The name of the file to be uploaded
65 pub filename: String,
66
67 /// The intended number of bytes to be uploaded
68 pub bytes: u64,
69
70 /// The intended purpose of the file. [Please refer here](https://platform.openai.com/docs/api-reference/files/object#files/object-purpose) for acceptable values.
71 pub purpose: UploadPurpose,
72
73 /// The status of the Upload.
74 pub status: UploadStatus,
75
76 /// The Unix timestamp (in seconds) for when the Upload will expire
77 pub expires_at: u32,
78
79 /// The object type, which is always "upload"
80 pub object: String,
81
82 /// The ready File object after the Upload is completed
83 #[serde(skip_serializing_if = "Option::is_none")]
84 pub file: Option<OpenAIFile>,
85}
86
87/// The status of an upload
88#[derive(Debug, Serialize, Deserialize)]
89#[serde(rename_all = "lowercase")]
90pub enum UploadStatus {
91 /// Upload is pending
92 Pending,
93 /// Upload has completed successfully
94 Completed,
95 /// Upload was cancelled
96 Cancelled,
97 /// Upload has expired
98 Expired,
99}
100
101/// The upload Part represents a chunk of bytes we can add to an Upload object.
102#[derive(Debug, Serialize, Deserialize)]
103pub struct UploadPart {
104 /// The upload Part unique identifier, which can be referenced in API endpoints
105 pub id: String,
106
107 /// The Unix timestamp (in seconds) for when the Part was created
108 pub created_at: u32,
109
110 /// The ID of the Upload object that this Part was added to
111 pub upload_id: String,
112
113 /// The object type, which is always `upload.part`
114 pub object: String,
115}
116
117/// Request parameters for adding a part to an Upload
118#[derive(Debug, Clone)]
119pub struct AddUploadPartRequest {
120 /// The chunk of bytes for this Part
121 pub data: InputSource,
122}
123
124/// Request parameters for completing an Upload
125#[derive(Debug, Serialize)]
126pub struct CompleteUploadRequest {
127 /// The ordered list of Part IDs
128 pub part_ids: Vec<String>,
129
130 /// The optional md5 checksum for the file contents to verify if the bytes uploaded matches what you expect
131 #[serde(skip_serializing_if = "Option::is_none")]
132 pub md5: Option<String>,
133}