About

Google announced that they are putting Gemini Nano — a lightweight Gemini Based LLM into Chrome and will be exposing the interface on the browser with window.ai.

The feature is currently experimental in the Chrome browser. You’ll need to have the latest version of Chrome, probably best to try it either with dev or canary build, and you’ll need to turn on certain flags.

If the above check says error, follow the instruction below to enable this feature on your Chrome.

  • chrome://flags/#prompt-api-for-gemini-nano: Enabled
  • chrome://flags/#optimization-guide-on-device-model: Enabled BypassPrefRequirement
  • chrome://components/: Click Optimization Guide On Device Model to download the model.

The API

The APIs are actually quite simple.

Check if the browser is supported.

await window.ai.canCreateTextSession();

Create a text session.

const session = await window.ai.createTextSession();

Execute the prompt.

await session.prompt("Why is Ruby better than Python?");

Execute the prompt and returns a ReadableStream object

await session.promptStreaming("Why is Ruby better than Python?");