Skip to main content

ChatGenerator

Trait ChatGenerator 

Source
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§

Source

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.

  • tools is the request’s tools array as a borrowed serde_json::Value — passed by reference so the generator parses the ONE it was already given instead of a Value→string→Value round-trip (see audit finding 6).
  • cancel is a PER-REQUEST abort latch. The server sets it when the client hangs up (streaming or not) 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 to max_tokens. An Arc so 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 ChatOutcome carrying the finish reason AND the real prompt/completion token counts so the handler can populate usage truthfully.
  • A request whose prompt does not fit the context should fail with a ContextLengthExceeded error. The server answers it as a 400 on a non-streaming request and as a terminal SSE error frame carrying the same typed error on a streaming one (the 200 is already sent by then), so the client can act on it rather than retry it.
Source

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".

Implementors§