pub trait ChatGenerator: Send + Sync {
// Required methods
fn chat(
&self,
messages: &[ChatMessage],
tools: Option<&Value>,
tool_choice: Option<&str>,
params: &GenParams,
cancel: &Arc<AtomicBool>,
on_delta: &mut dyn FnMut(Delta),
) -> Result<ChatOutcome>;
fn status(&self) -> GeneratorStatus;
}Expand description
Generation backend the server drives — it never knows the model/GPU underneath. Implemented by
the CLI’s per-arch adapters (infr-cli’s ParallelGenerator over infr_llama::ParallelSeam for
the Vulkan seam; SeamGenerator wraps any infr_llama::ChatModel for the rest, including
DiffusionGemmaChat — see docs/diffusion-gemma.md).
GenParams carries the request’s PER-REQUEST sampling config (temperature/top_p/top_k/seed/
penalties/stop/max_tokens). Every field is an Option whose None means “inherit the process
default” — so a request that sends nothing generates EXACTLY as it did before this existed.
&self, Send + Sync. This is the whole concurrency contract: the server calls chat from
N request tasks at once and the generator is responsible for its own slot allocation and GPU
turn-taking. It used to be &mut self behind an Arc<Mutex<_>> the handlers held for an ENTIRE
generation, which meant request #2 waited for request #1 to finish — head-of-line blocking, no
parallelism. An implementation that genuinely cannot run concurrently (CPU / Metal / diffusion
today) keeps an internal Mutex and is served with --parallel 1, which is honest rather than
silently serialising a server the user asked to parallelise.
Required Methods§
Sourcefn chat(
&self,
messages: &[ChatMessage],
tools: Option<&Value>,
tool_choice: Option<&str>,
params: &GenParams,
cancel: &Arc<AtomicBool>,
on_delta: &mut dyn FnMut(Delta),
) -> Result<ChatOutcome>
fn chat( &self, messages: &[ChatMessage], tools: Option<&Value>, tool_choice: Option<&str>, params: &GenParams, cancel: &Arc<AtomicBool>, on_delta: &mut dyn FnMut(Delta), ) -> Result<ChatOutcome>
Run one chat turn.
toolsis the request’stoolsarray as a borrowedserde_json::Value— passed by reference so the generator parses the ONE it was already given instead of aValue→string→Valueround-trip (see audit finding 6).cancelis a PER-REQUEST abort latch. The server sets it when the client disconnects (an SSEsendstarts failing) or the request deadline fires; the generator must poll it in its prefill AND decode loops and stop promptly so the GPU slot is freed rather than held tomax_tokens. AnArcso the generator can keep it for the whole call — a deadline that fires during prefill comes before any piece, so a copy made from a streaming callback is too late. It is ORed with the process-wide shutdown latch, never a replacement for it.- Returns a
ChatOutcomecarrying the finish reason AND the real prompt/completion token counts so the handler can populateusagetruthfully.
Sourcefn status(&self) -> GeneratorStatus
fn status(&self) -> GeneratorStatus
Where this generator runs and how much room it has, read fresh on each call, for
GET /status and the device gauges in /metrics. Report None for what the backend
cannot see rather than a placeholder. It must not wait on a generation: both routes call it
while requests are running.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".