1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//! Files are used to upload documents that can be used with features like [Fine-tuning](https://platform.openai.com/docs/api-reference/fine-tunes).
use crate::client::OpenAI;
use crate::interfaces::files;
use crate::shared::response_wrapper::OpenAIResponse;
use reqwest::multipart::Form;
pub struct Files<'a> {
openai: &'a OpenAI,
}
impl<'a> Files<'a> {
pub fn new(openai: &'a OpenAI) -> Self {
Self { openai }
}
/// Returns a list of files that belong to the user's organization.
pub async fn list(&self) -> OpenAIResponse<files::FileListResponse> {
self.openai.get("/files", &()).await
}
/// Upload a file that contains document(s) to be used across various endpoints/features.
/// Currently, the size of all the files uploaded by one organization can be up to 1 GB.
/// Please contact us if you need to increase the storage limit.
pub async fn upload(
&self,
req: &files::UploadFileRequest,
) -> OpenAIResponse<files::FileResponse> {
let file_part = reqwest::multipart::Part::stream(req.file.buffer.clone())
.file_name(req.file.filename.clone())
.mime_str("application/octet-stream")
.unwrap();
let form = Form::new()
.part("file", file_part)
.text("purpose", req.purpose.to_string());
self.openai.post_form("/files", form).await
}
/// Delete a file.
///
/// # Path parameters
///
/// - `file_id` - The ID of the file to use for this request
pub async fn delete(&self, file_id: &str) -> OpenAIResponse<files::DeleteFileResponse> {
self.openai.delete(&format!("/files/{file_id}"), &()).await
}
/// Returns information about a specific file.
///
/// # Path parameters
///
/// - `file_id` - The ID of the file to use for this request
pub async fn retrieve(&self, file_id: &str) -> OpenAIResponse<files::FileResponse> {
self.openai.get(&format!("/files/{file_id}"), &()).await
}
/// Returns the contents of the specified file.
///
/// # Path parameters
///
/// - `file_id` - The ID of the file to use for this request
///
/// TODO: Since free accounts cannot download fine-tune training files, I have to verify this api until purchase a Plus.
pub async fn retrieve_content(&self, file_id: &str) -> OpenAIResponse<String> {
self.openai
.get(&format!("/files/{file_id}/content"), &())
.await
}
}