Hugging FaceNico MartinTutorial
How to Use Transformers.js in a Chrome Extension
Summary
The guide explains how to build a Chrome extension that runs local AI with Transformers.js under Manifest V3, using a browser assistant as the reference architecture. It assigns model orchestration, lifecycle management, tool execution, and conversation state to a background service worker, while a side panel handles chat and a content script handles page extraction and highlighting. The implementation uses Gemma 4 E2B in q4f16 for text generation and all-MiniLM-L6-v2 in fp32 for embeddings, with WebGPU, DynamicCache-based KV caching, explicit download progress, and reusable pipelines. Typed message contracts, model-output parsing, and separate internal and UI transcripts support a deterministic tool loop, while chrome.storage.local, IndexedDB, and background caches place data according to lifecycle and access needs; the guide concludes that separating these runtimes keeps the UI responsive and avoids duplicate model loads.
Context
The guide addresses how to run local AI features in a Chrome extension under Manifest V3 constraints, where separate runtime contexts must coordinate model inference, user interaction, and page-level actions. It also addresses service-worker suspension, model download and cache handling, permissions, and the placement of conversation, settings, retrieval, and page-content state.
Approach / What changed
The architecture places model initialization, inference, agent orchestration, tool execution, and conversation history in a background service worker. A side panel communicates through typed message contracts and renders updates, while a content script performs DOM extraction and highlighting. Gemma 4 E2B handles text generation and tool decisions; all-MiniLM-L6-v2 produces embeddings. The implementation uses explicit model lifecycle events, reusable pipelines, parsed tool calls, separate internal and UI transcripts, and lifecycle-specific local storage.
Takeaways
The background service worker is the single coordinator for model execution, agent state, and tool calls, while the side panel and content script act as focused clients for interaction and page access.
Gemma 4 E2B uses q4f16 text generation with WebGPU and DynamicCache-based KV caching; all-MiniLM-L6-v2 uses fp32 feature extraction with vector normalization for semantic search.
Conversation history stays in background memory, tool preferences use chrome.storage.local, semantic history vectors use IndexedDB, and extracted page content uses a URL-keyed background cache.
Source and freshness
- Source
- huggingface.co
- Published
- Added to Yomu
- Last updated
Summaries are generated from the original by a language model. Check the original before relying on it.
Keep reading
Related reading
The Good Stuff
Paid picks we use ourselves. Links carry a referral code — a subscription through one earns us a commission at no extra cost to you.
The best engineering reads, once a week
One useful email. Unsubscribe anytime.