ReqLLM.Scripts.Helpers (ReqLLM v1.0.0)

View Source

Shared utilities for example scripts in scripts/.

Provides common functionality for script execution including argument parsing, logging, timing, error handling, and multimodal content processing.

Summary

Functions

Prints a banner for a script.

Creates a context from a prompt and optional system message.

Returns the default embedding model.

Returns the default text generation model.

Ensures the application is started.

Handles errors by printing a formatted message and exiting.

Loads an image file and returns its base64-encoded content.

Loads a PDF file and returns its base64-encoded content.

Returns the log level based on verbosity flags or string level.

Conditionally adds a key-value pair to a keyword list.

Conditionally puts a key-value pair into a keyword list.

Determines the MIME type of a media file based on its file extension.

Parses command-line arguments using NimbleOptions.

Prints an object generation response with timing information.

Prints a text generation response with timing information.

Prints usage and timing information.

Times the execution of a function and returns {result, duration_ms}.

Formats usage information into printable lines.

Functions

banner!(script_name, description, opts)

@spec banner!(String.t(), String.t(), keyword()) :: :ok

Prints a banner for a script.

Parameters

  • script_name - Name of the script
  • description - Brief description of what the script does
  • opts - Parsed options to display

Examples

banner!("example.exs", "Demonstrates basic text generation", model: "openai:gpt-4o")

context(prompt, opts \\ [])

@spec context(
  String.t(),
  keyword()
) :: ReqLLM.Context.t()

Creates a context from a prompt and optional system message.

Examples

context("Hello!", system: "You are helpful")

default_embedding_model()

@spec default_embedding_model() :: String.t()

Returns the default embedding model.

default_text_model()

@spec default_text_model() :: String.t()

Returns the default text generation model.

ensure_app!()

@spec ensure_app!() :: :ok

Ensures the application is started.

Starts the :req_llm application and its dependencies. Exits with error if startup fails.

Examples

iex> ensure_app!()
:ok

handle_error!(error, script_name, opts)

@spec handle_error!(any(), String.t(), keyword()) :: no_return()

Handles errors by printing a formatted message and exiting.

Parameters

  • error - The error to handle
  • script_name - Name of the script for error messages
  • opts - Options for error handling

load_image_base64!(path)

@spec load_image_base64!(String.t()) :: String.t()

Loads an image file and returns its base64-encoded content.

Examples

load_image_base64!("path/to/image.png")

load_pdf_base64!(path)

@spec load_pdf_base64!(String.t()) :: String.t()

Loads a PDF file and returns its base64-encoded content.

Examples

load_pdf_base64!("path/to/document.pdf")

log_level(verbose)

@spec log_level(boolean() | integer() | String.t()) :: Logger.level()

Returns the log level based on verbosity flags or string level.

Parameters

  • verbose - Boolean or integer verbosity level or string ("debug", "info", "warning", "error")

Examples

iex> log_level(true)
:info

iex> log_level(false)
:warning

iex> log_level("debug")
:debug

maybe_add(opts, key, value)

@spec maybe_add(keyword(), atom(), any()) :: keyword()

Conditionally adds a key-value pair to a keyword list.

If the value is nil, returns the original list unchanged. Otherwise, appends the key-value pair to the list (allows duplicates).

Examples

iex> maybe_add([], :stop, "END")
[stop: "END"]

iex> maybe_add([], :stop, nil)
[]

maybe_put(opts, key, value)

@spec maybe_put(keyword(), atom(), any()) :: keyword()

Conditionally puts a key-value pair into a keyword list.

If the value is nil, returns the original list unchanged. Otherwise, puts the key-value pair into the list.

Examples

iex> maybe_put([], :max_tokens, 100)
[max_tokens: 100]

iex> maybe_put([], :max_tokens, nil)
[]

media_type(path)

@spec media_type(String.t()) :: String.t()

Determines the MIME type of a media file based on its file extension.

Examples

iex> media_type("image.png")
"image/png"

iex> media_type("photo.jpg")
"image/jpeg"

parse_args(argv, schema, script_name)

@spec parse_args([String.t()], keyword(), String.t()) :: keyword()

Parses command-line arguments using NimbleOptions.

Parameters

  • argv - List of command-line arguments
  • schema - NimbleOptions schema definition
  • script_name - Name of the script for error messages

Examples

schema = [
  model: [type: :string, default: "openai:gpt-4o"],
  prompt: [type: :string, required: true]
]
parse_args(["--prompt", "Hello"], schema, "example.exs")

time(fun)

@spec time((-> any())) :: {any(), non_neg_integer()}

Times the execution of a function and returns {result, duration_ms}.

Examples

{result, ms} = time(fn -> ReqLLM.generate_text!("openai:gpt-4o", "Hi") end)

usage_lines(usage)

@spec usage_lines(map()) :: [String.t()]

Formats usage information into printable lines.