發布日期:2024 年 11 月 11 日,上次更新日期:2025 年 7 月 30 日
Browser Support
使用者可以將長篇大論、複雜文件,甚至是熱烈的對話內容,濃縮成簡潔精闢的摘要。
您可以使用 Summarizer API,以不同長度和格式生成各種摘要,例如句子、段落、項目符號清單等。我們認為這個 API 適用於下列情境:
- 歸納文章或即時通訊對話的重點。
- 建議文章標題和標題。
- 生成長篇文字的精簡摘要。
- 根據書評生成書籍預告。
開始使用
Summarizer API 可在 Chrome 138 穩定版中使用。
使用這項 API 前,請先詳閱並同意Google 的生成式 AI 使用限制政策。
執行功能偵測,確認瀏覽器是否支援 Summarizer API。
if ('Summarizer' in self) {
// The Summarizer API is supported.
}
查看硬體需求
開發人員和在 Chrome 中使用這些 API 運作功能的使用者,都必須遵守下列規定。其他瀏覽器的操作需求可能不同。
語言偵測器和翻譯器 API 適用於電腦版 Chrome。這些 API 不適用於行動裝置。在 Chrome 中使用 Prompt API、Summarizer API、Writer API 和 Rewriter API 時,須符合下列條件:
- 作業系統:Windows 10 或 11;macOS 13 以上版本 (Ventura 以上版本); Linux;或 ChromeOS (平台 16389.0.0 以上版本) 搭配 [Chromebook Plus](https://www.google.com/chromebook/chromebookplus/) 裝置。 使用 Gemini Nano 的 API 目前不支援 Android 版、iOS 版 Chrome,以及非 Chromebook Plus 裝置上的 ChromeOS。
- 儲存空間:包含 Chrome 設定檔的磁碟區至少要有 22 GB 的可用空間。
- GPU:VRAM 必須超過 4 GB。
- 網路:無限量數據或不計量的連線。
瀏覽器更新模型時,Gemini Nano 的確切大小可能會有所不同。如要判斷目前大小,請前往 chrome://on-device-internals
並前往「模型狀態」。開啟列出的「檔案路徑」,判斷模型大小。
下載模型
摘要產生器 API 使用的模型經過訓練,可生成高品質摘要。這項 API 已內建於 Chrome 中,而 Gemini Nano 是網站首次使用這項 API 時下載的模型。
如要判斷模型是否已可使用,請呼叫非同步 Summarizer.availability()
函式。如果 availability()
的回應為 downloadable
,請監聽下載進度,並告知使用者進度,因為這可能需要一段時間。
const availability = await Summarizer.availability();
如要觸發模型下載作業並建立摘要產生器,請檢查使用者啟用狀態,然後呼叫非同步 Summarizer.create()
函式。
// Proceed to request batch or streaming summarization
const summarizer = await Summarizer.create({
monitor(m) {
m.addEventListener('downloadprogress', (e) => {
console.log(`Downloaded ${e.loaded * 100}%`);
});
}
});
API 函式
您可以利用 create()
函式,根據需求設定新的摘要工具物件。這個函式會採用選用的 options
物件,並包含下列參數:
sharedContext
:可協助摘要工具的其他共用背景資訊。type
:摘要類型,允許的值為key-points
(預設)、tldr
、teaser
和headline
。詳情請參閱下表。format
:摘要格式,允許的值為markdown
(預設) 和plain-text
。length
:摘要長度,允許的值為short
、medium
(預設) 和long
。這些長度的意義會因type
要求而異。舉例來說,在 Chrome 的實作中,簡短重點摘要包含三個項目符號,簡短摘要則是一句話。
設定後即無法變更參數。如要修改參數,請建立新的摘要工具物件。
下表列出不同類型的摘要及其對應長度。這些長度代表可能的最大值,因為有時結果可能會較短。
類型 | 意義 | 長度 | ||||||
---|---|---|---|---|---|---|---|---|
"tldr" |
摘要應簡短扼要,讓忙碌的讀者快速瞭解輸入內容。 |
|
||||||
"teaser" |
摘要應著重於輸入內容最有趣或最引人入勝的部分,吸引讀者繼續閱讀。 |
|
||||||
"key-points" |
摘要應從輸入內容中擷取最重要的重點,並以項目符號清單的形式呈現。 |
|
||||||
"headline" |
摘要應以單一句話有效歸納輸入內容的重點,格式為文章標題。 |
|
舉例來說,您可以初始化摘要工具,以 Markdown 格式輸出中等長度的重點。
const options = {
sharedContext: 'This is a scientific article',
type: 'key-points',
format: 'markdown',
length: 'medium',
monitor(m) {
m.addEventListener('downloadprogress', (e) => {
console.log(`Downloaded ${e.loaded * 100}%`);
});
}
};
const availability = await Summarizer.availability();
if (availability === 'unavailable') {
// The Summarizer API isn't usable.
return;
}
// Check for user activation before creating the summarizer
if (navigator.userActivation.isActive) {
const summarizer = await Summarizer.create(options);
}
摘要工具的執行方式有兩種:串流和批次 (非串流)。
批次摘要
模型會將輸入內容視為一個整體進行處理,然後產生輸出內容。
如要取得批次摘要,請呼叫 summarize()
函式。第一個引數是要總結的文字。第二個選用引數是含有 context
欄位的物件。這個欄位可供您新增背景詳細資料,有助於改善摘要。
const longText = document.querySelector('article').innerHTML;
const summary = await summarizer.summarize(longText, {
context: 'This article is intended for a tech-savvy audience.',
});
串流摘要
串流摘要功能可即時提供結果。
輸入內容新增及調整時,輸出內容會持續更新。如要取得串流摘要,請呼叫 summarizeStreaming()
,而非 summarize()
。
const longText = document.querySelector('article').innerHTML;
const stream = summarizer.summarizeStreaming(longText, {
context: 'This article is intended for junior developers.',
});
for await (const chunk of stream) {
console.log(chunk);
}
示範
您可以在 Summarizer API Playground 中試用 Summarizer API。
權限政策、iframe 和 Web Worker
根據預設,Summarizer API 僅適用於頂層視窗和同源 iframe。您可以使用 Permission Policy allow=""
屬性,將 API 存取權委派給跨來源 iframe:
<!--
The hosting site at https://main.example.com can grant a cross-origin iframe
at https://cross-origin.example.com/ access to the Summarizer API by
setting the `allow="summarizer"` attribute.
-->
<iframe src="https://cross-origin.example.com/" allow="summarizer"></iframe>
目前 Web Workers 不支援 Summarizer API。這是因為要為每位工作人員建立負責文件,以檢查權限政策狀態,相當複雜。
提供意見
我們很想看看您使用 Summarizer API 打造的內容。歡迎在 X、YouTube 和 LinkedIn 上分享您的網站和網路應用程式。