Private & Fast: Building a Local-First Mental Health Assistant with WebLLM and WebGPU 🧠💻 A developer has built a local-first mental health assistant using WebLLM, TVM Unity, and React, which runs Llama 3 models directly in the browser via WebGPU. The assistant, designed for Cognitive Behavioral Therapy, keeps all sensitive data on the user's device, ensuring privacy and low latency. The project demonstrates the feasibility of edge AI for sensitive applications. Privacy is no longer just a feature; it's a human right—especially when it comes to mental health. Imagine a Cognitive Behavioral Therapy CBT assistant that lives entirely in your browser, never sends a single byte of your conversation to a remote server, and runs at lightning speed. Thanks to the explosion of Edge AI and the maturing WebGPU standard, this isn't science fiction anymore. In this tutorial, we will explore how to use WebLLM , TVM Unity , and React to build a high-performance, offline-capable mental health assistant. By leveraging WebLLM and Local-first AI principles, we can provide low-latency support while keeping sensitive user data exactly where it belongs: on the user's device. Mental health data is incredibly sensitive. Using traditional LLM APIs like OpenAI or Claude means sending private thoughts to the cloud. By using WebGPU acceleration , we can run models like Llama 3 or Mistral directly on the client's GPU via the browser. The magic happens through TVM Unity , which compiles machine learning models into high-performance kernels that the browser can execute via the WebGPU API. php graph TD A User Input -- B React UI State B -- C WebLLM Worker subgraph Browser Environment C -- D TVM Runtime D -- E WebGPU API E -- F Local GPU / VRAM F -- G Model Inference G -- D end D -- H Streaming Response H -- B I IndexedDB Cache -.- C To follow along, you’ll need: React , WebLLM , and Vite .First, let's install the core dependencies: npm install @mlc-ai/web-llm react The heart of our application is the ChatWorker . We want to run the LLM in a Web Worker to ensure the UI remains responsive during heavy inference. js // engine.ts import { CreateWebWorkerEngine, type ChatOptions } from "@mlc-ai/web-llm"; const SELECTED MODEL = "Llama-3-8B-Instruct-q4f16 1-MLC"; export async function initializeEngine onProgress: p: any = void { // This downloads the model and initializes the WebGPU pipeline const engine = await CreateWebWorkerEngine new Worker new URL "./worker.ts", import.meta.url , { type: "module" } , SELECTED MODEL, { initProgressCallback: onProgress } ; return engine; } For a mental health assistant, the system prompt is everything. We need to steer the model toward Cognitive Behavioral Therapy techniques—identifying cognitive distortions and suggesting reframing exercises. js const CBT SYSTEM PROMPT = You are a supportive Mental Health Assistant specialized in Cognitive Behavioral Therapy CBT . Your goal is to help users identify negative thought patterns. Rules: 1. Be empathetic and non-judgmental. 2. If a user mentions self-harm, immediately provide emergency resources. 3. Use Socratic questioning to help users reach their own conclusions. 4. Keep data privacy top-of-mind: remind users this is a local-only session. ; Now, let's build a custom hook to manage the chat state and the engine lifecycle. 🚀 js // useWebLLM.ts import { useState, useEffect } from 'react'; import as webllm from "@mlc-ai/web-llm"; export function useWebLLM { const engine, setEngine = useState