Nous.Eval.Online.Redactor behaviour (nous v0.21.1)

Copy Markdown View Source

Behaviour for the redactor Nous.Eval.Online requires (redact:).

A redactor receives a Nous.Eval.Online.Content and returns it with content values replaced. It runs in its own process with a time limit (redact_timeout), sees a copy of one captured trace, and must be pure and fast: same input, same output, no I/O.

defmodule MyApp.TraceRedactor do
  @behaviour Nous.Eval.Online.Redactor

  @email ~r/[\w.+-]+@[\w-]+\.[\w.]+/

  @impl true
  def redact(content),
    do: Nous.Eval.Online.Redactor.map_text(content, &Regex.replace(@email, &1, "[email]"))

  @impl true
  def version, do: "1"
end

redact: also accepts a 1-arity function, or :drop_content (no content is kept at all: only the skeleton is stored).

What Nous enforces

After redact/1 returns, the result must be a Nous.Eval.Online.Content with the same trace_id, the same skeleton and the same set of paths, and every value must be JSON (strings, numbers, booleans, nil, lists, string-keyed maps) or :redacted, and must decode as Codec JSON. On a raise, throw, exit, timeout or any violation the sample is stored without content (the skeleton only), marked redaction: "failed" with the failure kind; the exception and its message are never kept or logged, because they may quote the content. Kinds: :raised, :thrown, :exited, :timeout, :invalid_return, :trace_id_changed, :skeleton_changed, :keys_changed, :not_json, :undecodable.

Nous ships no default redactor: what is sensitive is the application's call. map_text/2 is a helper for writing one, never applied on its own.

Summary

Functions

Checks redactor on content the way Nous.Eval.Online applies it, and that it is deterministic (two calls give the same result). Use it in the application's tests with a realistic content.

Applies fun to every text leaf of every content value and returns the content.

Types

t()

@type t() ::
  :drop_content
  | (Nous.Eval.Online.Content.t() -> Nous.Eval.Online.Content.t())
  | module()

Callbacks

redact(t)

version()

(optional)
@callback version() :: String.t()

Functions

check(redactor, content, opts \\ [])

@spec check(t(), Nous.Eval.Online.Content.t(), keyword()) ::
  {:ok, Nous.Eval.Online.Content.t()} | {:error, atom()}

Checks redactor on content the way Nous.Eval.Online applies it, and that it is deterministic (two calls give the same result). Use it in the application's tests with a realistic content.

Returns {:ok, redacted} or {:error, kind} (the failure kinds of the moduledoc, or :nondeterministic).

map_text(content, fun)

Applies fun to every text leaf of every content value and returns the content.

Text leaves are plain strings, the keys of plain JSON objects, the text fields of a {"$msg": ...} message (content, reasoning_content, tool_calls, metadata - so metadata.content_parts too - and name), the content of a {"$part": ...} content part, and the text of an {"$opaque": ...} term. Codec structure ($atom, timestamps, $usage, message roles) is left alone, so the value still decodes. A {"$bin": ...} binary that is not UTF-8 is replaced by :redacted. fun must return a string; keys that it maps to the same string merge.