Skip to contents

module() turns a signature into a prediction module: one structured model call per input, whose output follows the signature. It is the usual starting point: signature() -> module() -> run() -> evaluate() -> compile().

Other kinds of program have their own constructors: chain_of_thought() adds a reasoning step, react() calls tools, multi_chain_comparison() compares several reasoning chains, and program_of_thought(), code_act(), rlm_module() and flex() run code.

Usage

module(
  signature,
  chat = NULL,
  template = "",
  demos = list(),
  config = list(),
  ...
)

Arguments

signature

A signature from signature(). A string is not accepted here; wrap it in signature().

chat

An ellmer Chat stored on the module. run() uses it unless you pass .llm; see get_default_chat() for the full order.

template

A glue template for the input part of the prompt, with input fields in single braces, as in "Review: {text}". The default ("") lists each input as name: value.

demos

Worked examples placed before the input in every prompt: a list of list(inputs = list(...), output = list(...)). compile() sets demos for you.

config

Model settings applied to a copy of the chat on every call: temperature, top_p, reasoning_effort, max_tokens, max_output_tokens, frequency_penalty, presence_penalty and service_tier, for example config = list(reasoning_effort = "low"). Reasoning models such as gpt-6-luna accept temperature and top_p only with reasoning_effort = "none". Chat settings such as model or provider are an error here; set them on the chat instead.

...

Must be empty. Arguments of other constructors, such as tools or type, give an error that names the constructor to use.

Value

A prediction module (an R6 object of class PredictModule) to use with run(), run_dataset(), evaluate() and compile().

Examples

classifier <- module(
  signature("text -> sentiment: enum('positive', 'negative', 'neutral')"),
  template = "Classify the sentiment of this review:\n{text}",
  demos = list(
    list(
      inputs = list(text = "Arrived broken."),
      output = list(sentiment = "negative")
    )
  ),
  config = list(reasoning_effort = "low")
)
classifier
#> 
#> ── PredictModule ──
#> 
#> ── Signature 
#> 
#> ── Signature ──
#> 
#> ── Inputs 
#> • text: "string" - Input: text
#> 
#> ── Output 
#> Type: "object(sentiment: enum(positive, negative, neutral))"
#> 
#> ── Instructions 
#> Given the fields `text`, produce the fields `sentiment`.
#> 
#> ── Template 
#> Classify the sentiment of this review:
#> {text}
#> 
#> ── Demos 
#> 1 demonstration(s) loaded

if (FALSE) { # \dontrun{
run(
  classifier,
  text = "Great package!",
  .llm = ellmer::chat_openai(model = "gpt-6-luna")
)
} # }