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
+71-3Lines changed: 71 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -141,7 +141,74 @@ Because of their special behavior of being preserved on context window overflow,
141
141
142
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
143
144
-
Here’s an example of how to use the `tools` option:
144
+
There are 2 tool use modes: with automatic execution (closed loop) and without automatic execution (open loop)
145
+
146
+
Regardless of with or without automatic execution, the session creation and appending signature are the same. Here’s an example:
147
+
148
+
```js
149
+
constsession=awaitLanguageModel.create({
150
+
initialPrompts: [
151
+
{
152
+
role:"system",
153
+
content:`You are a helpful assistant. You can use tools to help the user.`
154
+
}
155
+
],
156
+
tools: [
157
+
{
158
+
name:"getWeather",
159
+
description:"Get the weather in a location.",
160
+
inputSchema: {
161
+
type:"object",
162
+
properties: {
163
+
location: {
164
+
type:"string",
165
+
description:"The city to check for the weather condition.",
166
+
},
167
+
},
168
+
required: ["location"],
169
+
},
170
+
}
171
+
]
172
+
});
173
+
```
174
+
175
+
In this example, the `tools` array defines a `getWeather` tool, specifying its name, description and input schema.
176
+
177
+
Few shot examples of tool use can be appended like so:
178
+
179
+
```js
180
+
awaitsession.append([
181
+
{role:"user", content:"What is the weather in Seattle?"},
{role:"assistant", content:"The temperature in Seattle is 55F and humidity is 67%"},
185
+
]);
186
+
```
187
+
188
+
Note that "role" and "type" now supports "tool-call" and "tool-result". `content.result` is a list of a dictionary of `type` and `value`, where `type` can be `{"text", "image", "audio", "object" }` and `value` is `any`.
189
+
190
+
#### Open Loop:
191
+
192
+
Without automatic execution, the API will return a `ToolCall` object with `callId` (a unique identifier of this tool call), `name` (name of the tool), and `arguments` (a dictionary fitting the JSON input schema of the tool's declaration), and client is expected to handle the tool execution and append the tool result back to the session.
193
+
194
+
Example:
195
+
196
+
```js
197
+
constresult=awaitsession.prompt("What is the weather in Seattle?");
Note that we always require tool-response to immediately follow tool-call generated by the model.
207
+
208
+
209
+
#### Closed Loop:
210
+
211
+
To enable automatic execution, add a `execute` function for each tool's implementation, and add a `toolUseConfig` to indicate that execution is enabled and pose a max number of tool calls invoked in a single session generation:
constresult=awaitsession.prompt("What is the weather in Seattle?");
178
246
```
179
247
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.
248
+
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.
0 commit comments