Skip to contents

step() wraps a module for pipeline() when its inputs do not line up with the previous step's outputs by name, when only some of its outputs should go on to the next step, or when it needs fixed inputs.

Usage

step(module, map = list(), select = character(0), ...)

Arguments

module

The module to run at this step.

map

A named character vector (or list) that renames fields coming from the previous step. Names are the previous step's output fields and values are this module's input fields: map = c(summary = "passage") passes the upstream summary as passage. In the first step, map renames the pipeline's inputs.

select

Names of this step's output fields to pass to the next step. By default all of them are passed. For the last step, select also limits what the pipeline returns.

...

Fixed inputs for this step, as name = value: values for input fields of module that stay the same on every call. They are inputs, not settings, so something like system_prompt = "Be brief" would reach the prompt as an extra input field. They override mapped values with the same name.

Value

A pipeline step (an S7 object of class PipelineStep) to pass to pipeline().

Examples

summarize <- module_fn("text -> summary", function(text) substr(text, 1, 30))
translate <- module_fn(
  "passage, language -> translation, n_chars: int",
  function(passage, language) {
    list(translation = paste0("[", language, "] ", passage), n_chars = nchar(passage))
  }
)
shout <- module_fn("translation -> reply", function(translation) toupper(translation))

p <- pipeline(
  summarize,
  # `summary` becomes `passage`, `language` is fixed, and only
  # `translation` goes on to `shout`
  step(translate, map = c(summary = "passage"), select = "translation", language = "French"),
  shout
)
run(p, text = "dsprrr turns language model calls into programs.")
#> $reply
#> [1] "[FRENCH] DSPRRR TURNS LANGUAGE MODEL CA"
#>