async_openai_wasm/
file.rs

1use bytes::Bytes;
2
3use crate::{
4    Client, RequestOptions,
5    config::Config,
6    error::OpenAIError,
7    types::files::{CreateFileRequest, DeleteFileResponse, ListFilesResponse, OpenAIFile},
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}