# `Nous.DecisionModel.Question`
[🔗](https://github.com/nyo16/nous/blob/v0.21.1/lib/nous/decision_model/question.ex#L1)

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"]
      ]
    ]

# `criteria`

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

# `id`

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

# `input`

```elixir
@type input() ::
  [t() | {id(), keyword() | map()}] | %{optional(id()) =&gt; keyword() | map()}
```

Anything `normalize!/1` accepts.

# `option_id`

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

# `t`

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

# `type`

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

# `key_string`

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

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

# `new`

```elixir
@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!`

```elixir
@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"`).

---

*Consult [api-reference.md](api-reference.md) for complete listing*
