MistralClient.API.Files (mistralex_ai v0.1.0)

View Source

Files API for the Mistral AI client.

This module provides functions for managing files used with the Mistral API, including uploading files for fine-tuning, retrieving file information, and downloading files.

Features

  • File upload with multipart support
  • File listing and retrieval
  • File deletion
  • File download
  • Signed URL generation
  • File metadata management

Usage

# Upload a file
{:ok, file} = MistralClient.API.Files.upload("./training_data.jsonl", "fine-tune")

# List all files
{:ok, files} = MistralClient.API.Files.list()

# Download a file
{:ok, content} = MistralClient.API.Files.download("file-abc123")

# Delete a file
{:ok, _} = MistralClient.API.Files.delete("file-abc123")

Summary

Functions

Download the content of a file.

Check if a file exists.

Filter files by purpose.

Get a signed URL for downloading a file.

List all uploaded files with pagination and filtering support.

Retrieve information about a specific file.

Get the total size of all files.

Upload a file with support for progress tracking and validation.

Types

file_id()

@type file_id() :: String.t()

file_path()

@type file_path() :: String.t()

purpose()

@type purpose() :: String.t()

Functions

delete(file_id, client \\ nil)

@spec delete(file_id(), MistralClient.Client.t() | nil) ::
  {:ok, MistralClient.Models.DeleteFileOut.t()} | {:error, Exception.t()}

Delete a file.

Parameters

  • file_id - The ID of the file to delete
  • client - HTTP client (optional, uses default if not provided)

Examples

{:ok, result} = MistralClient.API.Files.delete("file-abc123")

download(file_id, client \\ nil)

@spec download(file_id(), MistralClient.Client.t() | nil) ::
  {:ok, binary()} | {:error, Exception.t()}

Download the content of a file.

Parameters

  • file_id - The ID of the file to download
  • client - HTTP client (optional, uses default if not provided)

Examples

{:ok, content} = MistralClient.API.Files.download("file-abc123")

exists?(file_id, client \\ nil)

@spec exists?(file_id(), MistralClient.Client.t() | nil) :: boolean()

Check if a file exists.

Parameters

  • file_id - The ID of the file to check
  • client - HTTP client (optional, uses default if not provided)

Examples

true = MistralClient.API.Files.exists?("file-abc123")
false = MistralClient.API.Files.exists?("non-existent-file")

filter_by_purpose(files, purpose)

@spec filter_by_purpose([MistralClient.Models.File.t()], purpose()) :: [
  MistralClient.Models.File.t()
]

Filter files by purpose.

Parameters

  • files - List of files to filter
  • purpose - Purpose to filter by

Examples

{:ok, all_files} = MistralClient.API.Files.list()
fine_tune_files = MistralClient.API.Files.filter_by_purpose(all_files, "fine-tune")

get_signed_url(file_id, options \\ [], client \\ nil)

@spec get_signed_url(file_id(), keyword(), MistralClient.Client.t() | nil) ::
  {:ok, MistralClient.Models.FileSignedURL.t()} | {:error, Exception.t()}

Get a signed URL for downloading a file.

Parameters

  • file_id - The ID of the file
  • options - Keyword list of options:
    • :expiry - Number of hours before the URL expires (default: 24)
  • client - HTTP client (optional, uses default if not provided)

Examples

# Get signed URL with default expiry (24 hours)
{:ok, signed_url_response} = MistralClient.API.Files.get_signed_url("file-abc123")

# Get signed URL with custom expiry (48 hours)
{:ok, signed_url_response} = MistralClient.API.Files.get_signed_url(
  "file-abc123",
  expiry: 48
)

list(options \\ [], client \\ nil)

@spec list(
  keyword(),
  MistralClient.Client.t() | nil
) :: {:ok, MistralClient.Models.FileList.t()} | {:error, Exception.t()}

List all uploaded files with pagination and filtering support.

Parameters

  • options - Keyword list of options:
    • :page - Page number (default: 0)
    • :page_size - Number of items per page (default: 100)
    • :purpose - Filter by purpose (e.g., "fine-tune", "assistants")
    • :sample_type - Filter by sample type
    • :source - Filter by source
    • :search - Search term to filter files
  • client - HTTP client (optional, uses default if not provided)

Examples

# List all files
{:ok, files} = MistralClient.API.Files.list()

# List with pagination
{:ok, files} = MistralClient.API.Files.list(page: 1, page_size: 50)

# Filter by purpose
{:ok, files} = MistralClient.API.Files.list(purpose: "fine-tune")

# Search files
{:ok, files} = MistralClient.API.Files.list(search: "training")

retrieve(file_id, client \\ nil)

@spec retrieve(file_id(), MistralClient.Client.t() | nil) ::
  {:ok, MistralClient.Models.File.t()} | {:error, Exception.t()}

Retrieve information about a specific file.

Parameters

  • file_id - The ID of the file to retrieve
  • client - HTTP client (optional, uses default if not provided)

Examples

{:ok, file} = MistralClient.API.Files.retrieve("file-abc123")

total_size(files)

@spec total_size([MistralClient.Models.File.t()]) :: integer()

Get the total size of all files.

Parameters

  • files - List of files

Examples

{:ok, files} = MistralClient.API.Files.list()
total_bytes = MistralClient.API.Files.total_size(files)

upload(file_path, purpose, options \\ [], client \\ nil)

@spec upload(file_path(), purpose(), keyword(), MistralClient.Client.t() | nil) ::
  {:ok, MistralClient.Models.FileUpload.t()} | {:error, Exception.t()}

Upload a file with support for progress tracking and validation.

Parameters

  • file_path - Path to the file to upload
  • purpose - Purpose of the file (e.g., "fine-tune", "assistants", "batch")
  • options - Keyword list of options:
    • :progress_callback - Function to call with upload progress (0.0 to 1.0)
    • :filename - Custom filename to use (defaults to basename of file_path)
  • client - HTTP client (optional, uses default if not provided)

Examples

# Simple upload
{:ok, file} = MistralClient.API.Files.upload("./training_data.jsonl", "fine-tune")

# Upload with custom filename
{:ok, file} = MistralClient.API.Files.upload(
  "./data.jsonl",
  "fine-tune",
  filename: "custom_training_data.jsonl"
)

# Upload with progress tracking
progress_fn = fn progress -> IO.puts("Upload progress: #{trunc(progress * 100)}%") end
{:ok, file} = MistralClient.API.Files.upload(
  "./large_file.jsonl",
  "fine-tune",
  progress_callback: progress_fn
)