Imp.Settings (Imp v0.5.0)

Copy Markdown View Source

OTP-backed global settings with process-local overrides.

The Imp OTP application owns the global settings process in normal production use. Calling this module before the application is started attempts to start the application, which gives the settings process the same supervision semantics as a regular OTP application. The global settings are intentionally mutable and node-local; use context/2 for process-local overrides around a request, task, or test.

Summary

Functions

Returns a specification to start this module under a supervisor.

Updates node-local Imp defaults.

Runs a zero-arity function with process-local settings overrides.

Fetches one effective setting or raises when the key is absent.

Returns the effective settings for the current process.

Restores global settings to Imp defaults.

Functions

child_spec(arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

configure(opts)

Updates node-local Imp defaults.

Use this for application-level defaults such as the LM client or adapter. For request, test, Livebook cell, or task-local overrides, prefer context/2 so the override is restored automatically.

Takes a keyword list or a map; a map may use string keys. An unknown setting raises ArgumentError. context/2 also carries keys of the caller's own, such as a request id.

Settings

  • :lm (term/0) - The LM a program without its own :lm calls. The default value is nil.

  • :adapter (term/0) - The adapter a program without its own :adapter uses. The default value is Imp.Adapter.Chat.

  • :async_max_workers (pos_integer/0) - How many Imp tasks run at once. The default value is 8.

  • :track_usage (boolean/0) - Whether each prediction carries its LM usage (Imp.Prediction.get_lm_usage/1). The default value is false.

  • :warn_on_type_mismatch (boolean/0) - Whether an input value that does not match its field's declared type logs a warning. The default value is true.

  • :two_step_extraction_lm (term/0) - The LM Imp.Adapter.TwoStep extracts outputs with, when not given to the adapter.

context(opts, fun)

Runs a zero-arity function with process-local settings overrides.

Each context snapshots all effective settings at entry, applies its overrides, and restores the previous snapshot even if the function raises. Child processes do not inherit process-local settings automatically; Imp-owned task helpers capture one complete effective snapshot when supervised async work is submitted.

Besides Imp's settings (listed under configure/1), a context carries keys of the caller's own, such as a request id, readable with get/0 and fetch!/1. Imp's own settings are type-checked as configure/1 checks them, and a key that is not a setting but reads like one (:max_errors, :retriever, :callbacks) raises ArgumentError rather than being carried unread.

iex> Imp.Settings.context([lm: :outer], fn ->
...>   Imp.Settings.context([adapter: :inner], fn ->
...>     {Imp.Settings.get().lm, Imp.Settings.get().adapter}
...>   end)
...> end)
{:outer, :inner}

iex> parent = self()
iex> Imp.Settings.context([lm: :parent_only], fn ->
...>   task = Task.async(fn -> send(parent, {:child_lm, Imp.Settings.get().lm}) end)
...>   Task.await(task)
...> end)
iex> receive do
...>   {:child_lm, value} -> value
...> end
nil

fetch!(key)

Fetches one effective setting or raises when the key is absent.

iex> Imp.Settings.context([request_id: "req-1"], fn -> Imp.Settings.fetch!(:request_id) end)
"req-1"

get()

Returns the effective settings for the current process.

Effective settings are the global defaults plus any nested context/2 overrides in the current process.

iex> Imp.Settings.context([lm: :local], fn -> Imp.Settings.get().lm end)
:local

reset()

Restores global settings to Imp defaults.

Process-local context/2 overrides are not global state and are restored by the context call itself.

start_link(opts)