|
| 1 | +<pre class='metadata'> |
| 2 | +Title: Prompt API |
| 3 | +Shortname: prompt |
| 4 | +Level: None |
| 5 | +Status: CG-DRAFT |
| 6 | +Group: webml |
| 7 | +Repository: webmachinelearning/prompt-api |
| 8 | +URL: https://webmachinelearning.github.io/prompt-api |
| 9 | +Editor: Domenic Denicola, Google https://google.com, [email protected], https://domenic.me/ |
| 10 | +Abstract: The prompt API gives web pages the ability to directly prompt a language model |
| 11 | +Markup Shorthands: markdown yes, css no |
| 12 | +Complain About: accidental-2119 yes, missing-example-ids yes |
| 13 | +Assume Explicit For: yes |
| 14 | +Default Biblio Status: current |
| 15 | +Boilerplate: omit conformance |
| 16 | +Indent: 2 |
| 17 | +Die On: warning |
| 18 | +</pre> |
| 19 | + |
| 20 | +<h2 id="intro">Introduction</h2> |
| 21 | + |
| 22 | +TODO |
| 23 | + |
| 24 | +<h2 id="dependencies">Dependencies</h2> |
| 25 | + |
| 26 | +This specification depends on the Infra Standard. [[!INFRA]] |
| 27 | + |
| 28 | +As with the rest of the web platform, human languages are identified in these APIs by BCP 47 language tags, such as "`ja`", "`en-US`", "`sr-Cyrl`", or "`de-CH-1901-x-phonebk-extended`". The specific algorithms used for validation, canonicalization, and language tag matching are those from the <cite>ECMAScript Internationalization API Specification</cite>, which in turn defers some of its processing to <cite>Unicode Locale Data Markup Language (LDML)</cite>. [[BCP47]] [[!ECMA-402]] [[UTS35]]. |
| 29 | + |
| 30 | +These APIs are part of a family of APIs expected to be powered by machine learning models, which share common API surface idioms and specification patterns. Currently, the specification text for these shared parts lives in [[WRITING-ASSISTANCE-APIS#supporting]], and the common privacy and security considerations are discussed in [[WRITING-ASSISTANCE-APIS#privacy]] and [[WRITING-ASSISTANCE-APIS#security]]. Implementing these APIs requires implementing that shared infrastructure, and conforming to those privacy and security considerations. But it does not require implementing or exposing the actual writing assistance APIs. [[!WRITING-ASSISTANCE-APIS]] |
| 31 | + |
| 32 | +<h2 id="api">The API</h2> |
| 33 | + |
| 34 | +<xmp class="idl"> |
| 35 | +[Exposed=Window, SecureContext] |
| 36 | +interface LanguageModel : EventTarget { |
| 37 | + static Promise<LanguageModel> create(optional LanguageModelCreateOptions options = {}); |
| 38 | + static Promise<Availability> availability(optional LanguageModelCreateCoreOptions options = {}); |
| 39 | + static Promise<LanguageModelParams?> params(); |
| 40 | + |
| 41 | + // These will throw "NotSupportedError" DOMExceptions if role = "system" |
| 42 | + Promise<DOMString> prompt( |
| 43 | + LanguageModelPrompt input, |
| 44 | + optional LanguageModelPromptOptions options = {} |
| 45 | + ); |
| 46 | + ReadableStream promptStreaming( |
| 47 | + LanguageModelPrompt input, |
| 48 | + optional LanguageModelPromptOptions options = {} |
| 49 | + ); |
| 50 | + Promise<undefined> append( |
| 51 | + LanguageModelPrompt input, |
| 52 | + optional LanguageModelAppendOptions options = {} |
| 53 | + ); |
| 54 | + |
| 55 | + Promise<double> measureInputUsage( |
| 56 | + LanguageModelPrompt input, |
| 57 | + optional LanguageModelPromptOptions options = {} |
| 58 | + ); |
| 59 | + readonly attribute double inputUsage; |
| 60 | + readonly attribute unrestricted double inputQuota; |
| 61 | + attribute EventHandler onquotaoverflow; |
| 62 | + |
| 63 | + readonly attribute unsigned long topK; |
| 64 | + readonly attribute float temperature; |
| 65 | + |
| 66 | + Promise<LanguageModel> clone(optional LanguageModelCloneOptions options = {}); |
| 67 | + undefined destroy(); |
| 68 | +}; |
| 69 | + |
| 70 | +[Exposed=Window, SecureContext] |
| 71 | +interface LanguageModelParams { |
| 72 | + readonly attribute unsigned long defaultTopK; |
| 73 | + readonly attribute unsigned long maxTopK; |
| 74 | + readonly attribute float defaultTemperature; |
| 75 | + readonly attribute float maxTemperature; |
| 76 | +}; |
| 77 | + |
| 78 | +dictionary LanguageModelCreateCoreOptions { |
| 79 | + // Note: these two have custom out-of-range handling behavior, not in the IDL layer. |
| 80 | + // They are unrestricted double so as to allow +Infinity without failing. |
| 81 | + unrestricted double topK; |
| 82 | + unrestricted double temperature; |
| 83 | + |
| 84 | + sequence<LanguageModelExpected> expectedInputs; |
| 85 | + sequence<LanguageModelExpected> expectedOutputs; |
| 86 | +}; |
| 87 | + |
| 88 | +dictionary LanguageModelCreateOptions : LanguageModelCreateCoreOptions { |
| 89 | + AbortSignal signal; |
| 90 | + CreateMonitorCallback monitor; |
| 91 | + |
| 92 | + sequence<LanguageModelMessage> initialPrompts; |
| 93 | +}; |
| 94 | + |
| 95 | +dictionary LanguageModelPromptOptions { |
| 96 | + object responseConstraint; |
| 97 | + AbortSignal signal; |
| 98 | +}; |
| 99 | + |
| 100 | +dictionary LanguageModelAppendOptions { |
| 101 | + AbortSignal signal; |
| 102 | +}; |
| 103 | + |
| 104 | +dictionary LanguageModelCloneOptions { |
| 105 | + AbortSignal signal; |
| 106 | +}; |
| 107 | + |
| 108 | +dictionary LanguageModelExpected { |
| 109 | + required LanguageModelMessageType type; |
| 110 | + sequence<DOMString> languages; |
| 111 | +}; |
| 112 | + |
| 113 | +// The argument to the prompt() method and others like it |
| 114 | + |
| 115 | +typedef ( |
| 116 | + sequence<LanguageModelMessage> |
| 117 | + // Shorthand for `[{ role: "user", content: [{ type: "text", value: providedValue }] }]` |
| 118 | + or DOMString |
| 119 | +) LanguageModelPrompt; |
| 120 | + |
| 121 | +dictionary LanguageModelMessage { |
| 122 | + required LanguageModelMessageRole role; |
| 123 | + |
| 124 | + // The DOMString branch is shorthand for `[{ type: "text", value: providedValue }]` |
| 125 | + required (DOMString or sequence<LanguageModelMessageContent>) content; |
| 126 | +}; |
| 127 | + |
| 128 | +dictionary LanguageModelMessageContent { |
| 129 | + required LanguageModelMessageType type; |
| 130 | + required LanguageModelMessageValue value; |
| 131 | +}; |
| 132 | + |
| 133 | +enum LanguageModelMessageRole { "system", "user", "assistant" }; |
| 134 | + |
| 135 | +enum LanguageModelMessageType { "text", "image", "audio" }; |
| 136 | + |
| 137 | +typedef ( |
| 138 | + ImageBitmapSource |
| 139 | + or AudioBuffer |
| 140 | + or BufferSource |
| 141 | + or DOMString |
| 142 | +) LanguageModelMessageValue; |
| 143 | +</xmp> |
| 144 | + |
| 145 | +<h3 id="permissions-policy">Permissions policy integration</h3> |
| 146 | + |
| 147 | +Access to the prompt API is gated behind the [=policy-controlled feature=] "<dfn permission>language-model</dfn>", which has a [=policy-controlled feature/default allowlist=] of <code>[=default allowlist/'self'=]</code>. |
| 148 | + |
| 149 | +<h2 id="privacy">Privacy considerations</h2> |
| 150 | + |
| 151 | +Please see [[WRITING-ASSISTANCE-APIS#privacy]] for a discussion of privacy considerations for the prompt API. That text was written to apply to all APIs sharing the same infrastructure, as noted in [[#dependencies]]. |
| 152 | + |
| 153 | +<h2 id="security">Security considerations</h2> |
| 154 | + |
| 155 | +Please see [[WRITING-ASSISTANCE-APIS#security]] for a discussion of security considerations for the prompt API. That text was written to apply to all APIs sharing the same infrastructure, as noted in [[#dependencies]]. |
0 commit comments