Optional Plug middleware for Phoenix / Plug applications.
Injects a pre-configured HuggingfaceClient.Client into the conn assigns
so controllers don't have to create a client on every request.
Usage in Phoenix
# In your endpoint or router:
plug HuggingfaceClient.Plug,
token_from: {:env, "HF_TOKEN"},
assign_as: :hf_client,
provider: "groq"
# In a controller:
def generate(conn, params) do
{:ok, resp} = HuggingfaceClient.chat_completion(conn.assigns.hf_client, %{
model: "meta-llama/Llama-3.1-8B-Instruct",
messages: [%{role: "user", content: params["prompt"]}]
})
json(conn, resp)
endToken sources
The :token_from option controls where the access token is read:
{:env, "VAR_NAME"}— read from OS environment at startup (default:{"env", "HF_TOKEN"}){:config, :app, :key}— read fromApplication.get_env/2{:static, "hf_..."}— hardcoded value (testing only):conn_assign— read fromconn.assigns[assign_as <> "_token"]nil— no token (public models only)