Generic LLM Typescript worker example
0
stars
0
commits
TypeScript
primary language
Jun 5, 2025
updated
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.
# Install dependencies
npm install
# Start development server
npm run dev
| Model | Size | Description | Use Case |
|---|---|---|---|
| SmolLM2 360M | ~300MB | Lightweight, fast responses | Quick tests, mobile devices |
| SmolLM2 1.7B | ~1.1GB | Better quality, slower | General purpose, desktop |
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β Main Thread β β Web Worker β β HF Transformers β
β (UI/Events) βββββΊβ (Model/Compute) βββββΊβ (Inference) β
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
main.ts: UI logic, event handling, worker communicationgeneric-llm-worker.ts: Worker implementation with model managementtypes.ts: TypeScript definitions for type safetystyle.css: Tailwind CSS directives| Browser | WebGPU Support | Status |
|---|---|---|
| Chrome 113+ | β | Fully supported |
| Edge 113+ | β | Fully supported |
| Firefox | β³ | In development |
| Safari | β³ | In development |
max_new_tokens in generation config# Check for type errors
npm run type-check
# Watch mode for development
npm run dev
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
}
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"
}
}
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
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
The project uses:
@tailwindcss/vite: Modern Tailwind CSS integration (no PostCSS needed)TypeScript
98.8%
HTML
1.2%
Generic LLM Typescript worker example
0
stars
0
commits
TypeScript
primary language
Jun 5, 2025
updated
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.
# Install dependencies
npm install
# Start development server
npm run dev
| Model | Size | Description | Use Case |
|---|---|---|---|
| SmolLM2 360M | ~300MB | Lightweight, fast responses | Quick tests, mobile devices |
| SmolLM2 1.7B | ~1.1GB | Better quality, slower | General purpose, desktop |
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β Main Thread β β Web Worker β β HF Transformers β
β (UI/Events) βββββΊβ (Model/Compute) βββββΊβ (Inference) β
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
main.ts: UI logic, event handling, worker communicationgeneric-llm-worker.ts: Worker implementation with model managementtypes.ts: TypeScript definitions for type safetystyle.css: Tailwind CSS directives| Browser | WebGPU Support | Status |
|---|---|---|
| Chrome 113+ | β | Fully supported |
| Edge 113+ | β | Fully supported |
| Firefox | β³ | In development |
| Safari | β³ | In development |
max_new_tokens in generation config# Check for type errors
npm run type-check
# Watch mode for development
npm run dev
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
}
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"
}
}
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
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
The project uses:
@tailwindcss/vite: Modern Tailwind CSS integration (no PostCSS needed)TypeScript
98.8%
HTML
1.2%