HuggingfaceClient.Inference.Provider behaviour
(huggingface_client v0.1.0)
Copy Markdown
View Source
Behaviour that every inference provider module must implement.
Mirrors the TaskProviderHelper abstract class from @huggingface/inference.
Provider anatomy
Each provider is responsible for:
make_route/2– Build the URL path segment (e.g."v1/chat/completions").prepare_payload/2– Transform unified task args into provider-specific body.get_response/3– Validate and normalise the raw JSON response.prepare_headers/3– Build auth/content-type headers.make_base_url/2– Override to route through HF router vs direct provider.
Auth method
The :auth_method key in url_params / header_params can be:
:hf_token– token starts with"hf_", routed through HF router:provider_key– bare API key, sent directly to the provider:credentials_include– browser cookie auth (no explicit token):none– unauthenticated / public endpoints
Summary
Callbacks
When true, the provider only supports direct API key auth.
hf_token auth will raise an InputError.
Validates and normalises the raw decoded JSON response.
Override to customise the base URL (e.g. HF router path for HF-hosted models).
Returns the path segment appended to the base URL.
Builds request headers. binary? is true when the body is raw bytes.
Transforms unified args into the provider-specific request body.
Returns the provider identifier atom (e.g. :groq, :hf_inference).
Used by the routing layer.
Types
@type auth_method() :: :hf_token | :provider_key | :credentials_include | :none
@type header_params() :: %{access_token: String.t() | nil, auth_method: auth_method()}
@type url_params() :: %{ model: String.t(), task: String.t() | nil, auth_method: auth_method(), url_transform: (String.t() -> String.t()) | nil }
Callbacks
@callback client_side_routing_only?() :: boolean()
When true, the provider only supports direct API key auth.
hf_token auth will raise an InputError.
@callback get_response(response :: term(), opts :: map()) :: {:ok, term()} | {:error, HuggingfaceClient.Error.ProviderOutputError.t()}
Validates and normalises the raw decoded JSON response.
Returns {:ok, normalised} or raises a ProviderOutputError.
@callback make_base_url(url_params()) :: String.t()
Override to customise the base URL (e.g. HF router path for HF-hosted models).
@callback make_route(url_params()) :: String.t()
Returns the path segment appended to the base URL.
@callback prepare_headers(header_params(), binary? :: boolean()) :: map()
Builds request headers. binary? is true when the body is raw bytes.
@callback prepare_payload(body_params()) :: map()
Transforms unified args into the provider-specific request body.
@callback provider_id() :: String.t()
Returns the provider identifier atom (e.g. :groq, :hf_inference).
Used by the routing layer.