On-device generative AI with Gemini Nano (with demo)

In this hands-on, I try to use Google Chrome's upcoming on-device AI feature. This feature is powered by Gemini Nano and is currently only available in the canary (v128+) or dev version of the browser.

If you'd like to try out the API, please feel free to request to join the early access via the form in this link.


How to enable it?

1) In Chrome Canary, go to chrome://flags/

  • Set "Enables optimization guide on device" to Enabled BypassPerfRequirement
  • Set "Prompt API for Gemini Nano" to Enabled

2) Go to chrome://components/ and check if the "Optimization Guide On Device Model" is updated


Using the API

3) First you need to check if the API is available to use by using the canCreateTextSession() API. As of the time this article is written, the result is a string value. If it is not a "no" then this browser is supported.

    async connect() {
        if (!window.ai) {
            return
        }
        const canCreate = await window.ai.canCreateTextSession();
        if (canCreate !== "no") {
            this.summaryTarget.classList.remove("collapse"); // unhide div
        }
    }

4) Next, we can try to send a prompt and receive the response, using createTextSession() and session.prompt() API

    async ask() {
        const session = await window.ai.createTextSession();
        const prompt = "Your prompt";
        this.outputTarget.textContent = await session.prompt(prompt);
    }

Demo

If you open this page in a supported browser, you will notice an experimental section shown at the bottom of the page.

It is configured to summarise the article you are viewing in English or Simplified Chinese.

For example, in this article, I try to summarize in English and Chinese, and I got:


Is it really "on-device"?

You can try to turn off your wifi and hit the English or Chinese link again. It should still work. 


AI Summary
gpt-4o-2024-05-13 2024-07-16 00:40:00
The blog details how to use Google Chrome's upcoming on-device AI feature, Gemini Nano, available in the Canary or Dev version (v128+). It provides steps for enabling the feature, checking API availability, and using it to generate text prompts. An example demo is included.
Chrome On-device AI 2024-10-22 08:39:02

Share Article