WardrikCore.Keys behaviour (wardrik_core v0.1.0-rc.3)

Copy Markdown

Gives your code its API keys, such as a model provider's.

defmodule MyApp.Keys do
  @behaviour WardrikCore.Keys

  @impl true
  def fetch(%WardrikCore.Keys.Request{name: name, principal: principal}, _opts) do
    case MyApp.Secrets.key(principal.id, name) do
      nil -> {:error, :not_found}
      key -> {:ok, key}
    end
  end
end

Give it to an agent as keys: {MyApp.Keys, opts}, or to Wardrik as key_source:. Your code fetches what it needs:

with {:ok, key} <- WardrikCore.Keys.fetch(context, "BRAVE_API_KEY") do
  Brave.search(key, query)
end
  • Each model call fetches its provider's key, such as ANTHROPIC_API_KEY. With none, ReqLLM uses its config or the environment.
  • Keys are never saved or emitted.
  • A person's own tokens belong in WardrikCore.Credentials.

Summary

Callbacks

Returns the key the request names, {:error, :not_found}, or why it can't.

Functions

Fetches the key name for the agent. Returns {:error, :not_found} if there's none.

Returns the name of the key model's provider takes, or nil.

Returns [api_key: key] for model's provider, or [] if there's no key.

Callbacks

fetch(t, opts)

@callback fetch(WardrikCore.Keys.Request.t(), opts :: term()) ::
  {:ok, String.t()} | {:error, :not_found} | {:error, term()}

Returns the key the request names, {:error, :not_found}, or why it can't.

Functions

fetch(context, name)

@spec fetch(WardrikCore.Context.t(), String.t()) ::
  {:ok, String.t()} | {:error, term()}

Fetches the key name for the agent. Returns {:error, :not_found} if there's none.

key_name(spec)

@spec key_name(LLMDB.Model.t() | String.t()) :: String.t() | nil

Returns the name of the key model's provider takes, or nil.

WardrikCore.Keys.key_name("zai:glm-5.1")
#=> "ZAI_API_KEY"

model_options(context, model)

@spec model_options(WardrikCore.Context.t(), LLMDB.Model.t() | String.t()) ::
  keyword()

Returns [api_key: key] for model's provider, or [] if there's no key.