Behaviour for LLM inference providers.
Each provider implements infer/2 which takes a prompt string and returns
the raw LLM response. Parsing and normalization are the caller's responsibility.
Shared helpers for API key resolution and HTTP error mapping are provided for use by provider implementations.
Summary
Functions
Extracts common provider options (model, max_tokens, temperature, base_url) from opts with provider-specific defaults. For use by provider implementations.
Resolves an API key from opts or an environment variable.
Maps a Req response to a provider result tuple.
Merges caller-supplied :req_options into the provider's Req options.
Returns a pre-built HTTP client from opts, or builds one via the given function.
Callbacks
Functions
@spec common_opts(keyword(), keyword()) :: %{ model: String.t(), max_tokens: integer(), temperature: number() | nil, base_url: String.t() }
Extracts common provider options (model, max_tokens, temperature, base_url) from opts with provider-specific defaults. For use by provider implementations.
Resolves an API key from opts or an environment variable.
Returns {:ok, key} or {:error, :missing_api_key} if nil or empty.
For use by provider implementations.
@spec map_response( {:ok, Req.Response.t()} | {:error, Exception.t()}, (term() -> {:ok, String.t()} | {:error, :empty_response}) ) :: {:ok, String.t()} | {:error, term()}
Maps a Req response to a provider result tuple.
Delegates to extract_text for HTTP 200; maps error status codes and
network failures to standard error tuples. For use by provider implementations.
Merges caller-supplied :req_options into the provider's Req options.
Applies shared HTTP defaults first (120s receive timeout, transient
retries), then the provider's own options, then anything in :req_options
— so callers can override any Req configuration (timeouts, retry policy,
pool settings, plug for testing, etc.) without the provider needing to
know about them.
@spec resolve_http_client( keyword(), (keyword() -> {:ok, Req.Request.t()} | {:error, term()}) ) :: {:ok, Req.Request.t()} | {:error, term()}
Returns a pre-built HTTP client from opts, or builds one via the given function.