發布日期:2024 年 11 月 11 日,上次更新日期:2025 年 5 月 20 日
說明 | 網頁 | 額外資訊 | Chrome 狀態 | Intent |
---|---|---|---|---|
GitHub | 查看 | 實驗意圖 |
透過 Prompt API,您可以將自然語言要求傳送至瀏覽器中的 Gemini Nano。
您可以在 Chrome 擴充功能中透過多種方式使用 Prompt API。例如:
- 即時日曆活動。開發 Chrome 擴充功能,自動從網頁擷取活動詳細資料,讓使用者只需幾個步驟就能建立日曆項目。
- 輕鬆擷取聯絡人資訊:建立擴充功能,從網站擷取聯絡資訊,方便使用者與商家聯絡,或將詳細資料新增至聯絡人清單。
- 動態內容篩選。建立 Chrome 擴充功能,分析新聞文章,並根據使用者定義的主題自動模糊處理或隱藏內容。
以上僅列舉幾個例子,我們很期待看到你的創作。
查看硬體需求
開發人員和使用者在 Chrome 中使用這些 API 運作功能時,必須遵守下列規定。其他瀏覽器的操作規定可能不同。
語言偵測器和翻譯器 API 適用於電腦版 Chrome。這些 API 不適用於行動裝置。在 Chrome 中使用 Prompt API、Summarizer API、Writer API 和 Rewriter API 時,須符合下列條件:
- 作業系統:Windows 10 或 11;macOS 13 以上版本 (Ventura 和後續版本);或 Linux。目前 Android 版、iOS 版和 ChromeOS 版 Chrome 尚未支援使用 Gemini Nano 的 API。
- 儲存空間:包含 Chrome 設定檔的磁碟區至少要有 22 GB 的空間。
- GPU:視訊記憶體必須超過 4 GB。
- 網路:無限量數據或不計量的連線。
Gemini Nano 的確切大小可能略有不同。如要查看目前大小,請前往 chrome://on-device-internals
並點選「Model status」(模型狀態)。開啟列出的「檔案路徑」,判斷模型大小。
在擴充功能中使用 Prompt API
使用這項 API 前,請先詳閱並同意《Google 生成式 AI 使用限制政策》。
LanguageModel
命名空間提供兩種擴充函式:
availability()
,查看模型的功能和可用性。create()
即可開始語言模型工作階段。
下載模型
Prompt API 會使用 Chrome 中的 Gemini Nano 模型。雖然 API 已內建於 Chrome,但擴充功能首次使用 API 時,系統會另外下載模型。
如要判斷模型是否已可使用,請呼叫非同步 LanguageModel.availability()
函式。這應該會傳回下列其中一項回應:
"unavailable"
表示實作項目不支援所要求的選項,或完全不支援提示語言模型。"downloadable"
表示實作方式支援所要求的選項,但必須先下載某些項目 (例如語言模型本身或微調),才能使用這些選項建立工作階段。"downloading"
表示實作作業支援所要求選項,但必須先完成進行中的下載作業,才能使用這些選項建立工作階段。"available"
表示實作作業支援所要求的選項,不需下載任何新項目。
如要觸發模型下載作業並建立語言模型工作階段,請呼叫非同步 LanguageModel.availability()
函式。如果對 availability()
的回應是 'downloadable'
,建議您監聽下載進度。這樣一來,如果下載需要時間,您就能通知使用者。
const session = await LanguageModel.create({
monitor(m) {
m.addEventListener("downloadprogress", (e) => {
console.log(`Downloaded ${e.loaded * 100}%`);
});
},
});
模型功能
params()
函式會提供語言模型的參數。這個物件包含下列欄位:
defaultTopK
:預設的「前 K 個」值 (預設值:3
)。maxTopK
:前 K 個最高值 (8
)。defaultTemperature
:預設溫度 (1.0
)。溫度值必須介於0.0
和2.0
之間。maxTemperature
:最高溫度。
await LanguageModel.params();
// {defaultTopK: 3, maxTopK: 8, defaultTemperature: 1, maxTemperature: 2}
建立工作階段
提示 API 執行後,您可以使用 create()
函式建立工作階段。您可以使用 prompt()
或 promptStreaming()
函式提示模型。
自訂工作階段
您可以使用選用的選項物件,透過 topK
和 temperature
自訂每個工作階段。這些參數的預設值會從 LanguageModel.params()
傳回。
const params = await LanguageModel.params();
// Initializing a new session must either specify both `topK` and
// `temperature` or neither of them.
const slightlyHighTemperatureSession = await LanguageModel.create({
temperature: Math.max(params.defaultTemperature * 1.2, 2.0),
topK: params.defaultTopK,
});
create()
函式的選用選項物件也會採用 signal
欄位,可讓您傳遞 AbortSignal
來終止工作階段。
const controller = new AbortController();
stopButton.onclick = () => controller.abort();
const session = await LanguageModel.create({
signal: controller.signal,
})
初始提示
透過初始提示,您可以向語言模型提供先前互動的相關背景資訊,例如允許使用者在重新啟動瀏覽器後,繼續使用儲存的工作階段。
const session = await LanguageModel.create({
initialPrompts: [
{ role: 'system', content: 'You are a helpful and friendly assistant.' },
{ role: 'user', content: 'What is the capital of Italy?' },
{ role: 'assistant', content: 'The capital of Italy is Rome.'},
{ role: 'user', content: 'What language is spoken there?' },
{ role: 'assistant', content: 'The official language of Italy is Italian. [...]' }
]
});
工作階段相關限制
特定語言模型工作階段可處理的權杖數量有上限。 您可以在工作階段物件中使用下列屬性,查看用量和進度:
console.log(`${session.inputUsage}/${session.inputQuota}`);
工作階段持續性
每個工作階段都會追蹤對話情境。系統會將先前的互動納入考量,直到工作階段的內容視窗已滿為止。
const session = await LanguageModel.create({
initialPrompts: [{
role: "system",
content: "You are a friendly, helpful assistant specialized in clothing choices."
}]
});
const result1 = await session.prompt(
"What should I wear today? It is sunny. I am unsure between a t-shirt and a polo."
);
console.log(result1);
const result2 = await session.prompt(
"That sounds great, but oh no, it is actually going to rain! New advice?"
);
console.log(result2);
複製課程
如要保留資源,可以使用 clone()
函式複製現有工作階段。對話內容會重設,但初始提示會保留。clone()
函式會採用選用的選項物件,其中包含 signal
欄位,可讓您傳遞 AbortSignal
來終止複製的工作階段。
const controller = new AbortController();
stopButton.onclick = () => controller.abort();
const clonedSession = await session.clone({
signal: controller.signal,
});
提示模型
您可以使用 prompt()
或 promptStreaming()
函式提示模型。
非串流輸出
如果預期結果很短,可以使用 prompt()
函式,在回應可用時傳回。
// Start by checking if it's possible to create a session based on the
// availability of the model, and the characteristics of the device.
const {defaultTemperature, maxTemperature, defaultTopK, maxTopK } =
await LanguageModel.params();
const available = await LanguageModel.availability();
if (available !== 'unavailable') {
const session = await LanguageModel.create();
// Prompt the model and wait for the whole result to come back.
const result = await session.prompt("Write me a poem!");
console.log(result);
}
串流輸出
如果預期回覆內容較長,建議使用 promptStreaming()
函式,以便在模型傳回部分結果時顯示。promptStreaming()
函式會傳回 ReadableStream
。
const {defaultTemperature, maxTemperature, defaultTopK, maxTopK } =
await LanguageModel.params();
const available = await LanguageModel.availability();
if (available !== 'unavailable') {
const session = await LanguageModel.create();
// Prompt the model and stream the result:
const stream = session.promptStreaming('Write me an extra-long poem!');
for await (const chunk of stream) {
console.log(chunk);
}
}
停止執行提示
prompt()
和 promptStreaming()
都接受含有 signal
欄位的選用第二個參數,可讓您停止執行提示。
const controller = new AbortController();
stopButton.onclick = () => controller.abort();
const result = await session.prompt(
'Write me a poem!',
{ signal: controller.signal }
);
終止工作階段
如不再需要工作階段,請呼叫 destroy()
釋出資源。工作階段遭到終止後,就無法再使用,且所有正在執行的作業都會中止。如果您打算經常提示模型,建議保留工作階段,因為建立工作階段可能需要一些時間。
await session.prompt(
"You are a friendly, helpful assistant specialized in clothing choices."
);
session.destroy();
// The promise is rejected with an error explaining that
// the session is destroyed.
await session.prompt(
"What should I wear today? It is sunny, and I am unsure between a
t-shirt and a polo."
);
示範
如要在 Chrome 擴充功能中測試 Prompt API,請安裝示範擴充功能。您可以在 GitHub 上找到擴充功能原始碼。
參與並分享意見
您的意見將直接影響我們建構及實作這個 API 和所有內建 AI API 的未來版本。
- 如要提供 Chrome 實作方面的意見,請提交錯誤報告或功能要求。
- 如要分享對 API 形式的意見回饋,請在現有問題中留言,或在 Prompt API GitHub 存放區中開啟新問題。
- 從 GitHub 下載 Prompt API 範例擴充功能。
- 加入 Web Incubator Community Group,參與標準制定工作。