Imp.Predict (Imp v0.5.0)

Copy Markdown View Source

Basic Imp program that maps signature inputs to typed outputs with an LM.

Predict is the smallest executable Imp module. It formats a signature and input map with an adapter, calls the configured LM, parses the result into a Imp.Prediction, and attaches trace metadata. Most higher-level modules such as ChainOfThought, RAG, ReAct, BestOfN, and optimizers eventually compose around this shape, which is why they live under Imp.Predict.*: as in DSPy, where dspy.ChainOfThought wraps a dspy.Predict, each of them holds or builds one.

Required inputs are validated before an LM call is made. This keeps missing data as a local program error instead of spending provider calls on malformed prompts.

Example

iex> lm = Imp.LM.Static.new(handler: fn _messages, _opts -> %{answer: "4"} end)
iex> program = Imp.Predict.new("question -> answer", lm: lm)
iex> {:ok, prediction} = Imp.Predict.call(program, %{question: "2+2?"})
iex> Imp.Prediction.get(prediction, :answer)
"4"

iex> silent_lm = Imp.LM.Static.new(handler: fn _messages, _opts -> %{answer: "unused"} end)
iex> missing = Imp.Predict.new("question, context -> answer", lm: silent_lm)
iex> Imp.Predict.call(missing, %{question: "2+2?"})
{:error, {:missing_input_fields, [:context]}}

Summary

Functions

Calls the program with a map or keyword list of inputs.

Calls the program with a per-call config override.

Serializes portable program state for Imp.Saving.

Builds a prediction program.

Returns a copy of the program with demonstrations attached.

Returns a copy of the program pinned to a concrete LM.

Returns a copy of the program with a new signature.

Functions

call(predict, inputs)

Calls the program with a map or keyword list of inputs.

Returns {:ok, prediction} on success or {:error, reason} for local input errors, LM errors, or adapter parse errors. Successful predictions include redacted trace metadata with the rendered messages and raw LM output.

A completion that could not be read as the outputs, after the chat and XML adapters' JSON fallback, is {:error, %Imp.AdapterParseError{}} with its :trace set; see that module for the kinds. An LM request that failed, including the fallback's own request, is the LM's error (Imp.LMError for Imp.Clients.ReqLLM).

call(predict, inputs, config)

Calls the program with a per-call config override.

config is a keyword list merged over the program's stored config for this invocation only; the program itself is not mutated. Every merged entry flows to the LM request, including a predicted-outputs :prediction map. Raises ArgumentError if config is not a keyword list.

Imp.Predict.call(program, %{question: "..."},
  temperature: 0.2,
  prediction: %{type: "content", content: "..."}
)

dump(predict)

Serializes portable program state for Imp.Saving.

new(signature, opts \\ [])

Builds a prediction program.

Pass :lm and :adapter for a self-contained program, or omit them to resolve from process/global Imp settings at call time. An unknown option raises ArgumentError.

Options

  • :lm - The model the program calls. When absent, each call uses Imp.Settings' :lm.

  • :adapter - The adapter that renders the request and parses the reply. When absent, each call uses Imp.Settings' :adapter.

  • :demos (list of term/0) - Worked examples rendered before the inputs, as Imp.Example values or maps. The default value is [].

  • :config (keyword/0) - Request options sent with every call: sampling (:temperature, :max_tokens, :n, ...) and the other options the LM client takes. The default value is [].

  • :adapter_opts (keyword/0) - Options handed to the adapter's format/3 on every call, beside :demos: how a program passes rendering data, and how a host injects renderers without writing a second adapter module. The default value is [].

  • :metadata (map of term/0 keys and term/0 values) - Free-form metadata kept on the program. The default value is %{}.

with_demos(predict, demos)

Returns a copy of the program with demonstrations attached.

with_lm(predict, lm)

Returns a copy of the program pinned to a concrete LM.

with_signature(predict, signature)

Returns a copy of the program with a new signature.