WebLLM: high-performance in-browser LLM inference engine
WebLLM: A High-Performance Inference Engine for the Browser
🌐 Overview
WebLLM is a cutting-edge inference engine designed to execute Large Language Models (LLMs) directly within a web browser. By utilizing hardware acceleration, it eliminates the need for server-side processing.
Everything is handled locally via WebGPU, ensuring that the computation happens on the user's machine. This architecture allows developers to implement an OpenAI-compatible API for various open-source models, supporting advanced features like:
- Real-time streaming
- JSON-mode for structured data
- Function-calling (currently Work-in-Progress)
WebLLM serves as a companion to MLC LLM, a project dedicated to the universal deployment of language models across diverse hardware environments.
By bringing GPU acceleration to the browser, WebLLM enables the creation of private, high-performance AI assistants.
System Architecture Flow
🚀 Key Features
WebLLM provides a robust suite of capabilities for modern web AI development:
- Local Browser Inference: Leverages WebGPU to run powerful models without relying on external servers.
- OpenAI API Parity: Easy integration using familiar OpenAI patterns, including seeding, logit-level control, and streaming.
- Optimized JSON Generation: Uses a high-performance WebAssembly (Wasm) implementation for structured output.
- Try it out: Check the WebLLM JSON Playground on HuggingFace.
- Broad Model Compatibility: Native support for industry-leading models (see the table below).
- Flexible Model Integration: Users can deploy their own custom models provided they are in the
MLCformat. - Developer-Friendly Setup: Modular design compatible with
NPM,Yarn, orCDNfor rapid prototyping. - Asynchronous Execution: Supports Web Workers and Service Workers to prevent UI freezing by offloading heavy math to background threads.
- Browser Extension Ready: Includes blueprints for building Chrome extensions.
📚 Supported Models
WebLLM supports a curated subset of models available via MLC Models. You can find the full list in the prebuiltAppConfig.model_list.
| Model Family | Supported Versions |
|---|---|
| Llama | Llama 3, Llama 2, Hermes-2-Pro-Llama-3 |
| Phi | Phi 3, Phi 2, Phi 1.5 |
| Gemma | Gemma-2B |
| Mistral | Mistral-7B-v0.3, Hermes-2-Pro-Mistral-7B, NeuralHermes-2.5-Mistral-7B, OpenHermes-2.5-Mistral-7B |
| Qwen | Qwen2 (0.5B, 1.5B, 7B) |
Note: If a specific model is missing, you can request it via a GitHub issue or compile your own.
🛠️ Getting Started
Quick Start Checklist
- Install a WebGPU-compatible browser (e.g., Chrome).
- Install the
@mlc-ai/web-llmpackage. - Initialize the
MLCEngine. - Generate your first completion.
Installation
You can add WebLLM to your project using your preferred package manager:
# Using npm
npm install @mlc-ai/web-llm
# Using yarn
yarn add @mlc-ai/web-llm
# Using pnpm
pnpm install @mlc-ai/web-llm
Integration
1. Module Import
Depending on your needs, you can import the entire library or specific functions:
// Import the full library
import * as webllm from "@mlc-ai/web-llm";
// Import only the engine factory
import { CreateMLCEngine } from "@mlc-ai/web-llm";
2. CDN Usage
For environments like JSFiddle or CodePen, use the ESM build:
import * as webllm from "https://esm.run/@mlc-ai/web-llm";
// Or via dynamic import
const webllm = await import("https://esm.run/@mlc-ai/web-llm");
Using the Engine
The primary gateway for all operations is the MLCEngine. To get started, use the CreateMLCEngine() factory function to instantiate the engine and load your chosen model.
In terms of computational complexity, the inference process generally scales with the sequence length , often represented as:
💡 Examples
To see WebLLM in action, explore these resources:
- Basic Chatbot: A simple implementation for generating chat completions.
- WebLLM Chat: A comprehensive, advanced project demonstrating full capabilities.
- Examples Folder: Visit the
/examplesdirectory in the GitHub repository for more use cases.