appunni/llm-ts-worker

Generic LLM Typescript worker example

0

stars

0

commits

TypeScript

primary language

Jun 5, 2025

updated

appunni.github.io/llm-ts-worker/

README

Generic LLM Worker Example

This example demonstrates how to use the generic-llm-worker.ts to run large language models directly in the browser using HuggingFace Transformers and WebGPU acceleration for conversational AI chat.

πŸš€ Live Demo - Try the app directly in your browser!

πŸ€– Built with GitHub Copilot - This project was developed with the assistance of GitHub Copilot for enhanced productivity and code quality.

Features

  • βœ… Modern Tech Stack: Vite + TypeScript + Tailwind CSS (with Vite plugin)
  • βœ… WebGPU Acceleration: Hardware-accelerated inference
  • βœ… Chat Interface: Conversational AI with context preservation
  • βœ… Real-time Streaming: Live token generation with performance stats
  • βœ… Progress Tracking: Visual progress bars during model loading
  • βœ… Session Management: Conversation history and context preservation
  • βœ… Dark/Light Theme: Automatic theme detection
  • βœ… Responsive Design: Works on desktop and mobile devices

Quick Start

Prerequisites

  • Node.js 18+
  • A modern browser with WebGPU support (Chrome 113+, Edge 113+)
  • At least 2GB of available RAM

Installation

# Install dependencies
npm install

# Start development server
npm run dev

Usage

  1. Check WebGPU Support: The app will automatically detect WebGPU availability
  2. Select a Model: Choose between SmolLM2 360M (lightweight) or 1.7B (better quality)
  3. Load Model: Click "Load Model" and wait for download/initialization
  4. Start Chatting: Type your message and press Enter or click Send

Available Models

ModelSizeDescriptionUse Case
SmolLM2 360M~300MBLightweight, fast responsesQuick tests, mobile devices
SmolLM2 1.7B~1.1GBBetter quality, slowerGeneral purpose, desktop

Performance Tips

  • First Load: Models are downloaded and cached on first use
  • WebGPU: Ensure your browser supports WebGPU for best performance
  • RAM Usage: Close other tabs to free up memory for larger models
  • Temperature: Lower values (0.1-0.3) for focused responses, higher (0.7-1.0) for creative

Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Main Thread   β”‚    β”‚   Web Worker     β”‚    β”‚  HF Transformers β”‚
β”‚   (UI/Events)   │◄──►│ (Model/Compute)  │◄──►│   (Inference)   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Key Components

  • main.ts: UI logic, event handling, worker communication
  • generic-llm-worker.ts: Worker implementation with model management
  • types.ts: TypeScript definitions for type safety
  • style.css: Tailwind CSS directives

Browser Compatibility

BrowserWebGPU SupportStatus
Chrome 113+βœ…Fully supported
Edge 113+βœ…Fully supported
Firefox⏳In development
Safari⏳In development

Troubleshooting

WebGPU Not Supported

  • Update your browser to the latest version
  • Enable WebGPU in browser flags if needed
  • Check hardware compatibility

Model Loading Fails

  • Check internet connection
  • Ensure sufficient RAM (model size + 1GB buffer)
  • Try the smaller 360M model first

Slow Performance

  • Use WebGPU-compatible hardware
  • Close other applications
  • Reduce max_new_tokens in generation config

TypeScript Errors

# Check for type errors
npm run type-check

# Watch mode for development
npm run dev

Configuration

Generation Parameters

You can modify generation settings in main.ts:

generationConfig: {
  max_new_tokens: 512,    // Maximum response length
  temperature: 0.7,       // Randomness (0.0-2.0)
  top_p: 0.9,            // Nucleus sampling
  repetition_penalty: 1.1 // Avoid repetition
}

Model Configuration

To add new models, update DEFAULT_MODELS in generic-llm-worker.ts:

const DEFAULT_MODELS = {
  'my-model': {
    id: "organization/model-name",
    dtype: "q4f16",
    device: "webgpu", 
    size: 1024 * 1024 * 1024, // 1GB
    description: "My custom model"
  }
}

Development

Project Structure

example/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ main.ts              # Main application logic
β”‚   β”œβ”€β”€ generic-llm-worker.ts # Web worker implementation  
β”‚   β”œβ”€β”€ types.ts             # TypeScript definitions
β”‚   └── style.css            # Tailwind CSS directives
β”œβ”€β”€ index.html               # HTML template
β”œβ”€β”€ vite.config.ts           # Vite configuration
└── package.json             # Dependencies and scripts

Scripts

npm run dev        # Start development server
npm run build      # Build for production
npm run preview    # Preview production build
npm run type-check # TypeScript type checking

Vite Configuration

The project uses:

  • @tailwindcss/vite: Modern Tailwind CSS integration (no PostCSS needed)
  • Worker support: ES modules in web workers
  • CORS headers: Required for SharedArrayBuffer/WebGPU

Resources

appunni/llm-ts-worker

Generic LLM Typescript worker example

0

stars

0

commits

TypeScript

primary language

Jun 5, 2025

updated

appunni.github.io/llm-ts-worker/

README

Generic LLM Worker Example

This example demonstrates how to use the generic-llm-worker.ts to run large language models directly in the browser using HuggingFace Transformers and WebGPU acceleration for conversational AI chat.

πŸš€ Live Demo - Try the app directly in your browser!

πŸ€– Built with GitHub Copilot - This project was developed with the assistance of GitHub Copilot for enhanced productivity and code quality.

Features

  • βœ… Modern Tech Stack: Vite + TypeScript + Tailwind CSS (with Vite plugin)
  • βœ… WebGPU Acceleration: Hardware-accelerated inference
  • βœ… Chat Interface: Conversational AI with context preservation
  • βœ… Real-time Streaming: Live token generation with performance stats
  • βœ… Progress Tracking: Visual progress bars during model loading
  • βœ… Session Management: Conversation history and context preservation
  • βœ… Dark/Light Theme: Automatic theme detection
  • βœ… Responsive Design: Works on desktop and mobile devices

Quick Start

Prerequisites

  • Node.js 18+
  • A modern browser with WebGPU support (Chrome 113+, Edge 113+)
  • At least 2GB of available RAM

Installation

# Install dependencies
npm install

# Start development server
npm run dev

Usage

  1. Check WebGPU Support: The app will automatically detect WebGPU availability
  2. Select a Model: Choose between SmolLM2 360M (lightweight) or 1.7B (better quality)
  3. Load Model: Click "Load Model" and wait for download/initialization
  4. Start Chatting: Type your message and press Enter or click Send

Available Models

ModelSizeDescriptionUse Case
SmolLM2 360M~300MBLightweight, fast responsesQuick tests, mobile devices
SmolLM2 1.7B~1.1GBBetter quality, slowerGeneral purpose, desktop

Performance Tips

  • First Load: Models are downloaded and cached on first use
  • WebGPU: Ensure your browser supports WebGPU for best performance
  • RAM Usage: Close other tabs to free up memory for larger models
  • Temperature: Lower values (0.1-0.3) for focused responses, higher (0.7-1.0) for creative

Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Main Thread   β”‚    β”‚   Web Worker     β”‚    β”‚  HF Transformers β”‚
β”‚   (UI/Events)   │◄──►│ (Model/Compute)  │◄──►│   (Inference)   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Key Components

  • main.ts: UI logic, event handling, worker communication
  • generic-llm-worker.ts: Worker implementation with model management
  • types.ts: TypeScript definitions for type safety
  • style.css: Tailwind CSS directives

Browser Compatibility

BrowserWebGPU SupportStatus
Chrome 113+βœ…Fully supported
Edge 113+βœ…Fully supported
Firefox⏳In development
Safari⏳In development

Troubleshooting

WebGPU Not Supported

  • Update your browser to the latest version
  • Enable WebGPU in browser flags if needed
  • Check hardware compatibility

Model Loading Fails

  • Check internet connection
  • Ensure sufficient RAM (model size + 1GB buffer)
  • Try the smaller 360M model first

Slow Performance

  • Use WebGPU-compatible hardware
  • Close other applications
  • Reduce max_new_tokens in generation config

TypeScript Errors

# Check for type errors
npm run type-check

# Watch mode for development
npm run dev

Configuration

Generation Parameters

You can modify generation settings in main.ts:

generationConfig: {
  max_new_tokens: 512,    // Maximum response length
  temperature: 0.7,       // Randomness (0.0-2.0)
  top_p: 0.9,            // Nucleus sampling
  repetition_penalty: 1.1 // Avoid repetition
}

Model Configuration

To add new models, update DEFAULT_MODELS in generic-llm-worker.ts:

const DEFAULT_MODELS = {
  'my-model': {
    id: "organization/model-name",
    dtype: "q4f16",
    device: "webgpu", 
    size: 1024 * 1024 * 1024, // 1GB
    description: "My custom model"
  }
}

Development

Project Structure

example/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ main.ts              # Main application logic
β”‚   β”œβ”€β”€ generic-llm-worker.ts # Web worker implementation  
β”‚   β”œβ”€β”€ types.ts             # TypeScript definitions
β”‚   └── style.css            # Tailwind CSS directives
β”œβ”€β”€ index.html               # HTML template
β”œβ”€β”€ vite.config.ts           # Vite configuration
└── package.json             # Dependencies and scripts

Scripts

npm run dev        # Start development server
npm run build      # Build for production
npm run preview    # Preview production build
npm run type-check # TypeScript type checking

Vite Configuration

The project uses:

  • @tailwindcss/vite: Modern Tailwind CSS integration (no PostCSS needed)
  • Worker support: ES modules in web workers
  • CORS headers: Required for SharedArrayBuffer/WebGPU

Resources

Languages

TypeScript

98.8%

HTML

1.2%