Skip to content

Commit 7859156

Browse files
committed
Remove the "AI" namespace
Follows webmachinelearning/writing-assistance-apis#45.
1 parent 2703a9c commit 7859156

File tree

1 file changed

+55
-69
lines changed

1 file changed

+55
-69
lines changed

README.md

Lines changed: 55 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Both of these potential goals could pose challenges to interoperability, so we w
5454
In this example, a single string is used to prompt the API, which is assumed to come from the user. The returned response is from the language model.
5555

5656
```js
57-
const session = await ai.languageModel.create();
57+
const session = await LanguageModel.create();
5858

5959
// Prompt the model and wait for the whole result to come back.
6060
const result = await session.prompt("Write me a poem.");
@@ -72,7 +72,7 @@ for await (const chunk of stream) {
7272
The language model can be configured with a special "system prompt" which gives it the context for future interactions:
7373

7474
```js
75-
const session = await ai.languageModel.create({
75+
const session = await LanguageModel.create({
7676
systemPrompt: "Pretend to be an eloquent hamster."
7777
});
7878

@@ -88,7 +88,7 @@ If the system prompt is too large, then the promise will be rejected with a `Quo
8888
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.
8989

9090
```js
91-
const session = await ai.languageModel.create({
91+
const session = await LanguageModel.create({
9292
initialPrompts: [
9393
{ role: "system", content: "Predict up to 5 emojis as a response to a comment. Output emojis, comma-separated." },
9494
{ role: "user", content: "This is amazing!" },
@@ -121,7 +121,7 @@ Some details on error cases:
121121
Our examples so far have provided `prompt()` and `promptStreaming()` with a single string. Such cases assume messages will come from the user role. These methods can also take in objects in the `{ role, content }` format, or arrays of such objects, in case you want to provide multiple user or assistant messages before getting another assistant message:
122122

123123
```js
124-
const multiUserSession = await ai.languageModel.create({
124+
const multiUserSession = await LanguageModel.create({
125125
systemPrompt: "You are a mediator in a discussion between two departments."
126126
});
127127

@@ -141,7 +141,7 @@ Because of their special behavior of being preserved on context window overflow,
141141
A special case of the above is using the assistant role to emulate tool use or function-calling, by marking a response as coming from the assistant side of the conversation:
142142

143143
```js
144-
const session = await ai.languageModel.create({
144+
const session = await LanguageModel.create({
145145
systemPrompt: `
146146
You are a helpful assistant. You have access to the following tools:
147147
- calculator: A calculator. To use it, write "CALCULATOR: <expression>" where <expression> is a valid mathematical expression.
@@ -186,7 +186,7 @@ Sessions that will include these inputs need to be created using the `expectedIn
186186
A sample of using these APIs:
187187

188188
```js
189-
const session = await ai.languageModel.create({
189+
const session = await LanguageModel.create({
190190
// { type: "text" } is not necessary to include explicitly, unless
191191
// you also want to include expected input languages for text.
192192
expectedInputs: [
@@ -237,9 +237,9 @@ Details:
237237
To help with programmatic processing of language model responses, the prompt API supports structured outputs defined by a JSON schema.
238238

239239
```js
240-
const session = await ai.languageModel.create();
240+
const session = await LanguageModel.create();
241241

242-
const responseJSONSchemaObj = new AILanguageModelResponseSchema({
242+
const responseJSONSchemaObj = new LanguageModelResponseSchema({
243243
type: "object",
244244
required: ["Rating"],
245245
additionalProperties: false,
@@ -271,13 +271,13 @@ In addition to the `systemPrompt` and `initialPrompts` options shown above, the
271271
_However, see [issue #42](https://github.com/webmachinelearning/prompt-api/issues/42): sampling hyperparameters are not universal among models._
272272

273273
```js
274-
const customSession = await ai.languageModel.create({
274+
const customSession = await LanguageModel.create({
275275
temperature: 0.8,
276276
topK: 10
277277
});
278278

279-
const params = await ai.languageModel.params();
280-
const conditionalSession = await ai.languageModel.create({
279+
const params = await LanguageModel.params();
280+
const conditionalSession = await LanguageModel.create({
281281
temperature: isCreativeTask ? params.defaultTemperature * 1.1 : params.defaultTemperature * 0.8,
282282
topK: isGeneratingIdeas ? params.maxTopK : params.defaultTopK
283283
});
@@ -298,7 +298,7 @@ Error-handling behavior:
298298
Each language model session consists of a persistent series of interactions with the model:
299299

300300
```js
301-
const session = await ai.languageModel.create({
301+
const session = await LanguageModel.create({
302302
systemPrompt: "You are a friendly, helpful assistant specialized in clothing choices."
303303
});
304304

@@ -316,7 +316,7 @@ const result2 = await session.prompt(`
316316
Multiple unrelated continuations of the same prompt can be set up by creating a session and then cloning it:
317317

318318
```js
319-
const session = await ai.languageModel.create({
319+
const session = await LanguageModel.create({
320320
systemPrompt: "You are a friendly, helpful assistant specialized in clothing choices."
321321
});
322322

@@ -338,7 +338,7 @@ A language model session can be destroyed, either by using an `AbortSignal` pass
338338
const controller = new AbortController();
339339
stopButton.onclick = () => controller.abort();
340340

341-
const session = await ai.languageModel.create({ signal: controller.signal });
341+
const session = await LanguageModel.create({ signal: controller.signal });
342342
```
343343

344344
or by calling `destroy()` on the session:
@@ -427,7 +427,7 @@ The default behavior for a language model session assumes that the input languag
427427
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:
428428

429429
```js
430-
const session = await ai.languageModel.create({
430+
const session = await LanguageModel.create({
431431
systemPrompt: `
432432
You are a foreign-language tutor for Japanese. The user is Korean. If necessary, either you or
433433
the user might "break character" and ask for or give clarification in Korean. But by default,
@@ -444,7 +444,7 @@ const session = await ai.languageModel.create({
444444
The expected input languages are supplied alongside the [expected input types](#multimodal-inputs), and can vary per type. Our above example assumes the default of `type: "text"`, but more complicated combinations are possible, e.g.:
445445

446446
```js
447-
const session = await ai.languageModel.create({
447+
const session = await LanguageModel.create({
448448
expectedInputs: [
449449
// Be sure to download any material necessary for English and Japanese text
450450
// prompts, or fail-fast if the model cannot support that.
@@ -465,9 +465,9 @@ Note that there is no way of specifying output languages, since these are govern
465465

466466
### Testing available options before creation
467467

468-
In the simple case, web developers should call `ai.languageModel.create()`, and handle failures gracefully.
468+
In the simple case, web developers should call `LanguageModel.create()`, and handle failures gracefully.
469469

470-
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.
470+
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 `LanguageModel.availability()` method. This method lets developers know, before calling `create()`, what is possible with the implementation.
471471

472472
The method will return a promise that fulfills with one of the following availability values:
473473

@@ -487,14 +487,14 @@ const options = {
487487
temperature: 2
488488
};
489489

490-
const availability = await ai.languageModel.availability(options);
490+
const availability = await LanguageModel.availability(options);
491491

492492
if (availability !== "unavailable") {
493493
if (availability !== "available") {
494494
console.log("Sit tight, we need to do some downloading...");
495495
}
496496

497-
const session = await ai.languageModel.create({ ...options, systemPrompt: "..." });
497+
const session = await LanguageModel.create({ ...options, systemPrompt: "..." });
498498
// ... Use session ...
499499
} else {
500500
// Either the API overall, or the expected languages and temperature setting, is not available.
@@ -507,7 +507,7 @@ if (availability !== "unavailable") {
507507
For cases where using the API is only possible after a download, you can monitor the download progress (e.g. in order to show your users a progress bar) using code such as the following:
508508

509509
```js
510-
const session = await ai.languageModel.create({
510+
const session = await LanguageModel.create({
511511
monitor(m) {
512512
m.addEventListener("downloadprogress", e => {
513513
console.log(`Downloaded ${e.loaded * 100}%`);
@@ -539,39 +539,25 @@ Finally, note that there is a sort of precedent in the (never-shipped) [`FetchOb
539539
### Full API surface in Web IDL
540540

541541
```webidl
542-
// Shared self.ai APIs:
543-
// See https://webmachinelearning.github.io/writing-assistance-apis/#shared-ai-api for most of them.
544-
545-
partial interface AI {
546-
readonly attribute AILanguageModelFactory languageModel;
547-
};
548-
```
549-
550-
```webidl
551-
// Language Model
552-
553542
[Exposed=(Window,Worker), SecureContext]
554-
interface AILanguageModelFactory {
555-
Promise<AILanguageModel> create(optional AILanguageModelCreateOptions options = {});
556-
Promise<AIAvailability> availability(optional AILanguageModelCreateCoreOptions options = {});
557-
Promise<AILanguageModelParams?> params();
558-
};
543+
interface LanguageModel : EventTarget {
544+
static Promise<LanguageModel> create(optional LanguageModelCreateOptions options = {});
545+
static Promise<AIAvailability> availability(optional LanguageModelCreateCoreOptions options = {});
546+
static Promise<LanguageModelParams?> params();
559547
560-
[Exposed=(Window,Worker), SecureContext]
561-
interface AILanguageModel : EventTarget {
562548
// These will throw "NotSupportedError" DOMExceptions if role = "system"
563549
Promise<DOMString> prompt(
564-
AILanguageModelPromptInput input,
565-
optional AILanguageModelPromptOptions options = {}
550+
LanguageModelPromptInput input,
551+
optional LanguageModelPromptOptions options = {}
566552
);
567553
ReadableStream promptStreaming(
568-
AILanguageModelPromptInput input,
569-
optional AILanguageModelPromptOptions options = {}
554+
LanguageModelPromptInput input,
555+
optional LanguageModelPromptOptions options = {}
570556
);
571557
572558
Promise<double> measureInputUsage(
573-
AILanguageModelPromptInput input,
574-
optional AILanguageModelPromptOptions options = {}
559+
LanguageModelPromptInput input,
560+
optional LanguageModelPromptOptions options = {}
575561
);
576562
readonly attribute double inputUsage;
577563
readonly attribute unrestricted double inputQuota;
@@ -580,81 +566,81 @@ interface AILanguageModel : EventTarget {
580566
readonly attribute unsigned long topK;
581567
readonly attribute float temperature;
582568
583-
Promise<AILanguageModel> clone(optional AILanguageModelCloneOptions options = {});
569+
Promise<LanguageModel> clone(optional LanguageModelCloneOptions options = {});
584570
undefined destroy();
585571
};
586572
587573
[Exposed=(Window,Worker), SecureContext]
588-
interface AILanguageModelParams {
574+
interface LanguageModelParams {
589575
readonly attribute unsigned long defaultTopK;
590576
readonly attribute unsigned long maxTopK;
591577
readonly attribute float defaultTemperature;
592578
readonly attribute float maxTemperature;
593579
};
594580
595581
[Exposed=(Window,Worker)]
596-
interface AILanguageModelResponseSchema {
582+
interface LanguageModelResponseSchema {
597583
constructor(object responseJSONSchemaObject);
598584
}
599585
600-
dictionary AILanguageModelCreateCoreOptions {
586+
dictionary LanguageModelCreateCoreOptions {
601587
// Note: these two have custom out-of-range handling behavior, not in the IDL layer.
602588
// They are unrestricted double so as to allow +Infinity without failing.
603589
unrestricted double topK;
604590
unrestricted double temperature;
605591
606-
sequence<AILanguageModelExpectedInput> expectedInputs;
592+
sequence<LanguageModelExpectedInput> expectedInputs;
607593
};
608594
609-
dictionary AILanguageModelCreateOptions : AILanguageModelCreateCoreOptions {
595+
dictionary LanguageModelCreateOptions : LanguageModelCreateCoreOptions {
610596
AbortSignal signal;
611597
AICreateMonitorCallback monitor;
612598
613599
DOMString systemPrompt;
614-
sequence<AILanguageModelPrompt> initialPrompts;
600+
sequence<LanguageModelPrompt> initialPrompts;
615601
};
616602
617-
dictionary AILanguageModelPromptOptions {
603+
dictionary LanguageModelPromptOptions {
618604
object responseJSONSchema;
619605
AbortSignal signal;
620606
};
621607
622-
dictionary AILanguageModelCloneOptions {
608+
dictionary LanguageModelCloneOptions {
623609
AbortSignal signal;
624610
};
625611
626-
dictionary AILanguageModelExpectedInput {
627-
required AILanguageModelPromptType type;
612+
dictionary LanguageModelExpectedInput {
613+
required LanguageModelPromptType type;
628614
sequence<DOMString> languages;
629615
};
630616
631617
// The argument to the prompt() method and others like it
632618
633-
typedef (AILanguageModelPrompt or sequence<AILanguageModelPrompt>) AILanguageModelPromptInput;
619+
typedef (LanguageModelPrompt or sequence<LanguageModelPrompt>) LanguageModelPromptInput;
634620
635621
// Prompt lines
636622
637623
typedef (
638-
DOMString // interpreted as { role: "user", type: "text", content: providedValue }
639-
or AILanguageModelPromptDict // canonical form
640-
) AILanguageModelPrompt;
641-
642-
dictionary AILanguageModelPromptDict {
643-
AILanguageModelPromptRole role = "user";
644-
AILanguageModelPromptType type = "text";
645-
required AILanguageModelPromptContent content;
624+
DOMString // interpreted as { role: "user", type: "text", content: providedValue }
625+
or LanguageModelPromptDict // canonical form
626+
) LanguageModelPrompt;
627+
628+
dictionary LanguageModelPromptDict {
629+
LanguageModelPromptRole role = "user";
630+
LanguageModelPromptType type = "text";
631+
required LanguageModelPromptContent content;
646632
};
647633
648-
enum AILanguageModelPromptRole { "system", "user", "assistant" };
634+
enum LanguageModelPromptRole { "system", "user", "assistant" };
649635
650-
enum AILanguageModelPromptType { "text", "image", "audio" };
636+
enum LanguageModelPromptType { "text", "image", "audio" };
651637
652638
typedef (
653639
ImageBitmapSource
654640
or AudioBuffer
655641
or BufferSource
656642
or DOMString
657-
) AILanguageModelPromptContent;
643+
) LanguageModelPromptContent;
658644
```
659645

660646
### Instruction-tuned versus base models
@@ -679,7 +665,7 @@ To actually get a response back from the model given a prompt, the following pos
679665
3. Add an initial prompt to establish context. (This will not generate a response.)
680666
4. Execute a prompt and receive a response.
681667

682-
We've chosen to manifest these 3-4 stages into the API as two methods, `ai.languageModel.create()` and `session.prompt()`/`session.promptStreaming()`, with some additional facilities for dealing with the fact that `ai.languageModel.create()` can include a download step. Some APIs simplify this into a single method, and some split it up into three (usually not four).
668+
We've chosen to manifest these 3-4 stages into the API as two methods, `LanguageModel.create()` and `session.prompt()`/`session.promptStreaming()`, with some additional facilities for dealing with the fact that `LanguageModel.create()` can include a download step. Some APIs simplify this into a single method, and some split it up into three (usually not four).
683669

684670
### Stateless or session-based
685671

0 commit comments

Comments
 (0)