Abstract
The
prompt
API
gives
web
pages
the
ability
to
directly
prompt
a
language
model
Status
of
this
document
Table
of
Contents
1
Introduction
2
Dependencies
3
The
API
3.1
Prompt
processing
3.2
Permissions
policy
integration
4
Privacy
considerations
5
Security
considerations
Index
Terms
defined
by
this
specification
Terms
defined
by
reference
References
Normative
References
Informative
References
IDL
Index
1.
Introduction
TODO
2.
Dependencies
This
specification
depends
on
the
Infra
Standard.
[INFRA]
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
ECMAScript
Internationalization
API
Specification
,
which
in
turn
defers
some
of
its
processing
to
Unicode
Locale
Data
Markup
Language
(LDML)
.
[BCP47]
[ECMA-402]
[UTS35]
.
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
§ 5
Shared
infrastructure
,
and
the
common
privacy
and
security
considerations
are
discussed
in
Writing
Assistance
APIs
§ 6
Privacy
considerations
and
Writing
Assistance
APIs
§ 7
Security
considerations
.
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]
3.
The
API
[Exposed =Window , SecureContext ]
interface LanguageModel : EventTarget {
static Promise <LanguageModel > create (optional LanguageModelCreateOptions options = {});
static Promise <Availability > availability (optional LanguageModelCreateCoreOptions options = {});
static Promise <LanguageModelParams ?> params ();
// These will throw "NotSupportedError" DOMExceptions if role = "system"
Promise <DOMString > prompt (
LanguageModelPrompt input ,
optional LanguageModelPromptOptions options = {}
);
ReadableStream promptStreaming (
LanguageModelPrompt input ,
optional LanguageModelPromptOptions options = {}
);
Promise <undefined > append (
LanguageModelPrompt input ,
optional LanguageModelAppendOptions options = {}
);
Promise <double > measureInputUsage (
LanguageModelPrompt input ,
optional LanguageModelPromptOptions options = {}
);
readonly attribute double inputUsage ;
readonly attribute unrestricted double inputQuota ;
attribute EventHandler onquotaoverflow ;
readonly attribute unsigned long topK ;
readonly attribute float temperature ;
Promise <LanguageModel > clone (optional LanguageModelCloneOptions options = {});
undefined destroy ();
};
[Exposed =Window , SecureContext ]
interface LanguageModelParams {
readonly attribute unsigned long defaultTopK ;
readonly attribute unsigned long maxTopK ;
readonly attribute float defaultTemperature ;
readonly attribute float maxTemperature ;
};
dictionary LanguageModelCreateCoreOptions {
// Note: these two have custom out-of-range handling behavior, not in the IDL layer.
// They are unrestricted double so as to allow +Infinity without failing.
unrestricted double topK ;
unrestricted double temperature ;
sequence <LanguageModelExpected > expectedInputs ;
sequence <LanguageModelExpected > expectedOutputs ;
};
dictionary LanguageModelCreateOptions : LanguageModelCreateCoreOptions {
AbortSignal signal ;
CreateMonitorCallback monitor ;
sequence <LanguageModelMessage > initialPrompts ;
};
dictionary LanguageModelPromptOptions {
object responseConstraint ;
AbortSignal signal ;
};
dictionary LanguageModelAppendOptions {
AbortSignal signal ;
};
dictionary LanguageModelCloneOptions {
AbortSignal signal ;
};
dictionary LanguageModelExpected {
required LanguageModelMessageType type ;
sequence <DOMString > languages ;
};
// The argument to the prompt() method and others like it
typedef (
sequence <LanguageModelMessage >
// Shorthand for `[{ role: "user", content: [{ type: "text", value: providedValue }] }]`
or DOMString
) LanguageModelPrompt ;
dictionary LanguageModelMessage {
required LanguageModelMessageRole role ;
// The DOMString branch is shorthand for `[{ type: "text", value: providedValue }]`
required (DOMString or sequence <LanguageModelMessageContent >) content ;
};
dictionary LanguageModelMessageContent {
required LanguageModelMessageType type ;
required LanguageModelMessageValue value ;
};
enum LanguageModelMessageRole { "system" , "user" , "assistant" };
enum LanguageModelMessageType { "text" , "image" , "audio" };
typedef (
ImageBitmapSource
or AudioBuffer
or BufferSource
or DOMString
) LanguageModelMessageValue ;
3.1.
Prompt
processing
This
will
be
incorporated
into
a
proper
part
of
the
specification
later.
For
now,
we’re
just
writing
out
this
algorithm
as
a
full
spec,
since
it’s
complicated.
To
validate
and
canonicalize
a
prompt
given
a
LanguageModelPrompt
input
,
a
list
of
LanguageModelMessageType
s
expectedTypes
,
and
a
boolean
isInitial
,
perform
the
following
steps.
The
return
value
will
be
a
non-empty
list
of
LanguageModelMessage
s
in
their
"longhand"
form.
Assert
:
expectedTypes
contains
"
text
".
If
input
is
a
string
,
then
return
«
«[
"
role
"
→
"
user
",
"
content
"
→
«
«[
"
type
"
→
"
text
",
"
value
"
→
input
]»
»
]»
»
.
Assert
:
input
is
a
list
of
LanguageModelMessage
s.
Let
seenNonSystemRole
be
false.
Let
messages
be
an
empty
list
of
LanguageModelMessage
s.
For
each
message
of
input
:
If
message
["
content
"]
is
a
string
,
then
set
message
to
«[
"
role
"
→
message
["
role
"],
"
content
"
→
«
«[
"
type
"
→
"
text
",
"
value
"
→
message
]»
»
]»
to
messages
.
For
each
content
of
message
["
content
"]:
If
message
["
role
"]
is
"
system
",
then:
If
isInitial
is
false,
then
throw
a
"
NotSupportedError
"
DOMException
.
If
seenNonSystemRole
is
true,
then
throw
a
"
SyntaxError
"
DOMException
.
If
message
["
role
"]
is
not
"
system
",
then
set
seenNonSystemRole
to
true.
If
message
["
role
"]
is
"
assistant
"
and
content
["
type
"]
is
not
"
text
",
then
throw
a
"
NotSupportedError
"
DOMException
.
If
content
["
type
"]
is
"
text
"
and
content
["
value
"]
is
not
a
string
,
then
throw
a
TypeError
.
If
content
["
type
"]
is
"
image
",
then:
If
expectedTypes
does
not
contain
"
image
",
then
throw
a
"
NotSupportedError
"
DOMException
.
If
content
["
value
"]
is
not
an
ImageBitmapSource
or
BufferSource
,
then
throw
a
TypeError
.
If
content
["
type
"]
is
"
audio
",
then:
If
expectedTypes
does
not
contain
"
audio
",
then
throw
a
"
NotSupportedError
"
DOMException
.
If
content
["
value
"]
is
not
an
AudioBuffer
,
BufferSource
,
or
Blob
,
then
throw
a
TypeError
.
Append
message
to
messages
.
If
messages
is
empty
,
then
throw
a
"
SyntaxError
"
DOMException
.
Return
messages
.
3.2.
Permissions
policy
integration
Access
to
the
prompt
API
is
gated
behind
the
policy-controlled
feature
"
language-model
",
which
has
a
default
allowlist
of
'self'
.
4.
Privacy
considerations
Please
see
Writing
Assistance
APIs
§ 6
Privacy
considerations
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
§ 2
Dependencies
.
5.
Security
considerations
Please
see
Writing
Assistance
APIs
§ 7
Security
considerations
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
§ 2
Dependencies
.
Index
Terms
defined
by
this
specification
append(input)
,
in
§ 3
append(input,
options)
,
in
§ 3
"assistant"
,
in
§ 3
"audio"
,
in
§ 3
availability()
,
in
§ 3
availability(options)
,
in
§ 3
clone()
,
in
§ 3
clone(options)
,
in
§ 3
content
,
in
§ 3
create()
,
in
§ 3
create(options)
,
in
§ 3
defaultTemperature
,
in
§ 3
defaultTopK
,
in
§ 3
destroy()
,
in
§ 3
expectedInputs
,
in
§ 3
expectedOutputs
,
in
§ 3
"image"
,
in
§ 3
initialPrompts
,
in
§ 3
inputQuota
,
in
§ 3
inputUsage
,
in
§ 3
language-model
,
in
§ 3.1
§ 3.2
LanguageModel
,
in
§ 3
LanguageModelAppendOptions
,
in
§ 3
LanguageModelCloneOptions
,
in
§ 3
LanguageModelCreateCoreOptions
,
in
§ 3
LanguageModelCreateOptions
,
in
§ 3
LanguageModelExpected
,
in
§ 3
LanguageModelMessage
,
in
§ 3
LanguageModelMessageContent
,
in
§ 3
LanguageModelMessageRole
,
in
§ 3
LanguageModelMessageType
,
in
§ 3
LanguageModelMessageValue
,
in
§ 3
LanguageModelParams
,
in
§ 3
LanguageModelPrompt
,
in
§ 3
LanguageModelPromptOptions
,
in
§ 3
languages
,
in
§ 3
maxTemperature
,
in
§ 3
maxTopK
,
in
§ 3
measureInputUsage(input)
,
in
§ 3
measureInputUsage(input,
options)
,
in
§ 3
monitor
,
in
§ 3
onquotaoverflow
,
in
§ 3
params()
,
in
§ 3
prompt(input)
,
in
§ 3
prompt(input,
options)
,
in
§ 3
promptStreaming(input)
,
in
§ 3
promptStreaming(input,
options)
,
in
§ 3
responseConstraint
,
in
§ 3
role
,
in
§ 3
signal
"system"
,
in
§ 3
temperature
"text"
,
in
§ 3
topK
type
"user"
,
in
§ 3
validate
and
canonicalize
a
prompt
,
in
§ 3.1
value
,
in
§ 3
Terms
defined
by
reference
[DOM]
defines
the
following
terms:
[ECMASCRIPT]
defines
the
following
terms:
[FileAPI]
defines
the
following
terms:
[HTML]
defines
the
following
terms:
EventHandler
ImageBitmapSource
[INFRA]
defines
the
following
terms:
append
assert
contain
for
each
is
empty
list
string
[PERMISSIONS-POLICY-1]
defines
the
following
terms:
'self'
default
allowlist
policy-controlled
feature
[STREAMS]
defines
the
following
terms:
[WEBAUDIO-1.0]
defines
the
following
terms:
[WEBIDL]
defines
the
following
terms:
BufferSource
DOMException
DOMString
Exposed
NotSupportedError
Promise
SecureContext
double
float
object
sequence
undefined
unrestricted
double
unsigned
long
[WRITING-ASSISTANCE-APIS]
defines
the
following
terms:
Availability
CreateMonitorCallback
References
Normative
References
[DOM]
Anne
van
Kesteren.
DOM
Standard
.
Living
Standard.
URL:
https://dom.spec.whatwg.org/
[ECMA-402]
ECMAScript
Internationalization
API
Specification
.
URL:
https://tc39.es/ecma402/
[ECMASCRIPT]
ECMAScript
Language
Specification
.
URL:
https://tc39.es/ecma262/multipage/
[FileAPI]
Marijn
Kruisselbrink.
File
API
.
URL:
https://w3c.github.io/FileAPI/
[HTML]
Anne
van
Kesteren;
et
al.
HTML
Standard
.
Living
Standard.
URL:
https://html.spec.whatwg.org/multipage/
[INFRA]
Anne
van
Kesteren;
Domenic
Denicola.
Infra
Standard
.
Living
Standard.
URL:
https://infra.spec.whatwg.org/
[PERMISSIONS-POLICY-1]
Ian
Clelland.
Permissions
Policy
.
URL:
https://w3c.github.io/webappsec-permissions-policy/
[STREAMS]
Adam
Rice;
et
al.
Streams
Standard
.
Living
Standard.
URL:
https://streams.spec.whatwg.org/
[WEBAUDIO-1.0]
Paul
Adenot;
Hongchan
Choi.
Web
Audio
API
.
URL:
https://webaudio.github.io/web-audio-api/
[WEBIDL]
Edgar
Chen;
Timothy
Gu.
Web
IDL
Standard
.
Living
Standard.
URL:
https://webidl.spec.whatwg.org/
[WRITING-ASSISTANCE-APIS]
Writing
Assistance
APIs
.
Draft
Community
Group
Report.
URL:
https://webmachinelearning.github.io/writing-assistance-apis/
[BCP47]
A.
Phillips,
Ed.;
M.
Davis,
Ed..
Tags
for
Identifying
Languages
.
September
2009.
Best
Current
Practice.
URL:
https://www.rfc-editor.org/rfc/rfc5646
[UTS35]
Mark
Davis;
et
al.
Unicode
Locale
Data
Markup
Language
(LDML)
.
23
October
2020.
Unicode
Technical
Standard
#35.
URL:
https://www.unicode.org/reports/tr35/tr35-61/tr35.html
IDL
Index
[Exposed =Window , SecureContext ]
interface LanguageModel : EventTarget {
static Promise <LanguageModel > create (optional LanguageModelCreateOptions options = {});
static Promise <Availability > availability (optional LanguageModelCreateCoreOptions options = {});
static Promise <LanguageModelParams ?> params ();
// These will throw "NotSupportedError" DOMExceptions if role = "system"
Promise <DOMString > prompt (
LanguageModelPrompt input ,
optional LanguageModelPromptOptions options = {}
);
ReadableStream promptStreaming (
LanguageModelPrompt input ,
optional LanguageModelPromptOptions options = {}
);
Promise <undefined > append (
LanguageModelPrompt input ,
optional LanguageModelAppendOptions options = {}
);
Promise <double > measureInputUsage (
LanguageModelPrompt input ,
optional LanguageModelPromptOptions options = {}
);
readonly attribute double inputUsage ;
readonly attribute unrestricted double inputQuota ;
attribute EventHandler onquotaoverflow ;
readonly attribute unsigned long topK ;
readonly attribute float temperature ;
Promise <LanguageModel > clone (optional LanguageModelCloneOptions options = {});
undefined destroy ();
};
[Exposed =Window , SecureContext ]
interface LanguageModelParams {
readonly attribute unsigned long defaultTopK ;
readonly attribute unsigned long maxTopK ;
readonly attribute float defaultTemperature ;
readonly attribute float maxTemperature ;
};
dictionary LanguageModelCreateCoreOptions {
// Note: these two have custom out-of-range handling behavior, not in the IDL layer.
// They are unrestricted double so as to allow +Infinity without failing.
unrestricted double topK ;
unrestricted double temperature ;
sequence <LanguageModelExpected > expectedInputs ;
sequence <LanguageModelExpected > expectedOutputs ;
};
dictionary LanguageModelCreateOptions : LanguageModelCreateCoreOptions {
AbortSignal signal ;
CreateMonitorCallback monitor ;
sequence <LanguageModelMessage > initialPrompts ;
};
dictionary LanguageModelPromptOptions {
object responseConstraint ;
AbortSignal signal ;
};
dictionary LanguageModelAppendOptions {
AbortSignal signal ;
};
dictionary LanguageModelCloneOptions {
AbortSignal signal ;
};
dictionary LanguageModelExpected {
required LanguageModelMessageType type ;
sequence <DOMString > languages ;
};
// The argument to the prompt() method and others like it
typedef (
sequence <LanguageModelMessage >
// Shorthand for `[{ role: "user", content: [{ type: "text", value: providedValue }] }]`
or DOMString
) LanguageModelPrompt ;
dictionary LanguageModelMessage {
required LanguageModelMessageRole role ;
// The DOMString branch is shorthand for `[{ type: "text", value: providedValue }]`
required (DOMString or sequence <LanguageModelMessageContent >) content ;
};
dictionary LanguageModelMessageContent {
required LanguageModelMessageType type ;
required LanguageModelMessageValue value ;
};
enum LanguageModelMessageRole { "system" , "user" , "assistant" };
enum LanguageModelMessageType { "text" , "image" , "audio" };
typedef (
ImageBitmapSource
or AudioBuffer
or BufferSource
or DOMString
) LanguageModelMessageValue ;