InferHub.Client
0.5.0
See the version list below for details.
dotnet add package InferHub.Client --version 0.5.0
NuGet\Install-Package InferHub.Client -Version 0.5.0
<PackageReference Include="InferHub.Client" Version="0.5.0" />
<PackageVersion Include="InferHub.Client" Version="0.5.0" />
<PackageReference Include="InferHub.Client" />
paket add InferHub.Client --version 0.5.0
#r "nuget: InferHub.Client, 0.5.0"
#:package InferHub.Client@0.5.0
#addin nuget:?package=InferHub.Client&version=0.5.0
#tool nuget:?package=InferHub.Client&version=0.5.0
InferHub.Client
A small, typed .NET client for InferHub — a self-hosted, Ollama-compatible inference mesh.
Point it at a coordinator, pass a Bearer token, and call chat, generate, model listing and status from C# with typed requests, dependency injection, and no heavy dependencies.
v0.4.0 — blocking + streaming inference, embeddings (batch + legacy), and the vector data plane (upsert / query / retrieve / get / delete). RAG retrieval and admin follow in later phases.
Install
dotnet add package InferHub.Client
Targets net9.0 and net10.0.
Quick start
using InferHub.Client;
using InferHub.Client.Configuration;
using InferHub.Client.Models.Ollama;
using Microsoft.Extensions.DependencyInjection;
var services = new ServiceCollection();
services.AddInferHubClient(o =>
{
o.BaseAddress = new Uri("http://localhost:5080");
o.ApiKey = "<your-client-api-key>";
});
var provider = services.BuildServiceProvider();
var client = provider.GetRequiredService<IInferHubClient>();
var chat = await client.ChatAsync(new ChatRequest
{
Model = "llama3",
Messages = new[]
{
new ChatMessage { Role = "user", Content = "Say hi in one word." }
},
Stream = false
}, CancellationToken.None);
Console.WriteLine(chat.Message?.Content);
What ships in v0.4.0
| Method | Endpoint |
|---|---|
ListModelsAsync |
GET /api/tags |
GenerateAsync (blocking) |
POST /api/generate with stream:false |
ChatAsync (blocking) |
POST /api/chat with stream:false |
ChatStreamAsync |
POST /api/chat with stream:true (NDJSON → IAsyncEnumerable<ChatResponse>) |
GenerateStreamAsync |
POST /api/generate with stream:true (NDJSON → IAsyncEnumerable<GenerateResponse>) |
EmbedAsync |
POST /api/embed (batch — single string or string[]) |
EmbedLegacyAsync |
POST /api/embeddings (legacy single prompt) |
UpsertAsync |
POST /api/vector/{collection}/upsert |
QueryAsync |
POST /api/vector/{collection}/query |
RetrieveAsync |
POST /api/vector/{collection}/retrieve |
GetRecordAsync |
GET /api/vector/{collection}/{id} (→ null on 404) |
DeleteRecordAsync |
DELETE /api/vector/{collection}/{id} (→ false on 404) |
GetStatusAsync |
GET /api/status |
PingAsync |
GET /health |
Streaming
await foreach (var chunk in client.ChatStreamAsync(new ChatRequest
{
Model = "llama3",
Messages = new[] { new ChatMessage { Role = "user", Content = "Stream me a haiku." } }
}, cancellationToken))
{
Console.Write(chunk.Message?.Content);
}
The enumerator stops as soon as a chunk arrives with done:true. A terminal error
chunk ({ "error": …, "done": true }) is surfaced as InferHubException — the client
never retries mid-stream, so a partial answer plus a clean exception is the contract.
Cancelling the token throws promptly out of the await foreach.
Request models carry an extension bag (AdditionalProperties), so any unknown fields
from the Ollama contract pass through untouched — you can hand-set options, format,
tool definitions, etc. without waiting on the client to type them.
Embeddings
// Single input.
var single = await client.EmbedAsync(
EmbedRequest.FromText("nomic-embed-text", "hello, world"));
// Batch — one vector per input, same order.
var batch = await client.EmbedAsync(EmbedRequest.FromTexts(
"nomic-embed-text",
new[] { "InferHub", "self-hosted", "inference mesh" }));
Console.WriteLine(batch.Embeddings.Length); // 3
Console.WriteLine(batch.Embeddings[0].Length); // model dimension
EmbedAsync targets the modern batch endpoint (/api/embed); EmbedLegacyAsync wraps
/api/embeddings for drop-in Ollama callers. An empty vector list on a 200 response is
treated as malformed and surfaced as InferHubException — the client never returns a
silent zero-vector result.
Vectors
Text in, ranked matches out. The coordinator embeds text on a node for you, so you never
have to hold a model client-side. Needs the coordinator running with VectorStore:Enabled=true
and the collection already created (admin plane, later phase).
using InferHub.Client.Models.Vector;
// Upsert — embed text on a node, keep the original as an opaque payload.
await client.UpsertAsync("docs", VectorUpsert
.FromText("doc-1", "InferHub is a self-hosted inference mesh.", "nomic-embed-text")
.WithPayload(new { title = "About" })
.WithMetadata(new Dictionary<string, string> { ["kind"] = "doc" }));
// Query — text in, closest matches out.
var matches = await client.QueryAsync("docs",
VectorQuery.FromText("what is InferHub?", "nomic-embed-text", k: 3));
foreach (var m in matches)
Console.WriteLine($"{m.Score:F3} {m.Id}");
// Read the payload back into your own type.
var record = await client.GetRecordAsync("docs", "doc-1"); // null if absent
var title = record?.Payload.As<Doc>()?.Title;
await client.DeleteRecordAsync("docs", "doc-1"); // false if it wasn't there
Pass a raw vector instead of text with VectorUpsert.FromVector / VectorQuery.FromVector.
payload is exposed as a JsonElement?; call .As<T>() to deserialize it. GetRecordAsync
returns null and DeleteRecordAsync returns false on a 404; every other non-success
status is an InferHubException. RetrieveAsync is the same call as QueryAsync under the
RAG-oriented name. See samples/MiniRag for a runnable embed-then-query loop.
Auth
- Non-loopback calls need
ApiKey(attached asAuthorization: Bearer <key>by aDelegatingHandler). - Loopback calls to the coordinator skip auth by default (unless the coordinator sets
Auth:RequireAuthForLoopback=true). /healthis always open.- Admin routes require a separate
AdminApiKeyand land on a dedicated interface in a later phase; a client key alone never surfaces admin methods.
Errors
Any non-success HTTP response is surfaced as InferHubException, carrying:
StatusCode— the HTTP statusMessage— the coordinator's{ "error": "…" }body if present
The client treats 404 (model missing) and 424 Failed Dependency (retrieval unavailable,
added in a later phase) as distinct signals worth checking with StatusCode.
Links
- InferHub server: https://github.com/Dev-Art-Solutions/InferHub
- Product page: https://inferhub.devart.solutions
- Blog: https://blog.devart.solutions
License
MIT — see LICENSE.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.0)
- Microsoft.Extensions.Http (>= 10.0.0)
-
net9.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.0)
- Microsoft.Extensions.Http (>= 10.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.