Skip to contents

R arm of morie.fn.hmicl: assembles the k-shot prompt from the demonstrations (template with x and y fields, joined by separator, the query line last), scores every candidate label with the supplied model(prompt, candidate) log-probability and returns the arg-max with the softmax posterior. Candidates default to the labels seen in the demonstrations.

Usage

Hmicl(
  model,
  examples,
  query,
  candidates = NULL,
  template = "{x} -> {y}",
  separator = "\n"
)

Arguments

model

Function (prompt, candidate) -> log-probability.

examples

List of list(x, y) demonstrations.

query

The input to classify.

candidates

Optional label set.

template

Format with the literal fields x and y in braces.

separator

Joined between lines.

Value

List with prediction, prompt, log_probs, posterior, n_shot, candidates, estimate.

References

Brown, T. B. et al. (2020). Language models are few-shot learners. NeurIPS 33, 1877-1901.

Examples

sc <- function(p, cand) lengths(regmatches(p, gregexpr(cand, p, fixed = TRUE)))
Hmicl(sc, list(list("a", "pos"), list("b", "pos"), list("c", "neg")), "d")$prediction
#> [1] "pos"