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.MockHTTPClientThen 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
Types
@type response() :: %{status: non_neg_integer(), body: term(), headers: list()}
Simplified response type returned by the HTTP client