Skip to content

Commit 153ee14

Browse files
authored
Rename input usage (#192)
* Update docs to explain renaming of API features * Update formatting * Update formatting * add onquotaoverflow deprecation comment to idl * add onquotaoverflow deprecation comment to idl * languagemodel.oncontextwindowoverflow should be languagemodel.oncontextoverflow * languagemodel.oncontextwindowoverflow should be languagemodel.oncontextoverflow --------- Co-authored-by: Isaac Ahouma <iahouma@google.com>
1 parent eaddfc9 commit 153ee14

2 files changed

Lines changed: 46 additions & 16 deletions

File tree

README.md

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ The following are potential goals we are not yet certain of:
4545

4646
Both of these potential goals could pose challenges to interoperability, so we want to investigate more how important such functionality is to developers to find the right tradeoff.
4747

48-
### Deprecation Notice
48+
### API Updates: Deprecations and Renaming
49+
50+
To improve API clarity, consistency, and address inconsistencies in parameter support across various models, the LanguageModel API has been updated.
51+
52+
**Deprecated Features:**
4953

5054
The following features of the LanguageModel API are **deprecated** and their functionality is now restricted to web extension contexts only:
5155

@@ -56,6 +60,19 @@ The following features of the LanguageModel API are **deprecated** and their fun
5660

5761
These features may be completely removed in the future. This change is intended to simplify the API and address inconsistencies in parameter support across various models.
5862

63+
**Renamed Features:**
64+
65+
The following features have been renamed. The old names are now deprecated and will only function as aliases within Chrome Extension contexts:
66+
67+
| Old Name (Deprecated in Extensions, Removed in Web) | New Name (Available in All Contexts) |
68+
| :-------------------------------------------------- | :----------------------------------------|
69+
| `languageModel.inputUsage` | `languageModel.contextUsage` |
70+
| `languageModel.inputQuota` | `languageModel.contextWindow` |
71+
| `languageModel.measureInputUsage()` | `languageModel.measureContextUsage()` |
72+
| `languagemodel.onquotaoverflow` | `languagemodel.oncontextoverflow`. |
73+
74+
**Note:** Developers using any of the deprecated features within an extension context will receive warnings in the DevTools Issues tab. These deprecated features and aliases may be completely removed in a future Chrome release.
75+
5976
## Examples
6077

6178
### Zero-shot prompting
@@ -339,7 +356,7 @@ The returned value will be a string that matches the input `RegExp`. If the user
339356

340357
If a value that is neither a `RegExp` object or a valid JSON schema object is given, the method will error with a `TypeError`.
341358

342-
By default, the implementation may include the schema or regular expression as part of the message sent to the underlying language model, which will use up some of the [input quota](#tokenization-context-window-length-limits-and-overflow). You can measure how much it will use up by passing the `responseConstraint` option to `session.measureInputUsage()`. If you want to avoid this behavior, you can use the `omitResponseConstraintInput` option. In such cases, it's strongly recommended to include some guidance in the prompt string itself:
359+
By default, the implementation may include the schema or regular expression as part of the message sent to the underlying language model, which will use up some of the [context window](#tokenization-context-window-length-limits-and-overflow). You can measure how much it will use up by passing the `responseConstraint` option to `session.measureContextUsage()`. If you want to avoid this behavior, you can use the `omitResponseConstraintInput` option. In such cases, it's strongly recommended to include some guidance in the prompt string itself:
343360

344361
```js
345362
const result = await session.prompt(`
@@ -429,7 +446,7 @@ analyzeButton.onclick = async (e) => {
429446

430447
The promise returned by `append()` will reject if the prompt cannot be appended (e.g., too big, invalid modalities for the session, etc.), or will fulfill once the prompt has been validated, processed, and appended to the session.
431448

432-
Note that `append()` can also cause [overflow](#tokenization-context-window-length-limits-and-overflow), in which case it will evict the oldest non-system prompts from the session and fire the `"quotaoverflow"` event.
449+
Note that `append()` can also cause [overflow](#tokenization-context-window-length-limits-and-overflow), in which case it will evict the oldest non-system prompts from the session and fire the `"contextoverflow"` event.
433450

434451
### Configuration of per-session parameters
435452

@@ -577,18 +594,18 @@ Finally, note that if either prompting or appending has caused an [overflow](#to
577594

578595
### Tokenization, context window length limits, and overflow
579596

580-
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:
597+
A given language model session will have a maximum number of tokens it can process. Developers can check their current context usage and progress toward that limit by using the following properties on the session object:
581598

582599
```js
583-
console.log(`${session.inputUsage} tokens used, out of ${session.inputQuota} tokens available.`);
600+
console.log(`${session.contextUsage} tokens used, out of ${session.contextWindow} tokens available.`);
584601
```
585602

586-
To know how many tokens a prompt will consume, without actually processing it, developers can use the `measureInputUsage()` method. This method accepts the same input types as `prompt()`, including strings and multimodal input arrays:
603+
To know how many tokens a prompt will consume, without actually processing it, developers can use the `measureContextUsage()` method. This method accepts the same input types as `prompt()`, including strings and multimodal input arrays:
587604

588605
```js
589-
const stringUsage = await session.measureInputUsage(promptString);
606+
const stringUsage = await session.measureContextUsage(promptString);
590607

591-
const audioUsage = await session.measureInputUsage([{
608+
const audioUsage = await session.measureContextUsage([{
592609
role: "user",
593610
content: [
594611
{ type: "text", value: "My response to your critique:" },
@@ -601,23 +618,23 @@ Some notes on this API:
601618

602619
* We do not expose the actual tokenization to developers since that would make it too easy to depend on model-specific details.
603620
* 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.
604-
* The counting process can be aborted by passing an `AbortSignal`, i.e. `session.measureInputUsage(promptString, { signal })`.
605-
* We use the phrases "input usage" and "input quota" in the API, to avoid being specific to the current language model tokenization paradigm. In the future, even if we change paradigms, we anticipate some concept of usage and quota still being applicable, even if it's just string length.
621+
* The counting process can be aborted by passing an `AbortSignal`, i.e. `session.measureContextUsage(promptString, { signal })`.
622+
* We use the phrases "context usage" and "context window" in the API, to avoid being specific to the current language model tokenization paradigm. In the future, even if we change paradigms, we anticipate some concept of usage and context window still being applicable, even if it's just string length.
606623

607-
It's possible to send a prompt that causes the context window to overflow. That is, consider a case where `session.measureInputUsage(promptString) > session.inputQuota - session.inputUsage` 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](#system-prompts), which is never removed.
624+
It's possible to send a prompt that causes the context window to overflow. That is, consider a case where `session.measureContextUsage(promptString) > session.contextWindow - session.contextUsage` 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](#system-prompts), which is never removed.
608625

609-
Such overflows can be detected by listening for the `"quotaoverflow"` event on the session:
626+
Such overflows can be detected by listening for the `"contextoverflow"` event on the session:
610627

611628
```js
612-
session.addEventListener("quotaoverflow", () => {
613-
console.log("We've gone past the quota, and some inputs will be dropped!");
629+
session.addEventListener("contextoverflow", () => {
630+
console.log("We've gone past the context window, and some inputs will be dropped!");
614631
});
615632
```
616633

617634
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 a `QuotaExceededError` exception and nothing will be removed. This is a proposed new type of exception, which subclasses `DOMException`, and replaces the web platform's existing `"QuotaExceededError"` `DOMException`. See [whatwg/webidl#1465](https://github.com/whatwg/webidl/pull/1465) for this proposal. For our purposes, the important part is that it has the following properties:
618635

619636
* `requested`: how many tokens the input consists of
620-
* `quota`: how many tokens were available (which will be less than `requested`, and equal to the value of `session.inputQuota - session.inputUsage` at the time of the call)
637+
* `context window`: how many tokens were available (which will be less than `requested`, and equal to the value of `session.contextWindow - session.contextUsage` at the time of the call)
621638

622639
### Multilingual content and expected input languages
623640

@@ -728,7 +745,7 @@ const options = {
728745
expectedInputs: [
729746
{ type: "text", languages: ["en", "es"] },
730747
{ type: "audio", languages: ["en", "es"] }
731-
],
748+
]
732749
};
733750

734751
const availability = await LanguageModel.availability(options);

index.bs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,25 @@ interface LanguageModel : EventTarget {
5454
optional LanguageModelAppendOptions options = {}
5555
);
5656

57+
58+
Promise<double> measureContextUsage(
59+
LanguageModelPrompt input,
60+
optional LanguageModelPromptOptions options = {}
61+
);
62+
readonly attribute double contextUsage;
63+
readonly attribute unrestricted double contextWindow;
64+
attribute EventHandler oncontextoverflow;
65+
66+
// **DEPRECATED**: This method is only available in extension contexts.
5767
Promise<double> measureInputUsage(
5868
LanguageModelPrompt input,
5969
optional LanguageModelPromptOptions options = {}
6070
);
71+
// **DEPRECATED**: This attribute is only available in extension contexts.
6172
readonly attribute double inputUsage;
73+
// **DEPRECATED**: This attribute is only available in extension contexts.
6274
readonly attribute unrestricted double inputQuota;
75+
// **DEPRECATED**: This attribute is only available in extension contexts.
6376
attribute EventHandler onquotaoverflow;
6477

6578
// **DEPRECATED**: This attribute is only available in extension contexts.

0 commit comments

Comments
 (0)