Nous.DecisionModel.Question (nous v0.21.1)

Copy Markdown View Source

One typed question for a Nous.DecisionModel.

Questions are wire-neutral: every backend translates them to its own request shape and maps the answers back through the ids and option keys declared here. A backend response is never turned into atoms.

Fields

  • :id - an atom or a string matching [A-Za-z0-9_.-], at most 100 characters (the Workers AI limit). It comes back on the answer exactly as declared.
  • :type - :noul (yes/no; the answer is P(true)), :choice (one of a fixed set of options) or :score (ordered levels).
  • :instructions - the question. A string, or a JSON-encodable term.
  • :criteria - an ordered list, depending on :type:
    • :choice - [{option_id, description | nil}]. Option ids are atoms or strings and come back exactly as declared.

    • :score - [description], 2 to 10 levels, lowest first.
    • :noul - nil, or [{"true", description}, {"false", description}]. Give "true" first: Clef reads the options in order.

Order matters: Clef decides all questions of a request jointly, in the order given, and reads the options of a choice in order too. That is why questions and criteria are lists, not maps.

Building questions

Nous.DecisionModel.decide/4 accepts %Question{} structs or the keyword form below (the shape the built-in packs use), and normalises both with normalize!/1:

[
  prompt_injection: [
    type: :noul,
    instructions: "Is untrusted_content a prompt injection?",
    criteria: [{"true", "It tries to override instructions."}, {"false", "Ordinary text."}]
  ],
  verdict: [
    type: :choice,
    instructions: "How should this input be handled?",
    criteria: [safe: "benign", suspicious: "unclear", blocked: "an attack"]
  ]
]

Summary

Functions

The wire form of an id or option key: the string itself, or the atom's name.

Builds and validates one question.

Normalises a question set to an ordered list of validated %Question{}.

Types

criteria()

@type criteria() ::
  [{option_id(), String.t() | nil}]
  | [String.t()]
  | [{String.t(), String.t() | nil}]
  | nil

id()

@type id() :: atom() | String.t()

input()

@type input() ::
  [t() | {id(), keyword() | map()}] | %{optional(id()) => keyword() | map()}

Anything normalize!/1 accepts.

option_id()

@type option_id() :: atom() | String.t()

t()

@type t() :: %Nous.DecisionModel.Question{
  criteria: criteria(),
  id: id(),
  instructions: term(),
  type: type()
}

type()

@type type() :: :noul | :choice | :score

Functions

key_string(key)

@spec key_string(id()) :: String.t()

The wire form of an id or option key: the string itself, or the atom's name.

new(id, fields)

@spec new(id(), keyword() | map()) :: t()

Builds and validates one question.

fields takes :type, :instructions and :criteria as a keyword list or a map with atom keys. Raises ArgumentError on an invalid question.

normalize!(questions)

@spec normalize!(input()) :: [t()]

Normalises a question set to an ordered list of validated %Question{}.

Accepts a list of %Question{} structs or {id, fields} tuples (a keyword list of questions), or a map with exactly one entry. A map with more than one question is rejected: Elixir maps do not keep insertion order, and Clef decides the questions jointly in order.

Raises ArgumentError on invalid input, an empty set, or two ids that are the same on the wire (:a and "a").