WeaviateEx.Types.MediaInput (WeaviateEx v0.7.4)

View Source

Handles media input for multi-modal search.

Accepts file paths, base64 strings, or raw binary data and converts them to base64-encoded format suitable for Weaviate API requests.

Input Types

This module can handle three types of input:

  1. File paths - Absolute or relative paths to media files on disk
  2. Base64 strings - Already encoded data (with or without data URI prefix)
  3. Raw binary - Binary data that will be base64-encoded

Examples

# From file path
{:ok, encoded} = MediaInput.prepare("/path/to/image.jpg")

# From base64 string (with data URI)
{:ok, encoded} = MediaInput.prepare("data:image/jpeg;base64,/9j/4AAQ...")

# From base64 string (without data URI)
{:ok, encoded} = MediaInput.prepare("/9j/4AAQSkZJRg...")

# From raw binary
{:ok, encoded} = MediaInput.prepare(<<0xFF, 0xD8, 0xFF, 0xE0>>)

Summary

Functions

Checks if the input appears to be base64-encoded.

Checks if the input is a valid file path that exists.

Prepares media input for API request.

Prepares media input, raising on error.

Types

input()

@type input() :: String.t() | binary()

Functions

base64?(input)

@spec base64?(String.t()) :: boolean()

Checks if the input appears to be base64-encoded.

Detects both data URI format and raw base64 strings.

Examples

MediaInput.base64?("data:image/png;base64,iVBORw0...")  # => true
MediaInput.base64?("iVBORw0KGgo...")  # => true (if valid base64)
MediaInput.base64?("not base64!")  # => false

file?(input)

@spec file?(String.t()) :: boolean()

Checks if the input is a valid file path that exists.

Examples

MediaInput.file?(Path.join(System.tmp_dir!(), "existing_file"))  # => true
MediaInput.file?("/nonexistent/path")  # => false

prepare(input)

@spec prepare(input()) :: {:ok, String.t()} | {:error, term()}

Prepares media input for API request.

Takes a file path, base64 string, or raw binary and returns the base64-encoded data suitable for sending to Weaviate.

Arguments

  • input - The media input (file path, base64 string, or raw binary)

Returns

  • {:ok, base64_string} - Successfully prepared media data
  • {:error, {:file_read_error, reason}} - Failed to read the file

Examples

# From file path
{:ok, encoded} = MediaInput.prepare("/path/to/image.jpg")

# From base64 string
{:ok, encoded} = MediaInput.prepare("data:image/jpeg;base64,/9j/4AAQ...")

# From raw binary
{:ok, encoded} = MediaInput.prepare(<<0xFF, 0xD8, 0xFF, 0xE0>>)

# Non-existent file
{:error, {:file_read_error, :enoent}} = MediaInput.prepare("/nonexistent.jpg")

prepare!(input)

@spec prepare!(input()) :: String.t()

Prepares media input, raising on error.

Same as prepare/1 but raises an ArgumentError on failure.

Examples

encoded = MediaInput.prepare!("/path/to/image.jpg")

# Raises ArgumentError for non-existent file
MediaInput.prepare!("/nonexistent.jpg")