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
Copy file name to clipboardExpand all lines: README.md
+95-42Lines changed: 95 additions & 42 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -137,48 +137,6 @@ const result = await multiUserSession.prompt([
137
137
138
138
Because of their special behavior of being preserved on context window overflow, system prompts cannot be provided this way.
139
139
140
-
### Tool use
141
-
142
-
The Prompt API supports **tool use** via the `tools` option, allowing you to define external capabilities that a language model can invoke in a model-agnostic way. Each tool is represented by an object that includes an `execute` member that specifies the JavaScript function to be called. When the language model initiates a tool use request, the user agent calls the corresponding `execute` function and sends the result back to the model.
143
-
144
-
Here’s an example of how to use the `tools` option:
145
-
146
-
```js
147
-
constsession=awaitLanguageModel.create({
148
-
initialPrompts: [
149
-
{
150
-
role:"system",
151
-
content:`You are a helpful assistant. You can use tools to help the user.`
152
-
}
153
-
],
154
-
tools: [
155
-
{
156
-
name:"getWeather",
157
-
description:"Get the weather in a location.",
158
-
inputSchema: {
159
-
type:"object",
160
-
properties: {
161
-
location: {
162
-
type:"string",
163
-
description:"The city to check for the weather condition.",
constresult=awaitsession.prompt("What is the weather in Seattle?");
178
-
```
179
-
180
-
In this example, the `tools` array defines a `getWeather` tool, specifying its name, description, input schema, and `execute` implementation. When the language model determines that a tool call is needed, the user agent invokes the `getWeather` tool's `execute()` function with the provided arguments and returns the result to the model, which can then incorporate it into its response.
181
-
182
140
### Multimodal inputs
183
141
184
142
All of the above examples have been of text prompts. Some language models also support other inputs. Our design initially includes the potential to support images and audio clips as inputs. This is done by using objects in the form `{ type: "image", content }` and `{ type: "audio", content }` instead of strings. The `content` values can be the following:
@@ -269,6 +227,101 @@ Details:
269
227
270
228
Future extensions may include more ambitious multimodal inputs, such as video clips, or realtime audio or video. (Realtime might require a different API design, more based around events or streams instead of messages.)
271
229
230
+
### Tool use
231
+
232
+
The Prompt API supports **tool use** via the `tools` option, allowing you to define external capabilities that a language model can invoke in a model-agnostic way. Each tool is represented by an object that includes an `execute` member that specifies the JavaScript function to be called. When the language model initiates a tool use request, the user agent calls the corresponding `execute` function and sends the result back to the model.
233
+
234
+
Here’s an example of how to use the `tools` option:
235
+
236
+
```js
237
+
constsession=awaitLanguageModel.create({
238
+
initialPrompts: [
239
+
{
240
+
role:"system",
241
+
content:`You are a helpful assistant. You can use tools to help the user.`
242
+
}
243
+
],
244
+
tools: [
245
+
{
246
+
name:"getWeather",
247
+
description:"Get the weather in a location.",
248
+
inputSchema: {
249
+
type:"object",
250
+
properties: {
251
+
location: {
252
+
type:"string",
253
+
description:"The city to check for the weather condition.",
constresult=awaitsession.prompt("What is the weather in Seattle?");
268
+
```
269
+
270
+
In this example, the `tools` array defines a `getWeather` tool, specifying its name, description, input schema, and `execute` implementation. When the language model determines that a tool call is needed, the user agent invokes the `getWeather` tool's `execute()` function with the provided arguments and returns the result to the model, which can then incorporate it into its response.
271
+
272
+
#### Tool return values
273
+
274
+
The above example shows tools returning a string. (In fact, stringified JSON.) Models which support [multimodal inputs](#multimodal-inputs) might also support interpreting image or audio results from tool calls.
275
+
276
+
Just like the `content` option to a `prompt()` call can accept either a string or an array of `{ type, value }` objects, web developer-provided tools can return either a string or such an array. Here's an example:
277
+
278
+
```js
279
+
let mutex, resolveMutex;
280
+
281
+
constsession=awaitLanguageModel.create({
282
+
tools: [
283
+
{
284
+
name:"grabKeyframe",
285
+
description:"Grab a keyframe from the video we're analyzing at the given time",
286
+
inputSchema: {
287
+
type:"number",
288
+
minimum:0,
289
+
exclusiveMaximum:videoEl.duration
290
+
},
291
+
expectedOutputs: {
292
+
types: ["image"]
293
+
},
294
+
asyncexecute(timestamp) {
295
+
if (mutex) {
296
+
// Since we're seeking a single video element, guard against concurrent calls.
297
+
await mutex;
298
+
}
299
+
try {
300
+
mutex =newPromise(r=> resolveMutex = r);
301
+
302
+
if (Math.abs(videoEl.currentTime- timestamp) >0.001) {
Note how the output types need to be specified in the tool definition, so that session creation can fail early if the model doesn't support processing multimodal tool outputs. If the return value contains non-text components without them being present in the tool specification, then the tool call will fail at prompting time, even if the model could support it.
320
+
321
+
Similarly, expected output languages can be provided (via `expectedOutputs: { languages: ["ja" ] }`) or similar, to get an early failure if the model doesn't support processing tool outputs in those languages. However, unlike modalities, there is no prompt-time checking of the tool call result's languages.
322
+
323
+
The above example shows a single-item array, but just like with prompt inputs, it's allowed to include multiple tool outputs. The same rules are followed as for inputs, e.g., concatenation of adjacent text chunks is done with a single space character.
324
+
272
325
### Structured output with JSON schema or RegExp constraints
273
326
274
327
To help with programmatic processing of language model responses, the prompt API supports constraining the response with either a JSON schema object or a `RegExp` passed as the `responseConstraint` option:
0 commit comments