HuggingfaceClient.Inference.HTTPClient behaviour (huggingface_client v0.1.0)

Copy Markdown View Source

Behaviour for the underlying HTTP client, enabling Mox-based testing.

In production the default adapter (HuggingfaceClient.Inference.HTTPClient.ReqAdapter) delegates to Req. In tests, set:

# config/test.exs
config :huggingface_client, :http_client, MyApp.MockHTTPClient

Then in your test module:

Mox.defmock(MyApp.MockHTTPClient, for: HuggingfaceClient.Inference.HTTPClient)

Example mock

import Mox

test "chat completion returns expected response", %{} do
  expect(MyApp.MockHTTPClient, :post, fn _url, _opts ->
    {:ok, %{status: 200, body: %{
      "id" => "test",
      "created" => 0,
      "model" => "test-model",
      "choices" => [%{"message" => %{"role" => "assistant", "content" => "Hi"}}],
      "usage" => %{}
    }, headers: []}}
  end)

  client = HuggingfaceClient.client("hf_test")
  assert {:ok, resp} = HuggingfaceClient.chat_completion(client, %{
    model: "test/model",
    messages: [%{role: "user", content: "hello"}]
  })
  assert resp["choices"] |> hd() |> get_in(["message", "content"]) == "Hi"
end

Summary

Types

Simplified response type returned by the HTTP client

Callbacks

Perform an HTTP GET request.

Perform an HTTP POST request.

Types

response()

@type response() :: %{status: non_neg_integer(), body: term(), headers: list()}

Simplified response type returned by the HTTP client

Callbacks

get(url, opts)

@callback get(url :: String.t(), opts :: keyword()) ::
  {:ok, response()} | {:error, term()}

Perform an HTTP GET request.

post(url, opts)

@callback post(url :: String.t(), opts :: keyword()) ::
  {:ok, response()} | {:error, term()}

Perform an HTTP POST request.