v148 · origin trial · on-device AI
Prompt API Sampling Parameters
Adds temperature and topK sampling controls to the Prompt API. Developers can now tune the creativity vs. consistency knob on each LanguageModel instance, plus read the static defaults the model ships with.
concepts
-
Sampling Playground
Live sliders for temperature and topK, with a prompt input. Submit and watch the same prompt produce different outputs at different sampling settings. Shows defaults via
LanguageModel.params(). -
Determinism Test
Fix the prompt, pick a temperature, run it N times in a row. Count distinct outputs. The other half of the sampling-parameters story: how to actually get repeatable answers out of the on-device model for tasks that need it.
-
Creative Writing Studio
Choose a preset (Deterministic / Balanced / Default / Creative / Chaotic) or dial your own
temperatureandtopK. The same creative-writing prompt runs twice in parallel — compare how much the outputs diverge at each setting. -
Creative Parameter Explorer
Three side-by-side panels (Deterministic / Balanced / Creative) send the same editable prompt via
LanguageModel.create()with different temperature/topK configs. Custom parameter sliders cover temperature, topK, and topP. A Markov chain simulator provides fallback output when the API is unavailable. -
TopK Visualizer
Canvas chart showing the token probability distribution as you adjust
temperatureandtopKsliders. See exactly how temperature flattens or sharpens logits before softmax, and how the topK cutoff line eliminates the long tail. Token chips show which tokens survive sampling, with entropy and top-1 probability readouts. Five presets from deterministic to chaotic. -
Local-First Workflow
Three real-world app flows — Code Reviewer, Customer Reply, Brainstorm — each with preset parameter recommendations for the task. Dial
temperatureandtopKyourself and see how the model's tone, precision, and diversity change per flow. All on-device. -
Quality Control Panel
Run the same prompt through Deterministic, Balanced, and Creative configurations simultaneously via
Promise.all(). Compare output diversity, latency bars, and word counts side by side. Choose from four prompt presets (poem, fact, list, story) or write your own. -
Safety Harness
12 edge-case tests: API detection, default params read-back, temperature=0 determinism, temperature=2.0 maximum, topK=1 greedy decoding, invalid temperature (negative), invalid topK=0, topK above maxTopK, session reuse, streaming chunks, AbortController cancel, and graceful fallback when the API is absent.
why it exists
Until now developers using the on-device Prompt API had to take whatever sampling behaviour the model shipped with. That meant either consistent-but-boring outputs everywhere, or no way to ask the model to be more deterministic for tasks that need it. temperature and topK are the two standard sampling knobs across most LLM toolkits; exposing them on each LanguageModel instance gives the on-device Prompt API parity with what developers are used to from server-side LLM SDKs.
references
implementation reference
Need the exact API surface, compatibility boundaries, errors, lifecycle, and source links? Read the matching gendn reference ↗