async_openai_wasm/
file.rs

1use bytes::Bytes;
2use serde::Serialize;
3
4use crate::{
5    Client,
6    config::Config,
7    error::OpenAIError,
8    types::{CreateFileRequest, DeleteFileResponse, ListFilesResponse, OpenAIFile},
9};
10
11/// Files are used to upload documents that can be used with features like Assistants and Fine-tuning.
12pub struct Files<'c, C: Config> {
13    client: &'c Client<C>,
14}
15
16impl<'c, C: Config> Files<'c, C> {
17    pub fn new(client: &'c Client<C>) -> Self {
18        Self { client }
19    }
20
21    /// 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 100 GB.
22    ///
23    /// 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.
24    ///
25    /// 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.
26    ///
27    ///The Batch API only supports `.jsonl` files up to 100 MB in size. The input also has a specific required [format](https://platform.openai.com/docs/api-reference/batch/request-input).
28    ///
29    /// Please [contact us](https://help.openai.com/) if you need to increase these storage limits.
30    #[crate::byot(
31        T0 = Clone,
32        R = serde::de::DeserializeOwned,
33        where_clause =  "reqwest::multipart::Form: crate::traits::AsyncTryFrom<T0, Error = OpenAIError>",
34    )]
35    pub async fn create(&self, request: CreateFileRequest) -> Result<OpenAIFile, OpenAIError> {
36        self.client.post_form("/files", request).await
37    }
38
39    /// Returns a list of files that belong to the user's organization.
40    #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
41    pub async fn list<Q>(&self, query: &Q) -> Result<ListFilesResponse, OpenAIError>
42    where
43        Q: Serialize + ?Sized,
44    {
45        self.client.get_with_query("/files", &query).await
46    }
47
48    /// Returns information about a specific file.
49    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
50    pub async fn retrieve(&self, file_id: &str) -> Result<OpenAIFile, OpenAIError> {
51        self.client.get(format!("/files/{file_id}").as_str()).await
52    }
53
54    /// Delete a file.
55    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
56    pub async fn delete(&self, file_id: &str) -> Result<DeleteFileResponse, OpenAIError> {
57        self.client
58            .delete(format!("/files/{file_id}").as_str())
59            .await
60    }
61
62    /// Returns the contents of the specified file
63    pub async fn content(&self, file_id: &str) -> Result<Bytes, OpenAIError> {
64        self.client
65            .get_raw(format!("/files/{file_id}/content").as_str())
66            .await
67    }
68}