最新资讯
dev.to 我们在 DolphinBench 上测试记忆检索:纯关键词搜索暴露了自身回归 dev.to 别再把 Agent 写成 while 循环,把它建模为状态机 bloomberg.com Rightmove 与 Scout24 借 AI 产品应对低迷房市并上调展望 stackoverflow.blog 欢迎来到芯片时代:半导体制造、AI 数据中心与芯片设计的未来 www.youtube.com AI Agent 总犯同样的错:CentralAgent AI 将记忆重构为经验系统 pub.towardsai.net Netflix 科学家建议重采样 LLM 评判:5 次仅将误差棒缩小 13% pub.towardsai.net Jev 作为 LLM 前的静默层:构建提示感知的模型路由器 reddit.com OpenAI 称 Astra 仅满足 10% 安全任务标准 dev.to FreeToken:在自有 GPU 上运行前沿 MoE 模型及其内存需求 dev.to 当 AI Agent 说“完成”时,究竟完成了什么? dev.to 部署 Underdog Bark 3.0 Pro:逐字转写、自定义后端与分块设置 dev.to 为艰难对话构建 AI:五个 Agent、一条安全正则与一位盲评监督者
多元动态
登录 注册
大模型 dev.to · @Beck_Moulton

超越API:用WebGPU和WebLLM构建隐私优先的药物交互工具

原文 我的收藏

教程展示如何利用WebGPU和WebLLM在浏览器中本地运行LLM,实现毫秒级药物兼容性检查,数据不出设备。架构上避免云端API,确保数据驻留和边缘计算性能。

AI推荐理由 教程介绍使用WebGPU和WebLLM构建隐私优先的药物交互工具,技术方案有创新性,但来源为个人博客,且医疗应用需谨慎。

65

正文

In the era of cloud-hosted AI, we’ve become comfortable sending our most sensitive data to remote servers. But when it comes to medical queries—like checking for drug-to-drug interactions—privacy isn't just a feature; it's a human right. 🛡️ With the recent explosion of WebGPU AI and the maturation of local LLMs, we can finally move the "brain" of our applications directly into the user's browser. In this tutorial, we are building a high-performance, browser-based AI tool that uses WebLLM and WebGPU to perform millisecond-level drug compatibility checks. No data ever leaves the device, ensuring 100% data residency and lightning-fast edge computing performance. The Architecture: Why WebGPU? Traditionally, running a Large Language Model (LLM) required a massive Python backend with expensive GPUs. WebGPU changes the game by providing low-level access to the local graphics card directly from the browser. WebLLM leverages this to run models like Llama-3 or Mistral in the browser sandbox. System Data Flow graph TD UserInput[User Inputs Medications] -->|React State| Engine[WebLLM Engine Instance] Engine -->|Compute Shaders| WebGPU[WebGPU API] WebGPU -->|Parallel Processing| LocalGPU[Device VRAM/GPU] LocalGPU -->|Token Generation| Engine Engine -->|Streamed Response| UI[React Frontend Display] subgraph Browser_Sandbox Engine WebGPU UI end subgraph Privacy_Boundary Browser_Sandbox end ExternalServer((Cloud / Internet)) -.->|Data Never Sent| Privacy_Boundary Enter fullscreen mode Exit fullscreen mode Prerequisites 🛠️ To follow this advanced guide, you'll need: Tech Stack: React (v18+), TypeScript, Vite. Library: @mlc-ai/web-llm. Hardware: A GPU supporting WebGPU (Latest Chrome/Edge/Arc). Step 1: Initializing the WebLLM Engine First, we need to create a singleton or a hook to manage our AI engine. Since loading a model (~2GB-5GB) takes time, we need to handle the progress state effectively. // useWebLLM.ts import { useState, useEffect } from "react"; import * as webllm from "@mlc-ai/web-llm"; export function useWebLLM(modelId: string) { const [engine, setEngine] = useState(null); const [loadingProgress, setLoadingProgress] = useState(0); const initEngine = async () => { const engineInstance = new webllm.MLCEngine(); // Callback to track model download/loading progress engineInstance.setInitProgressCallback((report) => { setLoadingProgress(Math.round(report.progress * 100)); }); await engineInstance.reload(modelId); setEngine(engineInstance); }; return { engine, loadingProgress, initEngine }; } Enter fullscreen mode Exit fullscreen mode Step 2: Crafting the Interaction Logic Drug interaction retrieval requires a high degree of accuracy. We will use a structured system prompt to ensure the LLM acts as a clinical pharmacist. const SYSTEM_PROMPT = ` You are a clinical pharmacy expert. Your task is to analyze two or more medications and identify potential drug-to-drug interactions. Provide the output in the following format: 1. Interaction Level (Mild, Moderate, Severe) 2. Mechanism of Action 3. Recommendation Be concise and stick to clinical facts. If no interaction is found, state so. `; const checkInteractions = async (engine: webllm.MLCEngine, drugs: string[]) => { const userPrompt = `Check interactions for: ${drugs.join(", ")}`; const messages: webllm.ChatCompletionMessageParam[] = [ { role: "system", content: SYSTEM_PROMPT }, { role: "user", content: userPrompt } ]; const chunks = await engine.chat.completions.create({ messages, stream: true, // We want that sweet typewriter effect! }); return chunks; }; Enter fullscreen mode Exit fullscreen mode Step 3: The React UI Layer We want a clean, professional interface that handles the streaming response and gives users confidence. import React, { useState } from 'react'; import { useWebLLM } from './hooks/useWebLLM'; const DrugChecker = () => { const { engine, loadingProgress, initEngine } = useWebLLM("Llama-3-8B-Instruct-v0.1-q4f16_1-MLC"); const [drugs, setDrugs] = useState(""); const [result, setResult] = useState(""); const handleConsult = async () => { if (!engine) return; setResult(""); // Clear previous const stream = await checkInteractions(engine, drugs.split(",")); for await (const chunk of stream) { const content = chunk.choices[0]?.delta?.content || ""; setResult((prev) => prev + content); } }; if (!engine) { return ( Initialize Secure Local AI Loading Model: {loadingProgress}% ); } return ( 🛡️ Local Drug Interaction Checker setDrugs(e.target.value)} /> Analyze Privately {result || "Analysis will appear here..."} ); }; Enter fullscreen mode Exit fullscreen mode The "Official" Way 🥑 Building a proof-of-concept is easy, but making Edge AI production-ready involves handling model caching, VRAM memory management, and specialized RAG (Retrieval-Augmented Generation) architectures to ensure medical data is up to date. For more advanced patterns on optimizing WebGPU shaders, managing local vector databases, and high-performance AI deployment strategies, I highly recommend checking out the WellAlly Technology Blog. It's the source of inspiration for this build and contains deeper dives into "Local-First" software engineering. Performance & Privacy Trade-offs 📊 Latency: Once the model is loaded into the browser cache (IndexedDB), the "Time to First Token" is often faster than a round-trip to OpenAI's servers. Cost: Your server costs drop to zero. The user provides the compute. 💸 Security: Since there is no backend API for the drug query, there is no log to breach. This is the ultimate HIPAA-compliant architecture by design. Conclusion The browser is no longer just a document viewer; it's a powerful AI execution environment. By combining WebGPU and WebLLM, we can build tools that were impossible a year ago. Next Steps: Try adding a local vector store (like Voy) to allow the AI to search through updated FDA documentation locally. Implement model-switching logic to use smaller models for simpler queries. What are you planning to build with WebGPU? Let me know in the comments below! 👇

来源报道 · 1 篇 · 以下链接均前往原始媒体

← 返回精选动态 原文

更多内容