Expand description
OpenAI-compatible HTTP server (axum + SSE). Talks only to infr-engine — never the GPU.
Reference for the wire mapping (streaming, reasoning_content, tool_calls): the working
shim at ~/Projects/scratch/dgemma-openai-server.py. See docs/plan.md “server”.
Routes (auth = gated by serve.api_key when one is configured — see auth_gate):
GET /health -> 200 OK (open)
GET /v1/models -> { object: “list”, data: [{ id, object, owned_by }] } (auth)
GET /metrics -> Prometheus text exposition (see metrics) (auth)
GET /status -> JSON: per-model backend, device, ctx, slots, memory (auth)
POST /v1/chat/completions -> chat.completion | SSE chat.completion.chunk stream (auth)
Two process-level limits bound one request’s hold on a --parallel slot: serve.max_tokens_cap
(tokens — see clamp_max_tokens) and serve.request_timeout_secs (wall clock — see
request_timeout). Both are serve.* config, never read from the environment here.
Delta mapping:
Delta::Reasoning -> delta.reasoning_content
Delta::Content -> delta.content
Delta::ToolCall -> delta.tool_calls[] (finish_reason “tool_calls”)
Structs§
- AppState
- Shared server state — a non-empty, ordered set of hosted
ModelEntrys. A request is routed to the entry whoseidmatches itsmodelfield; an unknown/emptymodelfalls to the DEFAULT (the first entry). The single-model server is just the one-entry case, so its behaviour — and the hot path — is byte-identical to before this became multi-model. - Assistant
Message - Chat
Completion Chunk - Chat
Completion Response - Chat
Message Dto - A single chat message.
contentmay be a JSON string or a content-part array. - Chat
Outcome - What one
ChatGenerator::chatcall produced: why it ended, plus the real token counts the handler needs for a truthfulusageblock (total = prompt + completion). Counts are what the generator actually tokenized/emitted — never acontent.len()/4estimate. - Chat
Request - Top-level chat completion request (OpenAI wire format).
- Chunk
Choice - Completion
Choice - Context
Length Exceeded - The request’s prompt does not fit the model’s context. A
ChatGeneratorreturns it (as theanyhowerror) so the server can answer400with OpenAI’scontext_length_exceededcode: OpenAI clients retry a5xxunchanged, but treat this code as final and shorten the conversation instead. - Delta
Payload - The
deltafield inside a streaming chunk. Fields absent whenNone. - Device
Memory - A device memory snapshot.
- GenParams
- The validated, per-request generation config handed to
ChatGenerator::chat.Nonefields mean “the request didn’t say” — the generator leaves the process default (INFR_TEMP/INFR_TOP_K/INFR_TOP_P/INFR_MAX_NEW) in charge for exactly those. - Generator
Status - Where a generator runs and how much room it has there, as
crate::ChatGenerator::statusreports it. - Model
Card - Models
Response - OAIFunction
- OAITool
Call - OpenAI-shaped tool call (used in both streaming and non-streaming responses).
- Param
Error - An OpenAI-shaped 400:
{"error":{"message":..,"type":"invalid_request_error","param":..}}. - Prompt
Tokens Details - Stop
Matcher - Incremental stop-sequence matcher over the DECODED text stream.
- Stream
Options - OpenAI
stream_options. Withinclude_usage, a streamed reply ends with one extra chunk that carriesusageand an emptychoicesarray, just before[DONE]. - Usage
Info
Enums§
- Finish
- Why generation ended — the OpenAI
finish_reason. The generator reports it; the handlers serialize it (a tool call still overrides toFinish::ToolCallsat the wire layer).
Traits§
- Chat
Generator - Generation backend the server drives — it never knows the model/GPU underneath. Implemented by
the CLI’s per-arch adapters (
infr-cli’sParallelGeneratoroverinfr_llama::ParallelSeamfor the Vulkan seam;SeamGeneratorwraps anyinfr_llama::ChatModelfor the rest, includingDiffusionGemmaChat— seedocs/diffusion-gemma.md).
Functions§
- build_
router - Build the axum
Router. Extracted so tests can call it with aAppState::headlessstate. - flatten_
content - Flatten a DTO
contentfield (string OR content-part array) to a plainString. - serve
- Start the OpenAI-compatible server bound to
addr, servingengineundermodel_idwithn_parallelconcurrent generation slots (--parallel N). - serve_
multi - Start the server hosting SEVERAL models at once, each routed by its
model_idand admitted with its ownn_parallelslot count (entries[0]is the default route). This is the multi-device host (infr multi): each generator can be pinned to a different physical GPU, and the server dispatches a request to the generator for the model it names. Graceful shutdown drains EVERY model’s in-flight requests before any backend drops (the axum layer stops accepting, then each generation returns at its next abort poll — seeshutdown_latched); whenserve_multireturns, the runtime drops and every backend’s device is destroyed in turn.