rs_openai/apis/
files.rs

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