MLServe.Backend.Static (MLServe v0.1.0)

Copy Markdown View Source

Always returns the same configured result, whatever the input.

Exists so that applications depending on MLServe can test their own code without a real model. Swap the backend in config/test.exs and every MLServe.predict/3 call in the system under test returns a known value, with the real routing, caching and telemetry still exercised:

# config/test.exs
config :ml_serve,
  models: [
    fraud_detection: [
      backend: MLServe.Backend.Static,
      config: [result: %{prediction: :fraud, probability: 0.94}]
    ]
  ]

Options

  • :result — the value returned by every prediction. Defaults to %{}.
  • :error — when set, every prediction returns {:error, value} instead. Useful for exercising your application's error path.
  • :delay — milliseconds to sleep before returning, for testing timeouts.

Examples

iex> {:ok, state} = MLServe.Backend.Static.load(result: %{score: 1})
iex> MLServe.Backend.Static.predict(state, :anything)
{:ok, %{score: 1}}

iex> {:ok, state} = MLServe.Backend.Static.load(error: :unavailable)
iex> MLServe.Backend.Static.predict(state, :anything)
{:error, :unavailable}