Skip to contents

A program is an R function that calls AI functions (and anything else): declared like an AI function, with a formula (what comes out ~ what goes in), a sentence, and a codebook of its fields, then the code. Calling it is one call: its inputs are bound to their types and checked before the code runs (interface-input), its answer checked when the code returns (interface-output), and the AI functions it calls are steps of it, in the call log (one tree), in a stream, and in a conversation.

Usage

ai_program(
  .formula,
  .description = "",
  .body,
  ...,
  .data = NULL,
  .name = NULL,
  .defined_in = NULL,
  .answer_from = NULL
)

program_from_interface(
  interface,
  .body,
  name = "program",
  .defined_in = NULL,
  ai = FALSE
)

Arguments

.formula

outputs ~ inputs, as in ai().

.description

What the program does.

.body

The code: a function of the inputs, by name.

...

The fields, as in ai() (opaque() for any R value); and settings, as dotted names (.log_content, .observers, .plugins).

.data, .name, .defined_in

As in ai().

.answer_from

The AI function whose answer is the program's, shown as the program's answer as it is written (a served program's callers see it stream).

interface

A program's interface, as data (ai_interface()'s form).

name

The program's name.

ai

Whether the interface is an AI function's (its shapes may carry lmcc's other keywords).

Value

A program (class functai_program), a function.

Details

Like an AI function, a program is vectorised: given columns, it runs once per row (one call each, in order: your code is R's to run, one row at a time; the AI functions it calls with a column still run that column at once). Its code is given one row's values, by name, as R values of their types (a choice is a factor, a record a one-row tibble). An input the caller leaves out with no default given in the codebook is left out: your code's own default applies. An opaque() input takes any R value, passed whole to every row.

With several outputs, the code returns them by name (a named list, or a one-row tibble), and the program returns a tibble, as an AI function does.

Examples

team <- ai(team ~ message, "Which team should answer?", team = choice("shipping", "billing"))
answer <- ai(reply ~ message + team, "Answer the customer, as that team.")
support <- ai_program(reply ~ message, "Answer a customer's message.", function(message) {
  answer(message, team(message))
})
support
#> <ai program> reply ~ message
#>   Answer a customer's message.
#>   calls: answer, team