async_openai/
file.rs

1use bytes::Bytes;
2
3use crate::{
4    config::Config,
5    error::OpenAIError,
6    types::files::{CreateFileRequest, DeleteFileResponse, ListFilesResponse, OpenAIFile},
7    Client, RequestOptions,
8};
9
10/// Files are used to upload documents that can be used with features like Assistants and Fine-tuning.
11pub struct Files<'c, C: Config> {
12    client: &'c Client<C>,
13    pub(crate) request_options: RequestOptions,
14}
15
16impl<'c, C: Config> Files<'c, C> {
17    pub fn new(client: &'c Client<C>) -> Self {
18        Self {
19            client,
20            request_options: RequestOptions::new(),
21        }
22    }
23
24    /// Upload a file that can be used across various endpoints. Individual files can be up to 512 MB, and the size of all files uploaded by one organization can be up to 1 TB.
25    ///
26    /// The Assistants API supports files up to 2 million tokens and of specific file types. See the [Assistants Tools guide](https://platform.openai.com/docs/assistants/tools) for details.
27    ///
28    /// The Fine-tuning API only supports `.jsonl` files. The input also has certain required formats for fine-tuning [chat](https://platform.openai.com/docs/api-reference/fine-tuning/chat-input) or [completions](https://platform.openai.com/docs/api-reference/fine-tuning/completions-input) models.
29    ///
30    /// The Batch API only supports `.jsonl` files up to 200 MB in size. The input also has a specific required [format](https://platform.openai.com/docs/api-reference/batch/request-input).
31    ///
32    /// Please [contact us](https://help.openai.com/) if you need to increase these storage limits.
33    #[crate::byot(
34        T0 = Clone,
35        R = serde::de::DeserializeOwned,
36        where_clause =  "reqwest::multipart::Form: crate::traits::AsyncTryFrom<T0, Error = OpenAIError>",
37    )]
38    pub async fn create(&self, request: CreateFileRequest) -> Result<OpenAIFile, OpenAIError> {
39        self.client
40            .post_form("/files", request, &self.request_options)
41            .await
42    }
43
44    /// Returns a list of files that belong to the user's organization.
45    #[crate::byot(R = serde::de::DeserializeOwned)]
46    pub async fn list(&self) -> Result<ListFilesResponse, OpenAIError> {
47        self.client.get("/files", &self.request_options).await
48    }
49
50    /// Returns information about a specific file.
51    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
52    pub async fn retrieve(&self, file_id: &str) -> Result<OpenAIFile, OpenAIError> {
53        self.client
54            .get(format!("/files/{file_id}").as_str(), &self.request_options)
55            .await
56    }
57
58    /// Delete a file.
59    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
60    pub async fn delete(&self, file_id: &str) -> Result<DeleteFileResponse, OpenAIError> {
61        self.client
62            .delete(format!("/files/{file_id}").as_str(), &self.request_options)
63            .await
64    }
65
66    /// Returns the contents of the specified file
67    pub async fn content(&self, file_id: &str) -> Result<Bytes, OpenAIError> {
68        let (bytes, _headers) = self
69            .client
70            .get_raw(
71                format!("/files/{file_id}/content").as_str(),
72                &self.request_options,
73            )
74            .await?;
75        Ok(bytes)
76    }
77}
78
79#[cfg(all(test, feature = "file"))]
80mod tests {
81    use crate::{
82        traits::RequestOptionsBuilder,
83        types::files::{
84            CreateFileRequestArgs, FileExpirationAfter, FileExpirationAfterAnchor, FilePurpose,
85        },
86        Client,
87    };
88
89    #[tokio::test]
90    async fn test_file_mod() {
91        let test_file_path = "/tmp/test.jsonl";
92        let contents = concat!(
93            "{\"prompt\": \"<prompt text>\", \"completion\": \"<ideal generated text>\"}\n", // \n is to make it valid jsonl
94            "{\"prompt\": \"<prompt text>\", \"completion\": \"<ideal generated text>\"}"
95        );
96
97        tokio::fs::write(test_file_path, contents).await.unwrap();
98
99        let client = Client::new();
100
101        let request = CreateFileRequestArgs::default()
102            .file(test_file_path)
103            .purpose(FilePurpose::FineTune)
104            .expires_after(FileExpirationAfter {
105                anchor: FileExpirationAfterAnchor::CreatedAt,
106                seconds: 3600,
107            })
108            .build()
109            .unwrap();
110
111        let openai_file = client.files().create(request).await.unwrap();
112
113        assert_eq!(openai_file.bytes, 135);
114        assert_eq!(openai_file.filename, "test.jsonl");
115        //assert_eq!(openai_file.purpose, "fine-tune");
116
117        //assert_eq!(openai_file.status, Some("processed".to_owned())); // uploaded or processed
118        let query = [("purpose", "fine-tune")];
119
120        let list_files = client.files().query(&query).unwrap().list().await.unwrap();
121
122        assert_eq!(list_files.data.into_iter().last().unwrap(), openai_file);
123
124        let retrieved_file = client.files().retrieve(&openai_file.id).await.unwrap();
125
126        assert_eq!(openai_file.created_at, retrieved_file.created_at);
127        assert_eq!(openai_file.bytes, retrieved_file.bytes);
128        assert_eq!(openai_file.filename, retrieved_file.filename);
129        assert_eq!(openai_file.purpose, retrieved_file.purpose);
130        assert_eq!(openai_file.expires_at, retrieved_file.expires_at);
131
132        /*
133        // "To help mitigate abuse, downloading of fine-tune training files is disabled for free accounts."
134        let retrieved_contents = client.files().retrieve_content(&openai_file.id)
135            .await
136            .unwrap();
137
138        assert_eq!(contents, retrieved_contents);
139        */
140
141        // Sleep to prevent "File is still processing. Check back later."
142        tokio::time::sleep(std::time::Duration::from_secs(15)).await;
143        let delete_response = client.files().delete(&openai_file.id).await.unwrap();
144
145        assert_eq!(openai_file.id, delete_response.id);
146        assert!(delete_response.deleted);
147    }
148
149    // Ensures that list files succeeds if there are no files in account
150    // Prerequisite: No files in account
151    #[tokio::test]
152    async fn test_empty_file_list() {
153        let client = Client::new();
154        let result = client.files().list().await.unwrap();
155        assert!(result.data.is_empty());
156    }
157}