DEV Community

wellallyTech
wellallyTech

Posted on

Your Browser, Your Doctor: Building a Private Drug Interaction Engine with WebLLM 💊💻

Privacy in the age of AI is a massive concern, especially when dealing with sensitive medical data. Sending your prescription history or a photo of a medication leaflet to a cloud-based server feels like a gamble with your personal information.

But what if we could process everything—from text extraction to complex reasoning—entirely within the browser? Welcome to the era of Edge AI. In this tutorial, we are building a high-performance, fully offline drug interaction checker using WebLLM, Transformers.js, and a local vector index. By leveraging Local Vector Databases and browser-native inference, we can ensure that zero bytes of medical data ever leave your machine.

Why Browser-Native AI? 🥑

Building a drug interaction engine locally isn't just about privacy; it's about Edge AI performance. Traditional LLM calls involve network latency and high token costs. By running models like Llama 3 or Mistral directly in the browser via WebGPU, we achieve:

  1. Absolute Privacy: Medical data stays in the client's RAM.
  2. Offline Availability: Works in remote clinics or during flight mode.
  3. Zero API Costs: Your GPU does the work, not your wallet.

The Architecture: Local RAG (Retrieval-Augmented Generation)

To check for drug interactions, we can't just feed a 50-page manual to an LLM. We need to chunk the data, embed it, and query the most relevant sections.

graph TD
    A[Drug Leaflet PDF/Text] --> B[Transformers.js]
    B -->|Embedding| C[Local Vector Index]
    D[User Query: Can I take Ibuprofen with this?] --> E[Transformers.js]
    E -->|Query Vector| F{Vector Search}
    C --> F
    F -->|Relevant Context| G[WebLLM Engine]
    G -->|Reasoning| H[Interaction Alert/Safe Confirmation]
    style G fill:#f96,stroke:#333,stroke-width:2px
    style C fill:#bbf,stroke:#333,stroke-width:2px
Enter fullscreen mode Exit fullscreen mode

Prerequisites

Before we dive into the code, ensure you have a modern browser with WebGPU support (Chrome 113+ or Edge).

  • WebLLM: For running the LLM in the browser.
  • Transformers.js: For generating vector embeddings locally.
  • Voyage AI Embeddings: Used as our high-fidelity reference model for indexing.

Step 1: Initializing the Local Embedding Engine

We use Transformers.js to run a feature-extraction pipeline. This turns clinical text into mathematical vectors that we can store in our local index.

import { pipeline } from '@xenova/transformers';

class EmbeddingPipeline {
    static instance = null;

    static async getInstance() {
        if (this.instance === null) {
            // Using a lightweight model for fast browser performance
            this.instance = await pipeline('feature-extraction', 'Xenova/all-MiniLM-L6-v2');
        }
        return this.instance;
    }
}

async function getEmbedding(text) {
    const embedder = await EmbeddingPipeline.getInstance();
    const output = await embedder(text, { pooling: 'mean', normalize: true });
    return Array.from(output.data);
}
Enter fullscreen mode Exit fullscreen mode

Step 2: Setting up WebLLM for Clinical Reasoning

WebLLM allows us to load massive models like Llama-3-8B-Instruct into the browser's WebGPU cache. This is the "brain" that will analyze the drug interactions.

import * as webllm from "@mlc-ai/web-llm";

const selectedModel = "Llama-3-8B-Instruct-q4f16_1-MLC";

const engine = await webllm.CreateMLCEngine(selectedModel, {
    initProgressCallback: (report) => console.log(report.text),
});

async function runInference(context, query) {
    const messages = [
        { role: "system", content: "You are a medical safety assistant. Use the provided leaflet context to check for drug interactions." },
        { role: "user", content: `Context: ${context}\n\nQuestion: ${query}` }
    ];

    const reply = await engine.chat.completions.create({ messages });
    return reply.choices[0].message.content;
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Local Vector Search Logic

Since we are keeping it 100% offline, we use a simple Cosine Similarity search over an IndexedDB or a local array to find the most relevant "Drug Warnings" or "Contraindications" from the leaflet.

function cosineSimilarity(vecA, vecB) {
    let dotProduct = 0;
    let normA = 0;
    let normB = 0;
    for (let i = 0; i < vecA.length; i++) {
        dotProduct += vecA[i] * vecB[i];
        normA += vecA[i] * vecA[i];
        normB += vecB[i] * vecB[i];
    }
    return dotProduct / (Math.sqrt(normA) * Math.sqrt(normB));
}
Enter fullscreen mode Exit fullscreen mode

The "Official" Way to Scale Production AI 🚀

While this browser-native approach is revolutionary for privacy, building production-grade healthcare AI requires more robust patterns for data synchronization and model fine-tuning.

For deeper insights into high-performance vector search architectures and production-ready RAG patterns, I highly recommend checking out the advanced guides over at WellAlly Tech Blog. They cover everything from hybrid cloud-edge strategies to optimizing LLM inference for specific domains.

Step 4: Putting it All Together

In your Chrome Extension or Web App, the workflow looks like this:

  1. Parse: Extract text from a medication PDF.
  2. Embed: Use getEmbedding() on chunks of 500 characters.
  3. Search: When a user asks "Can I drink grapefruit juice with this?", find the top 3 most similar chunks.
  4. Reason: Pass those chunks to runInference() and get a clinical summary.

Sample Code for Interaction Check:

async function checkSafety(userDrug, leafletText) {
    const chunks = chunkText(leafletText); // Simple split by paragraph
    const userVector = await getEmbedding(userDrug);

    // Find best match in the local vector DB
    const results = chunks.map(chunk => ({
        text: chunk,
        score: cosineSimilarity(userVector, await getEmbedding(chunk))
    })).sort((a, b) => b.score - a.score);

    const context = results.slice(0, 2).map(r => r.text).join("\n");
    const advice = await runInference(context, `Is it safe to take ${userDrug}? Explain why based on the text.`);

    return advice;
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

We've just built a fully private, offline-first medical AI. By combining WebLLM with Transformers.js, we turn the browser into a powerful inference engine that respects user privacy. This pattern—Edge RAG—is the future of personalized AI.

What do you think? Will local LLMs replace cloud APIs for sensitive tasks like healthcare? Let me know in the comments! 👇


If you enjoyed this deep dive, don't forget to ❤️ and bookmark! For more advanced Edge AI patterns, visit the WellAlly Blog. 🚀

Top comments (0)