LLM.Provider behaviour (LLM v0.1.2)

Copy Markdown View Source

Provider configuration behaviour.

A provider is either:

  • A map with :adapter, :base_url, and optionally :api_key
  • A module implementing the LLM.Provider behaviour

Built-in provider modules (LLM.Provider.OpenAI, LLM.Provider.Anthropic, etc.) implement this behaviour and provide sensible defaults for their respective APIs.

Implementing a custom provider

defmodule MyApp.Provider do
  @behaviour LLM.Provider

  @impl true
  def name, do: :my_custom

  @impl true
  def default_config do
    %{
      adapter: LLM.Adapter.OpenAI,
      base_url: "https://my-api.example.com/v1",
      api_key: Application.get_env(:my_app, :api_key)
    }
  end
end

Then use it:

LLM.generate("Hello", provider: MyApp.Provider, model: "my-model")

Summary

Callbacks

Return the default configuration map for this provider.

Return the provider name as an atom (e.g., :openai, :anthropic).

Types

config()

@type config() :: %{
  adapter: module(),
  base_url: String.t(),
  api_key: String.t() | nil,
  http_opts: keyword()
}

Callbacks

default_config()

@callback default_config() :: config()

Return the default configuration map for this provider.

Must include :adapter and :base_url. The :api_key should be fetched from LLM.get_key/1 or application config.

name()

@callback name() :: atom()

Return the provider name as an atom (e.g., :openai, :anthropic).