async_openai/types/
upload.rs

1use crate::error::OpenAIError;
2use derive_builder::Builder;
3use serde::{Deserialize, Serialize};
4
5use super::{InputSource, OpenAIFile};
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
33/// The intended purpose of the uploaded file.
34#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
35#[serde(rename_all = "snake_case")]
36pub enum UploadPurpose {
37    /// For use with Assistants and Message files
38    Assistants,
39    /// For Assistants image file inputs
40    Vision,
41    /// For use with the Batch API
42    Batch,
43    /// For use with Fine-tuning
44    #[default]
45    FineTune,
46}
47
48/// The Upload object can accept byte chunks in the form of Parts.
49#[derive(Debug, Serialize, Deserialize)]
50pub struct Upload {
51    /// The Upload unique identifier, which can be referenced in API endpoints
52    pub id: String,
53
54    /// The Unix timestamp (in seconds) for when the Upload was created
55    pub created_at: u32,
56
57    /// The name of the file to be uploaded
58    pub filename: String,
59
60    /// The intended number of bytes to be uploaded
61    pub bytes: u64,
62
63    /// The intended purpose of the file. [Pelase refer here]([Please refer here](/docs/api-reference/files/object#files/object-purpose) for acceptable values.)
64    pub purpose: UploadPurpose,
65
66    /// The status of the Upload.
67    pub status: UploadStatus,
68
69    /// The Unix timestamp (in seconds) for when the Upload was created
70    pub expires_at: u32,
71
72    /// The object type, which is always "upload"
73    pub object: String,
74
75    /// The ready File object after the Upload is completed
76    #[serde(skip_serializing_if = "Option::is_none")]
77    pub file: Option<OpenAIFile>,
78}
79
80/// The status of an upload
81#[derive(Debug, Serialize, Deserialize)]
82#[serde(rename_all = "lowercase")]
83pub enum UploadStatus {
84    /// Upload is pending
85    Pending,
86    /// Upload has completed successfully
87    Completed,
88    /// Upload was cancelled
89    Cancelled,
90    /// Upload has expired
91    Expired,
92}
93
94/// The upload Part represents a chunk of bytes we can add to an Upload object.
95#[derive(Debug, Serialize, Deserialize)]
96pub struct UploadPart {
97    /// The upload Part unique identifier, which can be referenced in API endpoints
98    pub id: String,
99
100    /// The Unix timestamp (in seconds) for when the Part was created
101    pub created_at: u32,
102
103    /// The ID of the Upload object that this Part was added to
104    pub upload_id: String,
105
106    /// The object type, which is always `upload.part`
107    pub object: String,
108}
109
110/// Request parameters for adding a part to an Upload
111#[derive(Debug, Clone)]
112pub struct AddUploadPartRequest {
113    /// The chunk of bytes for this Part
114    pub data: InputSource,
115}
116
117/// Request parameters for completing an Upload
118#[derive(Debug, Serialize)]
119pub struct CompleteUploadRequest {
120    /// The ordered list of Part IDs
121    pub part_ids: Vec<String>,
122
123    /// The optional md5 checksum for the file contents to verify if the bytes uploaded matches what you expect
124    #[serde(skip_serializing_if = "Option::is_none")]
125    pub md5: Option<String>,
126}