window.ai: Built-in AI from Devtools

Introduction 🚀

Developing AI features for the web often relies on server-side solutions due to the large size of models, which is impractical in some cases. To address this, a new Javascript API was introduced for integrating AI models like Gemini Nano directly into browsers.

This enables websites to perform AI tasks locally using window.ai and without even an internet connection.

The new feature is currently in Chrome canary (experimental feature) which means, it will be here soon ⏳

Make sure that Chrome Canary is installed on your machine


1. Enable "Prompt API for Gemini Nano"

Open Chrome Canary and enter "chrome://flags/" in the URL bar, then press Enter.

In the search box at the top, type "prompt API."

You should see "Prompt API for Gemini Nano" as the only option and switch it to Enabled.

2. Enable "Enables optimization guide on device"

On the same page, you need to enable a second item

Search now for "optimization guide on", you should see "Enables optimization guide on device" as the only option.

Enable it, selecting the "Enabled ByPassPerfRequirement" option.

3. Install Gemini Nano in Canary

As the final step, we need to install Gemini Nano on our device (The file is 1.5GB).

Go to "chrome://components/" and search for "Optimization Guide".

You will see an item called "Optimization Guide On Device Model". Click "Check for Update" to install the file.

Wait until is shows "up to date" and the version.


After this, reload Chrome Canary for the changes to take effect.

4. Feel free with window.ai

At this step, open the console of Devtools and start by creating a new chat session using createTextSession method.

const session = await window.ai.createTextSession()

Now, send a prompt using .prompt method on session object.

const response = await session.prompt("hello, what's the sum of 353 and 776")

Wait until the processing of the prompt finishes, then type response to view the result.

' The sum of 353 and 776 is 1,130.'

Isn't cool ! 😎

Finally, this feature is still in experimental mode but who knows once it'll be available it could be used maybe to develop "custom GPTs" and we could see amazing creations 🤩

Conclusion ✅

If you found this article is valuable and useful, share it. 🙏♻