dynamo_async_openai/
file.rs

1// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3//
4// Based on https://github.com/64bit/async-openai/ by Himanshu Neema
5// Original Copyright (c) 2022 Himanshu Neema
6// Licensed under MIT License (see ATTRIBUTIONS-Rust.md)
7//
8// Modifications Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES.
9// Licensed under Apache 2.0
10
11use bytes::Bytes;
12use serde::Serialize;
13
14use crate::{
15    Client,
16    config::Config,
17    error::OpenAIError,
18    types::{CreateFileRequest, DeleteFileResponse, ListFilesResponse, OpenAIFile},
19};
20
21/// Files are used to upload documents that can be used with features like Assistants and Fine-tuning.
22pub struct Files<'c, C: Config> {
23    client: &'c Client<C>,
24}
25
26impl<'c, C: Config> Files<'c, C> {
27    pub fn new(client: &'c Client<C>) -> Self {
28        Self { client }
29    }
30
31    /// 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.
32    ///
33    /// 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.
34    ///
35    /// 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.
36    ///
37    ///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).
38    ///
39    /// Please [contact us](https://help.openai.com/) if you need to increase these storage limits.
40    #[crate::byot(
41        T0 = Clone,
42        R = serde::de::DeserializeOwned,
43        where_clause =  "reqwest::multipart::Form: crate::traits::AsyncTryFrom<T0, Error = OpenAIError>",
44    )]
45    pub async fn create(&self, request: CreateFileRequest) -> Result<OpenAIFile, OpenAIError> {
46        self.client.post_form("/files", request).await
47    }
48
49    /// Returns a list of files that belong to the user's organization.
50    #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
51    pub async fn list<Q>(&self, query: &Q) -> Result<ListFilesResponse, OpenAIError>
52    where
53        Q: Serialize + ?Sized,
54    {
55        self.client.get_with_query("/files", &query).await
56    }
57
58    /// Returns information about a specific file.
59    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
60    pub async fn retrieve(&self, file_id: &str) -> Result<OpenAIFile, OpenAIError> {
61        self.client.get(format!("/files/{file_id}").as_str()).await
62    }
63
64    /// Delete a file.
65    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
66    pub async fn delete(&self, file_id: &str) -> Result<DeleteFileResponse, OpenAIError> {
67        self.client
68            .delete(format!("/files/{file_id}").as_str())
69            .await
70    }
71
72    /// Returns the contents of the specified file
73    pub async fn content(&self, file_id: &str) -> Result<Bytes, OpenAIError> {
74        self.client
75            .get_raw(format!("/files/{file_id}/content").as_str())
76            .await
77    }
78}