Nous.DecisionModel.Backend behaviour (nous v0.21.1)

Copy Markdown View Source

Behaviour for decision-model backends, the extension point behind Nous.DecisionModel.

A backend turns a state and an ordered list of Nous.DecisionModel.Question into a Nous.DecisionModel.Result. Nous ships three:

Contract

decide/4 receives the Nous.DecisionModel struct (so it can read model and opts), the state, the validated questions and per-call opts. By the time it is called, Nous.DecisionModel.decide/4 has already normalised the questions and defanged every string in the state; it also emits the telemetry events. A backend must:

  • keep question order and option order on the wire
  • map answers back through the declared ids and option keys, never by creating atoms from the response, and return {:error, %Nous.Errors.DecisionModelError{reason: :unknown_label}} when the response names an id, option or level that was not declared
  • return every answer in question order (Result.answers)
  • return {:error, %Nous.Errors.DecisionModelError{}} for every failure; never answer a failed request with probabilities

HTTP backends go through Nous.Providers.HTTP.post/4 and validate their URL with Nous.UrlGuard.

Optional callbacks

Example

defmodule MyApp.KeywordBackend do
  @behaviour Nous.DecisionModel.Backend

  alias Nous.DecisionModel.{Answer, Result}

  @impl true
  def decide(dm, state, questions, _opts) do
    answers =
      Enum.map(questions, fn %{id: id, type: :noul} ->
        p = if String.contains?(inspect(state), "ignore previous"), do: 0.99, else: 0.01
        {id, %Answer{type: :noul, noul: p, probabilities: [{"true", p}, {"false", 1 - p}]}}
      end)

    {:ok, %Result{answers: answers, backend: __MODULE__, model: dm.model}}
  end
end

Summary

Callbacks

A child spec for backends that own a process, such as a loaded local model.

The exact number of tokens text occupies for this backend's model.

Answers questions about state.

The token budget a single request (state plus questions) must fit in.

Callbacks

child_spec(keyword)

(optional)
@callback child_spec(keyword()) :: Supervisor.child_spec()

A child spec for backends that own a process, such as a loaded local model.

Optional. Start the child under your own supervision tree; see Nous.DecisionModel.Backend.LlamaCpp for the options it accepts.

count_tokens(t, t)

(optional)
@callback count_tokens(Nous.DecisionModel.t(), String.t()) :: non_neg_integer()

The exact number of tokens text occupies for this backend's model.

Optional. text is already defanged. Without this callback, Nous.DecisionModel.count_tokens/2 assumes one token per byte, an upper bound, so chunking never relies on server-side truncation.

decide(t, state, list, keyword)

Answers questions about state.

state is already defanged and the questions are already validated and in declared order. opts carries per-call options such as :timeout (ms).

Returns {:ok, %Nous.DecisionModel.Result{}} with one answer per question, in question order, keyed by the declared ids. Every failure, including a response that names an undeclared id, option or level, is {:error, %Nous.Errors.DecisionModelError{}}.

max_state_tokens(t)

(optional)
@callback max_state_tokens(Nous.DecisionModel.t()) :: pos_integer()

The token budget a single request (state plus questions) must fit in.

Optional. Nous.DecisionModel.max_state_tokens/1 prefers a :max_state_tokens option on the struct, then this callback, then a default. Hooks and guards size their chunks from this value.

Functions

error(backend, reason, message, fields \\ [])

Builds a Nous.Errors.DecisionModelError for backend.

fields takes :status, :retryable?, :retry_after_ms and :details. :retryable? defaults to true for :timeout and :backend_dead, false otherwise.