Skip to main content

Crate weirdnet_openai

Crate weirdnet_openai 

Source
Expand description

A loopback-only OpenAI-compatible bridge over a weirdnet-agent Pool.

Local tools that speak the OpenAI API (editors, notebooks, curl) reach the user’s inference nodes through it: /v1/chat/completions and /v1/completions (streaming or not), /v1/models (the pool catalog), /v1/embeddings, /v1/audio/speech and /v1/audio/transcriptions, plus Prometheus /metrics.

It binds 127.0.0.1 only, checks the Host header against its own address (DNS rebinding), requires a bearer token of at least 16 characters (compared in constant time), and sends no CORS headers, so web pages can’t use it. Answers carry x-weirdnet-model, and x-weirdnet-cache: hit and x-weirdnet-standin when the pool says so; a request with x-wait: 1 waits for its model instead of a stand-in. A request skips the nodes’ answer cache with "cache": false or x-weirdnet-cache: off, and refreshes it with "cache": "refresh" or x-weirdnet-cache: refresh (infer/2 Request::cache, weirdnet-agent 0.5.1).

Chat messages may carry image (image_url, a data:image/... URL), audio (input_audio) and video (video_url, a data:video/... URL; remote URLs are refused, never fetched) parts. They go to the pool unchanged, which sends them only to a model whose catalog entry takes them; when none does the answer is 400. /v1/models lists weirdnet.vision, weirdnet.audio and weirdnet.video per model (also together as weirdnet.inputs), weirdnet.purpose (chat, embed, transcribe, speak, …), weirdnet.speech for a speaking model that reports a profile (catalog/1 CatalogModel::speech, weirdnet-agent 0.5.11), weirdnet.base for a LoRA adapter (its base model), and weirdnet.origin (hub, upload or train) and weirdnet.license when the node says (catalog/1 CatalogModel::origin/license, weirdnet-agent 0.5.4; null otherwise). A body’s "adapter_scale" sets how strongly an adapter applies for that request (infer/2 Request::adapter_scale, weirdnet-agent 0.5.2; llama.cpp nodes only).

Bodies may be up to DEFAULT_MAX_REQUEST_BYTES (32 MiB) unless the bridge is set otherwise (Bridge::with_max_request_bytes). A larger body is refused with 413 (request_too_large) whose message names the limit, and the x-weirdnet-max-request-bytes header carries it. /v1/models reports it per model as weirdnet.max_request_bytes (the smaller of the bridge’s and the node’s), with the model’s media limits under weirdnet.limits (catalog/1 CatalogModel::limits, weirdnet-agent 0.5.7; only the limits the node knows).

POST /v1/audio/speech speaks text over infer/2 Speak (request::speech lists the body): the request is checked against the catalog first, so a voice the model lacks (OpenAI’s alloy, say) is a 400 that lists the model’s presets, never mapped to another voice. The answer’s Content-Type is the real format (audio/pcm;rate=24000 for raw PCM), with x-audio-sample-rate when the node knows it; stream_format: "audio" streams the bytes as they come. POST /v1/audio/transcriptions (multipart: file, model, language, prompt, response_format json, verbose_json, text, srt or vtt, and timestamp_granularities[]=segment) runs infer/2 Transcribe. Both are bounded by the same request limit (a 413 with x-weirdnet-max-request-bytes); neither is cached.

/v1/completions sends the prompt as an infer/2 raw prompt (weirdnet-agent 0.4.1); nodes that can’t take one answer 501. A streamed answer waits up to EARLY_META_WAIT after a node accepts it for early metadata, so x-weirdnet-cache: hit is sent on streams too.

Modules§

request
OpenAI request bodies to weirdnet-agent infer/2 requests.

Structs§

Bridge
A bound, not yet serving, bridge.
BridgeOptions
Where the bridge listens and the token it requires.

Constants§

DEFAULT_MAX_REQUEST_BYTES
The largest request body a bridge accepts unless set otherwise: 32 MiB.
EARLY_META_WAIT
How long a streamed answer’s headers wait, after a node accepted it, for early metadata (a cache hit) or the first output. A node that sends neither in time gets headers without it.

Functions§

new_token
A new random bridge token (32 bytes, hex).
status_of
The HTTP status and code for a pool error.