Skip to content

Commit 6956d4b

Browse files
authored
Add tokenization and overflow APIs
Closes #2. Closes #6 by adding more detail about the error cases.
1 parent fe41a59 commit 6956d4b

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

README.md

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ console.log(await session.prompt("What is your favorite food?"));
8181

8282
The system prompt is special, in that the assistant will not respond to it, and it will be preserved even if the context window otherwise overflows due to too many calls to `prompt()`.
8383

84+
If the system prompt is too large (see [below](#tokenization-context-window-length-limits-and-overflow)), then the promise will be rejected with a `"QuotaExceededError"` `DOMException`.
85+
8486
### N-shot prompting
8587

8688
If developers want to provide examples of the user/assistant interaction, they can use the `initialPrompts` array. This aligns with the common "chat completions API" format of `{ role, content }` pairs, including a `"system"` role which can be used instead of the `systemPrompt` option shown above.
@@ -107,7 +109,10 @@ const result1 = await predictEmoji("Back to the drawing board");
107109
const result2 = await predictEmoji("This code is so good you should get promoted");
108110
```
109111

110-
(Using both `systemPrompt` and a `{ role: "system" }` prompt in `initialPrompts`, or using multiple `{ role: "system" }` prompts, or placing the `{ role: "system" }` prompt anywhere besides at the 0th position in `initialPrompts`, will reject with a `TypeError`.)
112+
Some details on error cases:
113+
114+
* Using both `systemPrompt` and a `{ role: "system" }` prompt in `initialPrompts`, or using multiple `{ role: "system" }` prompts, or placing the `{ role: "system" }` prompt anywhere besides at the 0th position in `initialPrompts`, will reject with a `TypeError`.
115+
* If the combined token length of all the initial prompts (including the separate `systemPrompt`, if provided) is too large, then the promise will be rejected with a `"QuotaExceededError"` `DOMException`.
111116

112117
### Configuration of per-session options
113118

@@ -203,6 +208,36 @@ Note that because sessions are stateful, and prompts can be queued, aborting a s
203208
* If the prompt is being currently processed by the model, then it will be aborted, and the prompt/response pair will be removed from the conversation history.
204209
* If the prompt has already been fully processed by the model, then attempting to abort the prompt will do nothing.
205210

211+
### Tokenization, context window length limits, and overflow
212+
213+
A given assistant 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:
214+
215+
```js
216+
console.log(`${session.tokensSoFar}/${session.maxTokens} (${session.tokensLeft} left)`);
217+
```
218+
219+
To know how many tokens a string will consume, without actually processing it, developers can use the `countPromptTokens()` method:
220+
221+
```js
222+
const numTokens = await session.countPromptTokens(promptString);
223+
```
224+
225+
Some notes on this API:
226+
227+
* We do not expose the actual tokenization to developers since that would make it too easy to depend on model-specific details.
228+
* Implementations must include in their count any control tokens that will be necessary to process the prompt, e.g. ones indicating the start or end of the input.
229+
* The counting process can be aborted by passing an `AbortSignal`, i.e. `session.countPromptTokens(promptString, { signal })`.
230+
231+
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 assistant 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](#system-prompts), 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.
232+
233+
Such overflows can be detected by listening for the `"contextoverflow"` event on the session:
234+
235+
```js
236+
session.addEventListener("contextoverflow", () => {
237+
console.log("Context overflow!");
238+
});
239+
```
240+
206241
### Capabilities detection
207242

208243
In all our above examples, we call `ai.assistant.create()` and assume it will always succeed.
@@ -294,13 +329,20 @@ interface AIAssistantFactory {
294329
};
295330
296331
[Exposed=(Window,Worker)]
297-
interface AIAssistant {
332+
interface AIAssistant : EventTarget {
298333
Promise<DOMString> prompt(DOMString input, optional AIAssistantPromptOptions options = {});
299334
ReadableStream promptStreaming(DOMString input, optional AIAssistantPromptOptions options = {});
300335
336+
Promise<unsigned long long> countPromptTokens(DOMString input, optional AIAssistantPromptOptions options = {});
337+
readonly attribute unsigned long long maxTokens;
338+
readonly attribute unsigned long long tokensSoFar;
339+
readonly attribute unsigned long long tokensLeft;
340+
301341
readonly attribute unsigned long topK;
302342
readonly attribute float temperature;
303343
344+
attribute EventHandler oncontextoverflow;
345+
304346
Promise<AIAssistant> clone();
305347
undefined destroy();
306348
};

chrome-implementation-differences.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ _Tracking issue: <https://issues.chromium.org/issues/343325183>_
3838

3939
The `systemPrompt` and `initialPrompts` options are not yet supported. Instead, you have to manually emulate these by using special control tokens in the middle of your prompt.
4040

41+
## Tokenization and overflow APIs
42+
43+
_Tracking issue: <https://issues.chromium.org/issues/355962207>_
44+
45+
The `countPromptTokens()`, `maxTokens`, `tokensSoFar`, and `tokensLeft` APIs, as well as the `contextoverflow` event, are not yet implemented.
46+
4147
## `topK` and `temperature` both required
4248

4349
_Tracking issue: <https://issues.chromium.org/issues/343600797>_

0 commit comments

Comments
 (0)