ReqLLM.Scripts.Helpers (ReqLLM v1.0.0)
View SourceShared 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
Prints a banner for a script.
Parameters
script_name- Name of the scriptdescription- Brief description of what the script doesopts- Parsed options to display
Examples
banner!("example.exs", "Demonstrates basic text generation", model: "openai:gpt-4o")
@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")
@spec default_embedding_model() :: String.t()
Returns the default embedding model.
@spec default_text_model() :: String.t()
Returns the default text generation model.
@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
Handles errors by printing a formatted message and exiting.
Parameters
error- The error to handlescript_name- Name of the script for error messagesopts- Options for error handling
Loads an image file and returns its base64-encoded content.
Examples
load_image_base64!("path/to/image.png")
Loads a PDF file and returns its base64-encoded content.
Examples
load_pdf_base64!("path/to/document.pdf")
@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
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)
[]
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)
[]
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"
Parses command-line arguments using NimbleOptions.
Parameters
argv- List of command-line argumentsschema- NimbleOptions schema definitionscript_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")
@spec print_object_response(map(), map() | nil, non_neg_integer()) :: :ok
Prints an object generation response with timing information.
@spec print_text_response(ReqLLM.Response.t(), non_neg_integer(), keyword()) :: :ok
Prints a text generation response with timing information.
@spec print_usage_and_timing(map() | nil, non_neg_integer(), keyword()) :: :ok
Prints usage and timing information.
@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)
Formats usage information into printable lines.