It's possible an input fed to these APIs is too large. This will be signaled by a rejected promise, probably a "QuotaExceededError"DOMException.
However, @uskay points out that this does not allow you to give informative error messages, telling the user or web developer by how much the input is too large.
There are two possible APIs one could imagine here:
Measure, then summarize
const summarizer = await ai.summarizer.create();
const summarizerCapabilities = await ai.summarizer.capabilities();
const tokenCount = await summarizer.countTokens(input);
if (tokenCount > summarizerCapabilities.maxTokens) {
console.error(`Too large! You tried to summarize ${tokenCount} tokens, but only up to ${summarizerCapabilities.maxTokens} is possible!`);
} else {
console.log(await summarizer.summarize(input));
}
This API is probably bad because it requires two round-trips to the language model, one to tokenize, and then a second one to tokenize-plus-summarize.
More informative errors
This would probably look something like:
const summarizer = await ai.summarizer.create();
const summarizerCapabilities = await ai.summarizer.capabilities();
try {
console.log(await summarizer.summarize(input));
} catch (e) {
if (e.name === "TooManyTokensError") {
console.error(`Too large! You tried to summarize ${e.tokenCount} tokens, but only up to ${summarizerCapabilities.maxTokens} is possible!`);
} else {
throw e;
}
}
This is probably better since it only has one round trip.
It's possible an input fed to these APIs is too large. This will be signaled by a rejected promise, probably a
"QuotaExceededError"
DOMException
.However, @uskay points out that this does not allow you to give informative error messages, telling the user or web developer by how much the input is too large.
There are two possible APIs one could imagine here:
Measure, then summarize
This API is probably bad because it requires two round-trips to the language model, one to tokenize, and then a second one to tokenize-plus-summarize.
More informative errors
This would probably look something like:
This is probably better since it only has one round trip.