You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
WebLLM provider for local-first LLM inference. Uses 4-bit quantized models for efficient browser-based text generation.
Installation
pnpm install @localmode/webllm @localmode/core
Quick Start
import{generateText,streamText}from'@localmode/core';import{webllm}from'@localmode/webllm';// Generate textconst{ text, usage }=awaitgenerateText({model: webllm.languageModel('Llama-3.2-1B-Instruct-q4f16_1-MLC'),prompt: 'Explain quantum computing in simple terms.',});console.log(text);console.log(`Generated in ${usage.durationMs}ms`);
Streaming
import{streamText}from'@localmode/core';import{webllm}from'@localmode/webllm';constresult=awaitstreamText({model: webllm.languageModel('Llama-3.2-1B-Instruct-q4f16_1-MLC'),prompt: 'Write a haiku about programming.',});forawait(constchunkofresult.stream){process.stdout.write(chunk.text);}
Model Preloading
import{preloadModel,isModelCached,deleteModelCache}from'@localmode/webllm';// Check if model is already cachedif(!(awaitisModelCached('Llama-3.2-1B-Instruct-q4f16_1-MLC'))){// Preload with progressawaitpreloadModel('Llama-3.2-1B-Instruct-q4f16_1-MLC',{onProgress: (p)=>console.log(`Loading: ${p.progress?.toFixed(1)}%`),});}// Delete cached modelawaitdeleteModelCache('Llama-3.2-1B-Instruct-q4f16_1-MLC');
import{streamText}from'@localmode/core';import{webllm}from'@localmode/webllm';constmodel=webllm.languageModel('Phi-3.5-vision-instruct-q4f16_1-MLC');console.log(model.supportsVision);// trueconstresult=awaitstreamText({
model,prompt: '',messages: [{role: 'user',content: [{type: 'text',text: 'What is in this image?'},{type: 'image',data: base64Data,mimeType: 'image/jpeg'},],}],});
Structured Output (JSON mode)
Forward MLC's OpenAI-compatible response_format via providerOptions.webllm to force schema-conforming JSON through XGrammar-constrained decoding — far more reliable than prompting a small model for JSON:
import{generateObject,jsonSchema}from'@localmode/core';import{webllm}from'@localmode/webllm';import{z}from'zod';constschema=jsonSchema(z.object({name: z.string(),age: z.number()}));const{ object }=awaitgenerateObject({model: webllm.languageModel('Qwen3-1.7B-q4f16_1-MLC'),
schema,prompt: 'Generate a profile for a software engineer named Alex',providerOptions: {webllm: {response_format: {type: 'json_object',schema: JSON.stringify(schema.jsonSchema)}},},});
Custom Configuration
import{createWebLLM}from'@localmode/webllm';constmyWebLLM=createWebLLM({onProgress: (p)=>updateLoadingBar(p.progress),});constmodel=myWebLLM.languageModel('Llama-3.2-1B-Instruct-q4f16_1-MLC',{systemPrompt: 'You are a helpful coding assistant.',temperature: 0.5,maxTokens: 1024,});
Default Settings
Setting
Default
Description
temperature
0.7
Sampling temperature
topP
0.95
Nucleus sampling threshold
maxTokens
512
Maximum tokens to generate
contextLength
4096
Context window size
useIndexedDBCache
false
Use IndexedDB instead of Cache API for model storage. Useful for Chrome extensions with MV3 restrictions where Cache.add() can fail during multi-gigabyte downloads.
cacheBackend
'cache'
Explicit cache backend selection ('cache', 'indexeddb', or 'cross-origin'). Overrides useIndexedDBCache when set.
appConfig
undefined
Custom WebLLM AppConfig passed to CreateMLCEngine() for advanced model configuration.
Utilities
import{preloadModel,isModelCached,deleteModelCache,getModelSize,isWebGPUAvailable,}from'@localmode/webllm';// Check WebGPU supportconstgpuAvailable=awaitisWebGPUAvailable();// Get estimated model size in bytesconstsize=getModelSize('Llama-3.2-1B-Instruct-q4f16_1-MLC');// Delete cached model dataawaitdeleteModelCache('Llama-3.2-1B-Instruct-q4f16_1-MLC');
Requirements
WebGPU support (Chrome 113+, Edge 113+)
Sufficient GPU memory for the model
Some models (SmolLM2-1.7B, Gemma 2 2B) require the shader-f16 WebGPU extension, which is not available on all devices (e.g., Qualcomm/Android). Use q4f32_1 variants as fallbacks for broader compatibility.
Acknowledgments
This package is built on WebLLM by MLC AI — high-performance LLM inference in the browser with WebGPU.