Hi, I'm curious about the maximum token limits for the Gemini Nano APIs, especially the Prompt, Summarise, and Writer APIs. Could someone provide details or point me to the relevant documentation? Thanks!
--
You received this message because you are subscribed to the Google Groups "Chrome Built-in AI Early Preview Program Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chrome-ai-dev-previe...@chromium.org.
To view this discussion visit https://groups.google.com/a/chromium.org/d/msgid/chrome-ai-dev-preview-discuss/fa4b261d-8c0a-4b66-9f76-70170224ea96n%40chromium.org.
A given language model session will have a maximum number of tokens it can process. Developers can check their current usage and progress toward that limit by using the following properties on the session object:
console.log(`${session.tokensSoFar}/${session.maxTokens} (${session.tokensLeft} left)`);
To know how many tokens a string will consume, without actually processing it, developers can use the countPromptTokens()
method:
const numTokens = await session.countPromptTokens(promptString);
Some notes on this API:
AbortSignal
, i.e. session.countPromptTokens(promptString, { signal })
.It's possible to send a prompt that causes the context window to overflow. That is, consider a case where session.countPromptTokens(promptString) > session.tokensLeft
before calling session.prompt(promptString)
, and then the web developer calls session.prompt(promptString)
anyway. In such cases, the initial portions of the conversation with the language model will be removed, one prompt/response pair at a time, until enough tokens are available to process the new prompt. The exception is the system prompt, which is never removed. If it's not possible to remove enough tokens from the conversation history to process the new prompt, then the prompt()
or promptStreaming()
call will fail with an "QuotaExceededError"
DOMException
and nothing will be removed.
Such overflows can be detected by listening for the "contextoverflow"
event on the session:
session.addEventListener("contextoverflow", () => {
console.log("Context overflow!");
});