Skip to main content

outfox_openai/spec/uploads/
upload.rs

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