[小技巧] chrome开启本地模型local ai - gemini nano

date
Jan 21, 2025
slug
tip-chrome-local-gemini-nano-ai
status
Published
summary
小技巧
tags
tip
type
Post
URL

Problem & Summary

  • chrome://flags/#prompt-api-for-gemini-nano
notion image

Solution

下载Dev版本

Enable Gemini Nano and the Prompt API

Follow these steps to enable Gemini Nano and the Prompt API flags for local experimentation:
  1. Go to chrome://flags/#prompt-api-for-gemini-nano
  1. Select Enabled
  1. Relaunch Chrome.
 

BypassPerfRequirement

chrome://flags/#optimization-guide-on-device-model
选择 Enabled BypassPerfRequirement

下载模型

DevTools console:
 
allow pasting
await window.ai.createTextSession()
await ai.summarizer.create();
await ai.summarizer.capabilities();

# check
(await ai.languageModel.capabilities()).available;
notion image

Confirm availability of Gemini Nano

  1. Open DevTools and send (await ai.languageModel.capabilities()).available; in the console.
If this returns “readily”, then you are all set.

Test

notion image
  • in devtools console:
const canSummarize = await ai.summarizer.capabilities();
let summarizer;
if (canSummarize && canSummarize.available !== 'no') {
  if (canSummarize.available === 'readily') {
    // The summarizer can immediately be used.
    summarizer = await ai.summarizer.create();
  } else {
    // The summarizer can be used after the model download.
    summarizer = await ai.summarizer.create();
    summarizer.addEventListener('downloadprogress', (e) => {
      console.log(e.loaded, e.total);
    });
    await summarizer.ready;
  }
} else {
    // The summarizer can't be used at all.
}

check model download:

chrome://components/
notion image

if “Uncaught NotSupportedError: The model attempted to output text in an untested language, and was prevented from doing so.”

disable: chrome://flags/#text-safety-classifier
 

© Ying Bun 2021 - 2025