text-generation Β· chat LLM Β· WebLLM (MLC) Β· WebGPU Β· ~1.6 GB
A strong open model from Google, running entirely in your browser. Gemma 2 2B (2.6B parameters) is compiled to WebGPU by MLC's WebLLM engine β you get streaming tokens, sampling controls, a system prompt and full conversation history, with nothing uploaded and no API bill. It's meaningfully more capable than the sub-1B chat models here, and like the other WebLLM model it needs a real GPU: no WASM fallback.
| Model | google/gemma-2-2b-it |
|---|---|
| MLC build | gemma-2-2b-it-q4f16_1-MLC |
| Runtime | WebLLM / MLC β not Transformers.js. Weights are precompiled to WebGPU shaders. |
| Task | text-generation (instruction-tuned chat) |
| Params / dtype | 2.6B Β· q4f16_1 (4-bit weights, fp16 compute) |
| Chat template | <start_of_turn>user / <start_of_turn>model turns β no system role (folded into the first user turn) |
| Backend | WebGPU only β no WASM fallback. Gated on a real GPU adapter. ~2 GB GPU memory. |
| Download | ~1.6 GB, cached after first load |
| Context window | 8192 tokens (Gemma 2 config) |
| License | Gemma Terms of Use |
| Web APIs | WebGPU, Web Workers, ES module imports |
How it differs from the other chat pages: it's the biggest chat model in the catalogue and a Google open model (vs. Meta's Llama 3.2 1B, also WebLLM), and it uses Gemma's turn-based template with no system role. The Transformers.js pages (Qwen 0.5B, TinyLlama 1.1B) are smaller and can fall back to CPU; Gemma trades reach for capability.
An LLM does two very different jobs per turn: it prefills your whole prompt (fast, parallel) and then decodes the answer one token at a time (the part you watch stream). The split below is measured live β time-to-first-token is essentially the prefill cost; everything after is decode. WebLLM reuses a KV cache across turns, so later replies in the same conversation reprocess less of the history.
Send a message to see the messages array that WebLLM receives.
A genuinely useful chat LLM used to mean a server, an API key, and a per-token bill. Gemma 2 2B on WebLLM makes a capable conversational model a client-side primitive: private by construction, free to run, and available offline once the weights are cached β with more headroom for reasoning and structured tasks than the sub-1B models.
A clean streaming chat β the fundamental "talk to the model" loop.
Reasoning & structured extraction: step-by-step answers and strict JSON.
Persona role-play β fold a character into Gemma's first turn and become someone else.
RAG: MiniLM retrieves from a pasted doc, Gemma answers grounded in it.
WebLLM gives you an OpenAI-shaped chat API backed by a WebGPU engine. You create the engine
(which downloads and compiles the weights), then stream a completion. This page uses the shared
helpers in lib/webllm.js, running inside a Web Worker so the UI stays responsive.
Because Gemma 2's template has no system role, we fold the system prompt into the first user turn:
import { webGPUAdapterAvailable, createEngine, streamChat }
from "/lib/webllm.js";
// WebLLM is WebGPU-only β gate before doing anything (headless requestAdapter() β null).
if (!(await webGPUAdapterAvailable())) {
showNeedsWebGPU(); // never a faked reply
} else {
const engine = await createEngine({
model: "gemma-2-2b-it-q4f16_1-MLC",
onProgress: (p) => setProgress(p.text, p.progress), // ~1.6 GB, cached after first load
});
// Gemma 2 has no system role β fold it into the first user message.
const messages = [
{ role: "user", content: "You are a concise assistant.\n\nExplain WebGPU in one sentence." },
];
const full = await streamChat(
engine,
{ messages, temperature: 0.7, top_p: 0.95, max_tokens: 256 },
(delta) => appendToken(delta), // streams token-by-token on your GPU
);
// engine.interruptGenerate() stops decoding early; engine.runtimeStatsText() reports tok/s.
}
Everything here runs exactly that path in worker.js, and gates on a real WebGPU
adapter first. No adapter β a labelled needs-WebGPU state, never a canned answer.