pub struct Files { /* private fields */ }Expand description
Client for interacting with the OpenAI Files API.
This struct provides methods to upload, list, retrieve, delete files,
and get file content. Use Files::new() to create a new instance.
§Providers
The client supports two providers:
- OpenAI: Standard OpenAI API (default)
- Azure: Azure OpenAI Service
§Example
use openai_tools::files::request::{Files, FilePurpose};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let files = Files::new()?;
// Upload a file for fine-tuning
let file = files.upload_path("training_data.jsonl", FilePurpose::FineTune).await?;
println!("Uploaded: {} ({})", file.filename, file.id);
Ok(())
}Implementations§
Source§impl Files
impl Files
Sourcepub fn new() -> Result<Self>
pub fn new() -> Result<Self>
Creates a new Files client for OpenAI API.
Initializes the client by loading the OpenAI API key from
the environment variable OPENAI_API_KEY. Supports .env file loading
via dotenvy.
§Returns
Ok(Files)- A new Files client ready for useErr(OpenAIToolError)- If the API key is not found in the environment
§Example
use openai_tools::files::request::Files;
let files = Files::new().expect("API key should be set");Sourcepub fn with_auth(auth: AuthProvider) -> Self
pub fn with_auth(auth: AuthProvider) -> Self
Creates a new Files client with a custom authentication provider
Sourcepub fn detect_provider() -> Result<Self>
pub fn detect_provider() -> Result<Self>
Creates a new Files client by auto-detecting the provider
Sourcepub fn with_url<S: Into<String>>(base_url: S, api_key: S) -> Self
pub fn with_url<S: Into<String>>(base_url: S, api_key: S) -> Self
Creates a new Files client with URL-based provider detection
Sourcepub fn from_url<S: Into<String>>(url: S) -> Result<Self>
pub fn from_url<S: Into<String>>(url: S) -> Result<Self>
Creates a new Files client from URL using environment variables
Sourcepub fn auth(&self) -> &AuthProvider
pub fn auth(&self) -> &AuthProvider
Returns the authentication provider
Sourcepub fn timeout(&mut self, timeout: Duration) -> &mut Self
pub fn timeout(&mut self, timeout: Duration) -> &mut Self
Sets the request timeout duration.
§Arguments
timeout- The maximum time to wait for a response
§Returns
A mutable reference to self for method chaining
§Example
use std::time::Duration;
use openai_tools::files::request::Files;
let mut files = Files::new().unwrap();
files.timeout(Duration::from_secs(120)); // Longer timeout for file uploadsSourcepub async fn upload_path(
&self,
file_path: &str,
purpose: FilePurpose,
) -> Result<File>
pub async fn upload_path( &self, file_path: &str, purpose: FilePurpose, ) -> Result<File>
Uploads a file from a file path.
The file will be uploaded with the specified purpose. Individual files can be up to 512 MB, and the total size of all files uploaded by one organization can be up to 100 GB.
§Arguments
file_path- Path to the file to uploadpurpose- The intended purpose of the uploaded file
§Returns
Ok(File)- The uploaded file objectErr(OpenAIToolError)- If the file cannot be read or the upload fails
§Example
use openai_tools::files::request::{Files, FilePurpose};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let files = Files::new()?;
let file = files.upload_path("data.jsonl", FilePurpose::FineTune).await?;
println!("Uploaded: {}", file.id);
Ok(())
}Sourcepub async fn upload_bytes(
&self,
content: &[u8],
filename: &str,
purpose: FilePurpose,
) -> Result<File>
pub async fn upload_bytes( &self, content: &[u8], filename: &str, purpose: FilePurpose, ) -> Result<File>
Uploads a file from bytes.
The file will be uploaded with the specified filename and purpose.
§Arguments
content- The file content as bytesfilename- The name to give the filepurpose- The intended purpose of the uploaded file
§Returns
Ok(File)- The uploaded file objectErr(OpenAIToolError)- If the upload fails
§Example
use openai_tools::files::request::{Files, FilePurpose};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let files = Files::new()?;
let content = b"{\"messages\": [{\"role\": \"user\", \"content\": \"Hello\"}]}";
let file = files.upload_bytes(content, "training.jsonl", FilePurpose::FineTune).await?;
println!("Uploaded: {}", file.id);
Ok(())
}Sourcepub async fn list(
&self,
purpose: Option<FilePurpose>,
) -> Result<FileListResponse>
pub async fn list( &self, purpose: Option<FilePurpose>, ) -> Result<FileListResponse>
Lists all files that belong to the user’s organization.
Optionally filter by purpose.
§Arguments
purpose- Optional filter by file purpose
§Returns
Ok(FileListResponse)- The list of filesErr(OpenAIToolError)- If the request fails
§Example
use openai_tools::files::request::{Files, FilePurpose};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let files = Files::new()?;
// List all files
let all_files = files.list(None).await?;
println!("Total files: {}", all_files.data.len());
// List only fine-tuning files
let ft_files = files.list(Some(FilePurpose::FineTune)).await?;
println!("Fine-tuning files: {}", ft_files.data.len());
Ok(())
}Sourcepub async fn retrieve(&self, file_id: &str) -> Result<File>
pub async fn retrieve(&self, file_id: &str) -> Result<File>
Retrieves details of a specific file.
§Arguments
file_id- The ID of the file to retrieve
§Returns
Ok(File)- The file detailsErr(OpenAIToolError)- If the file is not found or the request fails
§Example
use openai_tools::files::request::Files;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let files = Files::new()?;
let file = files.retrieve("file-abc123").await?;
println!("File: {}", file.filename);
println!("Size: {} bytes", file.bytes);
println!("Purpose: {}", file.purpose);
Ok(())
}Sourcepub async fn delete(&self, file_id: &str) -> Result<DeleteResponse>
pub async fn delete(&self, file_id: &str) -> Result<DeleteResponse>
Deletes a file.
§Arguments
file_id- The ID of the file to delete
§Returns
Ok(DeleteResponse)- Confirmation of deletionErr(OpenAIToolError)- If the file cannot be deleted or the request fails
§Example
use openai_tools::files::request::Files;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let files = Files::new()?;
let result = files.delete("file-abc123").await?;
if result.deleted {
println!("File {} was deleted", result.id);
}
Ok(())
}Sourcepub async fn content(&self, file_id: &str) -> Result<Vec<u8>>
pub async fn content(&self, file_id: &str) -> Result<Vec<u8>>
Retrieves the content of a file.
§Arguments
file_id- The ID of the file to retrieve content from
§Returns
Ok(Vec<u8>)- The file content as bytesErr(OpenAIToolError)- If the file cannot be retrieved or the request fails
§Example
use openai_tools::files::request::Files;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let files = Files::new()?;
let content = files.content("file-abc123").await?;
// Convert to string if it's text content
let text = String::from_utf8(content)?;
println!("Content: {}", text);
Ok(())
}Auto Trait Implementations§
impl Freeze for Files
impl RefUnwindSafe for Files
impl Send for Files
impl Sync for Files
impl Unpin for Files
impl UnwindSafe for Files
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more