You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Overhaul availability testing and add expected input languages
Remove the `ai.languageModel.capabilities()` method and its accompanying `AILanguageModelCapabilities` class. Instead, replace it with:
* `ai.languageModel.availability(options)`, which takes the same options as `ai.languageModel.create()`, and returns the corresponding availability.
* `ai.languageModel.params()`, which returns the default and max params (currently top-K and temperature).
Additionally, add the `expectedInputLanguages` option to `create()` and `availability()`. The addition of this option to `create()` allows the web developer to signal the expected input languages ahead of time, allowing the downloading of additional material, or fast-failing if the additional material cannot be supported. The addition of this option to `availability()` replaces the `(await ai.languageModel.capabilities()).languageAvailable()` method.
Closes#29; see especially #29 (comment).
See also webmachinelearning/writing-assistance-apis#22 and webmachinelearning/translation-api#31.
Copy file name to clipboardExpand all lines: README.md
+66-34Lines changed: 66 additions & 34 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -173,28 +173,32 @@ console.log(await promptWithCalculator("What is 2 + 2?"));
173
173
174
174
We'll likely explore more specific APIs for tool- and function-calling in the future; follow along in [issue #7](https://github.com/webmachinelearning/prompt-api/issues/7).
175
175
176
-
### Configuration of per-session options
176
+
### Configuration of per-session parameters
177
177
178
-
In addition to the `systemPrompt` and `initialPrompts` options shown above, the currently-configurable options are [temperature](https://huggingface.co/blog/how-to-generate#sampling) and [top-K](https://huggingface.co/blog/how-to-generate#top-k-sampling). More information about the values for these parameters can be found using the `capabilities()` API explained [below](#capabilities-detection).
178
+
In addition to the `systemPrompt` and `initialPrompts` options shown above, the currently-configurable model parameters are [temperature](https://huggingface.co/blog/how-to-generate#sampling) and [top-K](https://huggingface.co/blog/how-to-generate#top-k-sampling). The `params()` API gives the default, minimum, and maximum values for these parameters.
179
+
180
+
_However, see [issue #42](https://github.com/webmachinelearning/prompt-api/issues/42): sampling hyperparameters are not universal among models._
In all our above examples, we call `ai.languageModel.create()` and assume it will always succeed.
325
+
The default behavior for a language model session assumes that the input languages are unknown. In this case, implementations will use whatever "base" capabilities they have available for the language model, and might throw `"NotSupportedError"``DOMException`s if they encounter languages they don't support.
322
326
323
-
However, sometimes a language model needs to be downloaded before the API can be used. In such cases, immediately calling `create()` will start the download, which might take a long time. The capabilities API gives you insight into the download status of the model:
327
+
It's better practice, if possible, to supply the `create()` method with information about the expected input languages. This allows the implementation to download any necessary supporting material, such as fine-tunings or safety-checking models, and to immediately reject the promise returned by `create()` if the web developer needs to use languages that the browser is not capable of supporting:
You are a foreign-language tutor for Japanese. The user is Korean. If necessary, either you or
333
+
the user might "break character" and ask for or give clarification in Korean. But by default,
334
+
prefer speaking in Japanese, and return to the Japanese conversation once any sidebars are
335
+
concluded.
336
+
`,
337
+
expectedInputLanguages: ["en"/* for the system prompt */, "ja", "kr"]
338
+
});
328
339
```
329
340
330
-
The `capabilities.available` property is a string that can take one of three values:
341
+
Note that there is no way of specifying output languages, since these are governed by the language model's own decisions. Similarly, the expected input languages do not affect the context or prompt the language model sees; they only impact the process of setting up the session and performing appropriate downloads.
342
+
343
+
### Testing available options before creation
344
+
345
+
In the simple case, web developers should call `ai.languageModel.create()`, and handle failures gracefully.
346
+
347
+
However, if the web developer wants to provide a differentiated user experience, which lets users know ahead of time that the feature will not be possible or might require a download, they can use the promise-returning `ai.languageModel.availability()` method. This method lets developers know, before calling `create()`, what is possible with the implementation.
348
+
349
+
The method will return a promise that fulfills with one of the following availability values:
350
+
351
+
* "`no`" means that the implementation does not support the requested options, or does not support prompting a language model at all.
352
+
* "`after-download`" means that the implementation supports the requested options, but it will have to download something (e.g. the language model itself, or a fine-tuning) before it can create a session using those options.
353
+
* "`readily`" means that the implementation supports the requested options without requiring any new downloads.
331
354
332
-
*`"no"`, indicating the device or browser does not support prompting a language model at all.
333
-
*`"after-download"`, indicating the device or browser supports prompting a language model, but it needs to be downloaded before it can be used.
334
-
*`"readily"`, indicating the device or browser supports prompting a language model and it’s ready to be used without any downloading steps.
355
+
An example usage is the following:
335
356
336
-
In the `"after-download"` case, developers might want to have users confirm before you call `create()` to start the download, since doing so uses up significant bandwidth and users might not be willing to wait for a large download before using the site or feature.
The capabilities API also contains other information about the model:
362
+
if (supportsOurUseCase !=="no") {
363
+
if (supportsOurUseCase ==="after-download") {
364
+
console.log("Sit tight, we need to do some downloading...");
365
+
}
341
366
342
-
*`defaultTemperature`, `maxTemperature`, `defaultTopK`, and `maxTopK` properties giving information about the model's sampling parameters.
343
-
*`languageAvailable(languageTag)`, which returns `"no"`, `"after-download"`, or `"readily"` to indicate whether the model supports conversing in a given human language.
0 commit comments