摘要生成器

網頁功能: 摘要工具

Summarizer API 可讓網頁程式開發人員使用 Chrome 的 Gemini Nano 或 Edge 的 Phi,直接在瀏覽器中提供本機 AI 輔助文字摘要功能。這項 API 支援多種格式,包括重點、標題和重點摘要,且完全在裝置端運作,確保使用者隱私。


開始使用

Summarizer API 自 Chrome 和 Edge 138 起提供。需要下載 Gemini Nano 或 Phi 模型 (一次性)。

軟硬體需求

  • OS:Windows 10/11、macOS 13 以上版本、Linux 或 ChromeOS (Chromebook Plus)。
  • 儲存空間:設定檔磁碟區有 22 GB 可用空間。
  • RAM/CPU:16 GB 以上 RAM 和 4 個以上 CPU 核心。
  • VRAM:4 GB 以上 (如果使用 GPU)。

模型下載和可用性

檢查模型是否已就緒、需要下載或無法使用。

必須傳遞選項:您必須將相同的設定選項物件傳遞至 Summarizer.availability(options) 和 Summarizer.create(options)。請勿使用已淘汰的 window.ai.summarizer API 介面。

強制監控進度:您必須監控模型下載進度,方法是提供 monitor(m) 回呼給 Summarizer.create(),並為 downloadprogress 事件新增事件監聽器。

使用者手勢規定:當 availability 為 'downloadable' 或 'downloading' 時,必須透過使用者手勢 (例如使用者點擊) 觸發 Summarizer.create(),才能實際下載。請務必將建立呼叫放在事件監聽器中,而不是在載入網頁時無條件呼叫,以免發生 NotAllowedError。

const options = {
  type: 'key-points',
  format: 'plain-text',
  length: 'medium'
};

const availability = await Summarizer.availability(options);

if (availability === 'available') {
  const summarizer = await Summarizer.create(options);
  // Ready to use immediately
} else if (availability === 'downloadable') {
  // A user gesture is strictly required to start the download
  document.getElementById('start-download-btn').addEventListener('click', async () => {
    const summarizer = await Summarizer.create({
      ...options,
      monitor(m) {
        m.addEventListener('downloadprogress', (e) => {
          console.log(`Downloaded ${Math.round((e.loaded / e.total) * 100)}%`);
        });
      },
    });
  });
}

API 函式和設定

透過 Summarizer.create(options) 建立摘要工具時,您可以自訂輸出內容:

參數 選項 說明
type key-points、tldr、teaser、headline 定義摘要策略。
format markdown、plain-text 輸出語法樣式。
length short、medium、long 目標長度 (例如 1 句或 5 句)。
preference auto、speed、capability 在延遲時間和品質之間取得平衡。

設定範例

const options = {
  sharedContext: 'This is a scientific article',
  type: 'key-points',
  format: 'markdown',
  length: 'medium',
};

if (navigator.userActivation.isActive) {
  const summarizer = await Summarizer.create(options);
}

語言支援

您可以指定預期語言,確保瀏覽器能處理特定摘要要求。

const summarizer = await Summarizer.create({
  type: 'key-points',
  expectedInputLanguages: ['en', 'ja'],
  outputLanguage: 'es',
});

摘要方法

1. 批次摘要

一次處理所有文字並傳回結果。

const longText = document.querySelector('article').innerText;
const summary = await summarizer.summarize(longText, {
  context: 'This article is intended for a tech-savvy audience.',
});
console.log(summary);

2. 串流摘要

模型生成結果時會即時傳回,提供更快速的回應。

const stream = summarizer.summarizeStreaming(longText);
for await (const chunk of stream) {
  console.log(chunk);
}

安全性和權限

  • 資料隱私權:不會將任何資料傳送給 Google,處理作業會在裝置本機進行。
  • 跨來源:可使用權限政策授予 iframe 存取權。

    <iframe src="https://example.com/" allow="summarizer"></iframe>
    
  • Web Worker:目前不支援。

備用策略

初始化或查詢可用性之前,請先檢查瀏覽器是否支援 Summarizer API:

if ('Summarizer' in self) {
  // The Summarizer API is supported.
} else {
  // Execute fallback strategy
}

如果系統不支援 Summarizer API,或可用性檢查傳回 'unavailable',您必須妥善處理。

建議選項:

  1. 遠端 API 備用方案:將精煉要求導向伺服器端點或遠端 API (例如 Vertex AI Gemini API),讓使用者仍可取得摘要。
  2. 優雅降級:在 UI 中以視覺方式停用摘要控制項,或隱藏按鈕並顯示友善訊息 (例如 "Local summarization is currently unsupported in this browser")。請勿允許互動觸發一般未處理的執行階段例外狀況。
  3. Polyfill 備用方案:您可以使用社群維護的 Polyfill (例如 built-in-ai-task-apis-polyfills 或 prompt-api-polyfill),透過雲端模型遠端服務或本機模型裝置端推論,模擬 API 介面。

隱私權和費用影響:這些 polyfill 可能會將要求代理至遠端伺服器 (例如透過雲端傳送至 Gemini API)。這會完全破壞原生內建 AI API 的裝置端隱私權保障,並產生伺服器端 API 使用費用。