Web AI Demos

repository·main·Indexed 18 days ago

https://github.com/googlechromelabs/web-ai-demos

A collection of client-side AI demos showcasing ways to run AI models in the browser using Chrome's experimental APIs, MediaPipe, and Transformers.js. Includes the Built-in AI Extension providing polyfills for LanguageModel, Summarizer, Writer, and Rewriter, as well as the built-in-ai-task-apis-polyfills for Summarizer, Writer, Rewriter, Language Detector, Translator, and SemanticEmbedder. Features utilities like FileProxyCache for caching large model files and cos-resource-fetcher for efficient resource retrieval via the Cross-Origin Storage (COS) API.

Tokens
35.4K
Snippets
115
Records
159
Agent score
62%

What's inside web-ai-demos

  1. Overview of Built-in AI Extension features

    main

    The Built-in AI Extension provides polyfills for emerging built-in AI APIs such as LanguageModel, Summarizer, and Writer.

    Key Technical Features:

    • Early Injection: Polyfills are injected at document_start within the MAIN world, ensuring they are available before other scripts run.
    • Force Injection: You can configure the extension to overwrite native APIs even if they are already present in the browser.
    • Configurable Backends: Supports local (Transformers.js) and cloud-based (Gemini, OpenAI, Firebase) processing.
  2. Overview of client-side AI demos

    main

    This repository provides a collection of demonstrations for implementing AI directly in the browser. The demos are categorized into two main approaches:

    1. Chrome Built-in AI: Utilizing experimental APIs provided directly by the Chrome browser (e.g., Prompt API, Summarization API, SemanticEmbedder API).
    2. Generic Client-side AI: Using external libraries and models that run in the browser, such as Transformers.js or Google's Gemma model via MediaPipe.

    Developers can use these demos to understand how to implement features like text summarization, semantic search, sentiment analysis, and speech recognition without relying on heavy server-side infrastructure.

  3. Overview of Sensitive Data Validator

    main
    The Sensitive Data Validator is a client-side chat guard designed to detect sensitive information in support chat messages before they are transmitted. It leverages Chrome's local Gemini Nano via the Prompt API to perform detection entirely on the user's machine. Because it runs locally, no data leaves the machine and no API keys are required.
  4. Understand the technology used in Toxic Comment Warning

    main

    This demo implements client-side AI (Web AI) to detect toxic comments in real-time.

    Key technical characteristics:

    • Engine: Uses Transformers.js for text classification.
    • Execution: Runs entirely in the browser (client-side).
    • Concurrency: Uses a Web Worker to run the model, ensuring the main thread remains responsive.
    • Communication: Implements message passing between the worker and the main thread to provide model readiness status updates to the user.
  5. Project structure of the Local LLM Judge evaluation

    main

    The evaluation tool consists of the following key files:

    • compare_judge.js: The primary execution script. It orchestrates the evaluation by invoking llama-cli to run the local LLM judge.
    • reference-dataset.jsonc: Contains the ground truth or reference data used for comparison. In this implementation, the reference evaluations are generated by Gemini Flash (cloud).
    • few-shots.jsonc: Contains 3 few-shot examples that are injected into the prompt templates to guide the local LLM judge.
  6. Understand the Local AI Smart Invoice workflow

    main

    The Invoice Engine is a zero-cloud, multimodal vision parser. It uses Chrome's built-in Gemini Nano via the Prompt API to process images locally.

    Workflow: Image $\rightarrow$ Gemini Nano (Multimodal vision) $\rightarrow$ Structured Invoice Fields

    This process allows for automatic extraction of sender, receiver, dates, line items, and VAT without any data leaving the user's machine or requiring external API keys.

  7. Use the new Chrome Built-in AI API naming conventions

    main

    The window.ai object is deprecated. Chrome Built-in AI APIs are now accessed via separate top-level objects. When writing code, ensure you use the new global identifiers instead of the old window.ai namespace.

    Deprecated vs. New Mappings:

    • window.ai.languageModel $\rightarrow$ window.LanguageModel
    • window.ai.summarizer $\rightarrow$ window.Summarizer
    • window.ai.writer $\rightarrow$ window.Writer
    • window.ai.Rewriter $\rightarrow$ window.Rewriter
    • window.ai.translator $\rightarrow$ window.Translator
    • window.ai.languageDetector $\rightarrow$ window.LanguageDetector

    Additionally, the capabilities() function is deprecated; use availability() instead.

  8. Check Model Availability

    main

    Before attempting to create a model or skill, check its availability using the availability() method. This prevents errors when models are not yet downloaded or supported.

    Possible Availability values:

    • "unavailable": The feature/model is not supported.
    • "downloadable": The model is supported but needs to be downloaded.
    • "downloading": The model is currently being downloaded.
    • "available": The model is ready to use.
    const status = await LanguageModel.availability();
    if (status === 'available') {
      const model = await LanguageModel.create();
    } else if (status === 'downloadable') {
      // Handle download logic
    }
  9. WebMCP Evals Architecture and Components

    main

    WebMCP Evals is designed to automate the comparison of actual tool calls against expected tool calls.

    Core Components:

    • Evaluation Loop: The engine that executes test cases against a model and verifies the output.
    • Backends: Implementations for different LLM providers. Currently supports Google GenAI (Gemini) and experimental Ollama support.
    • Schemas: Uses JSON schemas to define tools and evaluation test cases.

    Project Structure:

    • src/bin/runevals.ts: The main entry point for the evaluation runner.
    • src/backend/: Contains backend implementations like googleai.ts and ollama.ts.
    • src/types/: Contains TypeScript definitions for tools, messages, and evaluations.
    • examples/: Contains reference implementations, such as the travel/ directory which includes tools_schema.json and evals.json.
  10. Understand data handling in the Built-in AI Extension

    main

    The Built-in AI Extension acts as a proxy between web pages and AI backends. Your privacy and data handling behavior depend on the backend type you select in the Extension's settings:

    Local AI (Transformers.js)

    • Zero Data Retention: All processing occurs locally on your device within the browser's execution context.
    • Local Storage: Models are downloaded to and stored in your browser's local cache.
    • Privacy: No prompt data, generated text, or model-related information is sent to external servers.

    Cloud-Based Backends (Google Gemini, OpenAI, Firebase)

    • Data Transmission: Prompt data is sent directly from your browser to the respective cloud provider's API.
    • Provider Policies: Data usage is governed by the provider's policies (e.g., Google Privacy Policy for Gemini/Firebase, or OpenAI Privacy Policy).
    • No Intermediate Collection: The Extension does not log, monitor, or store prompts or responses on intermediate servers.
  11. Implementation details of Product review suggestions

    main

    The demo is built using client-side AI (in-browser) with the following architecture:

    • Model: Uses the Gemma 2B model.
    • Inference Engine: Uses the MediaPipe LLM Inference API via the @mediapipe/tasks-genai npm package.
    • Threading: Runs the model in a Web Worker to prevent blocking the main thread.
    • Communication: Implements message passing between the worker and the main thread to provide real-time model readiness status updates to the user.
  12. Configure the Prompt API Polyfill backend

    main

    The polyfill selects a backend based on which global configuration object is set on the window object before the polyfill is imported. If no configuration is provided, it defaults to Transformers.js.

    Supported Backends

    BackendGlobal Config KeyDescription
    Firebase AI Logicwindow.FIREBASE_CONFIGCloud-based via firebase/ai SDK.
    Google Gemini APIwindow.GEMINI_CONFIGCloud-based via @google/generative-ai SDK.
    OpenAI APIwindow.OPENAI_CONFIGCloud-based via openai SDK.
    Transformers.jswindow.TRANSFORMERS_CONFIGDefault. Local execution via @huggingface/transformers.
    WebLLMwindow.WEBLLM_CONFIGLocal execution via @mlc-ai/web-llm (WebGPU).