Summarizer: inputQuota 属性
Summarizer
接口的只读属性 inputQuota
返回浏览器用于生成摘要的可用输入配额。
值
一个指定可用输入配额的数字。此数字取决于实现。例如,如果没有超出用户内存和 JavaScript 字符串最大长度的限制,它可能是 Infinity
,或者在使用 token/credit 方案的 AI 模型的情况下,它可能是 token 的数量。
示例
检查您是否有足够的配额
在下面的代码片段中,我们使用 create()
创建一个新的 Summarizer
实例,然后通过 inputQuota
返回总输入配额,并通过 measureInputUsage()
返回用于摘要特定文本字符串的输入配额使用情况。
然后,我们测试该字符串的单个输入使用量是否大于总可用配额。如果是,则抛出适当的错误;否则,我们使用 summarize()
开始摘要该字符串。
js
const summarizer = await Summarizer.create({
sharedContext:
"A general summary to help a user decide if the text is worth reading",
type: "tldr",
length: "short",
});
const totalInputQuota = summarizer.inputQuota;
const inputUsage = await summarizer.measureInputUsage(myTextString);
if (inputUsage > totalInputQuota) {
throw new Error("Boo, insufficient quota to generate a summary.");
} else {
console.log("Yay, quota available to generate a summary.");
const summary = await summarizer.summarize(myTextString);
// ...
}
规范
规范 |
---|
写作辅助 API # dom-summarizer-inputquota |
浏览器兼容性
加载中…