clientollama

package module
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 30, 2026 License: MIT Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

client-ollama

Typed Ollama API client for omcrgnt apps: Embed, Generate, ProbeReady (via Heartbeat) over an already-instrumented client-http client. No config of its own — Label/BaseURL live entirely on the client-http.Client it wraps.

Wraps github.com/ollama/ollama/api — the official Ollama Go SDK — instead of hand-rolling request/response marshaling.

Catalog

import (
	clienthttp "github.com/omcrgnt/client-http"
	clientollama "github.com/omcrgnt/client-ollama"
)

type catalog struct {
	OllamaHTTP *clienthttp.Client   `ecfg:"OLLAMA"`
	Ollama     *clientollama.Client
}

Client depends on the injected *clienthttp.Client (Deps/Inject) — same slot, same env vars (OLLAMA_LABEL, OLLAMA_BASE_URL) as any other client-http consumer. See client-http's README for those.

Lifecycle

Hook Role
NewResource Zero *Client; Inject/StandBy fill it in
Deps / Inject Store the *clienthttp.Client pointer only — no computation here
StandBy Build the Ollama SDK client from HTTPClient() + BaseURL()
ProbeReady Heartbeat (HEAD /) — cheap, side-effect-free reachability check

Inject deliberately does nothing but store the pointer: sdi.Resolve runs Inject across every resource in one pass ordered by registration, not by the Deps() graph, so client-http.Client's own Inject isn't guaranteed to have run yet. StandBy runs later, once sdi.Resolve has finished entirely (every resource's Inject has run) — see the lifecycle safety rule in github.com/omcrgnt/app's package doc. Building the SDK client in Inject instead would reintroduce that ordering race.

No Config/BuildConfig, no runner.Starter, no Close: nothing here owns an env-configured identity or does I/O — StandBy is a sequential, zero-I/O hook, not a runner.Starter, and always returns a nil cleanup. See runner.StandBy's doc comment for why a nil cleanup is fine here (it names this package as the example).

API

vec, err := c.Embed(ctx, "all-minilm", "some text")

reply, err := c.Generate(ctx, "llama3", prompt, json.RawMessage(`"json"`))

Generate's schema parameter maps directly to Ollama's structured-output format field: nil for plain text, json.RawMessage("json") for unstructured-but-valid JSON, or a JSON Schema object for strict structure. Non-streaming only (Stream: false) — one response in, one string out.

Metrics & tracing

Come entirely from the wrapped client-http.Client — every Embed/ Generate/ProbeReady call goes through its instrumented transport, so http_client_request_duration_seconds{client="...",...} and otel spans already cover this client. Nothing extra to wire.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a typed Ollama API client (Embed/Generate/Heartbeat) over an already-instrumented client-http.Client. It has no ecfg config of its own: Label and BaseURL live entirely on the injected client-http.Client, same slot the app configures via ecfg (e.g. `ecfg:"OLLAMA"`).

Catalog field: *Client (ResourceFactory).

func (*Client) Deps

func (*Client) Deps() []any

func (*Client) Embed

func (c *Client) Embed(ctx context.Context, model, text string) ([]float32, error)

func (*Client) Generate

func (c *Client) Generate(ctx context.Context, model, prompt string, schema json.RawMessage) (string, error)

Generate produces a single, non-streaming response for prompt using model. schema constrains the response via Ollama's structured-output "format" field: pass json.RawMessage(`"json"`) for unstructured-but-valid JSON, a JSON Schema object for strict structure, or nil for plain text.

func (*Client) Inject

func (c *Client) Inject(args []any)

Inject only stores the dependency pointer. sdi.Resolve runs Inject across every resource in a single pass ordered by registration, not by the Deps() graph — a ResourceFactory (this type) is always registered before any Configurable (client-http.Client) materializes, regardless of catalog field order. Reading dep.HTTPClient() here would observe it before client-http.Client's own Inject has set it. StandBy runs later, once sdi.Resolve has finished entirely (every resource's Inject has run), which is where dep's state is safe to consume.

func (*Client) NewResource

func (*Client) NewResource() (any, error)

func (*Client) ProbeReady

func (c *Client) ProbeReady(ctx context.Context) error

ProbeReady reports whether the Ollama backend is reachable (ops/probe duck typing — see client-http's README for why this lives here and not on client-http itself: readiness is API-specific, and client-http has no idea what "up" means for the API it transports).

func (*Client) StandBy added in v0.2.0

func (c *Client) StandBy() (func(context.Context) error, error)

StandBy builds the Ollama SDK client from the now fully-wired client-http.Client. It runs once, sequentially, after sdi.Resolve and before runner.Run starts the concurrent Start phase — no I/O happens here, so there is no ordering hazard to guard against the way there would be for a runner.Starter. It always returns a nil cleanup — see runner.StandBy's doc comment, which names this exact type as the example of a StandBy with nothing to undo.

The url.Parse error below is not expected to trigger when this Client was wired through app.Bootstrap's normal pipeline: ecfg.LoadEnv validates BaseURL via protovalidate (http.v1.URL's string.uri rule) before Config.Build ever runs. That is a calling-convention guarantee, not a type-safety one — clienthttp.Config's own fields are exported and Config.Build itself only rejects an empty string, so anything calling Build directly (as this package's own tests do) bypasses ecfg entirely. See TestMarkClientHTTPLoadEnvRejectsMalformedBaseURL, which pins the ecfg.LoadEnv side of this assumption, and TestClient_StandBy_malformedBaseURL, which exercises this error branch directly via that same bypass.

No nil-guard on c.http here (unlike apiClient's): Deps declares it as a single required dependency, so sdi.Resolve errors out before runner.Run's StandBy phase is ever reached if it's missing — StandBy is only called by that same trusted pipeline, not by arbitrary callers the way the business methods below are.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址